1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
139 Thread thread = new Thread( runner);
140 thread.start();
141 }
142
143 }