PageRenderTime 101ms CodeModel.GetById 79ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 230 lines | 161 code | 21 blank | 48 comment | 16 complexity | 3f5f58be8f46ddc2928e78e524b71834 MD5 | raw file
  1/*
  2 * EncodingsOptionPane.java - Encodings options panel
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2006 Bj??rn Kautler
  7 * Portions copyright (C) 2010 Matthieu Casanova
  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
 26//{{{ Imports
 27import java.awt.event.ActionEvent;
 28import java.awt.event.ActionListener;
 29import java.util.ArrayList;
 30import java.util.Iterator;
 31import java.util.List;
 32import javax.swing.Box;
 33import javax.swing.JButton;
 34import javax.swing.JCheckBox;
 35import javax.swing.JComboBox;
 36import javax.swing.JTextField;
 37import org.gjt.sp.jedit.AbstractOptionPane;
 38import org.gjt.sp.jedit.gui.PingPongList;
 39import org.gjt.sp.jedit.jEdit;
 40import org.gjt.sp.jedit.buffer.JEditBuffer;
 41import org.gjt.sp.util.StandardUtilities;
 42import static java.awt.GridBagConstraints.BOTH;
 43import static java.util.Arrays.sort;
 44import static javax.swing.Box.createHorizontalBox;
 45import static javax.swing.Box.createHorizontalStrut;
 46import static org.gjt.sp.jedit.jEdit.getBooleanProperty;
 47import static org.gjt.sp.jedit.jEdit.getProperty;
 48import static org.gjt.sp.jedit.jEdit.setBooleanProperty;
 49import static org.gjt.sp.jedit.jEdit.unsetProperty;
 50import static org.gjt.sp.jedit.MiscUtilities.getEncodings;
 51//}}}
 52
 53//{{{ EncodingsOptionPane class
 54/**
 55 * Encodings options.
 56 * 
 57 * @author Bj??rn Kautler
 58 * @author Matthieu Casanova
 59 * @since jEdit 4.3pre6
 60 * @version $Id: EncodingsOptionPane.java 19644 2011-06-29 01:14:09Z ezust $
 61 */
 62public class EncodingsOptionPane extends AbstractOptionPane
 63{
 64	//{{{ Instance variables
 65	private JComboBox defaultEncoding;
 66	private JCheckBox encodingAutodetect;
 67	private JTextField encodingDetectors;
 68	private JTextField fallbackEncodings;
 69	private JComboBox lineSeparator;
 70	private JButton selectAllButton;
 71	private JButton selectNoneButton;
 72	private PingPongList<String> pingPongList;
 73	//}}}
 74
 75	//{{{ EncodingsOptionPane constructor
 76	public EncodingsOptionPane()
 77	{
 78		super("encodings");
 79	} //}}}
 80
 81	//{{{ _init() method
 82	@Override
 83	protected void _init()
 84	{
 85		
 86		/* Line separator */
 87		String[] lineSeps = { jEdit.getProperty("lineSep.unix"),
 88			jEdit.getProperty("lineSep.windows"),
 89			jEdit.getProperty("lineSep.mac") };
 90		lineSeparator = new JComboBox(lineSeps);
 91		String lineSep = jEdit.getProperty("buffer."+ JEditBuffer.LINESEP,
 92			System.getProperty("line.separator"));
 93		if("\n".equals(lineSep))
 94			lineSeparator.setSelectedIndex(0);
 95		else if("\r\n".equals(lineSep))
 96			lineSeparator.setSelectedIndex(1);
 97		else if("\r".equals(lineSep))
 98			lineSeparator.setSelectedIndex(2);
 99		addComponent(jEdit.getProperty("options.general.lineSeparator"),
100			lineSeparator);
101
102		
103		// Default file encoding
104		String[] encodings = getEncodings(true);
105		sort(encodings,new StandardUtilities.StringCompare<String>(true));
106		defaultEncoding = new JComboBox(encodings);
107		defaultEncoding.setEditable(true);
108		defaultEncoding.setSelectedItem(jEdit.getProperty("buffer."+JEditBuffer.ENCODING,
109			System.getProperty("file.encoding")));
110		addComponent(jEdit.getProperty("options.general.encoding"),defaultEncoding);
111
112		// Auto detect encoding
113		encodingAutodetect = new JCheckBox(jEdit.getProperty(
114			"options.general.encodingAutodetect"));
115		encodingAutodetect.setSelected(jEdit.getBooleanProperty(
116			"buffer.encodingAutodetect"));
117		addComponent(encodingAutodetect,BOTH);
118		
119		// Encoding detectors
120		encodingDetectors = new JTextField(jEdit.getProperty(
121			"encodingDetectors",""));
122		addComponent(jEdit.getProperty("options.general.encodingDetectors"),encodingDetectors);
123
124		// Fallback Encodings
125		fallbackEncodings = new JTextField(jEdit.getProperty(
126			"fallbackEncodings",""));
127		fallbackEncodings.setToolTipText(jEdit.getProperty(
128			"options.general.fallbackEncodings.tooltip"));
129		addComponent(jEdit.getProperty("options.general.fallbackEncodings"),fallbackEncodings);
130
131		// Encodings to display
132		encodings = getEncodings(false);
133		sort(encodings,new StandardUtilities.StringCompare<String>(true));
134		List<String> availableEncodings = new ArrayList<String>();
135		List<String> selectedEncodings = new ArrayList<String>();
136		for (String encoding : encodings)
137		{
138			boolean selected = !getBooleanProperty("encoding.opt-out."+encoding,false);
139			if (selected)
140				selectedEncodings.add(encoding);
141			else
142				availableEncodings.add(encoding);
143		}
144		pingPongList = new PingPongList<String>(availableEncodings, selectedEncodings);
145		pingPongList.setLeftTitle(getProperty("options.encodings.available"));
146		pingPongList.setRightTitle(getProperty("options.encodings.selected"));
147		pingPongList.setLeftTooltip(getProperty("options.encodings.available.tooltip"));
148		pingPongList.setRightTooltip(getProperty("options.encodings.selected.tooltip"));
149		addComponent(pingPongList,BOTH);
150
151		// Select All/None Buttons
152		Box buttonsBox = createHorizontalBox();
153		buttonsBox.add(createHorizontalStrut(12));
154		
155		ActionHandler actionHandler = new ActionHandler();
156		selectAllButton = new JButton(getProperty("options.encodings.selectAll"));
157		selectAllButton.addActionListener(actionHandler);
158		selectAllButton.setEnabled(pingPongList.getLeftSize() != 0);
159		buttonsBox.add(selectAllButton);
160		buttonsBox.add(createHorizontalStrut(12));
161
162		selectNoneButton = new JButton(getProperty("options.encodings.selectNone"));
163		selectNoneButton.addActionListener(actionHandler);
164		selectNoneButton.setEnabled(pingPongList.getRightSize() != 0);
165		buttonsBox.add(selectNoneButton);
166		buttonsBox.add(createHorizontalStrut(12));
167		
168		addComponent(buttonsBox);
169	} //}}}
170
171	//{{{ _save() method
172	@Override
173	protected void _save()
174	{
175		String lineSep = null;
176		switch(lineSeparator.getSelectedIndex())
177		{
178		case 0:
179			lineSep = "\n";
180			break;
181		case 1:
182			lineSep = "\r\n";
183			break;
184		case 2:
185			lineSep = "\r";
186			break;
187		}
188		jEdit.setProperty("buffer."+ JEditBuffer.LINESEP,lineSep);
189		
190		jEdit.setProperty("buffer."+ JEditBuffer.ENCODING,(String)
191			defaultEncoding.getSelectedItem());
192		jEdit.setBooleanProperty("buffer.encodingAutodetect",
193			encodingAutodetect.isSelected());
194		jEdit.setProperty("encodingDetectors",encodingDetectors.getText());
195		jEdit.setProperty("fallbackEncodings",fallbackEncodings.getText());
196		Iterator<String> available = pingPongList.getLeftDataIterator();
197		while (available.hasNext())
198		{
199			String encoding = available.next();
200			setBooleanProperty("encoding.opt-out."+encoding,true);
201
202		}
203		Iterator<String> selected = pingPongList.getRightDataIterator();
204		while (selected.hasNext())
205		{
206			String encoding = selected.next();
207			unsetProperty("encoding.opt-out."+encoding);
208		}
209	} //}}}
210
211	//{{{ Inner classes
212
213	//{{{ ActionHandler class
214	private class ActionHandler implements ActionListener
215	{
216		public void actionPerformed(ActionEvent ae)
217		{
218			Object source = ae.getSource();
219			if (source == selectAllButton)
220			{
221				pingPongList.moveAllToRight();
222			}
223			else if (source == selectNoneButton)
224			{
225				pingPongList.moveAllToLeft();
226			}      
227		}
228	} //}}}
229	//}}}
230} //}}}