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.FileOutputStream;
27 import java.io.IOException;
28 import java.net.URI;
29
30 import javax.swing.ImageIcon;
31 import javax.swing.KeyStroke;
32 import javax.swing.SwingUtilities;
33 import javax.xml.bind.JAXBContext;
34 import javax.xml.bind.JAXBException;
35 import javax.xml.bind.Marshaller;
36
37 import org.apache.log4j.Logger;
38 import org.bounce.RunnableAction;
39 import org.xmlhammer.gui.ProjectView;
40 import org.xmlhammer.gui.XMLHammer;
41 import org.xmlhammer.model.project.Project;
42
43 /***
44 * An action that can be used to save a XML Hammer project.
45 *
46 * @version $Revision: 1.19 $, $Date: 2007/07/04 19:42:49 $
47 * @author Edwin Dankert <edankert@gmail.com>
48 */
49 public class SaveAction extends RunnableAction {
50 private static final long serialVersionUID = 7171181601396051885L;
51
52 private XMLHammer parent = null;
53 private static JAXBContext context = null;
54
55 /***
56 * The constructor for the action which allows addition of
57 * documents to the application.
58 */
59 public SaveAction( XMLHammer parent) {
60 super( "Save");
61
62 putValue( MNEMONIC_KEY, new Integer( 'S'));
63 putValue( ACCELERATOR_KEY, KeyStroke.getKeyStroke( KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
64 putValue( SMALL_ICON, new ImageIcon( getClass().getResource( "/org/xmlhammer/gui/icons/etool16/save_edit.gif")));
65 putValue( SHORT_DESCRIPTION, "Save Project");
66
67 this.parent = parent;
68
69 setEnabled( false);
70 }
71
72 /***
73 * The implementation of the save project action, called
74 * after a user action.
75 */
76 public void run() {
77 final ProjectView view = parent.getProjectsView().getSelectedView();
78 final URI uri = view.getURI();
79 File file = null;
80
81 if ( uri != null) {
82 file = new File( uri);
83 }
84
85 if ( file == null) {
86 SaveAsAction.saveAs( parent);
87 } else {
88 save(parent, view, uri, file);
89 }
90 }
91
92 private static JAXBContext getContext() {
93 if ( context == null) {
94 try {
95 context = JAXBContext.newInstance( "org.xmlhammer.model.project" );
96 } catch ( JAXBException x) {
97
98 Logger.getLogger( SaveAction.class).error( "JAXB Error", x);
99 }
100 }
101
102 return context;
103 }
104
105 public static void save(final XMLHammer parent, final ProjectView view, final URI uri, final File file) {
106 parent.setWait(true);
107 view.getStatusBar().setStatus("Saving "+uri.toString()+" ...");
108
109 Runnable runner = new Runnable() {
110 public void run() {
111 try {
112 SaveAction.save( parent, view.getProject(uri), file);
113 } catch ( IOException x) {
114 Logger.getLogger( SaveAction.class).error( "IO Error", x);
115 } catch ( JAXBException x) {
116 Logger.getLogger( SaveAction.class).error( "JAXB Error", x);
117 }
118
119 SwingUtilities.invokeLater( new Runnable() {
120 public void run() {
121 try {
122 view.getUndoManager().mark();
123 view.getFieldManager().setChanged( false);
124 view.setLastModifiedTime(System.currentTimeMillis());
125 } finally {
126 view.getStatusBar().setStatus("Done");
127 parent.setWait(false);
128 }
129 }
130 });
131 }
132 };
133
134
135 Thread thread = new Thread( runner);
136 thread.start();
137 }
138
139 public static void save( XMLHammer parent, Project project, File file) throws JAXBException, IOException {
140
141 Marshaller marshaller = getContext().createMarshaller();
142 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
143
144 FileOutputStream stream = new FileOutputStream( file);
145 marshaller.marshal( project, stream);
146 stream.flush();
147 stream.close();
148 }
149 }