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