1 /* 2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab 3 * Copyright (C) 2010 - DIGITEO - Allan SIMON 4 * 5 * Copyright (C) 2012 - 2016 - Scilab Enterprises 6 * 7 * This file is hereby licensed under the terms of the GNU GPL v2.0, 8 * pursuant to article 5.3.4 of the CeCILL v.2.1. 9 * This file was originally licensed under the terms of the CeCILL v2.1, 10 * and continues to be available under such terms. 11 * For more information, see the COPYING file which you should have received 12 * along with this program. 13 * 14 */ 15 package org.scilab.modules.ui_data.variableeditor; 16 17 import java.awt.Component; 18 import java.util.HashMap; 19 import java.util.Map; 20 21 import javax.swing.JTable; 22 import javax.swing.SwingUtilities; 23 import javax.swing.event.ChangeListener; 24 25 import org.scilab.modules.action_binding.InterpreterManagement; 26 import org.scilab.modules.gui.bridge.window.SwingScilabDockingWindow; 27 import org.scilab.modules.gui.bridge.window.SwingScilabWindow; 28 import org.scilab.modules.gui.textbox.ScilabTextBox; 29 import org.scilab.modules.gui.textbox.TextBox; 30 import org.scilab.modules.localization.Messages; 31 32 /** 33 * Class ScilabVariableEditor 34 * Implements a ScilabWindow containing Variable Editor (JTable) 35 */ 36 @SuppressWarnings(value = { "serial" }) 37 public final class ScilabVariableEditor extends SwingScilabDockingWindow implements VariableEditor { 38 39 private static Map<String, Component> map = new HashMap(); 40 41 private static ScilabVariableEditor instance; 42 43 private static SwingScilabVariableEditor editorTab; 44 private static ScilabTabbedPane tabPane; 45 46 /** 47 * Constructor 48 * @param type the variable type 49 * @param data the JTable data. 50 * @param variableName the name in Scilab of the variable being edited. 51 */ ScilabVariableEditor(String type, Object[][] data, String variableName)52 private ScilabVariableEditor(String type, Object[][] data, String variableName) { 53 super(); 54 editorTab = new SwingScilabVariableEditor(variableName, type, data); 55 tabPane = editorTab.getTabPane(); 56 TextBox infobar = ScilabTextBox.createTextBox(); 57 editorTab.addInfoBar(infobar); 58 addTab(editorTab); 59 map.put(variableName, tabPane.getSelectedComponent()); 60 } 61 62 /** 63 * Close Variable Editor 64 */ close()65 public void close() { 66 SwingScilabWindow editvarWindow = SwingScilabWindow.allScilabWindows.get(editorTab.getParentWindowId()); 67 ChangeListener[] cl = tabPane.getChangeListeners(); 68 for (int i = 0; i < cl.length; i++) { 69 tabPane.removeChangeListener(cl[i]); 70 } 71 instance = null; 72 map.clear(); 73 super.close(); 74 } 75 76 /** 77 * Close the edition of the variable 78 */ closeEditVar()79 public static void closeEditVar() { 80 if (instance != null) { 81 instance.close(); 82 } 83 } 84 85 /** 86 * Close the edition of the variable 87 * @param name the variable 88 */ close(String name)89 public static void close(String name) { 90 map.remove(name); 91 } 92 93 /** 94 * Set data displayed in JTable 95 * @param name the variable name 96 * @param type the variable type 97 * @param data : data to be displayed in JTable 98 */ updateData(String name, String type, Object[][] data)99 public void updateData(String name, String type, Object[][] data) { 100 if (map.containsKey(name)) { 101 editorTab.updateData(map.get(name), name, type, data); 102 } else { 103 editorTab.setData(name, type, data); 104 map.put(name, tabPane.getSelectedComponent()); 105 } 106 } 107 108 /** 109 * Set data displayed in JTable 110 * @param name the variable name 111 * @param type the variable type 112 * @param data : data to be displayed in JTable 113 */ updateData(String name, String type, Object[][] data, double[] rowsIndex, double[] colsIndex)114 public void updateData(String name, String type, Object[][] data, double[] rowsIndex, double[] colsIndex) { 115 if (map.containsKey(name)) { 116 editorTab.updateData(map.get(name), name, type, data, rowsIndex, colsIndex); 117 } 118 } 119 120 /** 121 * Get the variable editor singleton 122 * @return the Variable Editor 123 */ getVariableEditor()124 public static ScilabVariableEditor getVariableEditor() { 125 return instance; 126 } 127 128 /** 129 * Get the variable editor singleton with specified data. 130 * @param type the variable type 131 * @param data : the data to fill the editor with 132 * @param variableName : the scilab name of the variable being edited. 133 * @return the Variable Editor 134 */ getVariableEditor(final String type, final Object[][] data, final String variableName)135 public static ScilabVariableEditor getVariableEditor(final String type, final Object[][] data, final String variableName) { 136 if (instance == null) { 137 try { 138 SwingUtilities.invokeAndWait(new Runnable() { 139 public void run() { 140 instance = new ScilabVariableEditor(type, data, variableName); 141 instance.setVisible(true); 142 } 143 }); 144 } catch (Exception e) { 145 System.err.println(e); 146 } 147 } else { 148 SwingUtilities.invokeLater(new Runnable() { 149 public void run() { 150 SwingScilabWindow window = (SwingScilabWindow) SwingUtilities.getAncestorOfClass(SwingScilabWindow.class, editorTab); 151 window.setVisible(true); 152 window.toFront(); 153 final JTable table = editorTab.getCurrentTable(); 154 int r = -1; 155 int c = -1; 156 157 if (table != null) { 158 r = table.getSelectedRow(); 159 c = table.getSelectedColumn(); 160 } 161 boolean b = map.containsKey(variableName); 162 instance.updateData(variableName, type, data); 163 if (b && r != -1 && c != -1) { 164 table.setRowSelectionInterval(r, r); 165 table.setColumnSelectionInterval(c, c); 166 } 167 168 int state = window.getExtendedState(); 169 if((state & SwingScilabWindow.ICONIFIED) == SwingScilabWindow.ICONIFIED) { 170 window.setExtendedState(state - SwingScilabWindow.ICONIFIED); 171 } 172 } 173 }); 174 } 175 176 editorTab.setName(Messages.gettext("Variable Editor") + " - " + variableName + " (" + type + ")"); 177 return instance; 178 } 179 refreshVariableEditor(final String type, final Object[][] data, double[] rowsIndex, double[] colsIndex, final String variableName)180 public static void refreshVariableEditor(final String type, final Object[][] data, double[] rowsIndex, double[] colsIndex, final String variableName) { 181 if (instance != null) { 182 instance.updateData(variableName, type, data, rowsIndex, colsIndex); 183 } 184 } 185 186 /** 187 * {@inheritDoc} 188 */ getVariablename()189 public String getVariablename() { 190 String title = tabPane.getScilabTitleAt(tabPane.getSelectedIndex()); 191 return title.substring(SwingScilabVariableEditor.PREFIX.length()); 192 } 193 194 /** 195 * {@inheritDoc} 196 */ setVariableName(String variableName)197 public void setVariableName(String variableName) { 198 tabPane.setTitleAt(tabPane.getSelectedIndex(), SwingScilabVariableEditor.PREFIX + variableName); 199 } 200 201 /** 202 * {@inheritDoc} 203 */ setVisible(boolean status)204 public void setVisible(boolean status) { 205 super.setVisible(status); 206 editorTab.setVisible(status); 207 } 208 209 /** 210 * This method is called by the OSXAdapter class when the specific Mac 211 * OS X "About" menu is called. It is the only case where this method 212 * should be used 213 */ macosxAbout()214 public void macosxAbout() { 215 InterpreterManagement.requestScilabExec("about()"); 216 } 217 218 /** 219 * This method is called by the OSXAdapter class when the specific Mac 220 * OS X "Quit Scilab" menu is called. It is the only case where this method 221 * should be used 222 */ macosxQuit()223 public boolean macosxQuit() { 224 InterpreterManagement.requestScilabExec("exit()"); 225 return false; 226 } 227 228 /** 229 * This method is called by the OSXAdapter class when the specific Mac 230 * OS X "Preferences" menu is called. It is the only case where this method 231 * should be used 232 */ macosxPreferences()233 public void macosxPreferences() { 234 InterpreterManagement.requestScilabExec("preferences();"); 235 } 236 237 238 } 239