001: /*
002: * BufferOptionPane.java -
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1998, 2003 Slava Pestov
007: * Portions copyright (C) 1999 mike dillon
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: */
023:
024: package org.gjt.sp.jedit.options;
025:
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.util.Arrays;
029:
030: import javax.swing.JCheckBox;
031: import javax.swing.JComboBox;
032:
033: import org.gjt.sp.jedit.AbstractOptionPane;
034: import org.gjt.sp.jedit.Buffer;
035: import org.gjt.sp.jedit.GUIUtilities;
036: import org.gjt.sp.jedit.MiscUtilities;
037: import org.gjt.sp.jedit.Mode;
038: import org.gjt.sp.jedit.jEdit;
039: import org.gjt.sp.jedit.buffer.FoldHandler;
040: import org.gjt.sp.jedit.buffer.JEditBuffer;
041:
042: public class BufferOptionPane extends AbstractOptionPane {
043: JComboBox encoding;
044: JComboBox lineSeparator;
045: JCheckBox gzipped;
046: Mode[] modes;
047: JComboBox mode;
048: JComboBox folding;
049: JComboBox wrap;
050: JComboBox maxLineLen;
051: JComboBox tabSize;
052: JComboBox indentSize;
053: JComboBox checkModStatus;
054: JCheckBox noTabs;
055: Buffer buffer;
056:
057: public BufferOptionPane() {
058: super ("Buffer Options");
059: init();
060: }
061:
062: //{{{ _init() method
063: protected void _init() {
064:
065: buffer = jEdit.getActiveView().getBuffer();
066: String filename = buffer.getName();
067: setName("Buffer: " + filename);
068: addComponent(GUIUtilities.createMultilineLabel(jEdit
069: .getProperty("buffer-options.caption")));
070:
071: addSeparator("buffer-options.loading-saving");
072:
073: //{{{ Line separator
074: String[] lineSeps = { jEdit.getProperty("lineSep.unix"),
075: jEdit.getProperty("lineSep.windows"),
076: jEdit.getProperty("lineSep.mac") };
077: lineSeparator = new JComboBox(lineSeps);
078: String lineSep = buffer.getStringProperty(JEditBuffer.LINESEP);
079: if (lineSep == null)
080: lineSep = System.getProperty("line.separator");
081: if ("\n".equals(lineSep))
082: lineSeparator.setSelectedIndex(0);
083: else if ("\r\n".equals(lineSep))
084: lineSeparator.setSelectedIndex(1);
085: else if ("\r".equals(lineSep))
086: lineSeparator.setSelectedIndex(2);
087: addComponent(jEdit.getProperty("buffer-options.lineSeparator"),
088: lineSeparator);
089: //}}}
090:
091: //{{{ Encoding
092: String[] encodings = MiscUtilities.getEncodings(true);
093: Arrays.sort(encodings, new MiscUtilities.StringICaseCompare());
094: encoding = new JComboBox(encodings);
095: encoding.setEditable(true);
096: encoding.setSelectedItem(buffer
097: .getStringProperty(JEditBuffer.ENCODING));
098: addComponent(jEdit.getProperty("buffer-options.encoding"),
099: encoding);
100: //}}}
101:
102: //{{{ GZipped setting
103: gzipped = new JCheckBox(jEdit
104: .getProperty("buffer-options.gzipped"));
105: gzipped.setSelected(buffer.getBooleanProperty(Buffer.GZIPPED));
106: addComponent(gzipped);
107: //}}}
108:
109: //{{{ Autoreload settings
110: /* Check mod status on focus */
111: String[] modCheckOptions = {
112: jEdit
113: .getProperty("options.general.checkModStatus.nothing"),
114: jEdit
115: .getProperty("options.general.checkModStatus.prompt"),
116: jEdit
117: .getProperty("options.general.checkModStatus.reload"),
118: jEdit
119: .getProperty("options.general.checkModStatus.silentReload") };
120: checkModStatus = new JComboBox(modCheckOptions);
121: if (buffer.getAutoReload()) {
122: if (buffer.getAutoReloadDialog())
123: // reload and notify
124: checkModStatus.setSelectedIndex(2);
125: else
126: // reload silently
127: checkModStatus.setSelectedIndex(3);
128: } else {
129: if (buffer.getAutoReloadDialog())
130: // prompt
131: checkModStatus.setSelectedIndex(1);
132: else
133: // do nothing
134: checkModStatus.setSelectedIndex(0);
135: }
136: addComponent(jEdit
137: .getProperty("options.general.checkModStatus"),
138: checkModStatus);
139:
140: // }}}
141:
142: addSeparator("buffer-options.editing");
143:
144: //{{{ Edit mode
145: modes = jEdit.getModes();
146: Arrays.sort(modes, new MiscUtilities.StringICaseCompare());
147: mode = new JComboBox(modes);
148: mode.setSelectedItem(buffer.getMode());
149: ActionHandler actionListener = new ActionHandler();
150: mode.addActionListener(actionListener);
151: addComponent(jEdit.getProperty("buffer-options.mode"), mode);
152: //}}}
153:
154: //{{{ Fold mode
155: String[] foldModes = FoldHandler.getFoldModes();
156:
157: folding = new JComboBox(foldModes);
158: folding.setSelectedItem(buffer.getStringProperty("folding"));
159: addComponent(jEdit.getProperty("options.editing.folding"),
160: folding);
161: //}}}
162:
163: //{{{ Wrap mode
164: String[] wrapModes = { "none", "soft", "hard" };
165:
166: wrap = new JComboBox(wrapModes);
167: wrap.setSelectedItem(buffer.getStringProperty("wrap"));
168: addComponent(jEdit.getProperty("options.editing.wrap"), wrap);
169: //}}}
170:
171: //{{{ Max line length
172: String[] lineLengths = { "0", "72", "76", "80" };
173:
174: maxLineLen = new JComboBox(lineLengths);
175: maxLineLen.setEditable(true);
176: maxLineLen.setSelectedItem(buffer
177: .getStringProperty("maxLineLen"));
178: addComponent(jEdit.getProperty("options.editing.maxLineLen"),
179: maxLineLen);
180: //}}}
181:
182: //{{{ Tab size
183: String[] tabSizes = { "2", "4", "8" };
184: tabSize = new JComboBox(tabSizes);
185: tabSize.setEditable(true);
186: tabSize.setSelectedItem(buffer.getStringProperty("tabSize"));
187: addComponent(jEdit.getProperty("options.editing.tabSize"),
188: tabSize);
189: //}}}
190:
191: //{{{ Indent size
192: indentSize = new JComboBox(tabSizes);
193: indentSize.setEditable(true);
194: indentSize.setSelectedItem(buffer
195: .getStringProperty("indentSize"));
196: addComponent(jEdit.getProperty("options.editing.indentSize"),
197: indentSize);
198: //}}}
199:
200: //{{{ Soft tabs
201: noTabs = new JCheckBox(jEdit
202: .getProperty("options.editing.noTabs"));
203: noTabs.setSelected(buffer.getBooleanProperty("noTabs"));
204: addComponent(noTabs);
205: //}}}
206: } //}}}
207:
208: //{{{ _save() method
209: protected void _save() {
210: int index = lineSeparator.getSelectedIndex();
211: String lineSep;
212: if (index == 0)
213: lineSep = "\n";
214: else if (index == 1)
215: lineSep = "\r\n";
216: else if (index == 2)
217: lineSep = "\r";
218: else
219: throw new InternalError();
220:
221: String oldLineSep = buffer
222: .getStringProperty(JEditBuffer.LINESEP);
223: if (oldLineSep == null)
224: oldLineSep = System.getProperty("line.separator");
225: if (!oldLineSep.equals(lineSep)) {
226: buffer.setStringProperty("lineSeparator", lineSep);
227: buffer.setDirty(true);
228: }
229:
230: String encoding = (String) this .encoding.getSelectedItem();
231: String oldEncoding = buffer
232: .getStringProperty(JEditBuffer.ENCODING);
233: if (!oldEncoding.equals(encoding)) {
234: buffer.setStringProperty(JEditBuffer.ENCODING, encoding);
235: buffer.setDirty(true);
236: // Disable auto-detect because user explicitly
237: // specify an encoding.
238: buffer
239: .setBooleanProperty(Buffer.ENCODING_AUTODETECT,
240: false);
241: }
242:
243: boolean gzippedValue = gzipped.isSelected();
244: boolean oldGzipped = buffer.getBooleanProperty(Buffer.GZIPPED);
245: if (gzippedValue != oldGzipped) {
246: buffer.setBooleanProperty(Buffer.GZIPPED, gzippedValue);
247: buffer.setDirty(true);
248: }
249:
250: buffer.setStringProperty("folding", (String) folding
251: .getSelectedItem());
252:
253: buffer.setStringProperty("wrap", (String) wrap
254: .getSelectedItem());
255:
256: try {
257: buffer.setProperty("maxLineLen", new Integer(maxLineLen
258: .getSelectedItem().toString()));
259: } catch (NumberFormatException nf) {
260: }
261:
262: try {
263: buffer.setProperty("tabSize", new Integer(tabSize
264: .getSelectedItem().toString()));
265: } catch (NumberFormatException nf) {
266: }
267:
268: try {
269: buffer.setProperty("indentSize", new Integer(indentSize
270: .getSelectedItem().toString()));
271: } catch (NumberFormatException nf) {
272: }
273:
274: buffer.setBooleanProperty("noTabs", noTabs.isSelected());
275:
276: index = mode.getSelectedIndex();
277: buffer.setMode(modes[index]);
278: switch (checkModStatus.getSelectedIndex()) {
279: case 0:
280: buffer.setAutoReloadDialog(false);
281: buffer.setAutoReload(false);
282: break;
283: case 1:
284: buffer.setAutoReloadDialog(true);
285: buffer.setAutoReload(false);
286: break;
287: case 2:
288: buffer.setAutoReloadDialog(true);
289: buffer.setAutoReload(true);
290: break;
291: case 3:
292: buffer.setAutoReloadDialog(false);
293: buffer.setAutoReload(true);
294: break;
295: }
296: } //}}}
297:
298: //{{{ ActionHandler() class
299: class ActionHandler implements ActionListener {
300: //{{{ actionPerformed() method
301: public void actionPerformed(ActionEvent evt) {
302: Object source = evt.getSource();
303: if (source == mode) {
304: Mode _mode = (Mode) mode.getSelectedItem();
305: folding.setSelectedItem(_mode.getProperty("folding"));
306: wrap.setSelectedItem(_mode.getProperty("wrap"));
307: maxLineLen.setSelectedItem(_mode
308: .getProperty("maxLineLen"));
309: tabSize.setSelectedItem(_mode.getProperty("tabSize"));
310: indentSize.setSelectedItem(_mode
311: .getProperty("indentSize"));
312: noTabs.setSelected(_mode.getBooleanProperty("noTabs"));
313: }
314: } //}}}
315: } //}}}
316:
317: }
|