View Javadoc

1   /*
2    * $Id: OpenAction.java,v 1.23 2008/03/05 22:13:43 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.event.InputEvent;
24  import java.awt.event.KeyEvent;
25  import java.io.File;
26  import java.net.URI;
27  
28  import javax.swing.ImageIcon;
29  import javax.swing.JFileChooser;
30  import javax.swing.KeyStroke;
31  import javax.swing.SwingUtilities;
32  import javax.xml.bind.JAXBContext;
33  import javax.xml.bind.JAXBException;
34  import javax.xml.bind.util.JAXBResult;
35  import javax.xml.transform.Transformer;
36  import javax.xml.transform.TransformerException;
37  import javax.xml.transform.TransformerFactory;
38  import javax.xml.transform.stream.StreamSource;
39  
40  import org.apache.log4j.Logger;
41  import org.bounce.DefaultFileFilter;
42  import org.bounce.RunnableAction;
43  import org.bounce.util.URIUtils;
44  import org.xmlhammer.PreferencesHandler;
45  import org.xmlhammer.gui.XMLHammer;
46  import org.xmlhammer.model.preferences.Preferences;
47  import org.xmlhammer.model.project.Project;
48  
49  /***
50   * An action that can be used to open a XML Hammer project.
51   *
52   * @version	$Revision: 1.23 $, $Date: 2008/03/05 22:13:43 $
53   * @author Edwin Dankert <edankert@gmail.com>
54   */
55  public class OpenAction extends RunnableAction {
56   	private static final long serialVersionUID = 4520565132051867283L;
57  
58  	private XMLHammer parent = null;
59  	private JFileChooser chooser = null;
60  
61   	/***
62  	 * The constructor for the action which allows opening 
63  	 * of XML Documents.
64  	 *
65  	 * @param parent the parent frame.
66  	 */
67   	public OpenAction( XMLHammer parent) {
68   		super( "Open ...");
69  		
70  		this.parent = parent;
71  
72  		putValue( MNEMONIC_KEY, new Integer( 'O'));
73  		putValue( ACCELERATOR_KEY, KeyStroke.getKeyStroke( KeyEvent.VK_O, InputEvent.CTRL_MASK, false));
74  		putValue( SMALL_ICON, new ImageIcon( getClass().getResource( "/org/xmlhammer/gui/icons/obj16/fldr_obj.gif")));
75  		putValue( SHORT_DESCRIPTION, "Open Project");
76   	}
77   	
78   	private JFileChooser getFileChooser() {
79   		if ( chooser == null) {
80   			chooser = new JFileChooser();
81              chooser.setAcceptAllFileFilterUsed( true);
82              chooser.addChoosableFileFilter( new DefaultFileFilter( "xhp", "XML Hammer Project"));
83   		}
84   		
85   		return chooser;
86   	}
87  
88   	/***
89  	 * The implementation of the add document action, called 
90  	 * after a user action.
91  	 */
92   	public void run() {
93  		final JFileChooser chooser = getFileChooser();
94          Preferences preferences = PreferencesHandler.getInstance().getPreferences();
95  
96  		if (preferences.getHistory().getProjects().getValue().size() > 0) {
97              URI uri = URIUtils.createURI(preferences.getHistory().getProjects().getValue().get(0));
98              chooser.setSelectedFile(new File(uri));
99          }
100 
101         int value = chooser.showOpenDialog( parent);
102 
103 	 	if ( value == JFileChooser.APPROVE_OPTION) {
104             File file = chooser.getSelectedFile();
105             final URI uri = file.toURI();
106 
107             parent.setWait(true);
108             parent.getStatusPanel().showView(null);
109             parent.getStatusPanel().getBase().setStatus("Opening "+uri.toString()+" ...");
110 //	 		parent.setStatus( "Opening ...");
111 
112 	 		// Run in Thread!!!
113 	 		Runnable runner = new Runnable() {
114 	 			public void run()  {
115                    Project project = null;
116                     try {
117                         project = open(uri);
118                     } catch (JAXBException e) {
119                     	Logger.getLogger(getClass()).error( "JAXB Error", e);
120                     } catch (TransformerException e) {
121                     	Logger.getLogger(getClass()).error( "Transformer Error", e);
122                     } finally {
123                         openProject(uri, project);
124                     }
125 	 			}
126 	 		};
127 	 		
128 	 		// Create and start the thread ...
129 	 		Thread thread = new Thread( runner);
130 	 		thread.start();
131 	 	}
132  	}
133 
134     private void openProject(final URI uri, final Project project) {
135         SwingUtilities.invokeLater( new Runnable() {
136             public void run() {
137                 try {
138                     if (project != null) {
139                         parent.openProject( uri, project);
140                     }
141                 } finally {
142                     parent.getStatusPanel().getBase().setStatus("Done");
143                     parent.getStatusPanel().showView(parent.getProjectsView().getSelectedView());
144                     parent.setWait(false);
145                 }
146             }
147         });
148     }
149     
150     public static Project open(URI uri) throws JAXBException, TransformerException {
151         Project project = null;
152         
153 //        try {
154             JAXBContext context = JAXBContext.newInstance( "org.xmlhammer.model.project");
155             
156             JAXBResult result = new JAXBResult(context);
157             
158             // set up XSLT transformation
159             TransformerFactory factory = TransformerFactory.newInstance();
160             Transformer transformer = factory.newTransformer(new StreamSource(OpenAction.class.getResourceAsStream("/org/xmlhammer/convert/convert-from-2006-01-to-2007.xsl")));
161                   
162             // run transformation
163             transformer.transform(new StreamSource(uri.toString()), result);
164             
165             project = (Project)result.getResult();
166             
167 //        } catch ( JAXBException e) {
168 //            Logger.getLogger(OpenAction.class).error( "JAXB Error", e);
169 //        } catch (TransformerException e) {
170 //            Logger.getLogger(OpenAction.class).error( "Transformer Error", e);
171 //        }
172         
173         return project;
174     }
175 }