PageRenderTime 134ms CodeModel.GetById 98ms app.highlight 31ms RepoModel.GetById 0ms app.codeStats 1ms

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

#
Java | 438 lines | 337 code | 56 blank | 45 comment | 32 complexity | 9a75b2ddf7000b1ce3029ab95b686c7d 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		MiscUtilities.quicksort(modes,new MiscUtilities.StringICaseCompare());
 45
 46		global = new ModeProperties();
 47		modeProps = new ModeProperties[modes.length];
 48
 49		String[] modeNames = new String[modes.length + 1];
 50		modeNames[0] = jEdit.getProperty("options.editing.global");
 51
 52		for(int i = 0; i < modes.length; i++)
 53		{
 54			modeProps[i] = new ModeProperties(modes[i]);
 55			modeNames[i + 1] = modes[i].getName();
 56		}
 57
 58		mode = new JComboBox(modeNames);
 59		mode.addActionListener(new ActionHandler());
 60
 61		captionBox = new Box(BoxLayout.X_AXIS);
 62		addComponent(captionBox);
 63
 64		addComponent(jEdit.getProperty("options.editing.mode"),mode);
 65
 66		useDefaults = new JCheckBox(jEdit.getProperty("options.editing.useDefaults"));
 67		useDefaults.addActionListener(new ActionHandler());
 68		addComponent(useDefaults);
 69
 70		addComponent(jEdit.getProperty("options.editing.noWordSep"),
 71			noWordSep = new JTextField());
 72
 73		String[] foldModes = FoldHandler.getFoldModes();
 74		addComponent(jEdit.getProperty("options.editing.folding"),
 75			folding = new JComboBox(foldModes));
 76
 77		addComponent(jEdit.getProperty("options.editing.collapseFolds"),
 78			collapseFolds = new JTextField());
 79
 80		String[] wrapModes = {
 81			"none",
 82			"soft",
 83			"hard"
 84		};
 85		addComponent(jEdit.getProperty("options.editing.wrap"),
 86			wrap = new JComboBox(wrapModes));
 87		wrap.addActionListener(new ActionHandler());
 88
 89		String[] lineLens = { "0", "72", "76", "80" };
 90		addComponent(jEdit.getProperty("options.editing.maxLineLen"),
 91			maxLineLen = new JComboBox(lineLens));
 92		maxLineLen.setEditable(true);
 93		maxLineLen.addActionListener(new ActionHandler());
 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			else if(source == wrap)
269			{
270				if(!wrap.getSelectedItem().equals("none"))
271				{
272					if(maxLineLen.getSelectedItem()
273						.equals("0"))
274					{
275						maxLineLen.setSelectedItem("80");
276					}
277				}
278			}
279			else if(source == maxLineLen)
280			{
281				if(!wrap.getSelectedItem().equals("none"))
282				{
283					if(maxLineLen.getSelectedItem()
284						.equals("0"))
285					{
286						wrap.setSelectedItem("none");
287					}
288				}
289			}
290		}
291	} //}}}
292
293	//{{{ ModeProperties class
294	class ModeProperties
295	{
296		//{{{ Instance variables
297		Mode mode;
298		boolean edited;
299		boolean loaded;
300
301		boolean useDefaults;
302		String filenameGlob;
303		String firstlineGlob;
304		String noWordSep;
305		String folding;
306		String collapseFolds;
307		String wrap;
308		String maxLineLen;
309		String tabSize;
310		String indentSize;
311		boolean noTabs;
312		boolean deepIndent;
313		//}}}
314
315		//{{{ ModeProperties constructor
316		ModeProperties()
317		{
318		} //}}}
319
320		//{{{ ModeProperties constructor
321		ModeProperties(Mode mode)
322		{
323			this.mode = mode;
324		} //}}}
325
326		//{{{ load() method
327		void load()
328		{
329			if(loaded)
330				return;
331
332			loaded = true;
333
334			if(mode != null)
335			{
336				mode.loadIfNecessary();
337
338				useDefaults = !jEdit.getBooleanProperty("mode."
339					+ mode.getName() + ".customSettings");
340				filenameGlob = (String)mode.getProperty("filenameGlob");
341				firstlineGlob = (String)mode.getProperty("firstlineGlob");
342				noWordSep = (String)mode.getProperty("noWordSep");
343				folding = mode.getProperty("folding").toString();
344				collapseFolds = mode.getProperty("collapseFolds").toString();
345				wrap = mode.getProperty("wrap").toString();
346				maxLineLen = mode.getProperty("maxLineLen").toString();
347				tabSize = mode.getProperty("tabSize").toString();
348				indentSize = mode.getProperty("indentSize").toString();
349				noTabs = mode.getBooleanProperty("noTabs");
350				deepIndent = mode.getBooleanProperty("deepIndent");
351			}
352			else
353			{
354				noWordSep = jEdit.getProperty("buffer.noWordSep");
355				folding = jEdit.getProperty("buffer.folding");
356				collapseFolds = jEdit.getProperty("buffer.collapseFolds");
357				wrap = jEdit.getProperty("buffer.wrap");
358				maxLineLen = jEdit.getProperty("buffer.maxLineLen");
359				tabSize = jEdit.getProperty("buffer.tabSize");
360				indentSize = jEdit.getProperty("buffer.indentSize");
361				noTabs = jEdit.getBooleanProperty("buffer.noTabs");
362				deepIndent = jEdit.getBooleanProperty("buffer.deepIndent");
363			}
364		} //}}}
365
366		//{{{ save() method
367		void save()
368		{
369			// don't do anything if the user didn't change
370			// any settings
371			if(!edited)
372				return;
373
374			String prefix;
375			if(mode != null)
376			{
377				prefix = "mode." + mode.getName() + ".";
378				jEdit.setBooleanProperty(prefix + "customSettings",!useDefaults);
379
380				// need to call Mode.init() if the file name or first line
381				// globs change
382				String oldFilenameGlob = (String)mode.getProperty("filenameGlob");
383				String oldFirstlineGlob = (String)mode.getProperty("firstlineGlob");
384				if(useDefaults)
385				{
386					jEdit.resetProperty(prefix + "filenameGlob");
387					jEdit.resetProperty(prefix + "firstlineGlob");
388					jEdit.resetProperty(prefix + "noWordSep");
389					jEdit.resetProperty(prefix + "folding");
390					jEdit.resetProperty(prefix + "collapseFolds");
391					jEdit.resetProperty(prefix + "wrap");
392					jEdit.resetProperty(prefix + "maxLineLen");
393					jEdit.resetProperty(prefix + "tabSize");
394					jEdit.resetProperty(prefix + "indentSize");
395					jEdit.resetProperty(prefix + "noTabs");
396					jEdit.resetProperty(prefix + "deepIndent");
397	
398					if(!(MiscUtilities.objectsEqual(oldFilenameGlob,
399						(String)mode.getProperty("filenameGlob"))
400						&& MiscUtilities.objectsEqual(oldFirstlineGlob,
401						(String)mode.getProperty("firstlineGlob"))))
402					{
403						mode.init();
404					}
405
406					return;
407				}
408				else
409				{
410					jEdit.setProperty(prefix + "filenameGlob",filenameGlob);
411					jEdit.setProperty(prefix + "firstlineGlob",firstlineGlob);
412
413					if(!(MiscUtilities.objectsEqual(oldFilenameGlob,
414						filenameGlob)
415						&& MiscUtilities.objectsEqual(oldFirstlineGlob,
416						firstlineGlob)))
417					{
418						mode.init();
419					}
420				}
421			}
422			else
423			{
424				prefix = "buffer.";
425			}
426
427			jEdit.setProperty(prefix + "noWordSep",noWordSep);
428			jEdit.setProperty(prefix + "folding",folding);
429			jEdit.setProperty(prefix + "collapseFolds",collapseFolds);
430			jEdit.setProperty(prefix + "wrap",wrap);
431			jEdit.setProperty(prefix + "maxLineLen",maxLineLen);
432			jEdit.setProperty(prefix + "tabSize",tabSize);
433			jEdit.setProperty(prefix + "indentSize",indentSize);
434			jEdit.setBooleanProperty(prefix + "noTabs",noTabs);
435			jEdit.setBooleanProperty(prefix + "deepIndent",deepIndent);
436		} //}}}
437	} //}}}
438}