View Javadoc

1   /*
2    * $Id: SaveAction.java,v 1.19 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.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  	 			// never thrown during running of application.
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         // Create and start the thread ...
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  		// create a Marshaller and marshal to a file
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 }