KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > options > EditingOptionPane


1 /*
2  * EditingOptionPane.java - Mode-specific options panel
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1998, 2002 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.options;
24
25 //{{{ Imports
26
import javax.swing.*;
27 import java.awt.event.*;
28 import java.util.Arrays JavaDoc;
29
30 import org.gjt.sp.jedit.*;
31 import org.gjt.sp.jedit.buffer.FoldHandler;
32 import org.gjt.sp.util.StandardUtilities;
33 //}}}
34

35 /**
36  * @version $Id: EditingOptionPane.java 7889 2006-11-03 19:28:38Z ezust $
37  */

38 public class EditingOptionPane extends AbstractOptionPane
39 {
40     //{{{ EditingOptionPane constructor
41
public EditingOptionPane()
42     {
43         super("editing");
44     } //}}}
45

46     //{{{ _init() method
47
protected void _init()
48     {
49         Mode[] modes = jEdit.getModes();
50         Arrays.sort(modes,new MiscUtilities.StringICaseCompare());
51
52         global = new ModeProperties();
53         modeProps = new ModeProperties[modes.length];
54
55         String JavaDoc[] modeNames = new String JavaDoc[modes.length + 1];
56         modeNames[0] = jEdit.getProperty("options.editing.global");
57
58         for(int i = 0; i < modes.length; i++)
59         {
60             modeProps[i] = new ModeProperties(modes[i]);
61             modeNames[i + 1] = modes[i].getName();
62         }
63
64         mode = new JComboBox(modeNames);
65         mode.addActionListener(new ActionHandler());
66
67         captionBox = new Box(BoxLayout.X_AXIS);
68         addComponent(captionBox);
69
70         addComponent(jEdit.getProperty("options.editing.mode"),mode);
71
72         useDefaults = new JCheckBox(jEdit.getProperty("options.editing.useDefaults"));
73         useDefaults.addActionListener(new ActionHandler());
74         addComponent(useDefaults);
75
76         addComponent(jEdit.getProperty("options.editing.noWordSep"),
77             noWordSep = new JTextField());
78
79         String JavaDoc[] foldModes = FoldHandler.getFoldModes();
80         addComponent(jEdit.getProperty("options.editing.folding"),
81             folding = new JComboBox(foldModes));
82
83         addComponent(jEdit.getProperty("options.editing.collapseFolds"),
84             collapseFolds = new JTextField());
85
86         String JavaDoc[] wrapModes = {
87             "none",
88             "soft",
89             "hard"
90         };
91         addComponent(jEdit.getProperty("options.editing.wrap"),
92             wrap = new JComboBox(wrapModes));
93
94         String JavaDoc[] lineLens = { "0", "72", "76", "80" };
95         maxLineLen = new JComboBox(lineLens);
96         maxLineLen.setToolTipText(jEdit.getProperty("options.editing.maxLineLen.tooltip"));
97         addComponent(jEdit.getProperty("options.editing.maxLineLen"), maxLineLen);
98         maxLineLen.setEditable(true);
99
100         String JavaDoc[] tabSizes = { "2", "4", "8" };
101         addComponent(jEdit.getProperty("options.editing.tabSize"),
102             tabSize = new JComboBox(tabSizes));
103         tabSize.setEditable(true);
104
105         addComponent(jEdit.getProperty("options.editing.indentSize"),
106             indentSize = new JComboBox(tabSizes));
107         indentSize.setEditable(true);
108
109         addComponent(noTabs = new JCheckBox(jEdit.getProperty(
110             "options.editing.noTabs")));
111
112         addComponent(deepIndent = new JCheckBox(jEdit.getProperty(
113             "options.editing.deepIndent")));
114
115         addComponent(jEdit.getProperty("options.editing.filenameGlob"),
116             filenameGlob = new JTextField());
117
118         addComponent(jEdit.getProperty("options.editing.firstlineGlob"),
119             firstlineGlob = new JTextField());
120
121         selectMode();
122
123         addSeparator();
124
125         defaultMode = new JComboBox(modes);
126         defaultMode.setSelectedItem(jEdit.getMode(
127             jEdit.getProperty("buffer.defaultMode")));
128         addComponent(jEdit.getProperty("options.editing.defaultMode"),
129             defaultMode);
130
131         undoCount = new JTextField(jEdit.getProperty("buffer.undoCount"));
132         addComponent(jEdit.getProperty("options.editing.undoCount"),undoCount);
133     } //}}}
134

135     //{{{ _save() method
136
protected void _save()
137     {
138         jEdit.setProperty("buffer.defaultMode",
139             ((Mode)defaultMode.getSelectedItem()).getName());
140         jEdit.setProperty("buffer.undoCount",undoCount.getText());
141
142         saveMode();
143
144         global.save();
145
146         for(int i = 0; i < modeProps.length; i++)
147         {
148             modeProps[i].save();
149         }
150     } //}}}
151

152     //{{{ Private members
153

154     //{{{ Instance variables
155
private JComboBox defaultMode;
156     private JTextField undoCount;
157     private ModeProperties global;
158     private ModeProperties[] modeProps;
159     private ModeProperties current;
160     private Box captionBox;
161     private JComboBox mode;
162     private JCheckBox useDefaults;
163     private JTextField filenameGlob;
164     private JTextField firstlineGlob;
165     private JTextField noWordSep;
166     private JComboBox folding;
167     private JTextField collapseFolds;
168     private JComboBox wrap;
169     private JComboBox maxLineLen;
170     private JComboBox tabSize;
171     private JComboBox indentSize;
172     private JCheckBox noTabs;
173     private JCheckBox deepIndent;
174     //}}}
175

176     //{{{ saveMode() method
177
private void saveMode()
178     {
179         current.useDefaults = useDefaults.isSelected();
180         current.filenameGlob = filenameGlob.getText();
181         current.firstlineGlob = firstlineGlob.getText();
182         current.noWordSep = noWordSep.getText();
183         current.folding = (String JavaDoc)folding.getSelectedItem();
184         current.collapseFolds = collapseFolds.getText();
185         current.wrap = (String JavaDoc)wrap.getSelectedItem();
186         current.maxLineLen = (String JavaDoc)maxLineLen.getSelectedItem();
187         current.tabSize = (String JavaDoc)tabSize.getSelectedItem();
188         current.indentSize = (String JavaDoc)indentSize.getSelectedItem();
189         current.noTabs = noTabs.isSelected();
190         current.deepIndent = deepIndent.isSelected();
191     } //}}}
192

193     //{{{ selectMode() method
194
private void selectMode()
195     {
196         int index = mode.getSelectedIndex();
197         current = index == 0 ? global : modeProps[index - 1];
198         current.edited = true;
199         current.load();
200
201         captionBox.removeAll();
202         captionBox.add(GUIUtilities.createMultilineLabel(
203             jEdit.getProperty("options.editing.caption-"
204             + (index == 0 ? "0" : "1"))));
205
206         useDefaults.setSelected(current.useDefaults);
207         filenameGlob.setText(current.filenameGlob);
208         firstlineGlob.setText(current.firstlineGlob);
209         noWordSep.setText(current.noWordSep);
210         folding.setSelectedItem(current.folding);
211         collapseFolds.setText(current.collapseFolds);
212         wrap.setSelectedItem(current.wrap);
213         maxLineLen.setSelectedItem(current.maxLineLen);
214         tabSize.setSelectedItem(current.tabSize);
215         indentSize.setSelectedItem(current.indentSize);
216         noTabs.setSelected(current.noTabs);
217         deepIndent.setSelected(current.deepIndent);
218
219         updateEnabled();
220         revalidate();
221     } //}}}
222

223     //{{{ updateEnabled() method
224
private void updateEnabled()
225     {
226         boolean enabled;
227         if(current == global)
228         {
229             enabled = true;
230             useDefaults.setEnabled(false);
231             filenameGlob.setEnabled(false);
232             firstlineGlob.setEnabled(false);
233         }
234         else
235         {
236             enabled = !modeProps[mode.getSelectedIndex() - 1]
237                 .useDefaults;
238             useDefaults.setEnabled(true);
239             filenameGlob.setEnabled(enabled);
240             firstlineGlob.setEnabled(enabled);
241         }
242
243         noWordSep.setEnabled(enabled);
244         folding.setEnabled(enabled);
245         collapseFolds.setEnabled(enabled);
246         wrap.setEnabled(enabled);
247         maxLineLen.setEnabled(enabled);
248         tabSize.setEnabled(enabled);
249         indentSize.setEnabled(enabled);
250         noTabs.setEnabled(enabled);
251         deepIndent.setEnabled(enabled);
252     } //}}}
253

254     //}}}
255

256     //{{{ ActionHandler class
257
class ActionHandler implements ActionListener
258     {
259         public void actionPerformed(ActionEvent evt)
260         {
261             Object JavaDoc source = evt.getSource();
262             if(source == mode)
263             {
264                 saveMode();
265                 selectMode();
266             }
267             else if(source == useDefaults)
268             {
269                 modeProps[mode.getSelectedIndex() - 1].useDefaults =
270                     useDefaults.isSelected();
271                 updateEnabled();
272             }
273         }
274     } //}}}
275

276     //{{{ ModeProperties class
277
static class ModeProperties
278     {
279         //{{{ Instance variables
280
Mode mode;
281         boolean edited;
282         boolean loaded;
283
284         boolean useDefaults;
285         String JavaDoc filenameGlob;
286         String JavaDoc firstlineGlob;
287         String JavaDoc noWordSep;
288         String JavaDoc folding;
289         String JavaDoc collapseFolds;
290         String JavaDoc wrap;
291         String JavaDoc maxLineLen;
292         String JavaDoc tabSize;
293         String JavaDoc indentSize;
294         boolean noTabs;
295         boolean deepIndent;
296         //}}}
297

298         //{{{ ModeProperties constructor
299
ModeProperties()
300         {
301         } //}}}
302

303         //{{{ ModeProperties constructor
304
ModeProperties(Mode mode)
305         {
306             this.mode = mode;
307         } //}}}
308

309         //{{{ load() method
310
void load()
311         {
312             if(loaded)
313                 return;
314
315             loaded = true;
316
317             if(mode != null)
318             {
319                 mode.loadIfNecessary();
320
321                 useDefaults = !jEdit.getBooleanProperty("mode."
322                     + mode.getName() + ".customSettings");
323                 filenameGlob = (String JavaDoc)mode.getProperty("filenameGlob");
324                 firstlineGlob = (String JavaDoc)mode.getProperty("firstlineGlob");
325                 noWordSep = (String JavaDoc)mode.getProperty("noWordSep");
326                 folding = mode.getProperty("folding").toString();
327                 collapseFolds = mode.getProperty("collapseFolds").toString();
328                 wrap = mode.getProperty("wrap").toString();
329                 maxLineLen = mode.getProperty("maxLineLen").toString();
330                 tabSize = mode.getProperty("tabSize").toString();
331                 indentSize = mode.getProperty("indentSize").toString();
332                 noTabs = mode.getBooleanProperty("noTabs");
333                 deepIndent = mode.getBooleanProperty("deepIndent");
334             }
335             else
336             {
337                 noWordSep = jEdit.getProperty("buffer.noWordSep");
338                 folding = jEdit.getProperty("buffer.folding");
339                 collapseFolds = jEdit.getProperty("buffer.collapseFolds");
340                 wrap = jEdit.getProperty("buffer.wrap");
341                 maxLineLen = jEdit.getProperty("buffer.maxLineLen");
342                 tabSize = jEdit.getProperty("buffer.tabSize");
343                 indentSize = jEdit.getProperty("buffer.indentSize");
344                 noTabs = jEdit.getBooleanProperty("buffer.noTabs");
345                 deepIndent = jEdit.getBooleanProperty("buffer.deepIndent");
346             }
347         } //}}}
348

349         //{{{ save() method
350
void save()
351         {
352             // don't do anything if the user didn't change
353
// any settings
354
if(!edited)
355                 return;
356
357             String JavaDoc prefix;
358             if(mode != null)
359             {
360                 prefix = "mode." + mode.getName() + '.';
361                 jEdit.setBooleanProperty(prefix + "customSettings",!useDefaults);
362
363                 // need to call Mode.init() if the file name or first line
364
// globs change
365
String JavaDoc oldFilenameGlob = (String JavaDoc)mode.getProperty("filenameGlob");
366                 String JavaDoc oldFirstlineGlob = (String JavaDoc)mode.getProperty("firstlineGlob");
367                 if(useDefaults)
368                 {
369                     jEdit.resetProperty(prefix + "filenameGlob");
370                     jEdit.resetProperty(prefix + "firstlineGlob");
371                     jEdit.resetProperty(prefix + "noWordSep");
372                     jEdit.resetProperty(prefix + "folding");
373                     jEdit.resetProperty(prefix + "collapseFolds");
374                     jEdit.resetProperty(prefix + "wrap");
375                     jEdit.resetProperty(prefix + "maxLineLen");
376                     jEdit.resetProperty(prefix + "tabSize");
377                     jEdit.resetProperty(prefix + "indentSize");
378                     jEdit.resetProperty(prefix + "noTabs");
379                     jEdit.resetProperty(prefix + "deepIndent");
380     
381                     if(!(StandardUtilities.objectsEqual(oldFilenameGlob,
382                         mode.getProperty("filenameGlob"))
383                         && StandardUtilities.objectsEqual(oldFirstlineGlob,
384                         mode.getProperty("firstlineGlob"))))
385                     {
386                         mode.init();
387                     }
388
389                     return;
390                 }
391                 else
392                 {
393                     jEdit.setProperty(prefix + "filenameGlob",filenameGlob);
394                     jEdit.setProperty(prefix + "firstlineGlob",firstlineGlob);
395
396                     if(!(StandardUtilities.objectsEqual(oldFilenameGlob,
397                         filenameGlob)
398                         && StandardUtilities.objectsEqual(oldFirstlineGlob,
399                         firstlineGlob)))
400                     {
401                         mode.init();
402                     }
403                 }
404             }
405             else
406             {
407                 prefix = "buffer.";
408             }
409
410             jEdit.setProperty(prefix + "noWordSep",noWordSep);
411             jEdit.setProperty(prefix + "folding",folding);
412             jEdit.setProperty(prefix + "collapseFolds",collapseFolds);
413             jEdit.setProperty(prefix + "wrap",wrap);
414             jEdit.setProperty(prefix + "maxLineLen",maxLineLen);
415             jEdit.setProperty(prefix + "tabSize",tabSize);
416             jEdit.setProperty(prefix + "indentSize",indentSize);
417             jEdit.setBooleanProperty(prefix + "noTabs",noTabs);
418             jEdit.setBooleanProperty(prefix + "deepIndent",deepIndent);
419         } //}}}
420
} //}}}
421
}
422
Popular Tags