shortcuts for xpad
[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.awt.Event;
17 import java.awt.event.ActionEvent;
18 import java.awt.event.KeyEvent;
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.Scanner;
22
23 import javax.swing.BorderFactory;
24 import javax.swing.JMenuItem;
25 import javax.swing.JOptionPane;
26 import javax.swing.JScrollPane;
27 import javax.swing.JTabbedPane;
28 import javax.swing.JTextPane;
29 import javax.swing.KeyStroke;
30 import javax.swing.undo.CannotRedoException;
31 import javax.swing.undo.CannotUndoException;
32 import javax.swing.undo.UndoManager;
33
34 import org.scilab.modules.gui.bridge.tab.SwingScilabTab;
35 import org.scilab.modules.gui.menu.Menu;
36 import org.scilab.modules.gui.menu.ScilabMenu;
37 import org.scilab.modules.gui.menubar.MenuBar;
38 import org.scilab.modules.gui.menubar.ScilabMenuBar;
39 import org.scilab.modules.gui.tab.SimpleTab;
40 import org.scilab.modules.gui.tab.Tab;
41 import org.scilab.modules.gui.textbox.TextBox;
42 import org.scilab.modules.gui.toolbar.ToolBar;
43 import org.scilab.modules.gui.window.ScilabWindow;
44 import org.scilab.modules.gui.window.Window;
45 import org.scilab.modules.xpad.actions.ASCIIEncodingAction;
46 import org.scilab.modules.xpad.actions.AboutAction;
47 import org.scilab.modules.xpad.actions.AutoIndentAction;
48 import org.scilab.modules.xpad.actions.CloseAction;
49 import org.scilab.modules.xpad.actions.ColorizeAction;
50 import org.scilab.modules.xpad.actions.CommentAction;
51 import org.scilab.modules.xpad.actions.CopyAction;
52 import org.scilab.modules.xpad.actions.CutAction;
53 import org.scilab.modules.xpad.actions.DeleteAction;
54 import org.scilab.modules.xpad.actions.EvaluateSelectionAction;
55 import org.scilab.modules.xpad.actions.ExitAction;
56 import org.scilab.modules.xpad.actions.FindAction;
57 import org.scilab.modules.xpad.actions.FindNextAction;
58 import org.scilab.modules.xpad.actions.FindPreviousAction;
59 import org.scilab.modules.xpad.actions.GotoLineAction;
60 import org.scilab.modules.xpad.actions.HelpAction;
61 import org.scilab.modules.xpad.actions.IndentAction;
62 import org.scilab.modules.xpad.actions.LineNumbersAction;
63 import org.scilab.modules.xpad.actions.LoadIntoScilabAction;
64 import org.scilab.modules.xpad.actions.NewAction;
65 import org.scilab.modules.xpad.actions.OpenAction;
66 import org.scilab.modules.xpad.actions.PageSetupAction;
67 import org.scilab.modules.xpad.actions.PasteAction;
68 import org.scilab.modules.xpad.actions.PrintAction;
69 import org.scilab.modules.xpad.actions.PrintPreviewAction;
70 import org.scilab.modules.xpad.actions.RedoAction;
71 import org.scilab.modules.xpad.actions.ResetFontAction;
72 import org.scilab.modules.xpad.actions.SaveAction;
73 import org.scilab.modules.xpad.actions.SaveAsAction;
74 import org.scilab.modules.xpad.actions.ScilabStyleAction;
75 import org.scilab.modules.xpad.actions.SelectAllAction;
76 import org.scilab.modules.xpad.actions.SetColorsAction;
77 import org.scilab.modules.xpad.actions.SetFontAction;
78 import org.scilab.modules.xpad.actions.ShowToolBarAction;
79 import org.scilab.modules.xpad.actions.TextStyleAction;
80 import org.scilab.modules.xpad.actions.UTF8EncodingAction;
81 import org.scilab.modules.xpad.actions.UnCommentAction;
82 import org.scilab.modules.xpad.actions.UndoAction;
83 import org.scilab.modules.xpad.actions.WordWrapAction;
84 import org.scilab.modules.xpad.actions.XMLStyleAction;
85 import org.scilab.modules.xpad.style.ScilabStyleDocument;
86 import org.scilab.modules.xpad.utils.ConfigXpadManager;
87
88 public class Xpad extends SwingScilabTab implements Tab { 
89
90     private final Window parentWindow;
91     private final JTabbedPane tabPane;
92
93     private static Xpad editor = null;
94
95     public static void main(String[] args) {
96         xpad();
97     }
98
99     public static void xpad() {
100         Xpad editor = launchXpad();
101         editor.addEmptyTab();
102     }
103
104     public static void xpad(String filePath) {
105         Xpad editor = launchXpad();
106         editor.readFile(new File(filePath));
107     }
108
109     private static Xpad launchXpad() {
110         if (editor == null) {
111             editor = createEditor();
112         }
113         return editor;
114     }
115
116     public static void closeXpad() {
117         editor = null;
118     }
119     
120     private static Xpad createEditor() {
121         ConfigXpadManager.createUserCopy();     
122
123         Window mainWindow = ScilabWindow.createWindow();
124         Xpad editor = new Xpad(mainWindow);
125
126         mainWindow.setTitle("Xpad");
127         mainWindow.addTab(editor);
128
129         // Set Xpad Window position /size 
130         mainWindow.setPosition( ConfigXpadManager.getMainWindowPosition());
131         mainWindow.setDims(ConfigXpadManager.getMainWindowSize());
132
133         MenuBar menuBar = ScilabMenuBar.createMenuBar();
134         //Create FILE menubar
135         Menu fileMenu = ScilabMenu.createMenu();
136         fileMenu.setText("File");
137         fileMenu.setMnemonic('F');
138         fileMenu.add(new NewAction(editor));
139         fileMenu.add(new OpenAction(editor));
140         fileMenu.addSeparator();
141         fileMenu.add(new SaveAction(editor));
142         fileMenu.add(new SaveAsAction(editor));
143         fileMenu.addSeparator();
144         fileMenu.add(new PageSetupAction(editor));
145         fileMenu.add(new PrintPreviewAction(editor));
146         fileMenu.add(new PrintAction(editor));
147         fileMenu.addSeparator();
148         fileMenu.add(new CloseAction(editor));
149         fileMenu.addSeparator();
150         fileMenu.add(new ExitAction(editor));
151         menuBar.add(fileMenu);
152
153         //Create EDIT menubar
154         Menu editMenu = ScilabMenu.createMenu();
155         editMenu.setText("Edit"); 
156         editMenu.setMnemonic('E');
157         editMenu.add(new UndoAction(editor));
158         editMenu.add(new RedoAction(editor));
159         editMenu.addSeparator(); 
160         editMenu.add(new CutAction(editor));
161         editMenu.add(new CopyAction(editor));
162         editMenu.add(new PasteAction(editor));
163         editMenu.addSeparator(); 
164         editMenu.add(new SelectAllAction(editor));
165         editMenu.add(new DeleteAction(editor));
166         editMenu.addSeparator();
167         editMenu.add(new CommentAction(editor));
168         editMenu.add(new UnCommentAction(editor));
169         editMenu.addSeparator();
170         editMenu.add(new IndentAction(editor));
171         menuBar.add(editMenu);
172         
173         // Create SEARCH menubar
174         Menu searchMenu = ScilabMenu.createMenu(); 
175         searchMenu.setText("Search");
176         searchMenu.setMnemonic('S');
177         searchMenu.add(new FindAction(editor));
178         searchMenu.add(new FindNextAction(editor));
179         searchMenu.add(new FindPreviousAction(editor));
180         searchMenu.add(new GotoLineAction(editor));
181         menuBar.add(searchMenu);
182
183         // Create VIEW Menubar
184         Menu viewMenu = ScilabMenu.createMenu();
185         viewMenu.setText("View");
186         viewMenu.add(new ShowToolBarAction(editor));
187         viewMenu.addSeparator();
188         viewMenu.add(new WordWrapAction(editor));
189         viewMenu.add(new LineNumbersAction(editor));
190         viewMenu.add(new SetColorsAction(editor));
191         viewMenu.add(new SetFontAction(editor));
192         viewMenu.add(new ResetFontAction(editor));
193         menuBar.add(viewMenu);
194
195         // Create DOCUMENT MenuBar
196         Menu documentMenu = ScilabMenu.createMenu();
197         documentMenu.setText("Document");
198         Menu syntaxTypeMenu = ScilabMenu.createMenu();
199         syntaxTypeMenu.setText("Syntax Type");
200         documentMenu.add(syntaxTypeMenu);
201         syntaxTypeMenu.add(new TextStyleAction(editor));
202         syntaxTypeMenu.add(new ScilabStyleAction(editor));
203         syntaxTypeMenu.add(new XMLStyleAction(editor));
204         documentMenu.addSeparator();
205         Menu encodingTypeMenu = ScilabMenu.createMenu();
206         encodingTypeMenu.setText("Encoding Type");
207         documentMenu.add(encodingTypeMenu);
208         encodingTypeMenu.add(new ASCIIEncodingAction(editor));
209         encodingTypeMenu.add(new UTF8EncodingAction(editor));
210         documentMenu.addSeparator();
211         documentMenu.add(new ColorizeAction(editor));
212         documentMenu.add(new AutoIndentAction(editor));
213         menuBar.add(documentMenu);
214
215         // Create EXECUTE menubar
216         Menu executeMenu = ScilabMenu.createMenu();
217         executeMenu.setText("Execute");
218         executeMenu.setMnemonic('e');
219         executeMenu.add(new LoadIntoScilabAction(editor));
220         executeMenu.add(new EvaluateSelectionAction(editor));
221         menuBar.add(executeMenu);
222
223         //Create HELP menubar
224         Menu helpMenu = ScilabMenu.createMenu();
225         helpMenu.setText("?");
226         helpMenu.add(new HelpAction(editor));
227         helpMenu.add(new AboutAction(editor));
228         menuBar.add(helpMenu);
229         
230         editor.setMenuBar(menuBar);
231         mainWindow.setTitle("XPad");
232         mainWindow.setVisible(true);
233         editor.setCallback((new ExitAction(editor)).getCallback());
234         return editor;
235     }
236
237     public Xpad(Window parentWindow) {
238         super("Xpad");
239         this.parentWindow = parentWindow;
240         tabPane = new JTabbedPane();
241         this.setContentPane(tabPane);
242     }
243
244     public void closeCurrentTab() {
245         tabPane.remove(tabPane.getSelectedComponent());
246     }
247     
248     public JTextPane addTab(String title) {
249         JTextPane textPane = new JTextPane() {
250             public boolean getScrollableTracksViewportWidth() {
251                 return false;
252             };
253         };
254
255         JScrollPane scrollingText = new JScrollPane(textPane);
256         tabPane.add(title, scrollingText);
257         tabPane.setSelectedIndex(tabPane.getTabCount() - 1);
258         this.setContentPane(tabPane);
259         textPane.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
260         textPane.setFont(ConfigXpadManager.getFont());
261
262         textPane.setBackground(ConfigXpadManager.getXpadBackgroundColor());
263         textPane.setCaretColor(Color.BLACK);
264         textPane.setStyledDocument(new ScilabStyleDocument());
265         textPane.setCharacterAttributes(textPane.getStyle("Default"), true);
266
267         textPane.setFocusable(true);
268
269         KeyStroke key;
270         key = KeyStroke.getKeyStroke(KeyEvent.VK_Z, Event.CTRL_MASK);
271         textPane.getInputMap().put(key, new UndoAction(this));
272         key = KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.CTRL_MASK);
273         textPane.getInputMap().put(key, new RedoAction(this));
274         key = KeyStroke.getKeyStroke(KeyEvent.VK_F, Event.CTRL_MASK);
275         textPane.getInputMap().put(key, new FindAction(this));
276
277         return textPane;
278     }
279
280     public JTextPane addEmptyTab() {
281         return addTab("Untitled");
282     }
283
284     public void setAutoIndent(boolean b) {
285         ((ScilabStyleDocument) getTextPane().getStyledDocument()).setAutoIndent(b);
286     }
287
288     public void undo() {
289         UndoManager undo = ((ScilabStyleDocument) getTextPane().getStyledDocument()).getUndoManager();
290         try {
291             System.err.println("Will undo "+undo.getUndoPresentationName());
292             undo.undo();
293         } catch (CannotUndoException ex) {
294             System.out.println("Unable to undo: " + ex);
295             ex.printStackTrace();
296         }
297     }
298
299     public void redo() {
300         UndoManager redo = ((ScilabStyleDocument) getTextPane().getStyledDocument()).getUndoManager();
301         try {
302             System.err.println("Will redo "+redo.getRedoPresentationName());
303             redo.redo();
304         } catch (CannotRedoException ex) {
305             System.out.println("Unable to redo: " + ex);
306             ex.printStackTrace();
307         }
308     }
309
310     public void readFile(File f) {
311         /* First try to open file before creating tab */
312         try {
313             StringBuilder contents = new StringBuilder();
314             Scanner scanner = new Scanner(f);
315             while (scanner.hasNextLine()) {
316                 contents.append(scanner.nextLine());
317                 contents.append(System.getProperty("line.separator"));
318             }
319             JTextPane textPane = addTab(f.getName()); 
320             System.out.println("File = "+f.getAbsolutePath());
321             textPane.setName(f.getAbsolutePath());
322
323             ((ScilabStyleDocument) textPane.getStyledDocument()).disableUpdaters(); 
324             textPane.setText(contents.toString());
325             if (((ScilabStyleDocument) textPane.getStyledDocument()).getColorize()) {
326                 ((ScilabStyleDocument) textPane.getStyledDocument()).colorize();
327             }
328             if (((ScilabStyleDocument) textPane.getStyledDocument()).getAutoIndent()) {
329                 ((ScilabStyleDocument) textPane.getStyledDocument()).indent();
330             }
331             ((ScilabStyleDocument) textPane.getStyledDocument()).enableUpdaters();
332         } catch (IOException ioex) {
333             JOptionPane.showMessageDialog(this, ioex.getMessage());
334         }
335     }
336
337         public JTextPane getTextPane() {
338             return (JTextPane) ((JScrollPane) tabPane.getSelectedComponent()).getViewport().getComponent(0);
339         }
340
341         public SimpleTab getAsSimpleTab() {
342             return this;
343         }
344
345         public Window getParentWindow() {
346             return parentWindow;
347         }
348
349         public void addInfoBar(TextBox infoBarToAdd) {
350             setInfoBar(infoBarToAdd);
351         }
352
353         public void addMenuBar(MenuBar menuBarToAdd) {
354             setMenuBar(menuBarToAdd);
355         }
356
357         public void addToolBar(ToolBar toolBarToAdd) {
358             setToolBar(toolBarToAdd);
359         }
360     }