PageRenderTime 244ms CodeModel.GetById 225ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/options/EditingOptionPane.java

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