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.reveng; |
40 | |
|
41 | |
import java.io.File; |
42 | |
import java.io.PrintWriter; |
43 | |
import java.io.StringWriter; |
44 | |
import java.util.ArrayList; |
45 | |
import java.util.Arrays; |
46 | |
import java.util.Collection; |
47 | |
import java.util.Collections; |
48 | |
import java.util.HashSet; |
49 | |
import java.util.Hashtable; |
50 | |
import java.util.List; |
51 | |
import java.util.StringTokenizer; |
52 | |
|
53 | |
import org.argouml.application.api.Argo; |
54 | |
import org.argouml.cognitive.Designer; |
55 | |
import org.argouml.configuration.Configuration; |
56 | |
import org.argouml.i18n.Translator; |
57 | |
import org.argouml.kernel.Project; |
58 | |
import org.argouml.kernel.ProjectManager; |
59 | |
import org.argouml.model.Model; |
60 | |
import org.argouml.taskmgmt.ProgressMonitor; |
61 | |
import org.argouml.ui.explorer.ExplorerEventAdaptor; |
62 | |
import org.argouml.ui.targetmanager.TargetManager; |
63 | |
import org.argouml.uml.diagram.ArgoDiagram; |
64 | |
import org.argouml.uml.diagram.static_structure.ClassDiagramGraphModel; |
65 | |
import org.argouml.uml.diagram.static_structure.layout.ClassdiagramLayouter; |
66 | |
import org.argouml.util.SuffixFilter; |
67 | |
import org.tigris.gef.base.Globals; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public abstract class ImportCommon implements ImportSettingsInternal { |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
protected static final int MAX_PROGRESS_PREPARE = 1; |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
protected static final int MAX_PROGRESS_IMPORT = 99; |
90 | |
|
91 | |
protected static final int MAX_PROGRESS = MAX_PROGRESS_PREPARE |
92 | |
+ MAX_PROGRESS_IMPORT; |
93 | |
|
94 | |
|
95 | |
|
96 | |
private Hashtable<String, ImportInterface> modules; |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
private ImportInterface currentModule; |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
private String srcPath; |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
private DiagramInterface diagramInterface; |
113 | |
|
114 | |
private File[] selectedFiles; |
115 | |
|
116 | |
private SuffixFilter selectedSuffixFilter; |
117 | |
|
118 | |
protected ImportCommon() { |
119 | 0 | super(); |
120 | 0 | modules = new Hashtable<String, ImportInterface>(); |
121 | |
|
122 | 0 | for (ImportInterface importer : ImporterManager.getInstance() |
123 | |
.getImporters()) { |
124 | 0 | modules.put(importer.getName(), importer); |
125 | |
} |
126 | 0 | if (modules.isEmpty()) { |
127 | 0 | throw new RuntimeException("Internal error. " |
128 | |
+ "No importer modules found."); |
129 | |
} |
130 | |
|
131 | |
|
132 | 0 | currentModule = modules.get("Java"); |
133 | 0 | if (currentModule == null) { |
134 | 0 | currentModule = modules.elements().nextElement(); |
135 | |
} |
136 | 0 | } |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public abstract int getImportLevel(); |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
protected void initCurrentDiagram() { |
148 | 0 | diagramInterface = getCurrentDiagram(); |
149 | 0 | } |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
private DiagramInterface getCurrentDiagram() { |
158 | 0 | DiagramInterface result = null; |
159 | 0 | if (Globals.curEditor().getGraphModel() |
160 | |
instanceof ClassDiagramGraphModel) { |
161 | 0 | result = new DiagramInterface(Globals.curEditor()); |
162 | |
} else { |
163 | 0 | Project p = ProjectManager.getManager().getCurrentProject(); |
164 | 0 | TargetManager.getInstance().setTarget(p.getInitialTarget()); |
165 | |
|
166 | 0 | if (Globals.curEditor().getGraphModel() |
167 | |
instanceof ClassDiagramGraphModel) { |
168 | 0 | result = new DiagramInterface(Globals.curEditor()); |
169 | |
} |
170 | |
} |
171 | 0 | return result; |
172 | |
} |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public abstract String getInputSourceEncoding(); |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
protected List<File> getFileList(ProgressMonitor monitor) { |
188 | 0 | List<File> files = Arrays.asList(getSelectedFiles()); |
189 | 0 | if (files.size() == 1) { |
190 | 0 | File file = files.get(0); |
191 | 0 | SuffixFilter suffixFilters[] = {selectedSuffixFilter}; |
192 | 0 | if (suffixFilters[0] == null) { |
193 | |
|
194 | 0 | suffixFilters = currentModule.getSuffixFilters(); |
195 | |
} |
196 | 0 | files = |
197 | |
FileImportUtils.getList( |
198 | |
file, isDescendSelected(), |
199 | |
suffixFilters, monitor); |
200 | 0 | if (file.isDirectory()) { |
201 | 0 | setSrcPath(file.getAbsolutePath()); |
202 | |
} else { |
203 | 0 | setSrcPath(null); |
204 | |
} |
205 | |
} |
206 | |
|
207 | |
|
208 | 0 | if (isChangedOnlySelected()) { |
209 | |
|
210 | 0 | Object model = |
211 | |
ProjectManager.getManager().getCurrentProject().getModel(); |
212 | 0 | for (int i = files.size() - 1; i >= 0; i--) { |
213 | 0 | File f = files.get(i); |
214 | 0 | String fn = f.getAbsolutePath(); |
215 | 0 | String lm = String.valueOf(f.lastModified()); |
216 | |
|
217 | 0 | if (Model.getFacade().getUmlVersion().charAt(0) == '1') { |
218 | |
|
219 | 0 | if (lm.equals( |
220 | |
Model.getFacade().getTaggedValueValue(model, fn))) { |
221 | 0 | files.remove(i); |
222 | |
} |
223 | |
} |
224 | |
} |
225 | |
} |
226 | |
|
227 | 0 | return files; |
228 | |
} |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
public void setSrcPath(String path) { |
236 | 0 | srcPath = path; |
237 | 0 | } |
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
public String getSrcPath() { |
243 | 0 | return srcPath; |
244 | |
} |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
private void setLastModified(Project project, File file) { |
254 | |
|
255 | 0 | String fn = file.getAbsolutePath(); |
256 | 0 | String lm = String.valueOf(file.lastModified()); |
257 | 0 | if (lm != null) { |
258 | 0 | Model.getCoreHelper() |
259 | |
.setTaggedValue(project.getModel(), fn, lm); |
260 | |
} |
261 | 0 | } |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
public abstract boolean isCreateDiagramsSelected(); |
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
public abstract boolean isMinimizeFigsSelected(); |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
public abstract boolean isDiagramLayoutSelected(); |
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
public abstract boolean isDescendSelected(); |
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
public abstract boolean isChangedOnlySelected(); |
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
public ImportInterface getImporter(String importerName) { |
295 | 0 | return getModules().get(importerName); |
296 | |
} |
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
public void setFiles(File[] files) { |
304 | 0 | if (files != null) { |
305 | 0 | setSelectedFiles(files); |
306 | |
} |
307 | 0 | } |
308 | |
|
309 | |
protected Hashtable<String, ImportInterface> getModules() { |
310 | 0 | return modules; |
311 | |
} |
312 | |
|
313 | |
protected void setSelectedFiles(final File[] files) { |
314 | 0 | selectedFiles = files; |
315 | 0 | } |
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
protected void setSelectedSuffixFilter(final SuffixFilter suffixFilter) { |
323 | 0 | selectedSuffixFilter = suffixFilter; |
324 | 0 | } |
325 | |
|
326 | |
protected File[] getSelectedFiles() { |
327 | 0 | File[] copy = new File[selectedFiles.length]; |
328 | 0 | for (int i = 0; i < selectedFiles.length; i++) { |
329 | 0 | copy[i] = selectedFiles[i]; |
330 | |
} |
331 | 0 | return copy; |
332 | |
|
333 | |
} |
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
public void setCurrentModule(ImportInterface module) { |
341 | 0 | currentModule = module; |
342 | 0 | } |
343 | |
|
344 | |
protected ImportInterface getCurrentModule() { |
345 | 0 | return currentModule; |
346 | |
} |
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
public List<String> getLanguages() { |
353 | 0 | return Collections.unmodifiableList( |
354 | |
new ArrayList<String>(modules.keySet())); |
355 | |
} |
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
public boolean isDescend() { |
364 | 0 | String flags = |
365 | |
Configuration.getString( |
366 | |
Argo.KEY_IMPORT_GENERAL_SETTINGS_FLAGS); |
367 | 0 | if (flags != null && flags.length() > 0) { |
368 | 0 | StringTokenizer st = new StringTokenizer(flags, ","); |
369 | 0 | if (st.hasMoreTokens() && st.nextToken().equals("false")) { |
370 | 0 | return false; |
371 | |
} |
372 | |
} |
373 | 0 | return true; |
374 | |
} |
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
public boolean isChangedOnly() { |
383 | 0 | String flags = |
384 | |
Configuration.getString(Argo.KEY_IMPORT_GENERAL_SETTINGS_FLAGS); |
385 | 0 | if (flags != null && flags.length() > 0) { |
386 | 0 | StringTokenizer st = new StringTokenizer(flags, ","); |
387 | 0 | skipTokens(st, 1); |
388 | 0 | if (st.hasMoreTokens() && st.nextToken().equals("false")) { |
389 | 0 | return false; |
390 | |
} |
391 | |
} |
392 | 0 | return true; |
393 | |
} |
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
public boolean isCreateDiagrams() { |
402 | 0 | String flags = |
403 | |
Configuration.getString( |
404 | |
Argo.KEY_IMPORT_GENERAL_SETTINGS_FLAGS); |
405 | 0 | if (flags != null && flags.length() > 0) { |
406 | 0 | StringTokenizer st = new StringTokenizer(flags, ","); |
407 | 0 | skipTokens(st, 2); |
408 | 0 | if (st.hasMoreTokens() && st.nextToken().equals("false")) { |
409 | 0 | return false; |
410 | |
} |
411 | |
} |
412 | 0 | return true; |
413 | |
} |
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
public boolean isMinimizeFigs() { |
422 | 0 | String flags = |
423 | |
Configuration.getString( |
424 | |
Argo.KEY_IMPORT_GENERAL_SETTINGS_FLAGS); |
425 | 0 | if (flags != null && flags.length() > 0) { |
426 | 0 | StringTokenizer st = new StringTokenizer(flags, ","); |
427 | 0 | skipTokens(st, 3); |
428 | 0 | if (st.hasMoreTokens() && st.nextToken().equals("false")) { |
429 | 0 | return false; |
430 | |
} |
431 | |
} |
432 | 0 | return true; |
433 | |
} |
434 | |
|
435 | |
private void skipTokens(StringTokenizer st, int count) { |
436 | 0 | for (int i = 0; i < count; i++) { |
437 | 0 | if (st.hasMoreTokens()) { |
438 | 0 | st.nextToken(); |
439 | |
} |
440 | |
} |
441 | 0 | } |
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | |
|
449 | |
public boolean isDiagramLayout() { |
450 | 0 | String flags = |
451 | |
Configuration.getString( |
452 | |
Argo.KEY_IMPORT_GENERAL_SETTINGS_FLAGS); |
453 | 0 | if (flags != null && flags.length() > 0) { |
454 | 0 | StringTokenizer st = new StringTokenizer(flags, ","); |
455 | 0 | skipTokens(st, 4); |
456 | 0 | if (st.hasMoreTokens() && st.nextToken().equals("false")) { |
457 | 0 | return false; |
458 | |
} |
459 | |
} |
460 | 0 | return true; |
461 | |
} |
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
public String getEncoding() { |
470 | 0 | String enc = Configuration.getString(Argo.KEY_INPUT_SOURCE_ENCODING); |
471 | 0 | if (enc == null || enc.trim().equals("")) { |
472 | 0 | enc = System.getProperty("file.encoding"); |
473 | |
} |
474 | |
|
475 | 0 | return enc; |
476 | |
} |
477 | |
|
478 | |
|
479 | |
|
480 | |
|
481 | |
|
482 | |
|
483 | |
|
484 | |
|
485 | |
|
486 | |
|
487 | |
public void layoutDiagrams(ProgressMonitor monitor, int startingProgress) { |
488 | |
|
489 | 0 | if (diagramInterface == null) { |
490 | 0 | return; |
491 | |
} |
492 | |
|
493 | |
|
494 | |
|
495 | 0 | List<ArgoDiagram> diagrams = diagramInterface.getModifiedDiagramList(); |
496 | 0 | int total = startingProgress + diagrams.size() |
497 | |
/ 10; |
498 | 0 | for (int i = 0; i < diagrams.size(); i++) { |
499 | 0 | ArgoDiagram diagram = diagrams.get(i); |
500 | 0 | ClassdiagramLayouter layouter = new ClassdiagramLayouter(diagram); |
501 | 0 | layouter.layout(); |
502 | 0 | int act = startingProgress + (i + 1) / 10; |
503 | 0 | int progress = MAX_PROGRESS_PREPARE |
504 | |
+ MAX_PROGRESS_IMPORT * act / total; |
505 | 0 | if (monitor != null) { |
506 | 0 | monitor.updateProgress(progress); |
507 | |
} |
508 | |
|
509 | |
} |
510 | |
|
511 | 0 | } |
512 | |
|
513 | |
|
514 | |
|
515 | |
|
516 | |
|
517 | |
|
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
public void doImport(ProgressMonitor monitor) { |
523 | |
|
524 | 0 | monitor.setMaximumProgress(MAX_PROGRESS); |
525 | 0 | int progress = 0; |
526 | 0 | monitor.updateSubTask(Translator.localize("dialog.import.preImport")); |
527 | 0 | List<File> files = getFileList(monitor); |
528 | 0 | progress += MAX_PROGRESS_PREPARE; |
529 | 0 | monitor.updateProgress(progress); |
530 | 0 | if (files.size() == 0) { |
531 | 0 | monitor.notifyNullAction(); |
532 | 0 | return; |
533 | |
} |
534 | 0 | Model.getPump().stopPumpingEvents(); |
535 | 0 | boolean criticThreadWasOn = Designer.theDesigner().getAutoCritique(); |
536 | 0 | if (criticThreadWasOn) { |
537 | 0 | Designer.theDesigner().setAutoCritique(false); |
538 | |
} |
539 | |
try { |
540 | 0 | doImportInternal(files, monitor, progress); |
541 | |
} finally { |
542 | 0 | if (criticThreadWasOn) { |
543 | 0 | Designer.theDesigner().setAutoCritique(true); |
544 | |
} |
545 | |
|
546 | 0 | ExplorerEventAdaptor.getInstance().structureChanged(); |
547 | 0 | Model.getPump().startPumpingEvents(); |
548 | 0 | } |
549 | 0 | } |
550 | |
|
551 | |
|
552 | |
|
553 | |
|
554 | |
|
555 | |
|
556 | |
|
557 | |
|
558 | |
private void doImportInternal(List<File> filesLeft, |
559 | |
final ProgressMonitor monitor, int progress) { |
560 | 0 | Project project = ProjectManager.getManager().getCurrentProject(); |
561 | 0 | initCurrentDiagram(); |
562 | 0 | final StringBuffer problems = new StringBuffer(); |
563 | 0 | Collection newElements = new HashSet(); |
564 | |
|
565 | |
try { |
566 | 0 | newElements.addAll(currentModule.parseFiles( |
567 | |
project, filesLeft, this, monitor)); |
568 | 0 | } catch (Exception e) { |
569 | 0 | problems.append(printToBuffer(e)); |
570 | 0 | } |
571 | |
|
572 | |
|
573 | 0 | if (isCreateDiagramsSelected() && diagramInterface != null) { |
574 | 0 | addFiguresToDiagrams(newElements); |
575 | |
} |
576 | |
|
577 | |
|
578 | 0 | if (isDiagramLayoutSelected()) { |
579 | |
|
580 | 0 | monitor.updateMainTask( |
581 | |
Translator.localize("dialog.import.postImport")); |
582 | 0 | monitor.updateSubTask( |
583 | |
Translator.localize("dialog.import.layoutAction")); |
584 | 0 | layoutDiagrams(monitor, progress + filesLeft.size()); |
585 | |
} |
586 | |
|
587 | |
|
588 | 0 | if (problems != null && problems.length() > 0) { |
589 | 0 | monitor.notifyMessage( |
590 | |
Translator.localize( |
591 | |
"dialog.title.import-problems"), |
592 | |
Translator.localize( |
593 | |
"label.import-problems"), |
594 | |
problems.toString()); |
595 | |
} |
596 | |
|
597 | 0 | monitor.updateMainTask(Translator.localize("dialog.import.done")); |
598 | 0 | monitor.updateSubTask(""); |
599 | 0 | monitor.updateProgress(MAX_PROGRESS); |
600 | |
|
601 | 0 | } |
602 | |
|
603 | |
|
604 | |
|
605 | |
|
606 | |
|
607 | |
|
608 | |
|
609 | |
|
610 | |
|
611 | |
private void addFiguresToDiagrams(Collection newElements) { |
612 | 0 | for (Object element : newElements) { |
613 | 0 | if (Model.getFacade().isAClassifier(element) |
614 | |
|| Model.getFacade().isAPackage(element)) { |
615 | |
|
616 | 0 | Object ns = Model.getFacade().getNamespace(element); |
617 | 0 | if (ns == null) { |
618 | 0 | diagramInterface.createRootClassDiagram(); |
619 | |
} else { |
620 | 0 | String packageName = getQualifiedName(ns); |
621 | |
|
622 | 0 | if (packageName != null |
623 | |
&& !packageName.equals("")) { |
624 | 0 | diagramInterface.selectClassDiagram(ns, |
625 | |
packageName); |
626 | |
} else { |
627 | 0 | diagramInterface.createRootClassDiagram(); |
628 | |
} |
629 | |
|
630 | 0 | if (Model.getFacade().isAInterface(element)) { |
631 | 0 | diagramInterface.addInterface(element, |
632 | |
isMinimizeFigsSelected()); |
633 | 0 | } else if (Model.getFacade().isAClass(element)) { |
634 | 0 | diagramInterface.addClass(element, |
635 | |
isMinimizeFigsSelected()); |
636 | 0 | } else if (Model.getFacade().isAPackage(element)) { |
637 | 0 | diagramInterface.addPackage(element); |
638 | |
} |
639 | |
} |
640 | 0 | } |
641 | |
} |
642 | 0 | } |
643 | |
|
644 | |
|
645 | |
|
646 | |
|
647 | |
|
648 | |
|
649 | |
|
650 | |
|
651 | |
private String getQualifiedName(Object element) { |
652 | 0 | StringBuffer sb = new StringBuffer(); |
653 | |
|
654 | 0 | Object ns = element; |
655 | 0 | while (ns != null) { |
656 | 0 | String name = Model.getFacade().getName(ns); |
657 | 0 | if (name == null) { |
658 | 0 | name = ""; |
659 | |
} |
660 | 0 | sb.insert(0, name); |
661 | 0 | ns = Model.getFacade().getNamespace(ns); |
662 | 0 | if (ns != null) { |
663 | 0 | sb.insert(0, "."); |
664 | |
} |
665 | 0 | } |
666 | 0 | return sb.toString(); |
667 | |
} |
668 | |
|
669 | |
|
670 | |
|
671 | |
|
672 | |
private StringBuffer printToBuffer(Exception e) { |
673 | 0 | StringWriter sw = new StringWriter(); |
674 | 0 | PrintWriter pw = new java.io.PrintWriter(sw); |
675 | 0 | e.printStackTrace(pw); |
676 | 0 | return sw.getBuffer(); |
677 | |
} |
678 | |
|
679 | |
} |