1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
package org.argouml.uml.ui; |
40 | |
|
41 | |
import java.awt.event.ActionEvent; |
42 | |
import java.io.File; |
43 | |
import java.io.FileNotFoundException; |
44 | |
import java.io.FileOutputStream; |
45 | |
import java.io.IOException; |
46 | |
import java.net.MalformedURLException; |
47 | |
|
48 | |
import javax.swing.AbstractAction; |
49 | |
import javax.swing.Action; |
50 | |
import javax.swing.JFileChooser; |
51 | |
import javax.swing.JOptionPane; |
52 | |
|
53 | |
import org.apache.log4j.Logger; |
54 | |
import org.argouml.application.api.CommandLineInterface; |
55 | |
import org.argouml.application.events.ArgoEventPump; |
56 | |
import org.argouml.application.events.ArgoEventTypes; |
57 | |
import org.argouml.application.events.ArgoStatusEvent; |
58 | |
import org.argouml.configuration.Configuration; |
59 | |
import org.argouml.i18n.Translator; |
60 | |
import org.argouml.kernel.Project; |
61 | |
import org.argouml.kernel.ProjectManager; |
62 | |
import org.argouml.ui.targetmanager.TargetManager; |
63 | |
import org.argouml.uml.diagram.ArgoDiagram; |
64 | |
import org.argouml.uml.diagram.DiagramUtils; |
65 | |
import org.argouml.util.ArgoFrame; |
66 | |
import org.tigris.gef.base.Diagram; |
67 | |
import org.tigris.gef.base.SaveGraphicsAction; |
68 | |
import org.tigris.gef.util.Util; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public class ActionSaveAllGraphics extends AbstractAction |
82 | |
implements CommandLineInterface { |
83 | |
|
84 | 900 | private static final Logger LOG = |
85 | |
Logger.getLogger(ActionSaveAllGraphics.class); |
86 | |
|
87 | |
private boolean overwrite; |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public ActionSaveAllGraphics() { |
94 | 1800 | super(Translator.localize("action.save-all-graphics"), |
95 | |
null); |
96 | |
|
97 | 1800 | putValue(Action.SHORT_DESCRIPTION, |
98 | |
Translator.localize("action.save-all-graphics")); |
99 | 1800 | } |
100 | |
|
101 | |
public void actionPerformed( ActionEvent ae ) { |
102 | 11 | trySave( false ); |
103 | 2 | } |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
public boolean trySave(boolean canOverwrite) { |
110 | 11 | return trySave(canOverwrite, null); |
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public boolean trySave(boolean canOverwrite, File directory) { |
122 | 11 | overwrite = canOverwrite; |
123 | 11 | Project p = ProjectManager.getManager().getCurrentProject(); |
124 | 11 | TargetManager tm = TargetManager.getInstance(); |
125 | 11 | File saveDir = (directory != null) ? directory : getSaveDir(p); |
126 | 2 | if (saveDir == null) { |
127 | |
|
128 | 1 | return false; |
129 | |
} |
130 | 1 | boolean okSoFar = true; |
131 | 1 | ArgoDiagram activeDiagram = DiagramUtils.getActiveDiagram(); |
132 | 1 | for (ArgoDiagram d : p.getDiagramList()) { |
133 | 2 | tm.setTarget(d); |
134 | 2 | okSoFar = trySaveDiagram(d, saveDir); |
135 | 2 | if (!okSoFar) { |
136 | 0 | break; |
137 | |
} |
138 | |
} |
139 | 1 | tm.setTarget(activeDiagram); |
140 | 1 | return okSoFar; |
141 | |
} |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
protected boolean trySaveDiagram(Object target, |
149 | |
File saveDir) { |
150 | 2 | if ( target instanceof Diagram ) { |
151 | 2 | String defaultName = ((Diagram) target).getName(); |
152 | 2 | defaultName = Util.stripJunk(defaultName); |
153 | |
|
154 | |
|
155 | |
|
156 | |
try { |
157 | 2 | File theFile = new File(saveDir, defaultName + "." |
158 | |
+ SaveGraphicsManager.getInstance().getDefaultSuffix()); |
159 | 2 | String name = theFile.getName(); |
160 | 2 | String path = theFile.getParent(); |
161 | 2 | SaveGraphicsAction cmd = SaveGraphicsManager.getInstance() |
162 | |
.getSaveActionBySuffix( |
163 | |
SaveGraphicsManager.getInstance().getDefaultSuffix()); |
164 | 2 | if (cmd == null) { |
165 | 0 | showStatus("Unknown graphics file type with extension " |
166 | |
+ SaveGraphicsManager.getInstance() |
167 | |
.getDefaultSuffix()); |
168 | 0 | return false; |
169 | |
} |
170 | 2 | showStatus( "Writing " + path + name + "..." ); |
171 | 2 | boolean result = saveGraphicsToFile(theFile, cmd); |
172 | 2 | showStatus( "Wrote " + path + name ); |
173 | 2 | return result; |
174 | |
} |
175 | 0 | catch ( FileNotFoundException ignore ) { |
176 | 0 | LOG.error("got a FileNotFoundException", ignore); |
177 | |
} |
178 | 0 | catch ( IOException ignore ) { |
179 | 0 | LOG.error("got an IOException", ignore); |
180 | 0 | } |
181 | |
} |
182 | 0 | return false; |
183 | |
} |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
protected File getSaveDir(Project p) { |
191 | 11 | JFileChooser chooser = getFileChooser(p); |
192 | |
|
193 | 11 | String fn = Configuration.getString( |
194 | |
SaveGraphicsManager.KEY_SAVEALL_GRAPHICS_PATH); |
195 | 11 | if (fn.length() > 0) { |
196 | 0 | chooser.setSelectedFile(new File(fn)); |
197 | |
} |
198 | |
|
199 | 11 | int retval = chooser.showSaveDialog(ArgoFrame.getFrame()); |
200 | |
|
201 | 2 | if ( retval == JFileChooser.APPROVE_OPTION ) { |
202 | 1 | File theFile = chooser.getSelectedFile(); |
203 | 1 | String path = theFile.getPath(); |
204 | 1 | Configuration.setString( |
205 | |
SaveGraphicsManager.KEY_SAVEALL_GRAPHICS_PATH, |
206 | |
path); |
207 | 1 | return theFile; |
208 | |
} |
209 | 1 | return null; |
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
private boolean saveGraphicsToFile(File theFile, SaveGraphicsAction cmd) |
219 | |
throws IOException { |
220 | 2 | if ( theFile.exists() && !overwrite ) { |
221 | 0 | String message = Translator.messageFormat( |
222 | |
"optionpane.confirm-overwrite", |
223 | |
new Object[] {theFile}); |
224 | 0 | String title = Translator.localize( |
225 | |
"optionpane.confirm-overwrite-title"); |
226 | |
|
227 | 0 | Object[] options = |
228 | |
{Translator.localize( |
229 | |
"optionpane.confirm-overwrite.overwrite"), |
230 | |
Translator.localize( |
231 | |
"optionpane.confirm-overwrite.overwrite-all"), |
232 | |
Translator.localize( |
233 | |
"optionpane.confirm-overwrite.skip-this-one"), |
234 | |
Translator.localize( |
235 | |
"optionpane.confirm-overwrite.cancel")}; |
236 | |
|
237 | 0 | int response = |
238 | |
JOptionPane.showOptionDialog(ArgoFrame.getFrame(), |
239 | |
message, |
240 | |
title, |
241 | |
JOptionPane.YES_NO_CANCEL_OPTION, |
242 | |
JOptionPane.QUESTION_MESSAGE, |
243 | |
null, |
244 | |
options, |
245 | |
options[0]); |
246 | |
|
247 | 0 | if (response == 1) { |
248 | 0 | overwrite = true; |
249 | |
} |
250 | 0 | if (response == 2) { |
251 | 0 | return true; |
252 | |
} |
253 | 0 | if (response == 3) { |
254 | 0 | return false; |
255 | |
} |
256 | 0 | if (response == JOptionPane.CLOSED_OPTION) { |
257 | 0 | return false; |
258 | |
} |
259 | |
} |
260 | 2 | FileOutputStream fo = null; |
261 | |
try { |
262 | 2 | fo = new FileOutputStream( theFile ); |
263 | 2 | cmd.setStream(fo); |
264 | 2 | cmd.setScale(Configuration.getInteger( |
265 | |
SaveGraphicsManager.KEY_GRAPHICS_RESOLUTION, 1)); |
266 | 2 | cmd.actionPerformed(null); |
267 | |
} finally { |
268 | 2 | if (fo != null) { |
269 | 2 | fo.close(); |
270 | |
} |
271 | |
} |
272 | 2 | return true; |
273 | |
} |
274 | |
|
275 | |
private JFileChooser getFileChooser(Project p) { |
276 | 11 | JFileChooser chooser = null; |
277 | |
try { |
278 | 11 | if ( p != null |
279 | |
&& p.getURI() != null |
280 | |
&& p.getURI().toURL().getFile().length() > 0 ) { |
281 | 0 | chooser = new JFileChooser(p.getURI().toURL().getFile()); |
282 | |
} |
283 | |
} |
284 | 0 | catch ( MalformedURLException ex ) { |
285 | 0 | LOG.error("exception in opening JFileChooser", ex); |
286 | 11 | } |
287 | |
|
288 | 11 | if ( chooser == null ) chooser = new JFileChooser(); |
289 | 11 | chooser.setDialogTitle( |
290 | |
Translator.localize("filechooser.save-all-graphics")); |
291 | 11 | chooser.setDialogType(JFileChooser.OPEN_DIALOG); |
292 | 11 | chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
293 | 11 | chooser.setMultiSelectionEnabled(false); |
294 | 11 | return chooser; |
295 | |
} |
296 | |
|
297 | |
private void showStatus(String text) { |
298 | 4 | ArgoEventPump.fireEvent(new ArgoStatusEvent( |
299 | |
ArgoEventTypes.STATUS_TEXT, this, text)); |
300 | 4 | } |
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
public boolean doCommand(String argument) { |
310 | 0 | File dir = new File(argument); |
311 | 0 | if (!dir.exists() || !dir.isDirectory()) { |
312 | 0 | LOG.error("The argument must be a path to an existing directory."); |
313 | 0 | return false; |
314 | |
} |
315 | 0 | boolean result = true; |
316 | 0 | for (Project p : ProjectManager.getManager().getOpenProjects()) { |
317 | 0 | TargetManager tm = TargetManager.getInstance(); |
318 | 0 | for (ArgoDiagram d : p.getDiagramList()) { |
319 | 0 | tm.setTarget(d); |
320 | 0 | if (!trySaveDiagram(d, dir)) { |
321 | 0 | result = false; |
322 | |
} |
323 | |
} |
324 | 0 | } |
325 | 0 | return result; |
326 | |
} |
327 | |
} |