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.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
111
112
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
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
154 JAXBContext context = JAXBContext.newInstance( "org.xmlhammer.model.project");
155
156 JAXBResult result = new JAXBResult(context);
157
158
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
163 transformer.transform(new StreamSource(uri.toString()), result);
164
165 project = (Project)result.getResult();
166
167
168
169
170
171
172
173 return project;
174 }
175 }