/*
 * Copyright 2008-2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package info.joseluismartin.gui.action;

import info.joseluismartin.gui.Editor;
import info.joseluismartin.gui.View;
import info.joseluismartin.gui.ViewDialog;
import info.joseluismartin.service.PersistentService;

import java.awt.event.ActionEvent;
import java.io.Serializable;

import javax.swing.JOptionPane;

/**
 * @author Jose Luis Martin - (jlm@joseluismartin.info)
 *
 */
public class ViewSaveAction extends ViewAction {

	private static final long serialVersionUID = 1L;
	
	PersistentService<Object, Serializable> service;
	
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		beforeSave();
		afterSave(save());
	}

	/** 
	 * Save the view model, show a message to user if there are
	 * validation errors.
	 */
	private boolean save() {
		View<?> view = getView();
		view.update();
		boolean valid = view.validateView();
		
		if (valid) {
			service.save(getView().getModel());
			getDialog().setVisible(false);
			getDialog().dispose();
			
			// FIXME: move to Editor
			if (getDialog() instanceof ViewDialog) {
				((ViewDialog<?>) getDialog()).setValue(ViewDialog.OK);
			}
			
			if (getDialog() instanceof Editor) {
				((Editor) getDialog()).save();
			}
		}
		else {
			if (onError()) {
				String errorMessage = view.getErrorMessage();
				JOptionPane.showMessageDialog(view.getPanel(),errorMessage, "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		
		return valid;
	}

	/**
	 * Hook method to let subclases to do something on validation errors.
	 * @return true to show message error, false otherwise
	 */
	protected boolean onError() {
		return true;
	}

	/**
	 * Hook method to let subclases to do something before save 
	 * the model 
	 * @param valid true if validation success
	 */
	protected void afterSave(boolean valid) {
		
	}

	/**
	 * Hook method to let subclases to do something after save 
	 * the model
	 */
	protected void beforeSave() {
		
	}

	public PersistentService<Object, Serializable> getService() {
		return service;
	}

	public void setService(PersistentService<Object, Serializable> service) {
		this.service = service;
	}
}

@@ -774,16 +774,63 @@
 Dialog;%0A
+import info.joseluismartin.gui.form.FormUtils;%0A
 import i
@@ -1117,39 +1117,125 @@
 nal 
-long serialVersionUID = 1L;%0A%09%0A%09
+String DEFAULT_ICON = %09%22/images/16x16/dialog-ok.png%22;%0A%09private static final String DEFAULT_NAME = %22Accept%22;%0A%09private 
 Pers
@@ -1283,110 +1283,132 @@
 ce;%0A
-%09
 %0A%09
-/* (non-Javadoc)%0A%09 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
+public ViewSaveAction() %7B%0A%09%09setIcon(FormUtils.getIcon(DEFAULT_ICON));%0A%09%09setName(DEFAULT_NAME);%0A%09%7D%0A%09%0A%09/**%0A%09 * %7B@inheritDoc%7D
 %0A%09 *
@@ -1719,16 +1719,59 @@
 alid) %7B%0A
+%09%09%09if (view.isDirty() && service != null)%0A%09
 %09%09%09servi
@@ -1801,16 +1801,20 @@
 del());%0A
+%09%09%09%0A
 %09%09%09getDi

Test for dirty state before save