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