Desktop: Add a button in dockbar to get the help about the component
[scilab.git] / scilab / modules / ui_data / src / java / org / scilab / modules / ui_data / variableeditor / SwingScilabVariableEditor.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2010 - DIGITEO - Allan SIMON
4  * Copyright (C) 2010 - Calixte DENIZET
5  *
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
11  *
12  */
13
14 package org.scilab.modules.ui_data.variableeditor;
15
16 import java.awt.Component;
17
18 import javax.swing.JMenu;
19 import javax.swing.JPopupMenu;
20 import javax.swing.JScrollPane;
21 import javax.swing.JTable;
22 import javax.swing.SwingUtilities;
23 import javax.swing.event.ChangeEvent;
24 import javax.swing.event.ChangeListener;
25 import javax.swing.event.TableModelEvent;
26 import javax.swing.table.TableModel;
27
28 import org.scilab.modules.gui.bridge.tab.SwingScilabTab;
29 import org.scilab.modules.gui.bridge.window.SwingScilabWindow;
30 import org.scilab.modules.gui.menu.Menu;
31 import org.scilab.modules.gui.menu.ScilabMenu;
32 import org.scilab.modules.gui.menubar.MenuBar;
33 import org.scilab.modules.gui.menubar.ScilabMenuBar;
34 import org.scilab.modules.gui.pushbutton.PushButton;
35 import org.scilab.modules.gui.tab.SimpleTab;
36 import org.scilab.modules.gui.tab.Tab;
37 import org.scilab.modules.gui.textbox.TextBox;
38 import org.scilab.modules.gui.toolbar.ScilabToolBar;
39 import org.scilab.modules.gui.toolbar.ToolBar;
40 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
41 import org.scilab.modules.gui.utils.UIElementMapper;
42 import org.scilab.modules.gui.utils.WindowsConfigurationManager;
43 import org.scilab.modules.gui.window.Window;
44 import org.scilab.modules.ui_data.datatable.SwingEditvarTableModel;
45 import org.scilab.modules.ui_data.rowheader.RowHeader;
46 import org.scilab.modules.ui_data.utils.UiDataMessages;
47 import org.scilab.modules.ui_data.variableeditor.actions.CloseAction;
48 import org.scilab.modules.ui_data.variableeditor.actions.CopyAction;
49 import org.scilab.modules.ui_data.variableeditor.actions.CopySelectionAsScilabCommandAction;
50 import org.scilab.modules.ui_data.variableeditor.actions.CopySelectionAsScilabMatrixAction;
51 import org.scilab.modules.ui_data.variableeditor.actions.CopySelectionAsScilabMatrixWithCRAction;
52 import org.scilab.modules.ui_data.variableeditor.actions.CreateNewVariableAction;
53 import org.scilab.modules.ui_data.variableeditor.actions.CreateVariableFromSelectionAction;
54 import org.scilab.modules.ui_data.variableeditor.actions.CutAction;
55 import org.scilab.modules.ui_data.variableeditor.actions.DuplicateVariableAction;
56 import org.scilab.modules.ui_data.variableeditor.actions.PasteAction;
57 import org.scilab.modules.ui_data.variableeditor.actions.PlotAction;
58 import org.scilab.modules.ui_data.variableeditor.actions.RedoAction;
59 import org.scilab.modules.ui_data.variableeditor.actions.RefreshAction;
60 import org.scilab.modules.ui_data.variableeditor.actions.SetPrecisionLongAction;
61 import org.scilab.modules.ui_data.variableeditor.actions.SetPrecisionLongeAction;
62 import org.scilab.modules.ui_data.variableeditor.actions.SetPrecisionShortAction;
63 import org.scilab.modules.ui_data.variableeditor.actions.SetPrecisionShorteAction;
64 import org.scilab.modules.ui_data.variableeditor.actions.SizeColumnsToFitAction;
65 import org.scilab.modules.ui_data.variableeditor.actions.SupprAction;
66 import org.scilab.modules.ui_data.variableeditor.actions.UndoAction;
67 import org.scilab.modules.ui_data.variableeditor.celleditor.CellEditorFactory;
68 import org.scilab.modules.ui_data.variableeditor.celleditor.ScilabGenericCellEditor;
69 import org.scilab.modules.ui_data.variableeditor.renderers.RendererFactory;
70 import org.scilab.modules.ui_data.variableeditor.undo.CellsUndoManager;
71
72 /**
73  * Swing implementation of Scilab Variable Editor
74  * uses JTable
75  * @author Allan SIMON
76  * @author Calixte DENIZET
77  */
78 public class SwingScilabVariableEditor extends SwingScilabTab implements Tab, SimpleVariableEditor {
79
80     /**
81      * Prefix used in the tabs titles.
82      */
83     public static final String PREFIX = "Var - ";
84
85     private static final String APOS = "'";
86     private static final long serialVersionUID = 1L;
87
88     private final ScilabTabbedPane tabPane;
89     private final PushButton refreshButton;
90     private final PushButton undoButton;
91     private final PushButton redoButton;
92
93     /**
94      * Create a JTable with data Model.
95      * @param name the variable name
96      * @param type the variable type
97      * @param data : The JTable data.
98      */
99     public SwingScilabVariableEditor(String name, String type, Object[][] data) {
100         super(UiDataMessages.VARIABLE_EDITOR);
101         setAssociatedXMLIDForHelp("editvar");
102         setWindowIcon("rrze_table");
103         refreshButton = RefreshAction.createButton(this, UiDataMessages.REFRESH);
104         undoButton = UndoAction.createButton(this, UiDataMessages.UNDO);
105         redoButton = RedoAction.createButton(this, UiDataMessages.REDO);
106         enableUndoButton(false);
107         enableRedoButton(false);
108         tabPane = new ScilabTabbedPane(this);
109         tabPane.addChangeListener(new ChangeListener() {
110                 @Override
111                 public void stateChanged(ChangeEvent e) {
112                     String name = tabPane.getScilabTitleAt(tabPane.getSelectedIndex());
113                     if (name.length() != 0) {
114                         name = name.substring(PREFIX.length());
115                         String tooltip = "";
116                         if (name != null && getCurrentModel() != null) {
117                             tooltip = UiDataMessages.REFRESH + APOS + name + APOS;
118                             String type = ((SwingEditvarTableModel) getCurrentModel()).getType();
119                             String title = UiDataMessages.VARIABLE_EDITOR + " - " + name + "  (" + type + ")";
120                             setName(title);
121                             SwingScilabWindow window = (SwingScilabWindow) SwingUtilities.getAncestorOfClass(SwingScilabWindow.class, tabPane);
122                             if (window != null) {
123                                 window.setTitle(title);
124                             }
125                         }
126                         CellsUndoManager undoManager = ((SwingEditvarTableModel) getCurrentModel()).getUndoManager();
127                         enableUndoButton(undoManager.canUndo());
128                         enableRedoButton(undoManager.canRedo());
129                         refreshButton.setToolTipText(tooltip);
130                     }
131                 }
132             });
133         setContentPane(tabPane);
134         setData(name, type, data);
135         WindowsConfigurationManager.restorationFinished(this);
136
137         addToolBar(createToolBar());
138         addMenuBar(createMenuBar());
139     }
140
141     /**
142      * {@inheritDoc}
143      */
144     @Override
145     public void addInfoBar(TextBox infoBarToAdd) {
146         setInfoBar(infoBarToAdd);
147     }
148
149     /**
150      * {@inheritDoc}
151      */
152     @Override
153     public void addMenuBar(MenuBar menuBarToAdd) {
154         setMenuBar(menuBarToAdd);
155     }
156
157     /**
158      * {@inheritDoc}
159      */
160     @Override
161     public void addToolBar(ToolBar toolBarToAdd) {
162         setToolBar(toolBarToAdd);
163     }
164
165     /**
166      * @return the TabbedPane used in this window
167      */
168     public ScilabTabbedPane getTabPane() {
169         return tabPane;
170     }
171
172     /**
173      * @return the current table
174      */
175     public JTable getCurrentTable() {
176         Component c = tabPane.getSelectedComponent();
177         if (c != null) {
178             return (JTable) ((JScrollPane) c).getViewport().getComponent(0);
179         }
180
181         return null;
182     }
183
184     /**
185      * @return the current table
186      */
187     public TableModel getCurrentModel() {
188         JTable t = getCurrentTable();
189         if (t != null) {
190             return t.getModel();
191         }
192
193         return null;
194     }
195
196     /**
197      * Create the toolbar
198      * @return the built toolbar
199      */
200     public ToolBar createToolBar() {
201         ToolBar toolBar = ScilabToolBar.createToolBar();
202         toolBar.add(refreshButton);
203         toolBar.addSeparator();
204         toolBar.add(CreateVariableFromSelectionAction.createButton(this, UiDataMessages.CREATE));
205         toolBar.add(CreateNewVariableAction.createButton(this, UiDataMessages.CREATENEWVAR));
206         toolBar.addSeparator();
207         toolBar.add(CutAction.createButton(this, UiDataMessages.CUT));
208         toolBar.add(CopyAction.createButton(this, UiDataMessages.COPY));
209         toolBar.add(PasteAction.createButton(this, UiDataMessages.PASTE));
210         toolBar.addSeparator();
211         toolBar.add(undoButton);
212         toolBar.add(redoButton);
213         toolBar.addSeparator();
214         toolBar.add(SizeColumnsToFitAction.createButton(this, UiDataMessages.FIT));
215         toolBar.addSeparator();
216         toolBar.add(SetPrecisionShortAction.createButton(this, UiDataMessages.SHORT));
217         toolBar.add(SetPrecisionShorteAction.createButton(this, UiDataMessages.SHORTE));
218         toolBar.add(SetPrecisionLongAction.createButton(this, UiDataMessages.LONG));
219         toolBar.add(SetPrecisionLongeAction.createButton(this, UiDataMessages.LONGE));
220         toolBar.addSeparator();
221         toolBar.add(PlotAction.createButton(this, UiDataMessages.PLOTSELECTION));
222
223         return toolBar;
224     }
225
226     /**
227      * @return a popup menu for the table
228      */
229     public JPopupMenu createPopupMenu() {
230         JPopupMenu popup = new JPopupMenu();
231         popup.setBorderPainted(true);
232         popup.add(UndoAction.createJMenuItem(this, UiDataMessages.UNDO));
233         popup.add(RedoAction.createJMenuItem(this, UiDataMessages.REDO));
234         popup.addSeparator();
235         popup.add(CutAction.createJMenuItem(this, UiDataMessages.CUT));
236         popup.add(CopyAction.createJMenuItem(this, UiDataMessages.COPY));
237         popup.add(PasteAction.createJMenuItem(this, UiDataMessages.PASTE));
238         popup.addSeparator();
239         popup.add(CopySelectionAsScilabCommandAction.createJMenuItem(this, UiDataMessages.COPYASSCILABCOM));
240         popup.add(CopySelectionAsScilabMatrixAction.createJMenuItem(this, UiDataMessages.COPYASSCILABMAT));
241         popup.add(CopySelectionAsScilabMatrixWithCRAction.createJMenuItem(this, UiDataMessages.COPYASSCILABMATWITHCR));
242         popup.addSeparator();
243         popup.add(CreateVariableFromSelectionAction.createJMenuItem(this, UiDataMessages.CREATE));
244         popup.add(CreateNewVariableAction.createJMenuItem(this, UiDataMessages.CREATENEWVAR));
245         popup.add(DuplicateVariableAction.createJMenuItem(this, UiDataMessages.DUPLICATE));
246
247         popup.addSeparator();
248
249         JMenu plots = new JMenu(UiDataMessages.PLOTSELECTION);
250         popup.add(plots);
251
252         plots.add(PlotAction.createJMenuItem(this, "plot2d", true));
253         plots.add(PlotAction.createJMenuItem(this, "Matplot", true));
254         plots.add(PlotAction.createJMenuItem(this, "grayplot", true));
255         plots.add(PlotAction.createJMenuItem(this, "Sgrayplot", true));
256         plots.add(PlotAction.createJMenuItem(this, "champ", true));
257         plots.add(PlotAction.createJMenuItem(this, "histplot", true));
258         plots.add(PlotAction.createJMenuItem(this, "mesh", true));
259         plots.add(PlotAction.createJMenuItem(this, "surf", true));
260         plots.add(PlotAction.createJMenuItem(this, "hist3d", true));
261         plots.add(PlotAction.createJMenuItem(this, "contour2d", true));
262         plots.add(PlotAction.createJMenuItem(this, "pie", true));
263
264         popup.addSeparator();
265
266         JMenu plotAll = new JMenu(UiDataMessages.PLOTALL);
267         popup.add(plotAll);
268
269         plotAll.add(PlotAction.createJMenuItem(this, "plot2d", false));
270         plotAll.add(PlotAction.createJMenuItem(this, "Matplot", false));
271         plotAll.add(PlotAction.createJMenuItem(this, "grayplot", false));
272         plotAll.add(PlotAction.createJMenuItem(this, "Sgrayplot", false));
273         plotAll.add(PlotAction.createJMenuItem(this, "champ", false));
274         plotAll.add(PlotAction.createJMenuItem(this, "histplot", false));
275         plotAll.add(PlotAction.createJMenuItem(this, "mesh", false));
276         plotAll.add(PlotAction.createJMenuItem(this, "surf", false));
277         plotAll.add(PlotAction.createJMenuItem(this, "hist3d", false));
278         plotAll.add(PlotAction.createJMenuItem(this, "contour2d", false));
279         plotAll.add(PlotAction.createJMenuItem(this, "pie", false));
280
281         return popup;
282     }
283
284     public MenuBar createMenuBar() {
285         MenuBar bar = ScilabMenuBar.createMenuBar();
286         Menu file = ScilabMenu.createMenu();
287         file.setText(UiDataMessages.FILE);
288         file.add(CreateVariableFromSelectionAction.createMenuItem(this, UiDataMessages.CREATE));
289         file.add(CreateNewVariableAction.createMenuItem(this, UiDataMessages.CREATENEWVAR));
290         file.add(DuplicateVariableAction.createMenuItem(this, UiDataMessages.DUPLICATE));
291         file.addSeparator();
292         file.add(CloseAction.createMenuItem(this, UiDataMessages.CLOSE));
293
294         bar.add(file);
295         Menu edition = ScilabMenu.createMenu();
296         edition.setText(UiDataMessages.EDITION);
297         edition.add(UndoAction.createMenuItem(this, UiDataMessages.UNDO));
298         edition.add(RedoAction.createMenuItem(this, UiDataMessages.REDO));
299         edition.addSeparator();
300         edition.add(CutAction.createMenuItem(this, UiDataMessages.CUT));
301         edition.add(CopyAction.createMenuItem(this, UiDataMessages.COPY));
302         edition.add(PasteAction.createMenuItem(this, UiDataMessages.PASTE));
303         edition.addSeparator();
304         edition.add(CopySelectionAsScilabCommandAction.createMenuItem(this, UiDataMessages.COPYASSCILABCOM));
305         edition.add(CopySelectionAsScilabMatrixAction.createMenuItem(this, UiDataMessages.COPYASSCILABMAT));
306         edition.add(CopySelectionAsScilabMatrixWithCRAction.createMenuItem(this, UiDataMessages.COPYASSCILABMATWITHCR));
307         edition.addSeparator();
308         edition.add(SizeColumnsToFitAction.createMenuItem(this, UiDataMessages.FIT));
309         bar.add(edition);
310
311         return bar;
312     }
313
314     /**
315      * {@inheritDoc}
316      */
317     @Override
318     public void setData(String name, String type, Object[][] data) {
319         TableVariableEditor table = new TableVariableEditor(this);
320
321         CopyAction.registerAction(this, table);
322         CutAction.registerAction(this, table);
323         PasteAction.registerAction(this, table);
324         SupprAction.registerAction(this, table);
325         UndoAction.registerAction(this, table);
326         RedoAction.registerAction(this, table);
327         SizeColumnsToFitAction.registerAction(this, table);
328         SetPrecisionShortAction.registerAction(this, table);
329         SetPrecisionShorteAction.registerAction(this, table);
330         SetPrecisionLongAction.registerAction(this, table);
331         SetPrecisionLongeAction.registerAction(this, table);
332         CreateVariableFromSelectionAction.registerAction(this, table);
333         CreateNewVariableAction.registerAction(this, table);
334
335         table.setComponentPopupMenu(createPopupMenu());
336
337         ScilabGenericCellEditor cellEditor = (ScilabGenericCellEditor) CellEditorFactory.createCellEditor(type);
338         int rows = Math.max(table.getMinimalRowNumber(), table.getModel().getRowCount());
339         int cols = table.getModel().getColumnCount();
340
341         SwingEditvarTableModel dataModel = new SwingEditvarTableModel(this, name, type, data, cellEditor, rows, cols);
342
343         dataModel.addTableModelListener(getRowHeader(table));
344         getRowHeader(table).tableChanged(new TableModelEvent(dataModel));
345         table.setModel(dataModel);
346         table.setDefaultEditor(Object.class, cellEditor);
347         table.setDefaultRenderer(Object.class, RendererFactory.createRenderer(type));
348
349         tabPane.add(PREFIX + name, table.getScrollPane());
350         tabPane.setSelectedComponent(table.getScrollPane());
351     }
352
353     /**
354      * @param table the table to update
355      * @param name the name of the variable
356      * @param type the type of the matrix
357      * @param data the matrix data
358      */
359     public void updateData(JTable table, String name, String type, Object[][] data) {
360         ScilabGenericCellEditor cellEditor = (ScilabGenericCellEditor) CellEditorFactory.createCellEditor(type);
361         int rows = Math.max(((TableVariableEditor) table).getMinimalRowNumber(), table.getModel().getRowCount());
362         int cols = table.getModel().getColumnCount();
363         CellsUndoManager undo = null;
364         if (table.getModel() instanceof SwingEditvarTableModel) {
365             undo = ((SwingEditvarTableModel) table.getModel()).getUndoManager();
366         }
367
368         SwingEditvarTableModel dataModel = (SwingEditvarTableModel) table.getModel();
369         getRowHeader((TableVariableEditor) table).tableChanged(new TableModelEvent(dataModel));
370         dataModel.changeData(type, data, cellEditor, rows, cols);
371         table.setDefaultEditor(Object.class, cellEditor);
372         table.setDefaultRenderer(Object.class, RendererFactory.createRenderer(type));
373     }
374
375     /**
376      * @param table the table to update
377      * @param name the name of the variable
378      * @param type the type of the matrix
379      * @param data the matrix datas
380      */
381     public void updateData(JTable table, String name, String type, Object[][] data, double[] rowsIndex, double[] colsIndex) {
382         SwingEditvarTableModel dataModel = (SwingEditvarTableModel) table.getModel();
383         if (!type.equals(dataModel.getType())) {
384             dataModel.refresh();
385         } else {
386             for (int i = 0; i < rowsIndex.length; i++) {
387                 for (int j = 0; j < colsIndex.length; j++) {
388                     int r = (int) rowsIndex[i] - 1;
389                     int c = (int) colsIndex[j] - 1;
390                     dataModel.setValueAtAndUpdate(false, false, data[i][j], r, c);
391                 }
392             }
393         }
394     }
395
396     /**
397      * @param c the component in the tabbedpane
398      * @param name the name of the variable
399      * @param type the type of the matrix
400      * @param data the matrix data
401      */
402     public void updateData(Component c, String name, String type, Object[][] data) {
403         tabPane.setSelectedComponent(c);
404         JTable table = (JTable) ((JScrollPane) c).getViewport().getComponent(0);
405         updateData(table, name, type, data);
406     }
407
408     /**
409      * @param c the component in the tabbedpane
410      * @param name the name of the variable
411      * @param type the type of the matrix
412      * @param data the matrix datas
413      */
414     public void updateData(Component c, String name, String type, Object[][] data, double[] rowsIndex, double[] colsIndex) {
415         tabPane.setSelectedComponent(c);
416         JTable table = (JTable) ((JScrollPane) c).getViewport().getComponent(0);
417         updateData(table, name, type, data, rowsIndex, colsIndex);
418     }
419
420     /**
421      * @return the row header used in this table
422      */
423     public RowHeader getRowHeader(TableVariableEditor table) {
424         return (RowHeader) table.getScrollPane().getRowHeader().getView();
425     }
426
427     /**
428      * {@inheritDoc}
429      */
430     @Override
431     public SimpleTab getAsSimpleTab() {
432         return this;
433     }
434
435     /**
436      * {@inheritDoc}
437      */
438     @Override
439     public Window getParentWindow() {
440         return (Window) UIElementMapper.getCorrespondingUIElement(getParentWindowId());
441     }
442
443     /**
444      * @param b true to enable the button
445      */
446     public void enableUndoButton(boolean b) {
447         if (undoButton != null) {
448             undoButton.setEnabled(b);
449         }
450     }
451
452     /**
453      * @param b true to enable the button
454      */
455     public void enableRedoButton(boolean b) {
456         if (redoButton != null) {
457             redoButton.setEnabled(b);
458         }
459     }
460 }