PageRenderTime 122ms CodeModel.GetById 100ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/options/ModeOptionPane.java

#
Java | 325 lines | 243 code | 40 blank | 42 comment | 10 complexity | 2cd75f4e525feb3a28a827d93b29d926 MD5 | raw file
  1/*
  2 * ModeOptionPane.java - Mode-specific options panel
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1998, 1999, 2000, 2001, 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.awt.*;
 29import org.gjt.sp.jedit.*;
 30//}}}
 31
 32public class ModeOptionPane extends AbstractOptionPane
 33{
 34	//{{{ ModeOptionPane constructor
 35	public ModeOptionPane()
 36	{
 37		super("mode");
 38	} //}}}
 39
 40	//{{{ _init() method
 41	protected void _init()
 42	{
 43		Mode[] modes = jEdit.getModes();
 44		String[] modeNames = new String[modes.length];
 45		modeProps = new ModeProperties[modes.length];
 46		for(int i = 0; i < modes.length; i++)
 47		{
 48			modeProps[i] = new ModeProperties(modes[i]);
 49			modeNames[i] = modes[i].getName();
 50		}
 51		mode = new JComboBox(modeNames);
 52		mode.addActionListener(new ActionHandler());
 53
 54		addComponent(jEdit.getProperty("options.mode.mode"),mode);
 55
 56		useDefaults = new JCheckBox(jEdit.getProperty("options.mode.useDefaults"));
 57		useDefaults.addActionListener(new ActionHandler());
 58		addComponent(useDefaults);
 59
 60		addComponent(jEdit.getProperty("options.mode.filenameGlob"),
 61			filenameGlob = new JTextField());
 62
 63		addComponent(jEdit.getProperty("options.mode.firstlineGlob"),
 64			firstlineGlob = new JTextField());
 65
 66		addComponent(jEdit.getProperty("options.editing.noWordSep"),
 67			noWordSep = new JTextField());
 68
 69		String[] foldModes = {
 70			"none",
 71			"indent",
 72			"explicit"
 73		};
 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
 88		String[] lineLens = { "0", "72", "76", "80" };
 89		addComponent(jEdit.getProperty("options.editing.maxLineLen"),
 90			maxLineLen = new JComboBox(lineLens));
 91		maxLineLen.setEditable(true);
 92
 93		String[] tabSizes = { "2", "4", "8" };
 94		addComponent(jEdit.getProperty("options.editing.tabSize"),
 95			tabSize = new JComboBox(tabSizes));
 96		tabSize.setEditable(true);
 97
 98		addComponent(jEdit.getProperty("options.editing.indentSize"),
 99			indentSize = new JComboBox(tabSizes));
100		indentSize.setEditable(true);
101
102		addComponent(noTabs = new JCheckBox(jEdit.getProperty(
103			"options.editing.noTabs")));
104
105		addComponent(indentOnTab = new JCheckBox(jEdit.getProperty(
106			"options.editing.indentOnTab")));
107
108		addComponent(indentOnEnter = new JCheckBox(jEdit.getProperty(
109			"options.editing.indentOnEnter")));
110
111		selectMode();
112	} //}}}
113
114	//{{{ _save() method
115	protected void _save()
116	{
117		saveMode();
118
119		for(int i = 0; i < modeProps.length; i++)
120		{
121			modeProps[i].save();
122		}
123	} //}}}
124
125	//{{{ Private members
126
127	//{{{ Instance variables
128	private ModeProperties[] modeProps;
129	private ModeProperties current;
130	private JComboBox mode;
131	private JCheckBox useDefaults;
132	private JTextField filenameGlob;
133	private JTextField firstlineGlob;
134	private JTextField noWordSep;
135	private JComboBox folding;
136	private JTextField collapseFolds;
137	private JComboBox wrap;
138	private JComboBox maxLineLen;
139	private JComboBox tabSize;
140	private JComboBox indentSize;
141	private JCheckBox noTabs;
142	private JCheckBox indentOnTab;
143	private JCheckBox indentOnEnter;
144	//}}}
145
146	//{{{ saveMode() method
147	private void saveMode()
148	{
149		current.useDefaults = useDefaults.isSelected();
150		current.filenameGlob = filenameGlob.getText();
151		current.firstlineGlob = firstlineGlob.getText();
152		current.noWordSep = noWordSep.getText();
153		current.folding = (String)folding.getSelectedItem();
154		current.collapseFolds = collapseFolds.getText();
155		current.wrap = (String)wrap.getSelectedItem();
156		current.maxLineLen = (String)maxLineLen.getSelectedItem();
157		current.tabSize = (String)tabSize.getSelectedItem();
158		current.indentSize = (String)indentSize.getSelectedItem();
159		current.noTabs = noTabs.isSelected();
160		current.indentOnEnter = indentOnEnter.isSelected();
161		current.indentOnTab = indentOnTab.isSelected();
162	} //}}}
163
164	//{{{ selectMode() method
165	private void selectMode()
166	{
167		current = modeProps[mode.getSelectedIndex()];
168		current.edited = true;
169		current.load();
170
171		useDefaults.setSelected(current.useDefaults);
172		filenameGlob.setText(current.filenameGlob);
173		firstlineGlob.setText(current.firstlineGlob);
174		noWordSep.setText(current.noWordSep);
175		folding.setSelectedItem(current.folding);
176		collapseFolds.setText(current.collapseFolds);
177		wrap.setSelectedItem(current.wrap);
178		maxLineLen.setSelectedItem(current.maxLineLen);
179		tabSize.setSelectedItem(current.tabSize);
180		indentSize.setSelectedItem(current.indentSize);
181		noTabs.setSelected(current.noTabs);
182		indentOnTab.setSelected(current.indentOnTab);
183		indentOnEnter.setSelected(current.indentOnEnter);
184
185		updateEnabled();
186	} //}}}
187
188	//{{{ updateEnabled() method
189	private void updateEnabled()
190	{
191		boolean enabled = !modeProps[mode.getSelectedIndex()].useDefaults;
192		filenameGlob.setEnabled(enabled);
193		firstlineGlob.setEnabled(enabled);
194		noWordSep.setEnabled(enabled);
195		folding.setEnabled(enabled);
196		collapseFolds.setEnabled(enabled);
197		wrap.setEnabled(enabled);
198		maxLineLen.setEnabled(enabled);
199		tabSize.setEnabled(enabled);
200		indentSize.setEnabled(enabled);
201		noTabs.setEnabled(enabled);
202		indentOnTab.setEnabled(enabled);
203		indentOnEnter.setEnabled(enabled);
204	} //}}}
205
206	//}}}
207
208	//{{{ ActionHandler class
209	class ActionHandler implements ActionListener
210	{
211		public void actionPerformed(ActionEvent evt)
212		{
213			if(evt.getSource() == mode)
214			{
215				saveMode();
216				selectMode();
217			}
218			else if(evt.getSource() == useDefaults)
219			{
220				modeProps[mode.getSelectedIndex()].useDefaults =
221					useDefaults.isSelected();
222				updateEnabled();
223			}
224		}
225	} //}}}
226
227	//{{{ ModeProperties class
228	class ModeProperties
229	{
230		//{{{ Instance variables
231		Mode mode;
232		boolean edited;
233		boolean loaded;
234
235		boolean useDefaults;
236		String filenameGlob;
237		String firstlineGlob;
238		String noWordSep;
239		String folding;
240		String collapseFolds;
241		String wrap;
242		String maxLineLen;
243		String tabSize;
244		String indentSize;
245		boolean noTabs;
246		boolean indentOnTab;
247		boolean indentOnEnter;
248		//}}}
249
250		//{{{ ModeProperties constructor
251		ModeProperties(Mode mode)
252		{
253			this.mode = mode;
254		} //}}}
255
256		//{{{ load() method
257		void load()
258		{
259			if(loaded)
260				return;
261
262			loaded = true;
263
264			mode.loadIfNecessary();
265
266			useDefaults = !jEdit.getBooleanProperty("mode."
267				+ mode.getName() + ".customSettings");
268			filenameGlob = (String)mode.getProperty("filenameGlob");
269			firstlineGlob = (String)mode.getProperty("firstlineGlob");
270			noWordSep = (String)mode.getProperty("noWordSep");
271			folding = mode.getProperty("folding").toString();
272			collapseFolds = mode.getProperty("collapseFolds").toString();
273			wrap = mode.getProperty("wrap").toString();
274			maxLineLen = mode.getProperty("maxLineLen").toString();
275			tabSize = mode.getProperty("tabSize").toString();
276			indentSize = mode.getProperty("indentSize").toString();
277			noTabs = mode.getBooleanProperty("noTabs");
278			indentOnTab = mode.getBooleanProperty("indentOnTab");
279			indentOnEnter = mode.getBooleanProperty("indentOnEnter");
280		} //}}}
281
282		//{{{ save() method
283		void save()
284		{
285			// don't do anything if the user didn't change
286			// any settings
287			if(!edited)
288				return;
289
290			String prefix = "mode." + mode.getName() + ".";
291			jEdit.setBooleanProperty(prefix + "customSettings",!useDefaults);
292
293			if(useDefaults)
294			{
295				jEdit.resetProperty(prefix + "filenameGlob");
296				jEdit.resetProperty(prefix + "firstlineGlob");
297				jEdit.resetProperty(prefix + "noWordSep");
298				jEdit.resetProperty(prefix + "folding");
299				jEdit.resetProperty(prefix + "collapseFolds");
300				jEdit.resetProperty(prefix + "wrap");
301				jEdit.resetProperty(prefix + "maxLineLen");
302				jEdit.resetProperty(prefix + "tabSize");
303				jEdit.resetProperty(prefix + "indentSize");
304				jEdit.resetProperty(prefix + "noTabs");
305				jEdit.resetProperty(prefix + "indentOnTab");
306				jEdit.resetProperty(prefix + "indentOnEnter");
307			}
308			else
309			{
310				jEdit.setProperty(prefix + "filenameGlob",filenameGlob);
311				jEdit.setProperty(prefix + "firstlineGlob",firstlineGlob);
312				jEdit.setProperty(prefix + "noWordSep",noWordSep);
313				jEdit.setProperty(prefix + "folding",folding);
314				jEdit.setProperty(prefix + "collapseFolds",collapseFolds);
315				jEdit.setProperty(prefix + "wrap",wrap);
316				jEdit.setProperty(prefix + "maxLineLen",maxLineLen);
317				jEdit.setProperty(prefix + "tabSize",tabSize);
318				jEdit.setProperty(prefix + "indentSize",indentSize);
319				jEdit.setBooleanProperty(prefix + "noTabs",noTabs);
320				jEdit.setBooleanProperty(prefix + "indentOnTab",indentOnTab);
321				jEdit.setBooleanProperty(prefix + "indentOnEnter",indentOnEnter);
322			}
323		} //}}}
324	} //}}}
325}