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