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.io.File;
24 import java.io.IOException;
25 import java.net.URI;
26
27 import javax.swing.ImageIcon;
28 import javax.swing.JFileChooser;
29 import javax.swing.JOptionPane;
30 import javax.swing.SwingUtilities;
31 import javax.xml.bind.JAXBException;
32
33 import org.apache.log4j.Logger;
34 import org.bounce.DefaultFileFilter;
35 import org.bounce.RunnableAction;
36 import org.bounce.util.URIUtils;
37 import org.xmlhammer.PreferencesHandler;
38 import org.xmlhammer.gui.ProjectView;
39 import org.xmlhammer.gui.XMLHammer;
40 import org.xmlhammer.gui.history.HistoryUtilities;
41 import org.xmlhammer.model.preferences.Preferences;
42
43 /***
44 * An action that can be used to save a XML Hammer project with a different name.
45 *
46 * @version $Revision: 1.24 $, $Date: 2007/07/04 19:42:49 $
47 * @author Edwin Dankert <edankert@gmail.com>
48 */
49 public class SaveAsAction extends RunnableAction {
50 private static final long serialVersionUID = 7929865451095502570L;
51
52 private XMLHammer parent = null;
53 private static JFileChooser chooser = null;
54
55 /***
56 * Constructs the save as action.
57 *
58 * @param parent the XML Hammer parent.
59 */
60 public SaveAsAction( XMLHammer parent) {
61 super( "Save As ...");
62
63 this.parent = parent;
64
65 putValue( MNEMONIC_KEY, new Integer( 'A'));
66 putValue( SMALL_ICON, new ImageIcon( getClass().getResource( "/org/xmlhammer/gui/icons/etool16/saveas_edit.gif")));
67 putValue( SHORT_DESCRIPTION, "Save As ...");
68
69 setEnabled( false);
70 }
71
72 private static JFileChooser getFileChooser() {
73 if ( chooser == null) {
74 chooser = new JFileChooser();
75 chooser.setAcceptAllFileFilterUsed( true);
76 chooser.addChoosableFileFilter( new DefaultFileFilter( "xhp", "XML Hammer Project"));
77 }
78
79 return chooser;
80 }
81
82 /***
83 * The implementation of the save as document action, called
84 * after a user action.
85 */
86 public void run() {
87 saveAs(parent);
88 }
89
90 public static void saveAs(XMLHammer parent) {
91 Preferences preferences = PreferencesHandler.getInstance().getPreferences();
92 ProjectView view = parent.getProjectsView().getSelectedView();
93 URI uri = view.getURI();
94 File file = null;
95 JFileChooser chooser = getFileChooser();
96
97 if ( uri != null) {
98 chooser.setSelectedFile(new File(uri));
99 } else if (preferences.getHistory().getProjects().getValue().size() > 0) {
100 uri = URIUtils.createURI(preferences.getHistory().getProjects().getValue().get(0));
101 chooser.setSelectedFile(new File(uri));
102 }
103
104 while ( file == null) {
105 int value = chooser.showSaveDialog( parent);
106
107 if ( value == JFileChooser.APPROVE_OPTION) {
108 file = chooser.getSelectedFile();
109 String path = file.getPath();
110
111 if ( path.indexOf( ".") == -1) {
112 file = new File( path+".xhp");
113 } else {
114 file = new File( path);
115 }
116
117 if ( file.exists()) {
118 int result = JOptionPane.OK_OPTION;
119
120 if ( !file.canWrite()) {
121 result = JOptionPane.showConfirmDialog( parent,
122 "The file \""+file.getName()+"\" already exists and is read-only.\n"+
123 "Please save the file with another name.", "Please Confirm", JOptionPane.OK_OPTION);
124 file = null;
125 } else {
126
127 result = JOptionPane.showConfirmDialog( parent,
128 "The file \""+file.getName()+"\" already exists,\n"+
129 "do you want to replace the existing file?", "Please Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
130
131 if ( result == JOptionPane.NO_OPTION) {
132 file = null;
133 } else if ( result == JOptionPane.CANCEL_OPTION) {
134 return;
135 }
136 }
137 }
138 } else {
139 return;
140 }
141 }
142 view.setURI(file.toURI());
143 save(parent, view, file);
144 }
145
146 private static void save(final XMLHammer parent, final ProjectView view, final File file) {
147 parent.setWait(true);
148 view.getStatusBar().setStatus("Saving "+view.getURI().toString()+" ...");
149
150 Runnable runner = new Runnable() {
151 public void run() {
152 try {
153 SaveAction.save( parent, view.getProject(view.getURI()), file);
154 } catch ( IOException x) {
155 Logger.getLogger( SaveAction.class).error( "IO Error", x);
156 } catch ( JAXBException x) {
157 Logger.getLogger( SaveAction.class).error( "JAXB Error", x);
158 }
159
160 SwingUtilities.invokeLater( new Runnable() {
161 public void run() {
162 try {
163 view.getUndoManager().mark();
164 view.getFieldManager().setChanged( false);
165 view.setLastModifiedTime(System.currentTimeMillis());
166
167 HistoryUtilities.getInstance().addOpenedProject(view.getURI());
168
169 parent.getProjectsView().notifyChange(view);
170 parent.updateTitle();
171 } finally {
172 view.getStatusBar().setStatus("Done");
173 parent.setWait(false);
174 }
175 }
176 });
177 }
178 };
179
180
181 Thread thread = new Thread( runner);
182 thread.start();
183 }
184 }