PageRenderTime 186ms CodeModel.GetById 153ms app.highlight 25ms RepoModel.GetById 4ms app.codeStats 0ms

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

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