PageRenderTime 51ms CodeModel.GetById 11ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/options/EditingOptionPane.java

#
Java | 416 lines | 314 code | 57 blank | 45 comment | 22 complexity | 80779e1351b86c7b349aa46d78f2d73e MD5 | raw file
  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
 23package org.gjt.sp.jedit.options;
 24
 25//{{{ Imports
 26import javax.swing.*;
 27import java.awt.event.*;
 28import java.util.Arrays;
 29
 30import org.gjt.sp.jedit.*;
 31import org.gjt.sp.jedit.buffer.FoldHandler;
 32//}}}
 33
 34public class EditingOptionPane extends AbstractOptionPane
 35{
 36	//{{{ EditingOptionPane constructor
 37	public EditingOptionPane()
 38	{
 39		super("editing");
 40	} //}}}
 41
 42	//{{{ _init() method
 43	protected void _init()
 44	{
 45		Mode[] modes = jEdit.getModes();
 46		Arrays.sort(modes,new MiscUtilities.StringICaseCompare());
 47
 48		global = new ModeProperties();
 49		modeProps = new ModeProperties[modes.length];
 50
 51		String[] modeNames = new String[modes.length + 1];
 52		modeNames[0] = jEdit.getProperty("options.editing.global");
 53
 54		for(int i = 0; i < modes.length; i++)
 55		{
 56			modeProps[i] = new ModeProperties(modes[i]);
 57			modeNames[i + 1] = modes[i].getName();
 58		}
 59
 60		mode = new JComboBox(modeNames);
 61		mode.addActionListener(new ActionHandler());
 62
 63		captionBox = new Box(BoxLayout.X_AXIS);
 64		addComponent(captionBox);
 65
 66		addComponent(jEdit.getProperty("options.editing.mode"),mode);
 67
 68		useDefaults = new JCheckBox(jEdit.getProperty("options.editing.useDefaults"));
 69		useDefaults.addActionListener(new ActionHandler());
 70		addComponent(useDefaults);
 71
 72		addComponent(jEdit.getProperty("options.editing.noWordSep"),
 73			noWordSep = new JTextField());
 74
 75		String[] foldModes = FoldHandler.getFoldModes();
 76		addComponent(jEdit.getProperty("options.editing.folding"),
 77			folding = new JComboBox(foldModes));
 78
 79		addComponent(jEdit.getProperty("options.editing.collapseFolds"),
 80			collapseFolds = new JTextField());
 81
 82		String[] wrapModes = {
 83			"none",
 84			"soft",
 85			"hard"
 86		};
 87		addComponent(jEdit.getProperty("options.editing.wrap"),
 88			wrap = new JComboBox(wrapModes));
 89
 90		String[] lineLens = { "0", "72", "76", "80" };
 91		addComponent(jEdit.getProperty("options.editing.maxLineLen"),
 92			maxLineLen = new JComboBox(lineLens));
 93		maxLineLen.setEditable(true);
 94
 95		String[] tabSizes = { "2", "4", "8" };
 96		addComponent(jEdit.getProperty("options.editing.tabSize"),
 97			tabSize = new JComboBox(tabSizes));
 98		tabSize.setEditable(true);
 99
100		addComponent(jEdit.getProperty("options.editing.indentSize"),
101			indentSize = new JComboBox(tabSizes));
102		indentSize.setEditable(true);
103
104		addComponent(noTabs = new JCheckBox(jEdit.getProperty(
105			"options.editing.noTabs")));
106
107		addComponent(deepIndent = new JCheckBox(jEdit.getProperty(
108			"options.editing.deepIndent")));
109
110		addComponent(jEdit.getProperty("options.editing.filenameGlob"),
111			filenameGlob = new JTextField());
112
113		addComponent(jEdit.getProperty("options.editing.firstlineGlob"),
114			firstlineGlob = new JTextField());
115
116		selectMode();
117
118		addSeparator();
119
120		defaultMode = new JComboBox(modes);
121		defaultMode.setSelectedItem(jEdit.getMode(
122			jEdit.getProperty("buffer.defaultMode")));
123		addComponent(jEdit.getProperty("options.editing.defaultMode"),
124			defaultMode);
125
126		undoCount = new JTextField(jEdit.getProperty("buffer.undoCount"));
127		addComponent(jEdit.getProperty("options.editing.undoCount"),undoCount);
128	} //}}}
129
130	//{{{ _save() method
131	protected void _save()
132	{
133		jEdit.setProperty("buffer.defaultMode",
134			((Mode)defaultMode.getSelectedItem()).getName());
135		jEdit.setProperty("buffer.undoCount",undoCount.getText());
136
137		saveMode();
138
139		global.save();
140
141		for(int i = 0; i < modeProps.length; i++)
142		{
143			modeProps[i].save();
144		}
145	} //}}}
146
147	//{{{ Private members
148
149	//{{{ Instance variables
150	private JComboBox defaultMode;
151	private JTextField undoCount;
152	private ModeProperties global;
153	private ModeProperties[] modeProps;
154	private ModeProperties current;
155	private Box captionBox;
156	private JComboBox mode;
157	private JCheckBox useDefaults;
158	private JTextField filenameGlob;
159	private JTextField firstlineGlob;
160	private JTextField noWordSep;
161	private JComboBox folding;
162	private JTextField collapseFolds;
163	private JComboBox wrap;
164	private JComboBox maxLineLen;
165	private JComboBox tabSize;
166	private JComboBox indentSize;
167	private JCheckBox noTabs;
168	private JCheckBox deepIndent;
169	//}}}
170
171	//{{{ saveMode() method
172	private void saveMode()
173	{
174		current.useDefaults = useDefaults.isSelected();
175		current.filenameGlob = filenameGlob.getText();
176		current.firstlineGlob = firstlineGlob.getText();
177		current.noWordSep = noWordSep.getText();
178		current.folding = (String)folding.getSelectedItem();
179		current.collapseFolds = collapseFolds.getText();
180		current.wrap = (String)wrap.getSelectedItem();
181		current.maxLineLen = (String)maxLineLen.getSelectedItem();
182		current.tabSize = (String)tabSize.getSelectedItem();
183		current.indentSize = (String)indentSize.getSelectedItem();
184		current.noTabs = noTabs.isSelected();
185		current.deepIndent = deepIndent.isSelected();
186	} //}}}
187
188	//{{{ selectMode() method
189	private void selectMode()
190	{
191		int index = mode.getSelectedIndex();
192		current = (index == 0 ? global : modeProps[index - 1]);
193		current.edited = true;
194		current.load();
195
196		captionBox.removeAll();
197		captionBox.add(GUIUtilities.createMultilineLabel(
198			jEdit.getProperty("options.editing.caption-"
199			+ (index == 0 ? "0" : "1"))));
200
201		useDefaults.setSelected(current.useDefaults);
202		filenameGlob.setText(current.filenameGlob);
203		firstlineGlob.setText(current.firstlineGlob);
204		noWordSep.setText(current.noWordSep);
205		folding.setSelectedItem(current.folding);
206		collapseFolds.setText(current.collapseFolds);
207		wrap.setSelectedItem(current.wrap);
208		maxLineLen.setSelectedItem(current.maxLineLen);
209		tabSize.setSelectedItem(current.tabSize);
210		indentSize.setSelectedItem(current.indentSize);
211		noTabs.setSelected(current.noTabs);
212		deepIndent.setSelected(current.deepIndent);
213
214		updateEnabled();
215		revalidate();
216	} //}}}
217
218	//{{{ updateEnabled() method
219	private void updateEnabled()
220	{
221		boolean enabled;
222		if(current == global)
223		{
224			enabled = true;
225			useDefaults.setEnabled(false);
226			filenameGlob.setEnabled(false);
227			firstlineGlob.setEnabled(false);
228		}
229		else
230		{
231			enabled = !modeProps[mode.getSelectedIndex() - 1]
232				.useDefaults;
233			useDefaults.setEnabled(true);
234			filenameGlob.setEnabled(enabled);
235			firstlineGlob.setEnabled(enabled);
236		}
237
238		noWordSep.setEnabled(enabled);
239		folding.setEnabled(enabled);
240		collapseFolds.setEnabled(enabled);
241		wrap.setEnabled(enabled);
242		maxLineLen.setEnabled(enabled);
243		tabSize.setEnabled(enabled);
244		indentSize.setEnabled(enabled);
245		noTabs.setEnabled(enabled);
246		deepIndent.setEnabled(enabled);
247	} //}}}
248
249	//}}}
250
251	//{{{ ActionHandler class
252	class ActionHandler implements ActionListener
253	{
254		public void actionPerformed(ActionEvent evt)
255		{
256			Object source = evt.getSource();
257			if(source == mode)
258			{
259				saveMode();
260				selectMode();
261			}
262			else if(source == useDefaults)
263			{
264				modeProps[mode.getSelectedIndex() - 1].useDefaults =
265					useDefaults.isSelected();
266				updateEnabled();
267			}
268		}
269	} //}}}
270
271	//{{{ ModeProperties class
272	static class ModeProperties
273	{
274		//{{{ Instance variables
275		Mode mode;
276		boolean edited;
277		boolean loaded;
278
279		boolean useDefaults;
280		String filenameGlob;
281		String firstlineGlob;
282		String noWordSep;
283		String folding;
284		String collapseFolds;
285		String wrap;
286		String maxLineLen;
287		String tabSize;
288		String indentSize;
289		boolean noTabs;
290		boolean deepIndent;
291		//}}}
292
293		//{{{ ModeProperties constructor
294		ModeProperties()
295		{
296		} //}}}
297
298		//{{{ ModeProperties constructor
299		ModeProperties(Mode mode)
300		{
301			this.mode = mode;
302		} //}}}
303
304		//{{{ load() method
305		void load()
306		{
307			if(loaded)
308				return;
309
310			loaded = true;
311
312			if(mode != null)
313			{
314				mode.loadIfNecessary();
315
316				useDefaults = !jEdit.getBooleanProperty("mode."
317					+ mode.getName() + ".customSettings");
318				filenameGlob = (String)mode.getProperty("filenameGlob");
319				firstlineGlob = (String)mode.getProperty("firstlineGlob");
320				noWordSep = (String)mode.getProperty("noWordSep");
321				folding = mode.getProperty("folding").toString();
322				collapseFolds = mode.getProperty("collapseFolds").toString();
323				wrap = mode.getProperty("wrap").toString();
324				maxLineLen = mode.getProperty("maxLineLen").toString();
325				tabSize = mode.getProperty("tabSize").toString();
326				indentSize = mode.getProperty("indentSize").toString();
327				noTabs = mode.getBooleanProperty("noTabs");
328				deepIndent = mode.getBooleanProperty("deepIndent");
329			}
330			else
331			{
332				noWordSep = jEdit.getProperty("buffer.noWordSep");
333				folding = jEdit.getProperty("buffer.folding");
334				collapseFolds = jEdit.getProperty("buffer.collapseFolds");
335				wrap = jEdit.getProperty("buffer.wrap");
336				maxLineLen = jEdit.getProperty("buffer.maxLineLen");
337				tabSize = jEdit.getProperty("buffer.tabSize");
338				indentSize = jEdit.getProperty("buffer.indentSize");
339				noTabs = jEdit.getBooleanProperty("buffer.noTabs");
340				deepIndent = jEdit.getBooleanProperty("buffer.deepIndent");
341			}
342		} //}}}
343
344		//{{{ save() method
345		void save()
346		{
347			// don't do anything if the user didn't change
348			// any settings
349			if(!edited)
350				return;
351
352			String prefix;
353			if(mode != null)
354			{
355				prefix = "mode." + mode.getName() + '.';
356				jEdit.setBooleanProperty(prefix + "customSettings",!useDefaults);
357
358				// need to call Mode.init() if the file name or first line
359				// globs change
360				String oldFilenameGlob = (String)mode.getProperty("filenameGlob");
361				String oldFirstlineGlob = (String)mode.getProperty("firstlineGlob");
362				if(useDefaults)
363				{
364					jEdit.resetProperty(prefix + "filenameGlob");
365					jEdit.resetProperty(prefix + "firstlineGlob");
366					jEdit.resetProperty(prefix + "noWordSep");
367					jEdit.resetProperty(prefix + "folding");
368					jEdit.resetProperty(prefix + "collapseFolds");
369					jEdit.resetProperty(prefix + "wrap");
370					jEdit.resetProperty(prefix + "maxLineLen");
371					jEdit.resetProperty(prefix + "tabSize");
372					jEdit.resetProperty(prefix + "indentSize");
373					jEdit.resetProperty(prefix + "noTabs");
374					jEdit.resetProperty(prefix + "deepIndent");
375	
376					if(!(MiscUtilities.objectsEqual(oldFilenameGlob,
377						mode.getProperty("filenameGlob"))
378						&& MiscUtilities.objectsEqual(oldFirstlineGlob,
379						mode.getProperty("firstlineGlob"))))
380					{
381						mode.init();
382					}
383
384					return;
385				}
386				else
387				{
388					jEdit.setProperty(prefix + "filenameGlob",filenameGlob);
389					jEdit.setProperty(prefix + "firstlineGlob",firstlineGlob);
390
391					if(!(MiscUtilities.objectsEqual(oldFilenameGlob,
392						filenameGlob)
393						&& MiscUtilities.objectsEqual(oldFirstlineGlob,
394						firstlineGlob)))
395					{
396						mode.init();
397					}
398				}
399			}
400			else
401			{
402				prefix = "buffer.";
403			}
404
405			jEdit.setProperty(prefix + "noWordSep",noWordSep);
406			jEdit.setProperty(prefix + "folding",folding);
407			jEdit.setProperty(prefix + "collapseFolds",collapseFolds);
408			jEdit.setProperty(prefix + "wrap",wrap);
409			jEdit.setProperty(prefix + "maxLineLen",maxLineLen);
410			jEdit.setProperty(prefix + "tabSize",tabSize);
411			jEdit.setProperty(prefix + "indentSize",indentSize);
412			jEdit.setBooleanProperty(prefix + "noTabs",noTabs);
413			jEdit.setBooleanProperty(prefix + "deepIndent",deepIndent);
414		} //}}}
415	} //}}}
416}