2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011 - Calixte DENIZET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 package org.scilab.modules.gui.tabfactory;
15 import java.awt.Component;
16 import java.util.HashMap;
18 import java.util.UUID;
20 import javax.swing.JTextArea;
22 import org.flexdock.docking.DockableFactory;
24 import org.scilab.modules.gui.bridge.tab.SwingScilabTab;
25 import org.scilab.modules.gui.utils.WindowsConfigurationManager;
26 import org.scilab.modules.jvm.LoadClassPath;
27 import org.scilab.modules.localization.Messages;
30 * The main Tab factory.
31 * A component which needs to restore a Tab with a given uuid must register its factory.
33 * @author Calixte DENIZET
35 public class ScilabTabFactory extends DockableFactory.Stub {
37 private static final ScilabTabFactory instance = new ScilabTabFactory();
38 private static final String EMPTYTAB = Messages.gettext("Empty tab");
39 private static final String NULLUUID = new UUID(0L, 0L).toString();
40 private static final String ERROR = Messages.gettext("The tab with uuid %s cannot be restored.\nPlease report a bug with the previous message and in attaching the file %s.");
43 Certains components could depend an other Tab and this last one could be created before
44 to be requested by the Tab restorator. So we cache it to be sure to have the same instance.
46 private Map<String, SwingScilabTab> cache = new HashMap<String, SwingScilabTab>();
47 private Map<String, AbstractScilabTabFactory> factories = new HashMap<String, AbstractScilabTabFactory>();
52 protected ScilabTabFactory() { }
57 public Component getDockableComponent(String uuid) {
62 * Adds a new Tab factory
63 * @param factory the factory to add
65 public void addTabFactory(AbstractScilabTabFactory factory) {
66 if (!factories.containsKey(factory.getClassName())) {
67 factories.put(factory.getClassName(), factory);
72 * Adds a new Tab factory
73 * @param pack the package to load in using LoadClassPath
74 * @param className the name of the factory
75 * @return the newly added factory
77 public AbstractScilabTabFactory addTabFactory(String pack, String className) {
78 AbstractScilabTabFactory factory = null;
79 if (!factories.containsKey(className)) {
80 if (pack != null && !pack.isEmpty()) {
81 LoadClassPath.loadOnUse(pack);
84 Class clazz = Class.forName(className);
85 factory = (AbstractScilabTabFactory) clazz.newInstance();
86 addTabFactory(factory);
87 } catch (ClassNotFoundException e) {
88 System.out.println(e);
90 catch (IllegalAccessException e) {
91 System.out.println(e);
93 catch (InstantiationException e) {
94 System.out.println(e);
102 * Removes a Tab factory
103 * @param factory the factory to remove
105 public void removeTabFactory(AbstractScilabTabFactory factory) {
106 factories.remove(factory);
110 * Creates a tab given an uuid
111 * @param uuid the uuid
112 * @return the corresponding tab
114 public SwingScilabTab getTab(String uuid) {
115 SwingScilabTab tab = cache.get(uuid);
120 for (String name : factories.keySet()) {
121 AbstractScilabTabFactory factory = factories.get(name);
122 if (factory.isAValidUUID(uuid)) {
123 tab = factory.getTab(uuid);
125 cache.put(uuid, tab);
131 return makeEmptyTab(uuid);
137 public String getPackage(String uuid) {
138 for (String name : factories.keySet()) {
139 if (factories.get(name).isAValidUUID(uuid)) {
140 return factories.get(name).getPackage();
149 public String getClassName(String uuid) {
150 for (String name : factories.keySet()) {
151 if (factories.get(name).isAValidUUID(uuid)) {
152 return factories.get(name).getClassName();
161 public String getApplication(String uuid) {
162 for (String name : factories.keySet()) {
163 if (factories.get(name).isAValidUUID(uuid)) {
164 return factories.get(name).getApplication();
173 public void clearCache() {
177 public void removeFromCache(String uuid) {
181 public SwingScilabTab getFromCache(String uuid) {
182 return cache.get(uuid);
185 public void addToCache(SwingScilabTab tab) {
186 cache.put(tab.getPersistentId(), tab);
190 * @return the instace (this class should be used as a singleton)
192 public static ScilabTabFactory getInstance() {
198 * @return an empty tab
200 private static final SwingScilabTab makeEmptyTab(String uuid) {
202 if (uuid == null || uuid.isEmpty()) {
206 SwingScilabTab tab = new SwingScilabTab(EMPTYTAB, u);
207 String text = String.format(ERROR, u, System.getProperty("user.home"));
208 JTextArea textarea = new JTextArea(text);
209 textarea.setEditable(false);
210 tab.setContentPane(textarea);