Gui icons: fix browsevar, filebrowser, and missing icon
[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.gui.utils.ScilabSwingUtilities;
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.TabManager;
50 import org.scilab.modules.scinotes.utils.ConfigSciNotesManager;
51 import org.scilab.modules.scinotes.utils.SciNotesMessages;
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     @Override
88     public void doAction() {
89         ScilabEditorPane sep = getEditor().getTextPane();
90         openConfTabWindow();
91     }
92
93     /**
94      * createMenu
95      * @param label label of the menu
96      * @param editor SciNotes
97      * @param key Keystroke
98      * @return MenuItem
99      */
100     public static MenuItem createMenu(String label, final SciNotes editor, KeyStroke key) {
101         return createMenu(label, null, new ConfigTabulationsAction(label, editor), key);
102     }
103
104     /**
105      * Close the window
106      */
107     public static void closeConfTabWindow() {
108         if (windowAlreadyExist) {
109             mainFrame.dispose();
110             mainFrame = null;
111             windowAlreadyExist = false;
112         }
113     }
114
115     /**
116      * Open a window
117      */
118     public void openConfTabWindow() {
119         if (windowAlreadyExist) {
120             mainFrame.setVisible(true);
121             return;
122         }
123
124         TabManager.Tabulation tabProps = ConfigSciNotesManager.getDefaultTabulation();
125
126         mainFrame = new JFrame();
127         mainFrame.setIconImage(new ImageIcon(ScilabSwingUtilities.findIcon("scilab")).getImage());
128         mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
129         mainFrame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE , 0), ESCAPE);
130         mainFrame.getRootPane().getActionMap().put(ESCAPE, new AbstractAction() {
131                 @Override
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                 @Override
190                 public void actionPerformed(ActionEvent e) {
191                     boolean b = !useWhitesCheckBox.isSelected();
192                     for (Component c : panelRepresentation.getComponents()) {
193                         c.setEnabled(b);
194                     }
195                 }
196             });
197
198         boolean b = !useWhitesCheckBox.isSelected();
199         for (Component c : panelRepresentation.getComponents()) {
200             c.setEnabled(b);
201         }
202
203         JPanel framePanel = new JPanel();
204         framePanel.setLayout(new BoxLayout(framePanel, BoxLayout.PAGE_AXIS));
205         framePanel.add(contentPanel);
206         framePanel.add(panelButton);
207
208         mainFrame.setContentPane(framePanel);
209
210         cancelButton.addActionListener(new ActionListener() {
211                 @Override
212                 public void actionPerformed(ActionEvent e) {
213                     ConfigTabulationsAction.windowAlreadyExist = false;
214                     mainFrame.dispose();
215                 }
216             });
217
218         okButton.addActionListener(new ActionListener() {
219                 @Override
220                 public void actionPerformed(ActionEvent e) {
221                     ConfigTabulationsAction.windowAlreadyExist = false;
222                     refreshPane();
223                     mainFrame.dispose();
224                 }
225             });
226
227         applyButton.addActionListener(new ActionListener() {
228                 @Override
229                 public void actionPerformed(ActionEvent e) {
230                     ScilabEditorPane sep = getEditor().getTextPane();
231                     IndentManager indent = sep.getIndentManager();
232                     ScilabDocument doc = (ScilabDocument) sep.getDocument();
233                     JScrollBar sb = sep.getScrollPane().getHorizontalScrollBar();
234                     int val = -1;
235                     if (sb != null) {
236                         val = sb.getValue();
237                     }
238                     refreshPane();
239                     indent.indentDoc(0, doc.getLength() - 1);
240                     if (val != -1) {
241                         sb.setValue(val);
242                     }
243                 }
244             });
245
246         mainFrame.addWindowListener(new WindowListener() {
247                 @Override
248                 public void windowClosed(WindowEvent arg0) { }
249                 @Override
250                 public void windowDeiconified(WindowEvent arg0) { }
251                 @Override
252                 public void windowActivated(WindowEvent arg0) { }
253
254                 @Override
255                 public void windowClosing(WindowEvent arg0) {
256                     closeConfTabWindow();
257                 }
258
259                 @Override
260                 public void windowDeactivated(WindowEvent arg0) { }
261                 @Override
262                 public void windowIconified(WindowEvent arg0) { };
263                 @Override
264                 public void windowOpened(WindowEvent arg0) { }
265             });
266
267         mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
268         mainFrame.setTitle(SciNotesMessages.CONFIGTAB);
269         mainFrame.pack();
270         mainFrame.setLocationRelativeTo(getEditor());
271         mainFrame.setVisible(true);
272     }
273
274     /**
275      * Refresh the pane
276      */
277     private void refreshPane() {
278         char tab = ' ';
279         if (!useWhitesCheckBox.isSelected()) {
280             tab = '\t';
281         }
282         int len = ((Integer) tabLengthSpinner.getValue()).intValue();
283         int type = ScilabView.TABNOTHING;
284         if (tab == '\t' && tabVRule.isSelected()) {
285             type = ScilabView.TABVERTICAL;
286         } else if (tabHRule.isSelected()) {
287             type = ScilabView.TABHORIZONTAL;
288         } else if (tabDbleChev.isSelected()) {
289             type = ScilabView.TABDOUBLECHEVRONS;
290         }
291         TabManager.Tabulation cfg = new TabManager.Tabulation(tab, len, type, ' ');
292         ConfigSciNotesManager.saveDefaultTabulation(cfg);
293         SciNotes.setDefaultTabulation(cfg);
294     }
295 }