SciNotes cleaning...
[scilab.git] / scilab / modules / scinotes / src / java / org / scilab / modules / scinotes / actions / ConfigTabulationsAction.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2010 - Calixte DENIZET
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.scinotes.actions;
14
15 import java.awt.Component;
16 import java.awt.FlowLayout;
17 import java.awt.GridLayout;
18 import java.awt.event.ActionEvent;
19 import java.awt.event.ActionListener;
20 import java.awt.event.KeyEvent;
21 import java.awt.event.WindowEvent;
22 import java.awt.event.WindowListener;
23
24 import javax.swing.AbstractAction;
25 import javax.swing.BorderFactory;
26 import javax.swing.BoxLayout;
27 import javax.swing.ButtonGroup;
28 import javax.swing.ImageIcon;
29 import javax.swing.JButton;
30 import javax.swing.JCheckBox;
31 import javax.swing.JComponent;
32 import javax.swing.JFrame;
33 import javax.swing.JLabel;
34 import javax.swing.JPanel;
35 import javax.swing.JRadioButton;
36 import javax.swing.JScrollBar;
37 import javax.swing.JSpinner;
38 import javax.swing.KeyStroke;
39 import javax.swing.SpinnerNumberModel;
40
41 import org.scilab.modules.commons.ScilabConstants;
42 import org.scilab.modules.gui.menuitem.MenuItem;
43 import org.scilab.modules.scinotes.IndentManager;
44 import org.scilab.modules.scinotes.SciNotes;
45 import org.scilab.modules.scinotes.ScilabDocument;
46 import org.scilab.modules.scinotes.ScilabEditorPane;
47 import org.scilab.modules.scinotes.ScilabView;
48 import org.scilab.modules.scinotes.TabManager;
49 import org.scilab.modules.scinotes.utils.ConfigSciNotesManager;
50 import org.scilab.modules.scinotes.utils.SciNotesMessages;
51
52 /**
53  * ConfigTabulationsAction Class
54  * @author Calixte DENIZET
55  */
56 public class ConfigTabulationsAction extends DefaultAction {
57
58     private static final long serialVersionUID = 1L;
59     private static final int GAP = 5;
60     private static final String ESCAPE = "escape";
61     private static final String SCI = ScilabConstants.SCI.getPath();
62
63     private static boolean windowAlreadyExist;
64     private static JFrame mainFrame;
65
66     private JButton okButton;
67     private JButton cancelButton;
68     private JButton applyButton;
69     private JRadioButton tabHRule;
70     private JRadioButton tabVRule;
71     private JRadioButton tabDbleChev;
72     private JCheckBox useWhitesCheckBox;
73     private JSpinner tabLengthSpinner;
74
75     /**
76      * Constructor
77      * @param name the name of the action
78      * @param editor SciNotes
79      */
80     public ConfigTabulationsAction(String name, SciNotes editor) {
81         super(name, editor);
82     }
83
84     /**
85      * doAction
86      */
87     public void doAction() {
88         ScilabEditorPane sep = (ScilabEditorPane) getEditor().getTextPane();
89         openConfTabWindow();
90     }
91
92     /**
93      * createMenu
94      * @param label label of the menu
95      * @param editor SciNotes
96      * @param key Keystroke
97      * @return MenuItem
98      */
99     public static MenuItem createMenu(String label, final SciNotes editor, KeyStroke key) {
100         return createMenu(label, null, new ConfigTabulationsAction(label, editor), key);
101     }
102
103     /**
104      * Close the window
105      */
106     public static void closeConfTabWindow() {
107         if (windowAlreadyExist) {
108             mainFrame.dispose();
109             mainFrame = null;
110             windowAlreadyExist = false;
111         }
112     }
113
114     /**
115      * Open a window
116      */
117     public void openConfTabWindow() {
118         if (windowAlreadyExist) {
119             mainFrame.setVisible(true);
120             return;
121         }
122
123         TabManager.Tabulation tabProps = ConfigSciNotesManager.getDefaultTabulation();
124
125         mainFrame = new JFrame();
126         mainFrame.setIconImage(new ImageIcon(SCI + "/modules/gui/images/icons/scilab.png").getImage());
127         mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
128         mainFrame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE , 0), ESCAPE);
129         mainFrame.getRootPane().getActionMap().put(ESCAPE, new AbstractAction() {
130                 public void actionPerformed(ActionEvent e) {
131                     closeConfTabWindow();
132                 }
133             });
134
135         windowAlreadyExist = true;
136
137         cancelButton = new JButton(SciNotesMessages.CANCEL);
138         okButton = new JButton(SciNotesMessages.OK);
139         okButton.setPreferredSize(cancelButton.getPreferredSize());
140         applyButton = new JButton(SciNotesMessages.APPLYREINDENT);
141
142         JPanel panelButton = new JPanel();
143         panelButton.setLayout(new GridLayout(1, 4, GAP, GAP));
144         panelButton.setBorder(BorderFactory.createEmptyBorder(GAP, 0, 0, 0));
145         panelButton.add(new JLabel());
146         panelButton.add(okButton);
147         panelButton.add(applyButton);
148         panelButton.add(cancelButton);
149
150         /* Main content pane */
151         JPanel contentPanel = new JPanel(new GridLayout(1, 2, GAP, GAP));
152         contentPanel.setBorder(BorderFactory.createEmptyBorder(2 * GAP, 2 * GAP, 2 * GAP, 2 * GAP));
153
154         /* Style */
155         JPanel stylePanel = new JPanel(new GridLayout(2, 1, GAP, GAP));
156         stylePanel.setBorder(BorderFactory.createTitledBorder(SciNotesMessages.TABSTYLE));
157
158         useWhitesCheckBox = new JCheckBox(SciNotesMessages.TABUSEWHITES, tabProps.tab == ' ');
159         stylePanel.add(useWhitesCheckBox);
160
161         JPanel tabLengthPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
162         tabLengthSpinner = new JSpinner(new SpinnerNumberModel(tabProps.number, 0, 16, 1));
163
164         tabLengthPanel.add(new JLabel(SciNotesMessages.TABLENGTH));
165         tabLengthPanel.add(tabLengthSpinner);
166
167         stylePanel.add(tabLengthPanel);
168
169         contentPanel.add(stylePanel);
170
171         /* Representation */
172         final JPanel panelRepresentation = new JPanel(new GridLayout(3, 1, GAP, GAP));
173         panelRepresentation.setBorder(BorderFactory.createTitledBorder(SciNotesMessages.TABREPRESENTATION));
174         tabHRule = new JRadioButton(SciNotesMessages.TABHRULE, tabProps.type == ScilabView.TABHORIZONTAL);
175         panelRepresentation.add(tabHRule);
176         tabVRule = new JRadioButton(SciNotesMessages.TABVRULE, tabProps.type == ScilabView.TABVERTICAL);
177         panelRepresentation.add(tabVRule);
178         tabDbleChev = new JRadioButton(SciNotesMessages.TABDOUBLECHEVRONS, tabProps.type == ScilabView.TABDOUBLECHEVRONS);
179         panelRepresentation.add(tabDbleChev);
180         ButtonGroup repGroup = new ButtonGroup();
181         repGroup.add(tabHRule);
182         repGroup.add(tabVRule);
183         repGroup.add(tabDbleChev);
184
185         contentPanel.add(panelRepresentation);
186         useWhitesCheckBox.addActionListener(new ActionListener() {
187                 public void actionPerformed(ActionEvent e) {
188                     boolean b = !useWhitesCheckBox.isSelected();
189                     for (Component c : panelRepresentation.getComponents()) {
190                         c.setEnabled(b);
191                     }
192                 }
193             });
194
195         boolean b = !useWhitesCheckBox.isSelected();
196         for (Component c : panelRepresentation.getComponents()) {
197             c.setEnabled(b);
198         }
199
200         JPanel framePanel = new JPanel();
201         framePanel.setLayout(new BoxLayout(framePanel, BoxLayout.PAGE_AXIS));
202         framePanel.add(contentPanel);
203         framePanel.add(panelButton);
204
205         mainFrame.setContentPane(framePanel);
206
207         cancelButton.addActionListener(new ActionListener() {
208                 public void actionPerformed(ActionEvent e) {
209                     ConfigTabulationsAction.windowAlreadyExist = false;
210                     mainFrame.dispose();
211                 }
212             });
213
214         okButton.addActionListener(new ActionListener() {
215                 public void actionPerformed(ActionEvent e) {
216                     ConfigTabulationsAction.windowAlreadyExist = false;
217                     refreshPane();
218                     mainFrame.dispose();
219                 }
220             });
221
222         applyButton.addActionListener(new ActionListener() {
223                 public void actionPerformed(ActionEvent e) {
224                     ScilabEditorPane sep = getEditor().getTextPane();
225                     IndentManager indent = sep.getIndentManager();
226                     ScilabDocument doc = (ScilabDocument) sep.getDocument();
227                     JScrollBar sb = sep.getScrollPane().getHorizontalScrollBar();
228                     int val = -1;
229                     if (sb != null) {
230                         val = sb.getValue();
231                     }
232                     refreshPane();
233                     indent.indentDoc(0, doc.getLength() - 1);
234                     if (val != -1) {
235                         sb.setValue(val);
236                     }
237                 }
238             });
239
240         mainFrame.addWindowListener(new WindowListener() {
241                 public void windowClosed(WindowEvent arg0) { }
242                 public void windowDeiconified(WindowEvent arg0) { }
243                 public void windowActivated(WindowEvent arg0) { }
244
245                 public void windowClosing(WindowEvent arg0) {
246                     closeConfTabWindow();
247                 }
248
249                 public void windowDeactivated(WindowEvent arg0) { }
250                 public void windowIconified(WindowEvent arg0) { };
251                 public void windowOpened(WindowEvent arg0) { }
252             });
253
254         mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
255         mainFrame.setTitle(SciNotesMessages.CONFIGTAB);
256         mainFrame.pack();
257         mainFrame.setLocationRelativeTo(getEditor());
258         mainFrame.setVisible(true);
259     }
260
261     /**
262      * Refresh the pane
263      */
264     private void refreshPane() {
265         char tab = ' ';
266         if (!useWhitesCheckBox.isSelected()) {
267             tab = '\t';
268         }
269         int len = ((Integer) tabLengthSpinner.getValue()).intValue();
270         int type = ScilabView.TABNOTHING;
271         if (tab == '\t' && tabVRule.isSelected()) {
272             type = ScilabView.TABVERTICAL;
273         } else if (tabHRule.isSelected()) {
274             type = ScilabView.TABHORIZONTAL;
275         } else if (tabDbleChev.isSelected()) {
276             type = ScilabView.TABDOUBLECHEVRONS;
277         }
278         TabManager.Tabulation cfg = new TabManager.Tabulation(tab, len, type, ' ');
279         ConfigSciNotesManager.saveDefaultTabulation(cfg);
280         SciNotes.setDefaultTabulation(cfg);
281     }
282 }