Enable Toolbar, mutualize methods for buttons and menus
[scilab.git] / scilab / modules / xpad / src / java / org / scilab / modules / xpad / Xpad.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009 - DIGITEO - Bruno JOFRET
4  *
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
10  *
11  */
12
13 package org.scilab.modules.xpad;
14
15 import java.awt.Color;
16 import java.io.File;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.Scanner;
20
21 import javax.swing.BorderFactory;
22 import javax.swing.JOptionPane;
23 import javax.swing.JScrollPane;
24 import javax.swing.JTabbedPane;
25 import javax.swing.JTextPane;
26 import javax.swing.undo.CannotRedoException;
27 import javax.swing.undo.CannotUndoException;
28 import javax.swing.undo.UndoManager;
29
30 import org.scilab.modules.gui.bridge.tab.SwingScilabTab;
31 import org.scilab.modules.gui.menu.Menu;
32 import org.scilab.modules.gui.menu.ScilabMenu;
33 import org.scilab.modules.gui.menubar.MenuBar;
34 import org.scilab.modules.gui.menubar.ScilabMenuBar;
35 import org.scilab.modules.gui.tab.SimpleTab;
36 import org.scilab.modules.gui.tab.Tab;
37 import org.scilab.modules.gui.textbox.ScilabTextBox;
38 import org.scilab.modules.gui.textbox.TextBox;
39 import org.scilab.modules.gui.toolbar.ScilabToolBar;
40 import org.scilab.modules.gui.toolbar.ToolBar;
41 import org.scilab.modules.gui.window.ScilabWindow;
42 import org.scilab.modules.gui.window.Window;
43 import org.scilab.modules.xpad.actions.ASCIIEncodingAction;
44 import org.scilab.modules.xpad.actions.AboutAction;
45 import org.scilab.modules.xpad.actions.AutoIndentAction;
46 import org.scilab.modules.xpad.actions.CloseAction;
47 import org.scilab.modules.xpad.actions.ColorizeAction;
48 import org.scilab.modules.xpad.actions.CommentAction;
49 import org.scilab.modules.xpad.actions.CopyAction;
50 import org.scilab.modules.xpad.actions.CutAction;
51 import org.scilab.modules.xpad.actions.DeleteAction;
52 import org.scilab.modules.xpad.actions.EvaluateSelectionAction;
53 import org.scilab.modules.xpad.actions.ExitAction;
54 import org.scilab.modules.xpad.actions.FindAction;
55 import org.scilab.modules.xpad.actions.GotoLineAction;
56 import org.scilab.modules.xpad.actions.HelpAction;
57 import org.scilab.modules.xpad.actions.HighlightCurrentLineAction;
58 import org.scilab.modules.xpad.actions.IndentAction;
59 import org.scilab.modules.xpad.actions.LineNumbersAction;
60 import org.scilab.modules.xpad.actions.LoadIntoScilabAction;
61 import org.scilab.modules.xpad.actions.NewAction;
62 import org.scilab.modules.xpad.actions.OpenAction;
63 import org.scilab.modules.xpad.actions.PageSetupAction;
64 import org.scilab.modules.xpad.actions.PasteAction;
65 import org.scilab.modules.xpad.actions.PrintAction;
66 import org.scilab.modules.xpad.actions.PrintPreviewAction;
67 import org.scilab.modules.xpad.actions.RecentFileAction;
68 import org.scilab.modules.xpad.actions.RedoAction;
69 import org.scilab.modules.xpad.actions.ResetFontAction;
70 import org.scilab.modules.xpad.actions.SaveAction;
71 import org.scilab.modules.xpad.actions.SaveAsAction;
72 import org.scilab.modules.xpad.actions.ScilabStyleAction;
73 import org.scilab.modules.xpad.actions.SelectAllAction;
74 import org.scilab.modules.xpad.actions.SetColorsAction;
75 import org.scilab.modules.xpad.actions.SetFontAction;
76 import org.scilab.modules.xpad.actions.ShowToolBarAction;
77 import org.scilab.modules.xpad.actions.TextStyleAction;
78 import org.scilab.modules.xpad.actions.UTF8EncodingAction;
79 import org.scilab.modules.xpad.actions.UnCommentAction;
80 import org.scilab.modules.xpad.actions.UndoAction;
81 import org.scilab.modules.xpad.actions.WordWrapAction;
82 import org.scilab.modules.xpad.actions.XMLStyleAction;
83 import org.scilab.modules.xpad.style.ScilabStyleDocument;
84 import org.scilab.modules.xpad.utils.ConfigXpadManager;
85 import org.scilab.modules.xpad.utils.XpadMessages;
86
87 public class Xpad extends SwingScilabTab implements Tab { 
88
89         private final Window parentWindow;
90         private JTabbedPane tabPane;
91         private JTextPane textPane;
92         private JScrollPane scrollingText;
93         private XpadLineNumberPanel xln;
94
95         private static Xpad editor = null;
96
97         public static void main(String[] args) {
98                 xpad();
99         }
100
101         public static void xpad() {
102                 Xpad editor = launchXpad();
103                 editor.addEmptyTab();
104         }
105
106         public static void xpad(String filePath) {
107                 Xpad editor = launchXpad();
108                 editor.readFile(new File(filePath));
109         }
110
111         private static Xpad launchXpad() {
112                 if (editor == null) {
113                         editor = createEditor();
114                 }
115                 return editor;
116         }
117
118         public static void closeXpad() {
119                 editor = null;
120         }
121
122         private static Xpad createEditor() {
123                 ConfigXpadManager.createUserCopy();     
124                 ArrayList<File> recentFiles = ConfigXpadManager.getAllRecentOpenedFiles() ;
125
126                 Window mainWindow = ScilabWindow.createWindow();
127                 Xpad editor = new Xpad(mainWindow);
128
129                 mainWindow.setTitle("Xpad");
130                 mainWindow.addTab(editor);
131
132                 // Set Xpad Window position /size 
133                 mainWindow.setPosition( ConfigXpadManager.getMainWindowPosition());
134                 mainWindow.setDims(ConfigXpadManager.getMainWindowSize());
135
136                 MenuBar menuBar = ScilabMenuBar.createMenuBar();
137                 //Create FILE menubar
138                 Menu fileMenu = ScilabMenu.createMenu();
139                 fileMenu.setText(XpadMessages.FILE);
140                 fileMenu.setMnemonic('F');
141                 fileMenu.add(NewAction.createMenu(editor));
142                 fileMenu.add(OpenAction.createMenu(editor));
143                 Menu recentsMenu = ScilabMenu.createMenu();
144                 recentsMenu.setText("Recent Files");
145                 for (int i = 0 ; i < recentFiles.size() ; i++ ){
146                         recentsMenu.add(RecentFileAction.createMenu(editor , recentFiles.get(i)));
147                 }
148                 
149                 fileMenu.add(recentsMenu);
150                 
151                 fileMenu.addSeparator();
152                 fileMenu.add(SaveAction.createMenu(editor));
153                 fileMenu.add(SaveAsAction.createMenu(editor));
154                 fileMenu.addSeparator();
155                 fileMenu.add(PageSetupAction.createMenu(editor));
156                 fileMenu.add(PrintPreviewAction.createMenu(editor));
157                 fileMenu.add(PrintAction.createMenu(editor));
158                 fileMenu.addSeparator();
159                 fileMenu.add(CloseAction.createMenu(editor));
160                 fileMenu.addSeparator();
161                 fileMenu.add(ExitAction.createMenu(editor));
162                 menuBar.add(fileMenu);
163
164                 //Create EDIT menubar
165                 Menu editMenu = ScilabMenu.createMenu();
166                 editMenu.setText(XpadMessages.EDIT); 
167                 editMenu.setMnemonic('E');
168                 editMenu.add(UndoAction.createMenu(editor));
169                 editMenu.add(RedoAction.createMenu(editor));
170                 editMenu.addSeparator(); 
171                 editMenu.add(CutAction.createMenu(editor));
172                 editMenu.add(CopyAction.createMenu(editor));
173                 editMenu.add(PasteAction.createMenu(editor));
174                 editMenu.addSeparator(); 
175                 editMenu.add(SelectAllAction.createMenu(editor));
176                 editMenu.add(DeleteAction.createMenu(editor));
177                 editMenu.addSeparator();
178                 editMenu.add(CommentAction.createMenu(editor));
179                 editMenu.add(UnCommentAction.createMenu(editor));
180                 editMenu.addSeparator();
181                 editMenu.add(IndentAction.createMenu(editor));
182                 menuBar.add(editMenu);
183
184                 // Create SEARCH menubar
185                 Menu searchMenu = ScilabMenu.createMenu(); 
186                 searchMenu.setText(XpadMessages.SEARCH);
187                 searchMenu.setMnemonic('S');
188                 searchMenu.add(FindAction.createMenu(editor));
189                 searchMenu.add(GotoLineAction.createMenu(editor));
190                 menuBar.add(searchMenu);
191
192                 // Create VIEW Menubar
193                 Menu viewMenu = ScilabMenu.createMenu();
194                 viewMenu.setText(XpadMessages.VIEW);
195                 viewMenu.add(ShowToolBarAction.createCheckBoxMenu(editor));
196                 viewMenu.addSeparator();
197                 viewMenu.add(HighlightCurrentLineAction.createCheckBoxMenu(editor));
198                 viewMenu.add(WordWrapAction.createCheckBoxMenu(editor));
199                 viewMenu.add(LineNumbersAction.createCheckBoxMenu(editor));
200                 viewMenu.add(SetColorsAction.createMenu(editor));
201                 viewMenu.add(SetFontAction.createMenu(editor));
202                 viewMenu.add(ResetFontAction.createMenu(editor));
203                 menuBar.add(viewMenu);
204
205                 // Create DOCUMENT MenuBar
206                 Menu documentMenu = ScilabMenu.createMenu();
207                 documentMenu.setText(XpadMessages.DOCUMENT);
208                 Menu syntaxTypeMenu = ScilabMenu.createMenu();
209                 syntaxTypeMenu.setText("Syntax Type");
210                 documentMenu.add(syntaxTypeMenu);
211                 syntaxTypeMenu.add(TextStyleAction.createCheckBoxMenu(editor));
212                 syntaxTypeMenu.add(ScilabStyleAction.createCheckBoxMenu(editor));
213                 syntaxTypeMenu.add(XMLStyleAction.createCheckBoxMenu(editor));
214                 documentMenu.addSeparator();
215                 Menu encodingTypeMenu = ScilabMenu.createMenu();
216                 encodingTypeMenu.setText("Encoding Type");
217                 documentMenu.add(encodingTypeMenu);
218                 encodingTypeMenu.add(ASCIIEncodingAction.createCheckBoxMenu(editor));
219                 encodingTypeMenu.add(UTF8EncodingAction.createCheckBoxMenu(editor));
220                 documentMenu.addSeparator();
221                 documentMenu.add(ColorizeAction.createMenu(editor));
222                 documentMenu.add(AutoIndentAction.createCheckBoxMenu(editor));
223                 menuBar.add(documentMenu);
224
225                 // Create EXECUTE menubar
226                 Menu executeMenu = ScilabMenu.createMenu();
227                 executeMenu.setText(XpadMessages.EXECUTE);
228                 executeMenu.setMnemonic('e');
229                 executeMenu.add(LoadIntoScilabAction.createMenu(editor));
230                 executeMenu.add(EvaluateSelectionAction.createMenu(editor));
231                 menuBar.add(executeMenu);
232
233                 //Create HELP menubar
234                 Menu helpMenu = ScilabMenu.createMenu();
235                 helpMenu.setText("?");
236                 helpMenu.add(HelpAction.createMenu(editor));
237                 helpMenu.add(AboutAction.createMenu(editor));
238                 menuBar.add(helpMenu);
239
240                 // Create TOOLBAR
241                 ToolBar toolBar = ScilabToolBar.createToolBar();
242                 toolBar.add(NewAction.createButton(editor)); // NEW
243                 toolBar.add(OpenAction.createButton(editor)); // OPEN
244                 toolBar.addSeparator();
245                 toolBar.add(SaveAction.createButton(editor)); // SAVE
246                 //toolBar.add(SaveAsAction.createButton(editor)); // SAVE AS
247                 toolBar.addSeparator();
248                 //toolBar.add(PrintPreviewAction.createButton(editor)); // PRINT PREVIEW
249                 toolBar.add(PrintAction.createButton(editor)); // PRINT
250                 toolBar.addSeparator();
251                 toolBar.add(UndoAction.createButton(editor)); // UNDO
252                 toolBar.add(RedoAction.createButton(editor)); // REDO
253                 toolBar.addSeparator();
254                 toolBar.add(CutAction.createButton(editor)); // CUT
255                 toolBar.add(CopyAction.createButton(editor)); // COPY
256                 toolBar.add(PasteAction.createButton(editor)); // PASTE
257                 toolBar.addSeparator();
258                 toolBar.add(FindAction.createButton(editor)); // FIND / REPLACE
259                 
260                 
261                 TextBox infoBar = ScilabTextBox.createTextBox();
262                 
263                 
264                 editor.setMenuBar(menuBar);
265                 editor.setToolBar(toolBar);
266                 editor.setInfoBar(infoBar);
267                 mainWindow.setTitle("XPad");
268                 mainWindow.setVisible(true);
269                 editor.setCallback(ExitAction.createMenu(editor).getCallback());
270                 return editor;
271         }
272
273         public Xpad(Window parentWindow) {
274                 super("Xpad");
275                 this.parentWindow = parentWindow;
276                 tabPane = new JTabbedPane();
277                 this.setContentPane(tabPane);
278         }
279
280         public void closeCurrentTab() {
281                 tabPane.remove(tabPane.getSelectedComponent());
282         }
283
284         public JTextPane addTab(String title) {
285                 textPane = new JTextPane();
286 // {
287 //                      public boolean getScrollableTracksViewportWidth() {
288 //                              return false;
289 //                      };
290 //              };
291
292                 scrollingText = new JScrollPane(textPane);
293                 
294                 // Panel of line number for the text pane
295                 xln = new XpadLineNumberPanel(textPane);
296                 scrollingText.setRowHeaderView(xln);
297                 
298                 tabPane.add(title, scrollingText);
299                 tabPane.setSelectedIndex(tabPane.getTabCount() - 1);
300                 this.setContentPane(tabPane);
301                 textPane.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
302                 textPane.setFont(ConfigXpadManager.getFont());
303
304                 textPane.setBackground(ConfigXpadManager.getXpadBackgroundColor());
305                 textPane.setCaretColor(Color.BLACK);
306                 textPane.setStyledDocument(new ScilabStyleDocument());
307                 textPane.setCharacterAttributes(textPane.getStyle("Default"), true);
308
309                 textPane.setFocusable(true);
310
311                 return textPane;
312         }
313
314         public JTextPane addEmptyTab() {
315                 return addTab("Untitled");
316         }
317
318         public void setAutoIndent(boolean b) {
319                 ((ScilabStyleDocument) getTextPane().getStyledDocument()).setAutoIndent(b);
320         }
321
322         public void undo() {
323                 UndoManager undo = ((ScilabStyleDocument) getTextPane().getStyledDocument()).getUndoManager();
324                 try {
325                         System.err.println("Will undo "+undo.getUndoPresentationName());
326                         undo.undo();
327                 } catch (CannotUndoException ex) {
328                         System.out.println("Unable to undo: " + ex);
329                         ex.printStackTrace();
330                 }
331         }
332
333         public void redo() {
334                 UndoManager redo = ((ScilabStyleDocument) getTextPane().getStyledDocument()).getUndoManager();
335                 try {
336                         System.err.println("Will redo "+redo.getRedoPresentationName());
337                         redo.redo();
338                 } catch (CannotRedoException ex) {
339                         System.out.println("Unable to redo: " + ex);
340                         ex.printStackTrace();
341                 }
342         }
343
344         public void readFile(File f) {
345                 /* First try to open file before creating tab */
346                 try {
347                         StringBuilder contents = new StringBuilder();
348                         Scanner scanner = new Scanner(f);
349                         while (scanner.hasNextLine()) {
350                                 contents.append(scanner.nextLine());
351                                 contents.append(System.getProperty("line.separator"));
352                         }
353                         JTextPane textPane = addTab(f.getName()); 
354                         System.out.println("File = "+f.getAbsolutePath());
355                         textPane.setName(f.getAbsolutePath());
356
357                         ((ScilabStyleDocument) textPane.getStyledDocument()).disableUpdaters(); 
358                         textPane.setText(contents.toString());
359                         if (((ScilabStyleDocument) textPane.getStyledDocument()).getColorize()) {
360                                 ((ScilabStyleDocument) textPane.getStyledDocument()).colorize();
361                         }
362                         if (((ScilabStyleDocument) textPane.getStyledDocument()).getAutoIndent()) {
363                                 ((ScilabStyleDocument) textPane.getStyledDocument()).indent();
364                         }
365                         ((ScilabStyleDocument) textPane.getStyledDocument()).enableUpdaters();
366                 } catch (IOException ioex) {
367                         JOptionPane.showMessageDialog(this, ioex.getMessage());
368                 }
369         }
370
371         public JTextPane getTextPane() {
372                 return (JTextPane) ((JScrollPane) tabPane.getSelectedComponent()).getViewport().getComponent(0);
373         }
374
375         public SimpleTab getAsSimpleTab() {
376                 return this;
377         }
378
379         public Window getParentWindow() {
380                 return parentWindow;
381         }
382
383         public void addInfoBar(TextBox infoBarToAdd) {
384                 setInfoBar(infoBarToAdd);
385         }
386
387         public void addMenuBar(MenuBar menuBarToAdd) {
388                 setMenuBar(menuBarToAdd);
389         }
390
391         public void addToolBar(ToolBar toolBarToAdd) {
392                 setToolBar(toolBarToAdd);
393         }
394
395         public JTabbedPane getTabPane() {
396                 return tabPane;
397         }
398
399         public void setTabPane(JTabbedPane tabPane) {
400                 this.tabPane = tabPane;
401         }
402
403         public XpadLineNumberPanel getXln() {
404                 return xln;
405         }
406
407         public void setXln(XpadLineNumberPanel xln) {
408                 this.xln = xln;
409         }
410         
411         public void displayLineNumbers(boolean display) {
412                 if (display) {
413                         scrollingText.setRowHeaderView(xln);
414                         repaint();
415                 } else {
416                         scrollingText.setRowHeaderView(null);
417                         repaint();
418                 }
419         }
420         
421         public void enableLineHighlight(boolean display) {
422                 xln.setCurrentLineHighlightColor(display);
423                 repaint();
424         }
425
426 }