2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Bruno JOFRET
4 * Copyright (C) 2010 - DIGITEO - Clement DAVID
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
14 package org.scilab.modules.xcos;
16 import static org.scilab.modules.xcos.utils.FileUtils.delete;
17 import static org.scilab.modules.xcos.utils.FileUtils.exists;
20 import java.io.IOException;
21 import java.lang.reflect.InvocationTargetException;
22 import java.util.ArrayDeque;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
31 import java.util.logging.LogManager;
33 import javax.swing.ImageIcon;
34 import javax.swing.SwingUtilities;
35 import javax.swing.SwingWorker;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.scilab.modules.action_binding.InterpreterManagement;
40 import org.scilab.modules.core.Scilab;
41 import org.scilab.modules.graph.utils.ScilabExported;
42 import org.scilab.modules.gui.bridge.tab.SwingScilabTab;
43 import org.scilab.modules.gui.messagebox.ScilabModalDialog;
44 import org.scilab.modules.gui.messagebox.ScilabModalDialog.AnswerOption;
45 import org.scilab.modules.gui.messagebox.ScilabModalDialog.ButtonType;
46 import org.scilab.modules.gui.messagebox.ScilabModalDialog.IconType;
47 import org.scilab.modules.gui.tabfactory.AbstractScilabTabFactory;
48 import org.scilab.modules.gui.tabfactory.ScilabTabFactory;
49 import org.scilab.modules.gui.utils.ClosingOperationsManager;
50 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
51 import org.scilab.modules.gui.utils.WindowsConfigurationManager;
52 import org.scilab.modules.localization.Messages;
53 import org.scilab.modules.xcos.block.BasicBlock;
54 import org.scilab.modules.xcos.block.SuperBlock;
55 import org.scilab.modules.xcos.configuration.ConfigurationManager;
56 import org.scilab.modules.xcos.configuration.model.DocumentType;
57 import org.scilab.modules.xcos.graph.DiagramComparator;
58 import org.scilab.modules.xcos.graph.SuperBlockDiagram;
59 import org.scilab.modules.xcos.graph.XcosDiagram;
60 import org.scilab.modules.xcos.io.scicos.H5RWHandler;
61 import org.scilab.modules.xcos.palette.PaletteBlockCtrl;
62 import org.scilab.modules.xcos.palette.PaletteManager;
63 import org.scilab.modules.xcos.palette.model.Category;
64 import org.scilab.modules.xcos.palette.model.PaletteBlock;
65 import org.scilab.modules.xcos.palette.model.PreLoaded;
66 import org.scilab.modules.xcos.palette.view.PaletteManagerView;
67 import org.scilab.modules.xcos.utils.FileUtils;
68 import org.scilab.modules.xcos.utils.XcosFileType;
69 import org.scilab.modules.xcos.utils.XcosMessages;
71 import com.mxgraph.model.mxGraphModel;
72 import com.mxgraph.util.mxEvent;
73 import com.mxgraph.util.mxEventObject;
74 import com.mxgraph.view.mxStylesheet;
77 * Xcos entry point class
79 // CSOFF: ClassFanOutComplexity
80 // CSOFF: ClassDataAbstractionCoupling
81 public final class Xcos {
83 * The current Xcos version
85 public static final String VERSION = "1.0";
87 * The current Xcos tradename
89 public static final String TRADENAME = "Xcos";
90 public static final ImageIcon ICON = new ImageIcon(ScilabSwingUtilities.findIcon("utilities-system-monitor", "256x256"));
92 private static final String LOAD_XCOS_LIBS_LOAD_SCICOS = "loadXcosLibs(); loadScicos();";
95 * Dependencies version
97 private static final List<String> MXGRAPH_VERSIONS = null;
98 private static final List<String> HDF5_VERSIONS = Arrays.asList(
99 "[1, 8, 4]", "[1, 8, 5]", "[1, 8, 6]", "[1, 8, 7]", "[1, 8, 8]");
100 private static final List<String> BATIK_VERSIONS = Arrays.asList("1.7");
102 private static final String UNABLE_TO_LOAD_JGRAPHX = Messages
103 .gettext("Unable to load the jgraphx library.\nExpecting version %s ; Getting version %s .");
104 private static final String UNABLE_TO_LOAD_JHDF5 = Messages
105 .gettext("Unable to load the hdf5-java (jhdf5) library. \nExpecting version %s ; Getting version %s .");
106 private static final String UNABLE_TO_LOAD_HDF5 = Messages
107 .gettext("Unable to load the native HDF5 library.");
108 private static final String UNABLE_TO_LOAD_BATIK = Messages
109 .gettext("Unable to load the Batik library. \nExpecting version %s ; Getting version %s .");
111 private static final String CALLED_OUTSIDE_THE_EDT_THREAD = "Called outside the EDT thread.";
112 private static final Log LOG = LogFactory.getLog(Xcos.class);
114 /** common shared instance */
115 private static volatile Xcos sharedInstance;
118 Scilab.registerInitialHook(new Runnable() {
121 /* load scicos libraries (macros) */
122 InterpreterManagement.requestScilabExec(LOAD_XCOS_LIBS_LOAD_SCICOS);
130 private final Map<File, Collection<XcosDiagram>> diagrams;
131 private boolean onDiagramIteration = false;
136 private final PaletteManager palette;
137 private final ConfigurationManager configuration;
138 private final mxStylesheet styleSheet;
139 private final XcosTabFactory factory;
142 * Construct an Xcos instance.
144 * There must be only one Xcos instance per Scilab application
146 private Xcos(final XcosTabFactory factory) {
148 * Read the configuration to support dynamic (before Xcos launch)
152 LogManager.getLogManager().readConfiguration();
153 } catch (final SecurityException e) {
155 } catch (final IOException e) {
159 /* Check the dependencies at startup time */
160 // checkDependencies();
165 diagrams = new HashMap<File, Collection<XcosDiagram>>();
166 // null is used for not saved diagrams
167 addDiagram(null, null);
170 * get the handlers instance
172 palette = PaletteManager.getInstance();
173 configuration = ConfigurationManager.getInstance();
174 styleSheet = new mxStylesheet();
177 FileUtils.decodeStyle(styleSheet);
178 } catch (final IOException e) {
183 * Register as an AbstractScilabTabFactory
185 if (factory == null) {
186 this.factory = new XcosTabFactory(false);
188 this.factory = factory;
190 ScilabTabFactory.getInstance().addTabFactory(this.factory);
194 * Check the dependencies and the version dependencies.
196 * This method use runtime class loading to handle ClassNotFoundException.
198 * This method catch any exception and rethrow it with a well defined
199 * message. Thus it doesn't pass the IllegalCatch metrics.
201 // CSOFF: IllegalCatch
202 // CSOFF: MagicNumber
203 private void checkDependencies() {
204 final ClassLoader loader = ClassLoader.getSystemClassLoader();
207 String mxGraphVersion = "";
209 final Class<?> klass = loader.loadClass("com.mxgraph.view.mxGraph");
210 mxGraphVersion = (String) klass.getDeclaredField("VERSION").get(
213 if (MXGRAPH_VERSIONS != null
214 && !MXGRAPH_VERSIONS.contains(mxGraphVersion)) {
215 throw new Exception();
217 } catch (final Throwable e) {
218 throw new RuntimeException(String.format(UNABLE_TO_LOAD_JGRAPHX,
219 MXGRAPH_VERSIONS.get(0), mxGraphVersion), e);
223 final int[] libVersion = new int[3];
225 final Class<?> klass = loader.loadClass("ncsa.hdf.hdf5lib.H5");
228 int ret = (Integer) klass.getMethod("H5get_libversion",
229 libVersion.getClass()).invoke(null, libVersion);
231 throw new Exception();
234 if (!HDF5_VERSIONS.contains(Arrays.toString(libVersion))) {
235 throw new Exception();
239 ret = (Integer) klass.getMethod("H5check_version", int.class,
240 int.class, int.class).invoke(null, libVersion[0],
241 libVersion[1], libVersion[2]);
243 throw new RuntimeException(UNABLE_TO_LOAD_HDF5);
246 } catch (final Throwable e) {
247 if (!(e instanceof RuntimeException)) {
248 throw new RuntimeException(String.format(UNABLE_TO_LOAD_JHDF5,
249 HDF5_VERSIONS.get(0), Arrays.toString(libVersion)), e);
254 String batikVersion = null;
256 final Class<?> klass = loader.loadClass("org.apache.batik.Version");
257 batikVersion = klass.getPackage().getImplementationVersion()
260 if (!BATIK_VERSIONS.contains(batikVersion)) {
261 throw new Exception();
264 } catch (final Throwable e) {
265 throw new RuntimeException(String.format(UNABLE_TO_LOAD_BATIK,
266 BATIK_VERSIONS.get(0), batikVersion), e);
271 // CSON: IllegalCatch
274 * @return the per Scilab application, Xcos instance
276 public static synchronized Xcos getInstance() {
277 return getInstance(null);
282 * the tab factory instance or null on creation
283 * @return the per Scilab application, Xcos instance
285 private static synchronized Xcos getInstance(final XcosTabFactory factory) {
286 if (sharedInstance == null) {
287 sharedInstance = new Xcos(factory);
290 * Lazy loading of HDF5 libraries to avoid first drag lag.
292 (new SwingWorker<Void, Void>() {
295 protected Void doInBackground() throws Exception {
297 final Category root = PaletteManager.getInstance()
300 final PaletteBlock b = ((PreLoaded) root.getNode().get(
301 0)).getBlock().get(0);
302 new PaletteBlockCtrl(b).getTransferable();
303 } catch (IndexOutOfBoundsException e) {
305 } catch (ClassCastException e) {
312 LOG.trace("Session started");
315 return sharedInstance;
321 public void quit(boolean force) {
322 if (sharedInstance == null) {
329 * Clear the shared instance.
331 private static synchronized void clearInstance() {
332 sharedInstance = null;
333 LOG.trace("Session ended");
337 * All Opened diagrams
339 * @return the opened diagrams list
341 public List<XcosDiagram> openedDiagrams() {
342 final List<XcosDiagram> opened = new ArrayList<XcosDiagram>();
343 for (File f : diagrams.keySet()) {
344 opened.addAll(openedDiagrams(f));
355 * @return the opened diagrams list
357 public List<XcosDiagram> openedDiagrams(File f) {
358 final List<XcosDiagram> opened = new ArrayList<XcosDiagram>();
359 for (XcosDiagram d : diagrams.get(f)) {
369 * Check if the in memory file representation is modified
373 * @return is modified
375 public boolean isModified(File f) {
376 for (XcosDiagram d : diagrams.get(f)) {
377 if (d.isModified()) {
386 * @return the global shared styleSheet
388 public mxStylesheet getStyleSheet() {
393 * Open a file from it's filename.
395 * This method must be called on the EDT thread. For other use, please use
396 * the {@link #xcos(String)} method.
399 * the file to open. If null an empty diagram is created.
401 public void open(final File filename) {
402 if (!SwingUtilities.isEventDispatchThread()) {
403 LOG.error(CALLED_OUTSIDE_THE_EDT_THREAD);
407 * If it is the first window opened, then open the palette first.
409 if (filename == null && openedDiagrams().isEmpty()) {
410 PaletteManager.setVisible(true);
413 XcosDiagram diag = null;
415 if (filename != null && filename.exists()) {
416 configuration.addToRecentFiles(filename);
419 * looking for an already opened diagram
421 final Collection<XcosDiagram> diags = diagrams.get(filename);
422 if (diags != null && !diags.isEmpty()) {
423 diag = diags.iterator().next();
430 * Allocate and setup a new diagram
432 diag = new XcosDiagram();
433 diag.installListeners();
436 * Create a visible window before loading
438 if (XcosTab.get(diag) == null) {
439 XcosTab.restore(diag);
443 * Load the file if applicable
445 if (filename != null) {
446 diag = diag.openDiagramFromFile(filename);
450 addDiagram(diag.getSavedFile(), diag);
455 diag.updateTabTitle();
460 * Get an unmodifiable view of the diagrams for a specific file
464 * @return the diagram collection
466 public Collection<XcosDiagram> getDiagrams(final File f) {
467 final Collection<XcosDiagram> diags = diagrams.get(f);
471 return Collections.unmodifiableCollection(diags);
475 * Add a diagram to the diagram list for a file. Be sure to set the right
476 * opened status on the diagram before calling this method.
483 public void addDiagram(final File f, final XcosDiagram diag) {
484 if (onDiagramIteration) {
485 throw new RuntimeException();
489 * Create the collection if it does not exist
491 Collection<XcosDiagram> diags = diagrams.get(f);
493 diags = createDiagramCollection();
494 diagrams.put(f, diags);
499 * Remove the diagram (and any child)
501 final Collection<XcosDiagram> toBeMoved = removeChildren(diag);
504 * Add the diagram to the collection
506 diags.addAll(toBeMoved);
510 private Collection<XcosDiagram> removeChildren(XcosDiagram diag) {
511 final Collection<XcosDiagram> removed = new HashSet<XcosDiagram>();
514 for (Collection<XcosDiagram> it : diagrams.values()) {
515 if (!it.contains(diag)) {
520 * Add all children to the removed collection.
522 for (XcosDiagram graph : it) {
523 if (graph instanceof SuperBlockDiagram) {
524 final XcosDiagram parent = ((SuperBlockDiagram) graph)
525 .getContainer().getParentDiagram();
527 // As "it" is sorted according to the hierarchy, "removed"
529 if (removed.contains(parent)) {
537 * really remove them all
539 it.removeAll(removed);
547 * Create a diagram collections (sorted List)
549 * @return the diagram collection
551 public Collection<XcosDiagram> createDiagramCollection() {
552 return new ArrayList<XcosDiagram>() {
554 public boolean add(XcosDiagram element) {
555 final boolean status = super.add(element);
556 DiagramComparator.sort(this);
561 public boolean addAll(Collection<? extends XcosDiagram> c) {
562 final boolean status = super.addAll(c);
563 DiagramComparator.sort(this);
570 * Try to close the graph (popup save dialog)
574 * @return if we can (or not) close the graph
576 public boolean canClose(final XcosDiagram graph) {
577 boolean canClose = false;
578 final File f = graph.getSavedFile();
580 final boolean wasLastOpened = openedDiagrams(f).size() <= 1;
581 final boolean isModified = isModified(f);
582 if (!(wasLastOpened && isModified)) {
587 final AnswerOption ans = ScilabModalDialog.show(XcosTab.get(graph),
588 XcosMessages.DIAGRAM_MODIFIED, XcosMessages.XCOS,
589 IconType.QUESTION_ICON, ButtonType.YES_NO_CANCEL);
593 canClose = diagrams.get(f).iterator().next().saveDiagram();
596 canClose = true; // can close
599 canClose = false; // operation canceled
605 * Update configuration before the destroy call to validate the uuid
608 configuration.addToRecentTabs(graph);
609 configuration.saveConfig();
617 * This method must be called on the EDT thread.
620 * the diagram to close
622 public void destroy(XcosDiagram graph) {
623 final File f = graph.getSavedFile();
624 final boolean wasLastOpenedForFile = openedDiagrams(f).size() <= 1;
626 if (!onDiagramIteration && wasLastOpenedForFile) {
633 * the graph to handle
635 * the diagram to check
636 * @return diagram name for the "Are your sure ?" dialog
638 public String askForClosing(final XcosDiagram graph,
639 final List<SwingScilabTab> list) {
642 if (wasLastOpened(list)) {
652 * Does Xcos will close or not ?
655 * the list to be closed
656 * @return true if all files will be close on tabs close.
658 public boolean wasLastOpened(final List<SwingScilabTab> list) {
659 final HashSet<String> opened = new HashSet<String>();
660 for (XcosDiagram diag : openedDiagrams()) {
661 opened.add(diag.getDiagramTab());
664 final HashSet<String> tabs = new HashSet<String>();
665 for (SwingScilabTab tab : list) {
667 tabs.add(tab.getPersistentId());
671 opened.removeAll(tabs);
673 return opened.isEmpty();
677 * Close the current xcos session.
679 * This method must be called on the EDT thread. For other use, please use
680 * the {@link #closeXcosFromScilab()} method.
682 public static synchronized void closeSession(final boolean ask) {
683 if (!SwingUtilities.isEventDispatchThread()) {
684 LOG.error(CALLED_OUTSIDE_THE_EDT_THREAD);
687 /* Doesn't instantiate xcos on close operation */
688 if (sharedInstance == null) {
693 * Try to close all opened files
695 final Xcos instance = sharedInstance;
698 final List<SwingScilabTab> tabs = new ArrayList<SwingScilabTab>();
699 for (final Collection<XcosDiagram> diags : instance.diagrams.values()) {
700 for (final XcosDiagram diag : diags) {
701 final SwingScilabTab tab = XcosTab.get(diag);
709 final boolean status = ClosingOperationsManager.startClosingOperation(tabs, ask, ask);
713 /* reset the shared instance state */
714 instance.diagrams.keySet().clear();
715 instance.addDiagram(null, null);
717 /* terminate any remaining simulation */
718 InterpreterManagement.requestScilabExec("haltscicos");
720 /* Saving modified data */
721 instance.palette.saveConfig();
722 instance.configuration.saveConfig();
727 * Scilab exported methods.
729 * All the following methods must use SwingUtilities method to assert that
730 * the operations will be called on the EDT thread.
732 * @see modules/xcos/src/jni/Xcos.giws.xml
734 * @see sci_gateway/xcos_gateway.xml
736 * @see modules/xcos/sci_gateway/cpp/sci_*.cpp
740 * Entry popint without filename.
742 * This method invoke Xcos operation on the EDT thread.
744 @ScilabExported(module = "xcos", filename = "Xcos.giws.xml")
745 public static void xcos() {
746 final Xcos instance = getInstance();
748 /* load scicos libraries (macros) */
749 InterpreterManagement.requestScilabExec(LOAD_XCOS_LIBS_LOAD_SCICOS);
751 SwingUtilities.invokeLater(new Runnable() {
760 * Entry point with filename
762 * This method invoke Xcos operation on the EDT thread.
767 @ScilabExported(module = "xcos", filename = "Xcos.giws.xml")
768 public static void xcos(final String fileName) {
769 final Xcos instance = getInstance();
770 final File filename = new File(fileName);
772 /* load scicos libraries (macros) */
773 InterpreterManagement.requestScilabExec(LOAD_XCOS_LIBS_LOAD_SCICOS);
775 SwingUtilities.invokeLater(new Runnable() {
778 instance.open(filename);
784 * Close the current xcos session from any thread.
786 * This method invoke Xcos operation on the EDT thread. Please prefer using
787 * {@link #closeSession()} when the caller is on the EDT thread.
789 @ScilabExported(module = "xcos", filename = "Xcos.giws.xml")
790 public static void closeXcosFromScilab() {
792 SwingUtilities.invokeAndWait(new Runnable() {
799 } catch (final InterruptedException e) {
801 } catch (final InvocationTargetException e) {
802 Throwable throwable = e;
803 String firstMessage = null;
804 while (throwable != null) {
805 firstMessage = throwable.getLocalizedMessage();
806 throwable = throwable.getCause();
809 throw new RuntimeException(firstMessage, e);
814 * Look in each diagram to find the block corresponding to the given uid and
815 * display a warning message.
817 * This method invoke Xcos operation on the EDT thread.
822 * The message to display.
824 @ScilabExported(module = "xcos", filename = "Xcos.giws.xml")
825 public static void warnCellByUID(final String[] uid, final String message) {
827 SwingUtilities.invokeAndWait(new Runnable() {
830 final ArrayDeque<String> deque = new ArrayDeque<String>(
833 warnCellByUID(deque, message);
836 } catch (final InterruptedException e) {
838 } catch (final InvocationTargetException e) {
839 Throwable throwable = e;
840 String firstMessage = null;
841 while (throwable != null) {
842 firstMessage = throwable.getLocalizedMessage();
843 throwable = throwable.getCause();
846 throw new RuntimeException(firstMessage, e);
850 private static void warnCellByUID(final ArrayDeque<String> deque, final String message) {
852 BasicBlock block = null;
853 Collection<XcosDiagram> diags = null;
855 // specific case with an empty array
856 if (deque.isEmpty()) {
863 getInstance().onDiagramIteration = true;
865 for (Collection<XcosDiagram> ds : getInstance().diagrams.values()) {
870 final XcosDiagram root = ds.iterator().next();
872 block = (BasicBlock) ((mxGraphModel) root.getModel()).getCell(id);
879 getInstance().onDiagramIteration = false;
882 // loop to get only the last diagram
883 while (block instanceof SuperBlock && !deque.isEmpty()) {
884 block.getParentDiagram().warnCellByUID(block.getId(), XcosMessages.ERROR_UNABLE_TO_COMPILE_THIS_SUPER_BLOCK);
886 final SuperBlock superBlock = (SuperBlock) block;
889 if (!diags.contains(superBlock.getChild()) || !superBlock.getChild().isOpened()) {
890 block.openBlockSettings(null);
893 final mxGraphModel model = ((mxGraphModel) superBlock.getChild().getModel());
894 block = (BasicBlock) model.getCell(id);
897 // We are unable to find the block with the right id
902 // finally perform the action on the last block
903 final XcosDiagram parent = findParent(block);
904 parent.warnCellByUID(block.getId(), message);
906 SwingUtilities.invokeLater(new Runnable() {
910 * Focus on an existing diagram
912 XcosTab.get(parent).setCurrent();
918 * This function convert a Xcos diagram to Scilab variable.
920 * This method invoke Xcos operation on the EDT thread.
923 * The xcos diagram file
927 * Does the file will be overwritten ?
928 * @return Not used (compatibility)
930 @ScilabExported(module = "xcos", filename = "Xcos.giws.xml")
931 public static int xcosDiagramToHDF5(final String xcosFile,
932 final String h5File, final boolean overwrite) {
933 final File file = new File(xcosFile);
935 if (exists(h5File)) {
943 if (!file.exists()) {
948 SwingUtilities.invokeAndWait(new Runnable() {
951 final XcosDiagram diagram = new XcosDiagram();
953 final XcosFileType filetype = XcosFileType.findFileType(xcosFile);
954 if (filetype != null) {
956 filetype.load(xcosFile, diagram);
957 new H5RWHandler(h5File).writeDiagram(diagram);
958 } catch (Exception e) {
959 throw new RuntimeException(e);
964 } catch (final InterruptedException e) {
965 throw new RuntimeException(e);
966 } catch (final InvocationTargetException e) {
967 Throwable throwable = e;
968 String firstMessage = null;
969 while (throwable != null) {
970 firstMessage = throwable.getLocalizedMessage();
971 throwable = throwable.getCause();
974 throw new RuntimeException(firstMessage, e);
981 * Open a diagram by uid.
983 * This method invoke Xcos operation on the EDT thread.
986 * UID path to a block.
988 @ScilabExported(module = "xcos", filename = "Xcos.giws.xml")
990 public static void xcosDiagramOpen(final String[] uid) {
991 throw new UnsupportedOperationException();
995 * Close a diagram by uid.
997 * This method invoke Xcos operation on the EDT thread.
1000 * The diagram id path
1002 @ScilabExported(module = "xcos", filename = "Xcos.giws.xml")
1004 public static void xcosDiagramClose(final String[] uid) {
1005 throw new UnsupportedOperationException();
1009 * Look for the parent diagram of the cell in the diagram hierarchy.
1012 * the cell to search for
1013 * @return the associated diagram
1015 public static XcosDiagram findParent(Object cell) {
1016 final Xcos instance = getInstance();
1018 instance.onDiagramIteration = true;
1020 for (Collection<XcosDiagram> diags : instance.diagrams.values()) {
1021 for (XcosDiagram diag : diags) {
1022 if (diag.getModel().contains(cell)) {
1023 if (cell instanceof BasicBlock) {
1024 ((BasicBlock) cell).setParentDiagram(diag);
1031 instance.onDiagramIteration = false;
1038 * @see org.scilab.modules.gui.tabfactory.AbstractScilabTabFactory
1040 public static class XcosTabFactory extends AbstractScilabTabFactory {
1045 private DocumentType cachedDocumentType;
1048 * Default constructor
1050 public XcosTabFactory() {
1054 private XcosTabFactory(boolean instanciateXcos) {
1055 if (instanciateXcos) {
1061 * Create/restore a tab for a given uuid
1065 * @return the tab instance
1068 public synchronized SwingScilabTab getTab(final String uuid) {
1073 SwingScilabTab tab = ScilabTabFactory.getInstance().getFromCache(
1076 // Palette manager restore
1078 if (PaletteManagerView.DEFAULT_TAB_UUID.equals(uuid)) {
1079 PaletteManagerView.restore(null, false);
1080 tab = PaletteManagerView.get();
1084 // diagram (tab or viewport) restore
1087 if (cachedDocumentType == null) {
1091 final boolean isTab = uuid.equals(cachedDocumentType.getUuid());
1092 final boolean isViewport = uuid.equals(cachedDocumentType
1095 final XcosDiagram graph = getDiagram(isTab, isViewport);
1096 if (graph != null && isTab) {
1097 XcosTab.restore(graph, false);
1098 graph.fireEvent(new mxEventObject(mxEvent.ROOT));
1099 tab = XcosTab.get(graph);
1100 } else if (graph != null && isViewport) {
1101 ViewPortTab.restore(graph, false);
1102 tab = ViewPortTab.get(graph);
1104 ClosingOperationsManager.addDependency(
1105 (SwingScilabTab) XcosTab.get(graph), tab);
1106 WindowsConfigurationManager.makeDependency(
1107 graph.getDiagramTab(), tab.getPersistentId());
1113 WindowsConfigurationManager.restorationFinished(tab);
1114 ScilabTabFactory.getInstance().addToCache(tab);
1119 private XcosDiagram getDiagram(boolean isTab, boolean isViewport) {
1120 XcosDiagram graph = null;
1122 // load a new diagram
1123 graph = getInstance().configuration
1124 .loadDiagram(cachedDocumentType);
1125 } else if (isViewport) {
1126 // get the cached diagram
1127 final File f = getInstance().configuration
1128 .getFile(cachedDocumentType);
1129 final Collection<XcosDiagram> diags = getInstance().diagrams
1132 for (XcosDiagram d : diags) {
1133 final String id = d.getDiagramTab();
1134 if (id != null && id.equals(cachedDocumentType.getUuid())) {
1145 public synchronized boolean isAValidUUID(String uuid) {
1146 // check the Palette manager view (static uuid)
1147 if (PaletteManagerView.DEFAULT_TAB_UUID.equals(uuid)) {
1152 * Cache and check against cache to ease next getTab(uuid) call
1155 return cachedDocumentType != null;
1159 * Cache the {@link DocumentType} for the specific uuid
1164 private void cache(String uuid) {
1166 * Handle a non null cache
1168 if (cachedDocumentType != null) {
1169 final boolean isTab = uuid.equals(cachedDocumentType.getUuid());
1170 final boolean isViewport = uuid.equals(cachedDocumentType
1173 if (isTab || isViewport) {
1176 cachedDocumentType = null;
1181 * Invalid cache, look for the right one
1183 final ConfigurationManager config = getInstance().configuration;
1184 final List<DocumentType> docs = config.getSettings().getTab();
1185 for (DocumentType d : docs) {
1186 final boolean isTab = uuid.equals(d.getUuid());
1187 final boolean isViewport = uuid.equals(d.getViewport());
1189 if (isTab || isViewport) {
1190 cachedDocumentType = d;
1197 public String getPackage() {
1202 public String getClassName() {
1203 return XcosTabFactory.class.getName();
1207 public String getApplication() {
1212 // CSON: ClassDataAbstractionCoupling
1213 // CSON: ClassFanOutComplexity