001: /*
002: * EncodingsOptionPane.java - Encodings options panel
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2006 Björn Kautler
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.options;
024:
025: //{{{ Imports
026: import java.awt.Component;
027: import java.awt.Dimension;
028:
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031:
032: import java.util.Arrays;
033: import java.util.Vector;
034:
035: import javax.swing.Box;
036: import javax.swing.JButton;
037: import javax.swing.JCheckBox;
038: import javax.swing.JComboBox;
039: import javax.swing.JLabel;
040: import javax.swing.JPanel;
041: import javax.swing.JScrollPane;
042: import javax.swing.JTextField;
043:
044: import javax.swing.border.EmptyBorder;
045: import javax.swing.border.TitledBorder;
046:
047: import javax.swing.event.TableModelEvent;
048: import javax.swing.event.TableModelListener;
049:
050: import org.gjt.sp.jedit.AbstractOptionPane;
051: import org.gjt.sp.jedit.Buffer;
052: import org.gjt.sp.jedit.MiscUtilities;
053: import org.gjt.sp.jedit.jEdit;
054:
055: import org.gjt.sp.jedit.MiscUtilities.StringICaseCompare;
056:
057: import org.gjt.sp.jedit.gui.JCheckBoxList;
058:
059: import org.gjt.sp.jedit.gui.JCheckBoxList.Entry;
060:
061: import static java.awt.GridBagConstraints.BOTH;
062:
063: import static java.util.Arrays.sort;
064:
065: import static javax.swing.Box.createHorizontalBox;
066: import static javax.swing.Box.createHorizontalStrut;
067:
068: import static org.gjt.sp.jedit.jEdit.getBooleanProperty;
069: import static org.gjt.sp.jedit.jEdit.getProperty;
070: import static org.gjt.sp.jedit.jEdit.setBooleanProperty;
071: import static org.gjt.sp.jedit.jEdit.unsetProperty;
072:
073: import static org.gjt.sp.jedit.MiscUtilities.getEncodings;
074:
075: //}}}
076:
077: //{{{ EncodingsOptionPane class
078: /**
079: * Encodings options.
080: *
081: * @author Björn Kautler
082: * @since jEdit 4.3pre6
083: * @version $Id: EncodingsOptionPane.java 9998 2007-07-11 10:25:00Z Vampire0 $
084: */
085: public class EncodingsOptionPane extends AbstractOptionPane {
086: //{{{ Instance variables
087: private JComboBox defaultEncoding;
088: private JCheckBox encodingAutodetect;
089: private JTextField encodingDetectors;
090: private JTextField fallbackEncodings;
091:
092: private JCheckBoxList encodingsList;
093: private JButton selectAllButton;
094: private JButton selectNoneButton;
095:
096: //}}}
097:
098: //{{{ EncodingsOptionPane constructor
099: public EncodingsOptionPane() {
100: super ("encodings");
101: } //}}}
102:
103: //{{{ _init() method
104: protected void _init() {
105: // Default file encoding
106: String[] encodings = getEncodings(true);
107: sort(encodings, new StringICaseCompare());
108: defaultEncoding = new JComboBox(encodings);
109: defaultEncoding.setEditable(true);
110: defaultEncoding
111: .setSelectedItem(jEdit.getProperty("buffer."
112: + Buffer.ENCODING, System
113: .getProperty("file.encoding")));
114: addComponent(jEdit.getProperty("options.general.encoding"),
115: defaultEncoding);
116:
117: // Auto detect encoding
118: encodingAutodetect = new JCheckBox(jEdit
119: .getProperty("options.general.encodingAutodetect"));
120: encodingAutodetect.setSelected(jEdit
121: .getBooleanProperty("buffer.encodingAutodetect"));
122: addComponent(encodingAutodetect, BOTH);
123:
124: // Encoding detectors
125: encodingDetectors = new JTextField(jEdit.getProperty(
126: "encodingDetectors", "BOM XML-PI"));
127: addComponent(jEdit
128: .getProperty("options.general.encodingDetectors"),
129: encodingDetectors);
130:
131: // Fallback Encodings
132: fallbackEncodings = new JTextField(jEdit.getProperty(
133: "fallbackEncodings", ""));
134: fallbackEncodings
135: .setToolTipText(jEdit
136: .getProperty("options.general.fallbackEncodings.tooltip"));
137: addComponent(jEdit
138: .getProperty("options.general.fallbackEncodings"),
139: fallbackEncodings);
140:
141: // Encodings to display
142: encodings = getEncodings(false);
143: sort(encodings, new StringICaseCompare());
144: Vector<Entry> encodingEntriesVector = new Vector<Entry>();
145: boolean enableSelectAll = false;
146: boolean enableSelectNone = false;
147: for (String encoding : encodings) {
148: boolean selected = !getBooleanProperty("encoding.opt-out."
149: + encoding, false);
150: enableSelectAll = enableSelectAll || !selected;
151: enableSelectNone = enableSelectNone || selected;
152: encodingEntriesVector.add(new Entry(selected, encoding));
153: }
154: encodingsList = new JCheckBoxList(encodingEntriesVector);
155: encodingsList.getModel().addTableModelListener(
156: new TableModelHandler());
157: JScrollPane encodingsScrollPane = new JScrollPane(encodingsList);
158: encodingsScrollPane.setBorder(new TitledBorder(
159: getProperty("options.encodings.selectEncodings")));
160: Dimension d = encodingsList.getPreferredSize();
161: d.height = Math.min(d.height, 200);
162: encodingsScrollPane.setPreferredSize(d);
163: addComponent(encodingsScrollPane, BOTH);
164:
165: // Select All/None Buttons
166: Box buttonsBox = createHorizontalBox();
167: buttonsBox.add(createHorizontalStrut(12));
168:
169: ActionHandler actionHandler = new ActionHandler();
170: selectAllButton = new JButton(
171: getProperty("options.encodings.selectAll"));
172: selectAllButton.addActionListener(actionHandler);
173: selectAllButton.setEnabled(enableSelectAll);
174: buttonsBox.add(selectAllButton);
175: buttonsBox.add(createHorizontalStrut(12));
176:
177: selectNoneButton = new JButton(
178: getProperty("options.encodings.selectNone"));
179: selectNoneButton.addActionListener(actionHandler);
180: selectNoneButton.setEnabled(enableSelectNone);
181: buttonsBox.add(selectNoneButton);
182: buttonsBox.add(createHorizontalStrut(12));
183:
184: addComponent(buttonsBox);
185: } //}}}
186:
187: //{{{ _save() method
188: protected void _save() {
189:
190: jEdit.setProperty("buffer." + Buffer.ENCODING,
191: (String) defaultEncoding.getSelectedItem());
192: jEdit.setBooleanProperty("buffer.encodingAutodetect",
193: encodingAutodetect.isSelected());
194: jEdit.setProperty("encodingDetectors", encodingDetectors
195: .getText());
196: jEdit.setProperty("fallbackEncodings", fallbackEncodings
197: .getText());
198:
199: for (Entry entry : encodingsList.getValues()) {
200: if (entry.isChecked()) {
201: unsetProperty("encoding.opt-out." + entry.getValue());
202: } else {
203: setBooleanProperty("encoding.opt-out."
204: + entry.getValue(), true);
205: }
206: }
207: } //}}}
208:
209: //{{{ Inner classes
210:
211: //{{{ ActionHandler class
212: class ActionHandler implements ActionListener {
213: public void actionPerformed(ActionEvent ae) {
214: Object source = ae.getSource();
215: if (source == selectAllButton) {
216: encodingsList.selectAll();
217: } else if (source == selectNoneButton) {
218: for (int i = 0, c = encodingsList.getRowCount(); i < c; i++) {
219: encodingsList.setValueAt(false, i, 0);
220: }
221: }
222: }
223: } //}}}
224:
225: //{{{ TableModelHandler class
226: class TableModelHandler implements TableModelListener {
227: public void tableChanged(TableModelEvent tme) {
228: int checkedAmount = encodingsList.getCheckedValues().length;
229: if (0 == checkedAmount) {
230: selectNoneButton.setEnabled(false);
231: } else {
232: selectNoneButton.setEnabled(true);
233: }
234: if (encodingsList.getValues().length == checkedAmount) {
235: selectAllButton.setEnabled(false);
236: } else {
237: selectAllButton.setEnabled(true);
238: }
239: }
240: } //}}}
241:
242: //}}}
243:
244: } //}}}
|