~ubuntu-branches/ubuntu/precise/scilab/precise

« back to all changes in this revision

Viewing changes to modules/history_browser/src/java/org/scilab/modules/history_browser/CommandHistory.java

Tags: 5.3.0-1ubuntu1
* New upstream release
* URL in the watch file updated

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 
3
 * Copyright (C) 2010 - DIGITEO - Vincent COUVERT
 
4
 * Copyright (C) 2010 - DIGITEO - Allan CORNET
 
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.history_browser;
 
15
 
 
16
import java.awt.BorderLayout;
 
17
 
 
18
import javax.swing.JPanel;
 
19
import javax.swing.JScrollPane;
 
20
import javax.swing.JTree;
 
21
import javax.swing.tree.DefaultMutableTreeNode;
 
22
import javax.swing.tree.DefaultTreeModel;
 
23
import javax.swing.tree.TreeNode;
 
24
import javax.swing.tree.TreePath;
 
25
 
 
26
import org.flexdock.docking.Dockable;
 
27
import org.flexdock.docking.DockingManager;
 
28
import org.scilab.modules.gui.bridge.tab.SwingScilabTab;
 
29
import org.scilab.modules.gui.menu.Menu;
 
30
import org.scilab.modules.gui.menu.ScilabMenu;
 
31
import org.scilab.modules.gui.menubar.MenuBar;
 
32
import org.scilab.modules.gui.menubar.ScilabMenuBar;
 
33
import org.scilab.modules.gui.messagebox.MessageBox;
 
34
import org.scilab.modules.gui.messagebox.ScilabMessageBox;
 
35
import org.scilab.modules.gui.tab.ScilabTab;
 
36
import org.scilab.modules.gui.tab.Tab;
 
37
import org.scilab.modules.gui.textbox.ScilabTextBox;
 
38
import org.scilab.modules.gui.toolbar.ScilabToolBar;
 
39
import org.scilab.modules.gui.toolbar.ToolBar;
 
40
import org.scilab.modules.gui.utils.Size;
 
41
import org.scilab.modules.gui.window.ScilabWindow;
 
42
import org.scilab.modules.gui.window.Window;
 
43
import org.scilab.modules.history_manager.HistoryManagement;
 
44
import org.scilab.modules.history_browser.CommandHistoryMessages;
 
45
import org.scilab.modules.history_browser.CommandHistoryMouseListener;
 
46
import org.scilab.modules.history_browser.actions.ClearAction;
 
47
import org.scilab.modules.history_browser.actions.CloseAction;
 
48
import org.scilab.modules.history_browser.actions.CopyAction;
 
49
import org.scilab.modules.history_browser.actions.CutAction;
 
50
import org.scilab.modules.history_browser.actions.DeleteAction;
 
51
import org.scilab.modules.history_browser.actions.EditInScinotesAction;
 
52
import org.scilab.modules.history_browser.actions.EvaluateAction;
 
53
import org.scilab.modules.history_browser.actions.HelpAction;
 
54
 
 
55
/**
 
56
 * Main Scilab Command History GUI
 
57
 * @author Vincent COUVERT
 
58
 */
 
59
public final class CommandHistory {
 
60
 
 
61
        private static final int DEFAULT_WIDTH = 450;
 
62
        private static final int DEFAULT_HEIGHT = 550;
 
63
        private static final String NEWLINE = "\n";
 
64
        private static final String SESSION_BEGINNING = "// -- ";
 
65
        private static final String SESSION_ENDING = " -- //";
 
66
 
 
67
        private static JTree scilabHistoryTree;
 
68
 
 
69
        private static DefaultMutableTreeNode scilabHistoryRootNode;
 
70
 
 
71
        private static DefaultMutableTreeNode currentSessionNode;
 
72
 
 
73
        private static DefaultTreeModel scilabHistoryTreeModel;
 
74
 
 
75
        private static Tab browserTab;
 
76
 
 
77
        private static JScrollPane scrollPane;
 
78
 
 
79
        /**
 
80
         * Constructor
 
81
         */
 
82
        private CommandHistory() {
 
83
        }
 
84
 
 
85
        /**
 
86
         * Initialize the History Browser at Scilab launch
 
87
         * Called directly from Scilab
 
88
         */
 
89
        public static void initialize() {
 
90
                Window browserWindow = ScilabWindow.createWindow();
 
91
                browserWindow.setDims(new Size(DEFAULT_WIDTH, DEFAULT_HEIGHT));
 
92
 
 
93
                browserTab = ScilabTab.createTab(CommandHistoryMessages.TITLE);
 
94
                browserTab.setCallback(CloseAction.getCallBack());
 
95
                browserTab.addMenuBar(createMenuBar());
 
96
                browserTab.addToolBar(createToolBar());
 
97
                browserTab.addInfoBar(ScilabTextBox.createTextBox());
 
98
 
 
99
                browserWindow.addTab(browserTab);
 
100
 
 
101
                scilabHistoryRootNode = new DefaultMutableTreeNode(""); // Will not be visible
 
102
                scilabHistoryTreeModel = new DefaultTreeModel(scilabHistoryRootNode);
 
103
                scilabHistoryTree = new JTree(scilabHistoryTreeModel);
 
104
                scilabHistoryTree.setShowsRootHandles(true);
 
105
                scilabHistoryTree.setDragEnabled(true);
 
106
                scilabHistoryTree.setEnabled(true);
 
107
                scilabHistoryTree.setRootVisible(false);
 
108
                scilabHistoryTree.setScrollsOnExpand(true);
 
109
 
 
110
                scilabHistoryTree.addMouseListener(new CommandHistoryMouseListener());
 
111
 
 
112
                DeleteAction.registerKeyAction();
 
113
                EvaluateAction.registerKeyAction();
 
114
                CopyAction.registerKeyAction();
 
115
                CutAction.registerKeyAction();
 
116
                CloseAction.registerKeyAction();
 
117
 
 
118
                scrollPane = new JScrollPane(scilabHistoryTree);
 
119
                JPanel contentPane = new JPanel(new BorderLayout());
 
120
                contentPane.add(scrollPane);
 
121
                ((SwingScilabTab) browserTab.getAsSimpleTab()).setContentPane(contentPane);
 
122
                setVisible(false);
 
123
        }
 
124
 
 
125
        /**
 
126
         * Update the browser once an history file has been loaded
 
127
         */
 
128
        public static void loadFromFile() {
 
129
                reset();
 
130
                String historyLines[] = HistoryManagement.getAllLinesOfScilabHistory();
 
131
                int nbEntries = historyLines.length;
 
132
                for (int entryIndex = 0; entryIndex < nbEntries; entryIndex++) {
 
133
                   /* Do not expand at each insertion for performances reasons */
 
134
                   appendLineAndExpand(historyLines[entryIndex], false);
 
135
                }
 
136
                /* Expand all sessions tree */
 
137
                scilabHistoryTreeModel.nodeStructureChanged((TreeNode) scilabHistoryTreeModel.getRoot());
 
138
                for (int i = 0; i < scilabHistoryTree.getRowCount(); i++) {
 
139
                   scilabHistoryTree.expandRow(i);
 
140
                }
 
141
                scilabHistoryTree.scrollPathToVisible(scilabHistoryTree.getPathForRow(scilabHistoryTree.getRowCount() - 1));
 
142
        }
 
143
 
 
144
        /**
 
145
         * Add a new line the the History Browser
 
146
         * @param lineToAppend the line to append
 
147
         */
 
148
        public static void appendLine(String lineToAppend) {
 
149
                appendLineAndExpand(lineToAppend, true);
 
150
        }
 
151
 
 
152
        /**
 
153
        * check if line is a begin session
 
154
        * @param line to check
 
155
        * @retour true or false
 
156
        */
 
157
        private static boolean isBeginSessionLine(String lineToAppend) {
 
158
                if (lineToAppend.startsWith(SESSION_BEGINNING)
 
159
                        && lineToAppend.endsWith(SESSION_ENDING)) {
 
160
                       return true;
 
161
                    }
 
162
               return false;
 
163
        }
 
164
 
 
165
        /**
 
166
         * Add a new line the the History Browser
 
167
         * @param lineToAppend the line to append
 
168
         * @param expand do we need to expand all session nodes?
 
169
         */
 
170
        public static void appendLineAndExpand(String lineToAppend, boolean expand) {
 
171
                if (isBeginSessionLine(lineToAppend)) {
 
172
                        // Create a new session node
 
173
                        currentSessionNode = new DefaultMutableTreeNode(lineToAppend);
 
174
                        scilabHistoryTreeModel.insertNodeInto(currentSessionNode, scilabHistoryRootNode, scilabHistoryRootNode.getChildCount());
 
175
                        if (expand) {
 
176
                                /* Expand all sessions tree */
 
177
                                for (int i = 0; i < scilabHistoryTree.getRowCount(); i++) {
 
178
                                        scilabHistoryTree.expandRow(i);
 
179
                                }
 
180
                                scilabHistoryTree.scrollPathToVisible(new TreePath(currentSessionNode.getPath()));
 
181
                                scilabHistoryTreeModel.nodeStructureChanged((TreeNode) scilabHistoryTreeModel.getRoot());
 
182
                        }
 
183
                } else {
 
184
                        DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(lineToAppend);
 
185
                        scilabHistoryTreeModel.insertNodeInto(childNode, currentSessionNode, currentSessionNode.getChildCount());
 
186
                        if (expand) {
 
187
                                scilabHistoryTree.expandRow(scilabHistoryTree.getRowCount() - 1);
 
188
                                scilabHistoryTree.scrollPathToVisible(new TreePath(childNode.getPath()));
 
189
                                scilabHistoryTreeModel.nodeStructureChanged((TreeNode) currentSessionNode);
 
190
                        }
 
191
                }
 
192
        }
 
193
 
 
194
        /**
 
195
         * Reset the History Browser (after a clear)
 
196
         */
 
197
        public static void reset() {
 
198
                scilabHistoryRootNode.removeAllChildren();
 
199
                scilabHistoryTreeModel.reload();
 
200
                currentSessionNode = null;
 
201
        }
 
202
 
 
203
        /**
 
204
         * Remove an entry from History
 
205
         * @param lineNumber the number of the line
 
206
         */
 
207
        public static void deleteLine(int lineNumber) {
 
208
                int numberOfSessions = scilabHistoryRootNode.getChildCount();
 
209
                int sessionIndex = 0;
 
210
                int numberOfLines = 0;
 
211
                while (sessionIndex < numberOfSessions) {
 
212
 
 
213
                        if (numberOfLines == lineNumber) {
 
214
                                if (sessionIndex == (numberOfSessions - 1)) {
 
215
                                        /* Can not remove current session node */
 
216
                                        MessageBox errorMsg = ScilabMessageBox.createMessageBox();
 
217
                                        errorMsg.setTitle(CommandHistoryMessages.ERROR);
 
218
                                        errorMsg.setMessage(CommandHistoryMessages.CANNOT_DELETE_CURRENT_SESSION_NODE);
 
219
                                        errorMsg.setIcon("error");
 
220
                                        errorMsg.displayAndWait();
 
221
                                        return;
 
222
                                }
 
223
                                scilabHistoryRootNode.remove(sessionIndex);
 
224
                                scilabHistoryTreeModel.nodeStructureChanged((TreeNode) scilabHistoryTreeModel.getRoot());
 
225
                                break;
 
226
                        }
 
227
 
 
228
                        /* Session line */
 
229
                        numberOfLines++;
 
230
 
 
231
                        if (numberOfLines + scilabHistoryRootNode.getChildAt(sessionIndex).getChildCount() > lineNumber) {
 
232
                                /* The line has to be remove in current session */
 
233
                                ((DefaultMutableTreeNode) scilabHistoryRootNode.getChildAt(sessionIndex)).remove(lineNumber - numberOfLines);
 
234
                                scilabHistoryTreeModel.nodeStructureChanged((TreeNode) scilabHistoryRootNode.getChildAt(sessionIndex));
 
235
                                break;
 
236
                        } else {
 
237
                                /* An other session */
 
238
                                numberOfLines += scilabHistoryRootNode.getChildAt(sessionIndex).getChildCount();
 
239
                                sessionIndex++;
 
240
                        }
 
241
 
 
242
                }
 
243
        }
 
244
 
 
245
        /**
 
246
         * Manage History Browser visibility
 
247
         * @param status visibility status
 
248
         */
 
249
        public static void setVisible(boolean status) {
 
250
                if (status && browserTab.getParentWindow() == null) {
 
251
                    Window browserWindow = ScilabWindow.createWindow();
 
252
                    browserWindow.setVisible(true);
 
253
                    browserWindow.addTab(browserTab);
 
254
                }
 
255
 
 
256
                if (browserTab.getParentWindow() != null) {
 
257
                    if (browserTab.getParentWindow().getNbDockedObjects() == 1) {
 
258
                        browserTab.getParentWindow().setVisible(status);
 
259
                    } else {
 
260
                        DockingManager.undock((Dockable) browserTab.getAsSimpleTab());
 
261
                        browserTab.setParentWindowId(-1);
 
262
                    }
 
263
                }
 
264
 
 
265
                browserTab.setVisible(status);
 
266
        }
 
267
 
 
268
        /**
 
269
         * Get History Browser visibility
 
270
         * @return visibility status
 
271
         */
 
272
        private static boolean isVisible() {
 
273
                return browserTab.isVisible();
 
274
        }
 
275
 
 
276
        /**
 
277
         * Toggle History Browser visibility
 
278
         */
 
279
        public static void toggleVisibility() {
 
280
                setVisible(!isVisible());
 
281
        }
 
282
 
 
283
        /**
 
284
         * Create History Browser MenuBar
 
285
         * @return the menu bar
 
286
         */
 
287
        private static MenuBar createMenuBar() {
 
288
                MenuBar menuBar = ScilabMenuBar.createMenuBar();
 
289
 
 
290
                Menu fileMenu = ScilabMenu.createMenu();
 
291
                fileMenu.setText(CommandHistoryMessages.FILE);
 
292
                fileMenu.setMnemonic('F');
 
293
 
 
294
                fileMenu.add(CloseAction.createMenuItem());
 
295
 
 
296
                menuBar.add(fileMenu);
 
297
 
 
298
                Menu editMenu = ScilabMenu.createMenu();
 
299
                editMenu.setText(CommandHistoryMessages.EDIT);
 
300
                editMenu.setMnemonic('E');
 
301
 
 
302
                editMenu.add(CopyAction.createMenuItem());
 
303
                editMenu.add(CutAction.createMenuItem());
 
304
                editMenu.add(EvaluateAction.createMenuItem());
 
305
                editMenu.add(EditInScinotesAction.createMenuItem());
 
306
                editMenu.addSeparator();
 
307
                editMenu.add(DeleteAction.createMenuItem());
 
308
                editMenu.add(ClearAction.createMenuItem());
 
309
 
 
310
                menuBar.add(editMenu);
 
311
 
 
312
                Menu helpMenu = ScilabMenu.createMenu();
 
313
                helpMenu.setText(CommandHistoryMessages.HELP);
 
314
                helpMenu.setMnemonic('?');
 
315
 
 
316
                helpMenu.add(HelpAction.createMenuItem());
 
317
 
 
318
                menuBar.add(helpMenu);
 
319
 
 
320
                return menuBar;
 
321
        }
 
322
 
 
323
        /**
 
324
         * Create History Browser ToolBar
 
325
         * @return the tool bar
 
326
         */
 
327
        private static ToolBar createToolBar() {
 
328
                ToolBar toolBar = ScilabToolBar.createToolBar();
 
329
 
 
330
                toolBar.add(CopyAction.createPushButton());
 
331
                toolBar.add(CutAction.createPushButton());
 
332
                toolBar.add(DeleteAction.createPushButton());
 
333
 
 
334
                toolBar.addSeparator();
 
335
 
 
336
                toolBar.add(HelpAction.createPushButton());
 
337
 
 
338
                return toolBar;
 
339
        }
 
340
 
 
341
        /**
 
342
         * Get the JTree
 
343
         * @return the tree
 
344
         */
 
345
        public static JTree getTree() {
 
346
                return scilabHistoryTree;
 
347
        }
 
348
 
 
349
        /**
 
350
         * Get the JTree Model
 
351
         * @return the tree model
 
352
         */
 
353
        public static DefaultTreeModel getTreeModel() {
 
354
                return scilabHistoryTreeModel;
 
355
        }
 
356
 
 
357
        /**
 
358
         * Get the selected commands and store them into an "executable" string
 
359
         * @return the string
 
360
         */
 
361
        public static String getSelectedCommands() {
 
362
                TreePath[] selectedPaths = CommandHistory.getTree().getSelectionPaths();
 
363
 
 
364
                if (selectedPaths == null) {
 
365
                        return null;
 
366
                }
 
367
 
 
368
                String selectedEntries = new String();
 
369
 
 
370
                for (int i = 0; i < selectedPaths.length; i++) {
 
371
                        selectedEntries += selectedPaths[i].getLastPathComponent().toString();
 
372
                        if (i < selectedPaths.length - 1) {
 
373
                                selectedEntries += NEWLINE;
 
374
                        }
 
375
                }
 
376
 
 
377
                return selectedEntries;
 
378
        }
 
379
}