PageRenderTime 36ms CodeModel.GetById 8ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/options/BufferOptionPane.java

#
Java | 349 lines | 255 code | 39 blank | 55 comment | 26 complexity | af49cca99372ccc2cf1e8aaa67cabaf6 MD5 | raw file
  1/*
  2 * BufferOptionPane.java -
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1998, 2003 Slava Pestov
  7 * Portions copyright (C) 1999 mike dillon
  8 *
  9 * This program is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU General Public License
 11 * as published by the Free Software Foundation; either version 2
 12 * of the License, or any later version.
 13 *
 14 * This program is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 * GNU General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with this program; if not, write to the Free Software
 21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 22 */
 23
 24package org.gjt.sp.jedit.options;
 25
 26import java.awt.event.ActionEvent;
 27import java.awt.event.ActionListener;
 28import java.util.Arrays;
 29
 30import javax.swing.JCheckBox;
 31import javax.swing.JComboBox;
 32
 33import org.gjt.sp.jedit.AbstractOptionPane;
 34import org.gjt.sp.jedit.Buffer;
 35import org.gjt.sp.jedit.GUIUtilities;
 36import org.gjt.sp.jedit.MiscUtilities;
 37import org.gjt.sp.jedit.Mode;
 38import org.gjt.sp.jedit.jEdit;
 39import org.gjt.sp.jedit.buffer.FoldHandler;
 40import org.gjt.sp.jedit.buffer.JEditBuffer;
 41import org.gjt.sp.util.StandardUtilities;
 42
 43
 44public class BufferOptionPane extends AbstractOptionPane
 45{
 46	private JComboBox encoding;
 47	private JComboBox lineSeparator;
 48	private JCheckBox gzipped;
 49	private Mode[] modes;
 50	private JComboBox mode;
 51	private JComboBox folding;
 52	private JComboBox wrap;
 53	private JComboBox maxLineLen;
 54	private JComboBox tabSize;
 55	private JComboBox indentSize;
 56	private JComboBox checkModStatus;
 57	private JCheckBox noTabs;
 58	private Buffer buffer;
 59	private JCheckBox elasticTabstops;
 60
 61	public BufferOptionPane()
 62	{
 63		super("Buffer Options");
 64		init();
 65	}
 66
 67	//{{{ _init() method
 68	@Override
 69	protected void _init()
 70	{
 71		buffer = jEdit.getActiveView().getBuffer();
 72		String filename = buffer.getName();
 73		setName("Buffer: " + filename);
 74		addComponent(GUIUtilities.createMultilineLabel(
 75			jEdit.getProperty("buffer-options.caption")));
 76
 77		addSeparator("buffer-options.loading-saving");
 78
 79		//{{{ Line separator
 80		String[] lineSeps = { jEdit.getProperty("lineSep.unix"),
 81			jEdit.getProperty("lineSep.windows"),
 82			jEdit.getProperty("lineSep.mac") };
 83		lineSeparator = new JComboBox(lineSeps);
 84		String lineSep = buffer.getStringProperty(JEditBuffer.LINESEP);
 85		if(lineSep == null)
 86			lineSep = System.getProperty("line.separator");
 87		if("\n".equals(lineSep))
 88			lineSeparator.setSelectedIndex(0);
 89		else if("\r\n".equals(lineSep))
 90			lineSeparator.setSelectedIndex(1);
 91		else if("\r".equals(lineSep))
 92			lineSeparator.setSelectedIndex(2);
 93		addComponent(jEdit.getProperty("buffer-options.lineSeparator"),
 94			lineSeparator);
 95		//}}}
 96
 97		//{{{ Encoding
 98		String[] encodings = MiscUtilities.getEncodings(true);
 99		Arrays.sort(encodings,new StandardUtilities.StringCompare<String>(true));
100		encoding = new JComboBox(encodings);
101		encoding.setEditable(true);
102		encoding.setSelectedItem(buffer.getStringProperty(JEditBuffer.ENCODING));
103		addComponent(jEdit.getProperty("buffer-options.encoding"),
104			encoding);
105		//}}}
106
107		//{{{ GZipped setting
108		gzipped = new JCheckBox(jEdit.getProperty(
109			"buffer-options.gzipped"));
110		gzipped.setSelected(buffer.getBooleanProperty(Buffer.GZIPPED));
111		addComponent(gzipped);
112		//}}}
113
114		//{{{ Autoreload settings
115		/* Check mod status on focus */
116		String[] modCheckOptions = {
117			jEdit.getProperty("options.general.checkModStatus.nothing"),
118			jEdit.getProperty("options.general.checkModStatus.prompt"),
119			jEdit.getProperty("options.general.checkModStatus.reload"),
120			jEdit.getProperty("options.general.checkModStatus.silentReload")
121		};
122		checkModStatus = new JComboBox(modCheckOptions);
123		if(buffer.getAutoReload())
124		{
125			if(buffer.getAutoReloadDialog())
126				// reload and notify
127				checkModStatus.setSelectedIndex(2);
128			else	// reload silently
129				checkModStatus.setSelectedIndex(3);
130		}
131		else
132		{
133			if(buffer.getAutoReloadDialog())
134				// prompt
135				checkModStatus.setSelectedIndex(1);
136			else	// do nothing
137				checkModStatus.setSelectedIndex(0);
138		}
139		addComponent(jEdit.getProperty("options.general.checkModStatus"),
140			checkModStatus);
141
142		// }}}
143
144		addSeparator("buffer-options.editing");
145
146		//{{{ Edit mode
147		modes = jEdit.getModes();
148		Arrays.sort(modes,new StandardUtilities.StringCompare<Mode>(true));
149		mode = new JComboBox(modes);
150		mode.setSelectedItem(buffer.getMode());
151		ActionHandler actionListener = new ActionHandler();
152		mode.addActionListener(actionListener);
153		addComponent(jEdit.getProperty("buffer-options.mode"),mode);
154		//}}}
155
156		//{{{ Fold mode
157		String[] foldModes = FoldHandler.getFoldModes();
158
159		folding = new JComboBox(foldModes);
160		folding.setSelectedItem(buffer.getStringProperty("folding"));
161		addComponent(jEdit.getProperty("options.editing.folding"),
162			folding);
163		//}}}
164
165		//{{{ Wrap mode
166		String[] wrapModes = {
167			"none",
168			"soft",
169			"hard"
170		};
171
172		wrap = new JComboBox(wrapModes);
173		wrap.setSelectedItem(buffer.getStringProperty("wrap"));
174		addComponent(jEdit.getProperty("options.editing.wrap"),
175			wrap);
176		//}}}
177
178		//{{{ Max line length
179		String[] lineLengths = { "0", "72", "76", "80" };
180
181		maxLineLen = new JComboBox(lineLengths);
182		maxLineLen.setEditable(true);
183		maxLineLen.setSelectedItem(buffer.getStringProperty("maxLineLen"));
184		addComponent(jEdit.getProperty("options.editing.maxLineLen"),
185			maxLineLen);
186		//}}}
187
188		//{{{ Tab size
189		String[] tabSizes = { "2", "4", "8" };
190		tabSize = new JComboBox(tabSizes);
191		tabSize.setEditable(true);
192		tabSize.setSelectedItem(buffer.getStringProperty("tabSize"));
193		addComponent(jEdit.getProperty("options.editing.tabSize"),tabSize);
194		//}}}
195
196		//{{{ Indent size
197		indentSize = new JComboBox(tabSizes);
198		indentSize.setEditable(true);
199		indentSize.setSelectedItem(buffer.getStringProperty("indentSize"));
200		addComponent(jEdit.getProperty("options.editing.indentSize"),
201			indentSize);
202		//}}}
203
204		//{{{ Soft tabs
205		noTabs = new JCheckBox(jEdit.getProperty(
206			"options.editing.noTabs"));
207		noTabs.setSelected(buffer.getBooleanProperty("noTabs"));
208		addComponent(noTabs);
209		//}}}
210		
211		//{{{ Elastic tabstops
212		elasticTabstops = new JCheckBox(jEdit.getProperty(
213			"options.editing.elasticTabstops"));
214		elasticTabstops.setToolTipText(jEdit.getProperty("options.editing.elasticTabstops.tooltip"));
215		elasticTabstops.setSelected(buffer.getBooleanProperty("elasticTabstops"));
216		addComponent(elasticTabstops);
217		//}}}
218	} //}}}
219
220	//{{{ _save() method
221	@Override
222	protected void _save()
223	{
224		int index = lineSeparator.getSelectedIndex();
225		String lineSep;
226		if(index == 0)
227			lineSep = "\n";
228		else if(index == 1)
229			lineSep = "\r\n";
230		else if(index == 2)
231			lineSep = "\r";
232		else
233			throw new InternalError();
234
235		String oldLineSep = buffer.getStringProperty(JEditBuffer.LINESEP);
236		if(oldLineSep == null)
237			oldLineSep = System.getProperty("line.separator");
238		if(!oldLineSep.equals(lineSep))
239		{
240			buffer.setStringProperty(JEditBuffer.LINESEP, lineSep);
241			buffer.setDirty(true);
242		}
243
244		String encoding = (String)this.encoding.getSelectedItem();
245		String oldEncoding = buffer.getStringProperty(JEditBuffer.ENCODING);
246		if(!oldEncoding.equals(encoding))
247		{
248			buffer.setStringProperty(JEditBuffer.ENCODING,encoding);
249			buffer.setDirty(true);
250			// Disable auto-detect because user explicitly
251			// specify an encoding.
252			buffer.setBooleanProperty(Buffer.ENCODING_AUTODETECT,false);
253		}
254
255		boolean gzippedValue = gzipped.isSelected();
256		boolean oldGzipped = buffer.getBooleanProperty(
257			Buffer.GZIPPED);
258		if(gzippedValue != oldGzipped)
259		{
260			buffer.setBooleanProperty(Buffer.GZIPPED,gzippedValue);
261			buffer.setDirty(true);
262		}
263
264		buffer.setStringProperty("folding",(String)folding.getSelectedItem());
265
266		buffer.setStringProperty("wrap",(String)wrap.getSelectedItem());
267
268		try
269		{
270			buffer.setProperty("maxLineLen",new Integer(
271				maxLineLen.getSelectedItem().toString()));
272		}
273		catch(NumberFormatException nf)
274		{
275		}
276
277		try
278		{
279			buffer.setProperty("tabSize",new Integer(
280				tabSize.getSelectedItem().toString()));
281		}
282		catch(NumberFormatException nf)
283		{
284		}
285
286		try
287		{
288			buffer.setProperty("indentSize",new Integer(
289				indentSize.getSelectedItem().toString()));
290		}
291		catch(NumberFormatException nf)
292		{
293		}
294
295		buffer.setBooleanProperty("noTabs",noTabs.isSelected());
296		buffer.setBooleanProperty("elasticTabstops",elasticTabstops.isSelected());
297
298		index = mode.getSelectedIndex();
299		buffer.setMode(modes[index]);
300		switch(checkModStatus.getSelectedIndex())
301		{
302		case 0:
303			buffer.setAutoReloadDialog(false);
304			buffer.setAutoReload(false);
305			break;
306		case 1:
307			buffer.setAutoReloadDialog(true);
308			buffer.setAutoReload(false);
309			break;
310		case 2:
311			buffer.setAutoReloadDialog(true);
312			buffer.setAutoReload(true);
313			break;
314		case 3:
315			buffer.setAutoReloadDialog(false);
316			buffer.setAutoReload(true);
317			break;
318		}
319	} //}}}
320
321	//{{{ ActionHandler() class
322	private class ActionHandler implements ActionListener
323	{
324		//{{{ actionPerformed() method
325		public void actionPerformed(ActionEvent evt)
326		{
327			Object source = evt.getSource();
328			if(source == mode)
329			{
330				Mode _mode = (Mode)mode.getSelectedItem();
331				folding.setSelectedItem(_mode.getProperty(
332					"folding"));
333				wrap.setSelectedItem(_mode.getProperty(
334					"wrap"));
335				maxLineLen.setSelectedItem(_mode.getProperty(
336					"maxLineLen"));
337				tabSize.setSelectedItem(_mode.getProperty(
338					"tabSize"));
339				indentSize.setSelectedItem(_mode.getProperty(
340					"indentSize"));
341				noTabs.setSelected(_mode.getBooleanProperty(
342					"noTabs"));
343				elasticTabstops.setSelected(_mode.getBooleanProperty(
344				"elasticTabstops"));
345			}
346		} //}}}
347	} //}}}
348
349}