Add @Override and final keyword
[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  * ConfigTabulationsAction Class
53  * @author Calixte DENIZET
54  */
55 public class ConfigTabulationsAction extends DefaultAction {
56
57     private static final long serialVersionUID = 1L;
58     private static final int GAP = 5;
59     private static final String ESCAPE = "escape";
60     private static final String SCI = ScilabConstants.SCI.getPath();
61
62     private static boolean windowAlreadyExist;
63     private static JFrame mainFrame;
64
65     private JButton okButton;
66     private JButton cancelButton;
67     private JButton applyButton;
68     private JRadioButton tabHRule;
69     private JRadioButton tabVRule;
70     private JRadioButton tabDbleChev;
71     private JCheckBox useWhitesCheckBox;
72     private JSpinner tabLengthSpinner;
73
74     /**
75      * Constructor
76      * @param name the name of the action
77      * @param editor SciNotes
78      */
79     public ConfigTabulationsAction(String name, SciNotes editor) {
80         super(name, editor);
81     }
82
83     /**
84      * doAction
85      */
86     @Override
87     public void doAction() {
88         ScilabEditorPane sep = 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                 @Override
131                 public void actionPerformed(ActionEvent e) {
132                     closeConfTabWindow();
133                 }
134             });
135
136         windowAlreadyExist = true;
137
138         cancelButton = new JButton(SciNotesMessages.CANCEL);
139         okButton = new JButton(SciNotesMessages.OK);
140         okButton.setPreferredSize(cancelButton.getPreferredSize());
141         applyButton = new JButton(SciNotesMessages.APPLYREINDENT);
142
143         JPanel panelButton = new JPanel();
144         panelButton.setLayout(new GridLayout(1, 4, GAP, GAP));
145         panelButton.setBorder(BorderFactory.createEmptyBorder(GAP, 0, 0, 0));
146         panelButton.add(new JLabel());
147         panelButton.add(okButton);
148         panelButton.add(applyButton);
149         panelButton.add(cancelButton);
150
151         /* Main content pane */
152         JPanel contentPanel = new JPanel(new GridLayout(1, 2, GAP, GAP));
153         contentPanel.setBorder(BorderFactory.createEmptyBorder(2 * GAP, 2 * GAP, 2 * GAP, 2 * GAP));
154
155         /* Style */
156         JPanel stylePanel = new JPanel(new GridLayout(2, 1, GAP, GAP));
157         stylePanel.setBorder(BorderFactory.createTitledBorder(SciNotesMessages.TABSTYLE));
158
159         useWhitesCheckBox = new JCheckBox(SciNotesMessages.TABUSEWHITES, tabProps.tab == ' ');
160         stylePanel.add(useWhitesCheckBox);
161
162         JPanel tabLengthPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
163         tabLengthSpinner = new JSpinner(new SpinnerNumberModel(tabProps.number, 0, 16, 1));
164
165         tabLengthPanel.add(new JLabel(SciNotesMessages.TABLENGTH));
166         tabLengthPanel.add(tabLengthSpinner);
167
168         stylePanel.add(tabLengthPanel);
169
170         contentPanel.add(stylePanel);
171
172         /* Representation */
173         final JPanel panelRepresentation = new JPanel(new GridLayout(3, 1, GAP, GAP));
174         panelRepresentation.setBorder(BorderFactory.createTitledBorder(SciNotesMessages.TABREPRESENTATION));
175         tabHRule = new JRadioButton(SciNotesMessages.TABHRULE, tabProps.type == ScilabView.TABHORIZONTAL);
176         panelRepresentation.add(tabHRule);
177         tabVRule = new JRadioButton(SciNotesMessages.TABVRULE, tabProps.type == ScilabView.TABVERTICAL);
178         panelRepresentation.add(tabVRule);
179         tabDbleChev = new JRadioButton(SciNotesMessages.TABDOUBLECHEVRONS, tabProps.type == ScilabView.TABDOUBLECHEVRONS);
180         panelRepresentation.add(tabDbleChev);
181         ButtonGroup repGroup = new ButtonGroup();
182         repGroup.add(tabHRule);
183         repGroup.add(tabVRule);
184         repGroup.add(tabDbleChev);
185
186         contentPanel.add(panelRepresentation);
187         useWhitesCheckBox.addActionListener(new ActionListener() {
188                 @Override
189                 public void actionPerformed(ActionEvent e) {
190                     boolean b = !useWhitesCheckBox.isSelected();
191                     for (Component c : panelRepresentation.getComponents()) {
192                         c.setEnabled(b);
193                     }
194                 }
195             });
196
197         boolean b = !useWhitesCheckBox.isSelected();
198         for (Component c : panelRepresentation.getComponents()) {
199             c.setEnabled(b);
200         }
201
202         JPanel framePanel = new JPanel();
203         framePanel.setLayout(new BoxLayout(framePanel, BoxLayout.PAGE_AXIS));
204         framePanel.add(contentPanel);
205         framePanel.add(panelButton);
206
207         mainFrame.setContentPane(framePanel);
208
209         cancelButton.addActionListener(new ActionListener() {
210                 @Override
211                 public void actionPerformed(ActionEvent e) {
212                     ConfigTabulationsAction.windowAlreadyExist = false;
213                     mainFrame.dispose();
214                 }
215             });
216
217         okButton.addActionListener(new ActionListener() {
218                 @Override
219                 public void actionPerformed(ActionEvent e) {
220                     ConfigTabulationsAction.windowAlreadyExist = false;
221                     refreshPane();
222                     mainFrame.dispose();
223                 }
224             });
225
226         applyButton.addActionListener(new ActionListener() {
227                 @Override
228                 public void actionPerformed(ActionEvent e) {
229                     ScilabEditorPane sep = getEditor().getTextPane();
230                     IndentManager indent = sep.getIndentManager();
231                     ScilabDocument doc = (ScilabDocument) sep.getDocument();
232                     JScrollBar sb = sep.getScrollPane().getHorizontalScrollBar();
233                     int val = -1;
234                     if (sb != null) {
235                         val = sb.getValue();
236                     }
237                     refreshPane();
238                     indent.indentDoc(0, doc.getLength() - 1);
239                     if (val != -1) {
240                         sb.setValue(val);
241                     }
242                 }
243             });
244
245         mainFrame.addWindowListener(new WindowListener() {
246                 @Override
247                 public void windowClosed(WindowEvent arg0) { }
248                 @Override
249                 public void windowDeiconified(WindowEvent arg0) { }
250                 @Override
251                 public void windowActivated(WindowEvent arg0) { }
252
253                 @Override
254                 public void windowClosing(WindowEvent arg0) {
255                     closeConfTabWindow();
256                 }
257
258                 @Override
259                 public void windowDeactivated(WindowEvent arg0) { }
260                 @Override
261                 public void windowIconified(WindowEvent arg0) { };
262                 @Override
263                 public void windowOpened(WindowEvent arg0) { }
264             });
265
266         mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
267         mainFrame.setTitle(SciNotesMessages.CONFIGTAB);
268         mainFrame.pack();
269         mainFrame.setLocationRelativeTo(getEditor());
270         mainFrame.setVisible(true);
271     }
272
273     /**
274      * Refresh the pane
275      */
276     private void refreshPane() {
277         char tab = ' ';
278         if (!useWhitesCheckBox.isSelected()) {
279             tab = '\t';
280         }
281         int len = ((Integer) tabLengthSpinner.getValue()).intValue();
282         int type = ScilabView.TABNOTHING;
283         if (tab == '\t' && tabVRule.isSelected()) {
284             type = ScilabView.TABVERTICAL;
285         } else if (tabHRule.isSelected()) {
286             type = ScilabView.TABHORIZONTAL;
287         } else if (tabDbleChev.isSelected()) {
288             type = ScilabView.TABDOUBLECHEVRONS;
289         }
290         TabManager.Tabulation cfg = new TabManager.Tabulation(tab, len, type, ' ');
291         ConfigSciNotesManager.saveDefaultTabulation(cfg);
292         SciNotes.setDefaultTabulation(cfg);
293     }
294 }