View Javadoc

1   /*
2    * $Id: CloseAction.java,v 1.15 2007/07/04 19:42:49 edankert Exp $
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  package org.xmlhammer.gui.actions;
22  
23  import java.awt.Toolkit;
24  import java.awt.event.KeyEvent;
25  import java.io.File;
26  import java.io.IOException;
27  import java.net.URI;
28  
29  import javax.swing.JOptionPane;
30  import javax.swing.KeyStroke;
31  import javax.swing.SwingUtilities;
32  import javax.xml.bind.JAXBException;
33  
34  import org.apache.log4j.Logger;
35  import org.bounce.RunnableAction;
36  import org.xmlhammer.gui.ProjectView;
37  import org.xmlhammer.gui.XMLHammer;
38  
39  /***
40   * An action that can be used to close a Project.
41   *
42   * @version	$Revision: 1.15 $, $Date: 2007/07/04 19:42:49 $
43   * @author Edwin Dankert <edankert@gmail.com>
44   */
45   public class CloseAction extends RunnableAction {
46   	private static final long serialVersionUID = 6537950233425958359L;
47  
48   	private XMLHammer parent = null;
49  	
50   	/***
51  	 * The constructor for the action which allows addition of 
52  	 * documents to the application.
53       * 
54       * @param parent the parent frame.
55  	 */
56   	public CloseAction( XMLHammer parent) {
57   		super( "Close");
58  
59          putValue( MNEMONIC_KEY, new Integer( 'C'));
60  		putValue( ACCELERATOR_KEY, KeyStroke.getKeyStroke( KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
61  		putValue( SHORT_DESCRIPTION, "Close Project");
62  		
63  		this.parent = parent;
64  		
65  		setEnabled( false);
66   	}
67   	
68  	/***
69  	 * The implementation of the close project action, called 
70  	 * after a user action.
71  	 */
72   	public void run() {
73          execute( parent, parent.getProjectsView().getSelectedView());
74   	}
75   	
76   	public static int execute( XMLHammer parent, ProjectView view) {
77         int result = JOptionPane.OK_OPTION;
78  
79         if ( view.getModuleThread() != null && view.getModuleThread().isAlive()) {
80             JOptionPane.showMessageDialog( parent, "The \""+view.getName()+"\" project is currently running.\nPlease stop the execution before closing.", "Project Running", JOptionPane.OK_OPTION);
81             return JOptionPane.CANCEL_OPTION;
82         }
83  
84         if (view.isChanged()) {
85              result = JOptionPane.showConfirmDialog( parent, "Save changes to \""+view.getName()+"\"?", "Please Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
86  			
87  			if ( result == JOptionPane.OK_OPTION) {
88                 URI uri = view.getURI();
89                 File file = null;
90  
91                 if ( uri != null) {
92                     file = new File( uri);
93                 }
94  
95                 if ( file == null) {
96                      SaveAsAction.saveAs(parent);
97                  } else {
98                      close(parent, view, uri, file);
99                  }
100 			} else if ( result == JOptionPane.CANCEL_OPTION) {
101 				return result;
102 			} 
103 			
104             parent.removeView( view);
105         } else {
106             parent.removeView( view);
107  		}
108 
109         return result;
110  	}
111 
112     private static void close(final XMLHammer parent, final ProjectView view, final URI uri, final File file) {
113         parent.setWait(true);
114         view.getStatusBar().setStatus("Saving "+uri.toString()+" ...");
115 
116         Runnable runner = new Runnable() {
117             public void run()  {
118                 try {
119                     SaveAction.save( parent, view.getProject(uri), file);
120                 } catch ( IOException x) {
121                     Logger.getLogger( SaveAction.class).error( "IO Error", x);
122                 } catch ( JAXBException x) {
123                     Logger.getLogger( SaveAction.class).error( "JAXB Error", x);
124                 }
125 
126                 SwingUtilities.invokeLater( new Runnable() {
127                     public void run() {
128                         try {
129                             parent.removeView( view);
130                         } finally {
131                             parent.setWait(false);
132                         }
133                     }
134                 });
135             }
136         };
137         
138         // Create and start the thread ...
139         Thread thread = new Thread( runner);
140         thread.start();
141     }
142 
143 }