1 package org.devaki.nextobjects.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import java.io.File;
17 import java.io.InputStream;
18 import java.io.FileOutputStream;
19 import java.io.IOException;
20 import java.util.Vector;
21 import java.util.Iterator;
22 import java.awt.Cursor;
23 import javax.swing.JFileChooser;
24 import javax.swing.JOptionPane;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.devaki.nextobjects.NextObjects;
28 import org.devaki.nextobjects.ui.menus.NOMenuBar;
29 import org.devaki.nextobjects.ui.toolbars.NOToolBar1;
30 import org.devaki.nextobjects.workspace.models.BaseModel;
31 import org.devaki.nextobjects.workspace.models.ConceptualModel;
32 import org.devaki.nextobjects.workspace.models.PhysicalModel;
33 import org.w3c.dom.Document;
34 /***
35 * This class is responsible for managing model's files
36 *
37 * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
38 */
39 public final class NOFileManager
40 {
41 /*** Logger */
42 private static Log logger = LogFactory.getLog(NextObjects.class.getName());
43 /*** The file chooser */
44 private static JFileChooser chooser = new JFileChooser();
45 /*** The file, filter (images) */
46 private static NOFileFilter filterImage =
47 new NOFileFilter(new String[] {"png" }, "Images");
48 /*** The File filter (CDM) */
49 private static NOFileFilter filterCDM =
50 new NOFileFilter(new String[] {"cdm" }, "Conceptual Data Model");
51 /*** The File filter (PDM) */
52 private static NOFileFilter filterPDM =
53 new NOFileFilter(new String[] {"pdm" }, "Physical Data Model");
54 /*** The File filter (project-schema) */
55 private static NOFileFilter filterTorque =
56 new NOFileFilter(new String[] {"xml" }, "[Torque] project-schema.xml");
57 /*** The File filter (project-schema) */
58 private static NOFileFilter filterMaven =
59 new NOFileFilter(new String[] {"xml" }, "[Maven] project.xml");
60 /***
61 * private constructor avoid instanciation
62 *
63 */
64 private NOFileManager()
65 {
66 }
67 /***
68 * Open default model
69 *
70 * @param pDefault Description of the Parameter
71 * @return file
72 */
73 private static File openDefaultModel(final String pDefault)
74 {
75 File fp = null;
76 try
77 {
78 fp = File.createTempFile("default", "xml");
79
80 ClassLoader cl =
81 NextObjects.getReference().getClass().getClassLoader();
82 InputStream in = cl.getResourceAsStream(pDefault);
83 FileOutputStream out = new FileOutputStream(fp);
84 int c;
85 while ((c = in.read()) != -1)
86 {
87 out.write(c);
88 }
89 }
90 catch (Throwable t)
91 {
92 logger.error("Unable to load default model :(");
93 return null;
94 }
95 return fp;
96 }
97 /*** Open the default physical model */
98 public static void newPhysicalModel()
99 {
100 openModelFile(
101 openDefaultModel("org/devaki/nextobjects/defaultpdm.xml"),
102 false);
103 }
104 /*** Open the default model */
105 public static void newConceptualModel()
106 {
107 openModelFile(
108 openDefaultModel("org/devaki/nextobjects/defaultcdm.xml"),
109 false);
110 }
111 /***
112 * Save a model
113 *
114 * @param pModel the model
115 * @return done
116 */
117 public static boolean saveToFile(final BaseModel pModel)
118 {
119 BaseModel model = pModel;
120 boolean ok = false;
121 chooser.addChoosableFileFilter(filterImage);
122 chooser.addChoosableFileFilter(filterMaven);
123 if (pModel instanceof PhysicalModel)
124 {
125 chooser.addChoosableFileFilter(filterPDM);
126 chooser.addChoosableFileFilter(filterTorque);
127 chooser.setFileFilter(filterPDM);
128 chooser.removeChoosableFileFilter(filterCDM);
129 }
130 else
131 {
132 chooser.addChoosableFileFilter(filterCDM);
133 chooser.setFileFilter(filterCDM);
134 chooser.removeChoosableFileFilter(filterPDM);
135 chooser.removeChoosableFileFilter(filterTorque);
136 }
137
138 chooser.setDialogTitle("Save model");
139
140 chooser.setApproveButtonText("Save");
141
142 int returnVal =
143 chooser.showOpenDialog(NextObjects.getReference().getParent());
144
145 if (returnVal == JFileChooser.APPROVE_OPTION)
146 {
147
148 File out = chooser.getSelectedFile();
149 if (chooser.getFileFilter().equals(filterImage))
150 {
151 if (model instanceof PhysicalModel)
152 {
153 NOImageTransform.writeImage(
154 (PhysicalModel) pModel,
155 chooser.getSelectedFile());
156 }
157 else
158 {
159 NOImageTransform.writeImage(
160 (ConceptualModel) pModel,
161 chooser.getSelectedFile());
162 }
163 }
164 else
165 if (chooser.getFileFilter().equals(filterPDM)
166 || chooser.getFileFilter().equals(filterCDM))
167 {
168 pModel.setFileForSave(out.toString());
169 save(ModelMan.getCurrentModel());
170 NORecentFile.add(out);
171 }
172 else
173 if (chooser.getFileFilter().equals(filterMaven))
174 {
175 try
176 {
177 FileOutputStream fops = new FileOutputStream(out);
178 NOXMLOutputer.writeProjectDotXml(
179 ModelMan.getCurrentModel(),
180 fops);
181 fops.close();
182 }
183 catch (Exception e)
184 {
185 logger.error(e.getMessage());
186 }
187 }
188 else
189 if (chooser.getFileFilter().equals(filterTorque))
190 {
191 logger.error("Not implemented");
192 }
193 }
194 return ok;
195 }
196 /*** Operations to make when exiting nextObjects */
197 public static void goodbye()
198 {
199 boolean save = false;
200 Vector vctModelToSave = new Vector();
201 if (new Boolean(NOPreferences.getProperty("nextobjects.io.saveOnExit"))
202 .booleanValue())
203 {
204 NOPreferences.saveProperties();
205 }
206 Iterator itModels = ModelMan.getModels().iterator();
207 while (itModels.hasNext())
208 {
209 BaseModel mod = (BaseModel) itModels.next();
210 if (!mod.isSaved())
211 {
212 vctModelToSave.addElement(mod);
213 save = true;
214 }
215 }
216 if (save)
217 {
218 int n =
219 confirmDialog(
220 "There are unsaved model",
221 "There are unsaved model\nDo you want to save them ?",
222 "yes",
223 "no",
224 "cancel");
225 if (n == JOptionPane.NO_OPTION)
226 {
227 System.exit(0);
228 }
229 else
230 if (n == JOptionPane.YES_OPTION)
231 {
232 Iterator itToSave = vctModelToSave.iterator();
233 while (itToSave.hasNext())
234 {
235 BaseModel mod = (BaseModel) itToSave.next();
236 if (!mod.isSaved())
237 {
238 saveToFile(mod);
239 }
240 }
241 System.exit(0);
242 }
243 }
244 else
245 {
246 System.exit(0);
247 }
248 }
249 /***
250 * A dialog box to save or no models when exiting nextObjects
251 *
252 * @param title title
253 * @param question question
254 * @param opt1 opt1
255 * @param opt2 opt2
256 * @param opt3 opt3
257 * @return dialog result
258 */
259 private static int confirmDialog(
260 final String title,
261 final String question,
262 final String opt1,
263 final String opt2,
264 final String opt3)
265 {
266 Object[] options = {opt1, opt2, opt3 };
267 int n =
268 JOptionPane.showOptionDialog(
269 null,
270 question,
271 title,
272 JOptionPane.YES_NO_CANCEL_OPTION,
273 JOptionPane.QUESTION_MESSAGE,
274 null,
275 options,
276 opt2);
277 return n;
278 }
279 /***
280 * Save as a model
281 *
282 * @return done
283 */
284 public static boolean saveToFile()
285 {
286 return saveToFile(ModelMan.getCurrentModel());
287 }
288 /***
289 * Actions executed when closing an internal frame
290 *
291 * @param pNOModel the model
292 * @return done
293 */
294 public static boolean close(final BaseModel pNOModel)
295 {
296 if (pNOModel != null)
297 {
298 if (!pNOModel.isSaved())
299 {
300 int n =
301 confirmDialog(
302 "Model is unsaved",
303 "Do you want to save " + pNOModel + " ?",
304 "yes",
305 "no",
306 "cancel");
307 if (n == JOptionPane.NO_OPTION)
308 {
309 ModelMan.removeModel(pNOModel);
310 }
311 else
312 if (n == JOptionPane.CANCEL_OPTION)
313 {
314 return false;
315 }
316 else
317 if (n == JOptionPane.YES_OPTION)
318 {
319 if (NOFileManager.saveToFile(pNOModel))
320 {
321 ModelMan.removeModel(pNOModel);
322 }
323 }
324 else
325 {
326 ModelMan.setCurrentModel(null);
327 }
328 }
329 else
330 {
331 ModelMan.removeModel(pNOModel);
332 }
333 return true;
334 }
335 else
336 {
337 return false;
338 }
339 }
340 /***
341 * Ask for a file and the write a project.xml
342 *
343 * @param pModel the model to write
344 * @return sucess
345 */
346 public static boolean dumpProjectDotXml(final BaseModel pModel)
347 {
348 boolean how = false;
349 chooser.setFileFilter(filterMaven);
350
351 chooser.setDialogTitle("Dump project.xml");
352
353 chooser.setApproveButtonText("Save");
354 chooser.setSelectedFile(
355 new File(
356 ModelMan.getCurrentModel().getLocalDirectory()
357 + File.separator
358 + "project.xml"));
359
360 int returnVal =
361 chooser.showOpenDialog(NextObjects.getReference().getParent());
362 chooser.showOpenDialog(NextObjects.getReference().getParent());
363
364 if (returnVal == JFileChooser.APPROVE_OPTION)
365 {
366
367 File out = chooser.getSelectedFile();
368 try
369 {
370 FileOutputStream flux = new FileOutputStream(out);
371 NOXMLOutputer.writeProjectDotXml(pModel, flux);
372 }
373 catch (IOException ioex)
374 {
375 logger.error(ioex);
376 }
377 }
378 how = true;
379 return how;
380 }
381 /***
382 * Save the current model to its default file
383 * @param pModel the model
384 * */
385 public static void save(final BaseModel pModel)
386 {
387
388 File out = new File(ModelMan.getCurrentModel().getFileForSave());
389 save(pModel, out);
390 }
391 /***
392 * Save the current model
393 * @param pModel the model
394 * @param out the output file
395 * */
396 public static void save(final BaseModel pModel, final File out)
397 {
398 NextObjects.getReference().setCursor(new Cursor(Cursor.WAIT_CURSOR));
399
400 try
401 {
402
403 FileOutputStream flux = new FileOutputStream(out);
404 if (pModel instanceof ConceptualModel)
405 {
406 NOXMLOutputer.writeModel((ConceptualModel) pModel, flux);
407 }
408 else
409 if (pModel instanceof PhysicalModel)
410 {
411 NOXMLOutputer.writeModel((PhysicalModel) pModel, flux);
412 }
413 flux.close();
414 }
415 catch (Exception ioex)
416 {
417 logger.error(ioex);
418 ioex.printStackTrace();
419 JOptionPane.showMessageDialog(
420 NextObjects.getReference(),
421 ioex,
422 "Error saving model - devaki-nextobjects",
423 JOptionPane.ERROR_MESSAGE);
424 }
425
426 pModel.setSaved(true);
427 logger.info("Saved " + out.toString());
428 NextObjects.getReference().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
429 }
430 /***
431 * Open a model
432 *
433 * @param in the file
434 * @param log Description of the Parameter
435 */
436 public static void openModelFile(final File in, final boolean log)
437 {
438 int docType = NOXMLFactory.DOCTYPE_UNKNOW;
439 NextObjects.getReference().setCursor(new Cursor(Cursor.WAIT_CURSOR));
440 if (!in.exists())
441 {
442 logger.info("This file doesn't exist anymore " + in);
443 }
444 else
445 {
446 try
447 {
448 Document doc = NOXMLFactory.loadModel(in);
449 docType = NOXMLFactory.parseDocType(doc);
450 switch (docType)
451 {
452 case NOXMLFactory.DOCTYPE_CDM :
453 ConceptualModel newCDM = new ConceptualModel("--//--");
454 ModelMan.newConceptualDatamodel(newCDM);
455 NOXMLFactory.loadCdm(doc, newCDM);
456 ModelMan.getCurrentModel().setFileForSave(
457 in.toString());
458 newCDM.getRedoLog().clear();
459 break;
460 case NOXMLFactory.DOCTYPE_PDM :
461 PhysicalModel newPDM = new PhysicalModel("--//--");
462 ModelMan.newPhysicalDatamodel(newPDM);
463 NOXMLFactory.loadPdm(doc, newPDM);
464 ModelMan.getCurrentModel().setFileForSave(
465 in.toString());
466 newPDM.getRedoLog().clear();
467 break;
468 case NOXMLFactory.DOCTYPE_TORQUE :
469 PhysicalModel newPDM2 = new PhysicalModel("--//--");
470 ModelMan.newPhysicalDatamodel(newPDM2);
471 NOXMLFactory.loadPdm(doc, newPDM2);
472 DatabaseLoader.fixIt(newPDM2);
473 ModelMan.getCurrentModel().setFileForSave(
474 in.toString());
475 newPDM2.getRedoLog().clear();
476 break;
477 case NOXMLFactory.DOCTYPE_MAVEN :
478 if (ModelMan.getCurrentModel() != null)
479 {
480 NOXMLFactory.loadProjectDotXml(
481 in,
482 ModelMan.getCurrentModel());
483 }
484 else
485 {
486 logger.info(
487 "Don't know to wich model add "
488 + "project.xml infos");
489 logger.info("Please open a model before.");
490 }
491 break;
492 case NOXMLFactory.DOCTYPE_UNKNOW :
493 default :
494 logger.error("Unknow doctype. Giving up.");
495 break;
496 }
497 if (log && NOXMLFactory.DOCTYPE_UNKNOW != docType)
498 {
499 NORecentFile.add(in);
500 }
501 }
502 catch (Exception ex)
503 {
504 logger.error(ex.getMessage());
505 logger.error("This doesn't look good XML for me. Giving up.");
506 logger.error(ex.getStackTrace()[0]);
507 }
508 }
509
510 NOMenuBar.fixFileMenu();
511 NOToolBar1.fixIcons();
512 NextObjects.getReference().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
513 }
514 /*** Open a model */
515 public static void openModel()
516 {
517
518 chooser.setApproveButtonText("Open");
519 chooser.removeChoosableFileFilter(filterImage);
520 chooser.addChoosableFileFilter(filterPDM);
521 chooser.addChoosableFileFilter(filterCDM);
522
523 int returnVal =
524 chooser.showOpenDialog(NextObjects.getReference().getParent());
525
526 if (returnVal == JFileChooser.APPROVE_OPTION)
527 {
528
529 File in = chooser.getSelectedFile();
530 openModelFile(in, true);
531 }
532 }
533 /***
534 * the chooser accessor
535 *
536 * @return the chooser
537 */
538 public static JFileChooser getChooser()
539 {
540 return chooser;
541 }
542 /*** init the chooser */
543 public static void initChooser()
544 {
545 chooser.setCurrentDirectory(new File(System.getProperty("user.home")));
546 chooser.addChoosableFileFilter(filterMaven);
547 chooser.addChoosableFileFilter(filterTorque);
548 chooser.addChoosableFileFilter(filterPDM);
549 chooser.addChoosableFileFilter(filterCDM);
550 }
551 }