/plugins/CharacterMap/trunk/charactermap/CharacterMapOptionPane.java

# · Java · 343 lines · 220 code · 28 blank · 95 comment · 2 complexity · 65b761b3fe948376f58e75a5f5cbc2f9 MD5 · raw file

  1. /*
  2. * CharacterMapOptionPane.java
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * Copyright (C) 2003 Mark Wickens
  6. * Copyright (C) 2012 Max Funk
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package charactermap;
  23. //{{{ Imports
  24. import java.awt.GridBagConstraints;
  25. import java.awt.event.ActionEvent;
  26. import java.awt.event.ActionListener;
  27. import java.util.ArrayList;
  28. import javax.swing.*;
  29. import org.gjt.sp.jedit.AbstractOptionPane;
  30. import org.gjt.sp.jedit.EditAction;
  31. import org.gjt.sp.jedit.PluginJAR;
  32. import org.gjt.sp.jedit.gui.ColorWellButton;
  33. import org.gjt.sp.jedit.gui.DockableWindowManager;
  34. import org.gjt.sp.jedit.jEdit;
  35. //import org.gjt.sp.util.Log;
  36. //}}}
  37. /**
  38. * Options pane displayed when Character Map
  39. * is selected in the Plugin Options... tree
  40. * Allows the user to customize the appearence of the
  41. * character map plugin.
  42. */
  43. public class CharacterMapOptionPane extends AbstractOptionPane
  44. {
  45. //{{{ Private Variables
  46. /** Shortcut to CharacterMapPlugin.NAME_PREFIX */
  47. private final String NAME_PREFIX = CharacterMapPlugin.NAME_PREFIX;
  48. /** Shortcut to CharacterMapPlugin.OPTION_PREFIX */
  49. private final String OPTION_PREFIX = CharacterMapPlugin.OPTION_PREFIX;
  50. /** Checkbox controlling display of encoding combo box */
  51. private JCheckBox encoding;
  52. /** Checkbox controlling display of unicode blocks slider */
  53. private JCheckBox blocks;
  54. /** Checkbox to controlling alphabetic order of unicode blocks */
  55. private JCheckBox blocksABC;
  56. /** Checkbox to enable higher Unicode planes (Characters above 0xFFFF) */
  57. private JCheckBox higherPlanes;
  58. /** Checkbox controlling anti-aliasing */
  59. private JCheckBox antiAlias;
  60. /** Checkbox controlling display of status line */
  61. private JCheckBox status;
  62. /** Spinner controlling number of columns displayed in floating table */
  63. private JSpinner columnsSpinner;
  64. /** Spinner controlling number of columns displayed in left/right docked table */
  65. private JSpinner columnsSpinnerDockLR;
  66. /** Spinner controlling number of columns displayed in top/bottom docked table */
  67. private JSpinner columnsSpinnerDockTB;
  68. /** Label for spinner options (floating) */
  69. private JLabel columnsSpinnerLabel;
  70. /** Label for spinner options (docked left/right) */
  71. private JLabel columnsSpinnerDockLRLabel;
  72. /** Label for spinner options (docked top/bottom) */
  73. private JLabel columnsSpinnerDockTBLabel;
  74. /** List for spinner with possible numbers of columns */
  75. private ArrayList<Integer> spinnerValues;
  76. /** Model for spinner options (floating) */
  77. private SpinnerModel spinnerModel;
  78. /** Model for spinner options (docked left/right) */
  79. private SpinnerModel spinnerModelDockLR;
  80. /** Model for spinner options (docked top/bottom) */
  81. private SpinnerModel spinnerModelDockTB;
  82. /** Selector for normal character color */
  83. private ColorWellButton normalColor;
  84. /** Selector for control character color */
  85. private ColorWellButton controlColor;
  86. /** Selector for private use character color */
  87. private ColorWellButton privateColor;
  88. /** Selector for unassigned character color */
  89. private ColorWellButton unassignedColor;
  90. /** Selector for invalid character color */
  91. private ColorWellButton invalidColor;
  92. /** Checkbox controlling display of large character */
  93. private JCheckBox showLarge;
  94. /** Textfield containing size of large character in points */
  95. private JTextField largeSize;
  96. /** Label for textfield containing large character size */
  97. private JLabel largeSizeLabel;
  98. /** Checkbox controlling display of super-size character */
  99. private JCheckBox showSuper;
  100. /** Textfield containing size of super character in points */
  101. private JTextField superSize;
  102. /** Label for textfield containing super character size */
  103. private JLabel superSizeLabel;
  104. //}}}
  105. //{{{ Constructor
  106. /**
  107. * Default constructor.
  108. */
  109. public CharacterMapOptionPane()
  110. {
  111. super(CharacterMapPlugin.NAME);
  112. }
  113. //}}}
  114. //{{{ _init() method
  115. /**
  116. * Create and initialise the options page with options
  117. * and labels read from the properties for this plugin
  118. */
  119. @Override
  120. public void _init()
  121. {
  122. //Initialise general option components
  123. encoding = new JCheckBox(jEdit.getProperty(OPTION_PREFIX + "encoding.label"),
  124. jEdit.getBooleanProperty(OPTION_PREFIX + "encoding"));
  125. blocks = new JCheckBox(jEdit.getProperty(OPTION_PREFIX + "blocks.label"),
  126. jEdit.getBooleanProperty(OPTION_PREFIX + "blocks"));
  127. blocksABC = new JCheckBox(jEdit.getProperty(OPTION_PREFIX + "blocks-abc.label"),
  128. jEdit.getBooleanProperty(OPTION_PREFIX + "blocks-abc"));
  129. higherPlanes = new JCheckBox(jEdit.getProperty(OPTION_PREFIX + "higher-planes.label"),
  130. jEdit.getBooleanProperty(OPTION_PREFIX + "higher-planes"));
  131. antiAlias = new JCheckBox(jEdit.getProperty(OPTION_PREFIX + "anti-alias.label"),
  132. jEdit.getBooleanProperty(OPTION_PREFIX + "anti-alias"));
  133. status = new JCheckBox(jEdit.getProperty(OPTION_PREFIX + "status.label"),
  134. jEdit.getBooleanProperty(OPTION_PREFIX + "status"));
  135. //Initialise table option components
  136. spinnerValues = new ArrayList<Integer>();
  137. for (int i = 8; i >= 0; i -= 1) {
  138. spinnerValues.add(new Integer(1 << i));
  139. }
  140. columnsSpinnerLabel = new JLabel(jEdit.getProperty(OPTION_PREFIX + "columns.label"));
  141. spinnerModel = new SpinnerListModel(spinnerValues);
  142. columnsSpinner = new JSpinner(spinnerModel);
  143. JSpinner.DefaultEditor spinnerEditor = (JSpinner.DefaultEditor) columnsSpinner.getEditor();
  144. JTextField columnsTextField = spinnerEditor.getTextField();
  145. columnsTextField.setColumns(3);
  146. columnsTextField.setHorizontalAlignment(JTextField.LEFT);
  147. int column = jEdit.getIntegerProperty(OPTION_PREFIX + "columns");
  148. columnsSpinner.setValue(new Integer(column));
  149. columnsSpinnerDockLRLabel = new JLabel(jEdit.getProperty(OPTION_PREFIX + "columns-dock-lr.label"));
  150. spinnerModelDockLR = new SpinnerListModel(spinnerValues);
  151. columnsSpinnerDockLR = new JSpinner(spinnerModelDockLR);
  152. spinnerEditor = (JSpinner.DefaultEditor) columnsSpinnerDockLR.getEditor();
  153. columnsTextField = spinnerEditor.getTextField();
  154. columnsTextField.setColumns(3);
  155. columnsTextField.setHorizontalAlignment(JTextField.LEFT);
  156. column = jEdit.getIntegerProperty(OPTION_PREFIX + "columns-dock-lr");
  157. columnsSpinnerDockLR.setValue(new Integer(column));
  158. columnsSpinnerDockTBLabel = new JLabel(jEdit.getProperty(OPTION_PREFIX + "columns-dock-tb.label"));
  159. spinnerModelDockTB = new SpinnerListModel(spinnerValues);
  160. columnsSpinnerDockTB = new JSpinner(spinnerModelDockTB);
  161. spinnerEditor = (JSpinner.DefaultEditor) columnsSpinnerDockTB.getEditor();
  162. columnsTextField = spinnerEditor.getTextField();
  163. columnsTextField.setColumns(3);
  164. columnsTextField.setHorizontalAlignment(JTextField.LEFT);
  165. column = jEdit.getIntegerProperty(OPTION_PREFIX + "columns-dock-tb");
  166. columnsSpinnerDockTB.setValue(new Integer(column));
  167. //Initialise color option components
  168. normalColor = new ColorWellButton(
  169. jEdit.getColorProperty(OPTION_PREFIX + "color-normal"));
  170. controlColor = new ColorWellButton(
  171. jEdit.getColorProperty(OPTION_PREFIX + "color-control"));
  172. privateColor = new ColorWellButton(
  173. jEdit.getColorProperty(OPTION_PREFIX + "color-private"));
  174. unassignedColor = new ColorWellButton(
  175. jEdit.getColorProperty(OPTION_PREFIX + "color-unassigned"));
  176. invalidColor = new ColorWellButton(
  177. jEdit.getColorProperty(OPTION_PREFIX + "color-invalid"));
  178. //Initialise character option components
  179. showLarge = new JCheckBox(jEdit.getProperty(OPTION_PREFIX + "large.label"),
  180. jEdit.getBooleanProperty(OPTION_PREFIX + "large"));
  181. largeSizeLabel = new JLabel(jEdit.getProperty(OPTION_PREFIX + "large-size.label"));
  182. largeSize = new JTextField(jEdit.getProperty(OPTION_PREFIX + "large-size"));
  183. largeSize.setEnabled(showLarge.isSelected());
  184. largeSizeLabel.setEnabled(showLarge.isSelected());
  185. showSuper = new JCheckBox(jEdit.getProperty(OPTION_PREFIX + "super.label"),
  186. jEdit.getBooleanProperty(OPTION_PREFIX + "super"));
  187. superSizeLabel = new JLabel(jEdit.getProperty(OPTION_PREFIX + "super-size.label"));
  188. superSizeLabel.setEnabled(showSuper.isSelected());
  189. superSize = new JTextField(jEdit.getProperty(OPTION_PREFIX + "super-size"));
  190. superSize.setEnabled(showSuper.isSelected());
  191. showLarge.addActionListener(
  192. new ActionListener()
  193. {
  194. @Override
  195. public void actionPerformed(ActionEvent evt)
  196. {
  197. boolean selected = showLarge.isSelected();
  198. largeSizeLabel.setEnabled(selected);
  199. largeSize.setEnabled(selected);
  200. }
  201. });
  202. showSuper.addActionListener(
  203. new ActionListener()
  204. {
  205. @Override
  206. public void actionPerformed(ActionEvent evt)
  207. {
  208. boolean selected = showSuper.isSelected();
  209. superSizeLabel.setEnabled(selected);
  210. superSize.setEnabled(selected);
  211. }
  212. });
  213. //Display all components
  214. addSeparator(OPTION_PREFIX + "separator-general.label");
  215. addComponent(encoding);
  216. addComponent(blocks);
  217. addComponent(blocksABC);
  218. addComponent(higherPlanes);
  219. addComponent(antiAlias);
  220. addComponent(status);
  221. addSeparator(OPTION_PREFIX + "separator-table.label");
  222. addComponent(columnsSpinnerLabel, columnsSpinner);
  223. addComponent(columnsSpinnerDockLRLabel, columnsSpinnerDockLR);
  224. addComponent(columnsSpinnerDockTBLabel, columnsSpinnerDockTB);
  225. addSeparator(OPTION_PREFIX + "separator-color.label");
  226. addComponent(jEdit.getProperty(OPTION_PREFIX + "color-normal.label"),
  227. normalColor, GridBagConstraints.VERTICAL);
  228. addComponent(jEdit.getProperty(OPTION_PREFIX + "color-control.label"),
  229. controlColor, GridBagConstraints.VERTICAL);
  230. addComponent(jEdit.getProperty(OPTION_PREFIX + "color-private.label"),
  231. privateColor, GridBagConstraints.VERTICAL);
  232. addComponent(jEdit.getProperty(OPTION_PREFIX + "color-unassigned.label"),
  233. unassignedColor, GridBagConstraints.VERTICAL);
  234. addComponent(jEdit.getProperty(OPTION_PREFIX + "color-invalid.label"),
  235. invalidColor, GridBagConstraints.VERTICAL);
  236. addSeparator(OPTION_PREFIX + "separator-chars.label");
  237. addComponent(showLarge);
  238. addComponent(largeSizeLabel, largeSize);
  239. addComponent(showSuper);
  240. addComponent(superSizeLabel, superSize);
  241. }
  242. //}}}
  243. //{{{ _save() method
  244. /**
  245. * Store the options selected on the pane back to the
  246. * jedit properties.
  247. */
  248. @Override
  249. public void _save()
  250. {
  251. // Save options
  252. jEdit.setBooleanProperty(OPTION_PREFIX + "encoding", encoding.isSelected());
  253. jEdit.setBooleanProperty(OPTION_PREFIX + "blocks", blocks.isSelected());
  254. jEdit.setBooleanProperty(OPTION_PREFIX + "blocks-abc", blocksABC.isSelected());
  255. jEdit.setBooleanProperty(OPTION_PREFIX + "higher-planes", higherPlanes.isSelected());
  256. jEdit.setBooleanProperty(OPTION_PREFIX + "anti-alias", antiAlias.isSelected());
  257. jEdit.setBooleanProperty(OPTION_PREFIX + "status", status.isSelected());
  258. int column = ((Integer) columnsSpinner.getValue()).intValue();
  259. jEdit.setIntegerProperty(OPTION_PREFIX + "columns", column);
  260. column = ((Integer) columnsSpinnerDockLR.getValue()).intValue();
  261. jEdit.setIntegerProperty(OPTION_PREFIX + "columns-dock-lr", column);
  262. column = ((Integer) columnsSpinnerDockTB.getValue()).intValue();
  263. jEdit.setIntegerProperty(OPTION_PREFIX + "columns-dock-tb", column);
  264. jEdit.setColorProperty(OPTION_PREFIX + "color-normal",
  265. normalColor.getSelectedColor());
  266. jEdit.setColorProperty(OPTION_PREFIX + "color-control",
  267. controlColor.getSelectedColor());
  268. jEdit.setColorProperty(OPTION_PREFIX + "color-private",
  269. privateColor.getSelectedColor());
  270. jEdit.setColorProperty(OPTION_PREFIX + "color-unassigned",
  271. unassignedColor.getSelectedColor());
  272. jEdit.setColorProperty(OPTION_PREFIX + "color-invalid",
  273. invalidColor.getSelectedColor());
  274. jEdit.setBooleanProperty(OPTION_PREFIX + "large", showLarge.isSelected());
  275. jEdit.setBooleanProperty(OPTION_PREFIX + "super", showSuper.isSelected());
  276. setIntegerPropertyFromTextField(OPTION_PREFIX + "large-size", largeSize, 36);
  277. setIntegerPropertyFromTextField(OPTION_PREFIX + "super-size", superSize, 128);
  278. // Reload CharacterMap.jar
  279. PluginJAR jar = jEdit.getPlugin("charactermap.CharacterMapPlugin").getPluginJAR();
  280. jEdit.removePluginJAR(jar,false);
  281. jEdit.addPluginJAR(jar.getPath());
  282. boolean isFloating = jEdit.getProperty(NAME_PREFIX + "dock-position",
  283. DockableWindowManager.FLOATING).equalsIgnoreCase(DockableWindowManager.FLOATING);
  284. if (!isFloating)
  285. {
  286. EditAction act = jEdit.getAction(CharacterMapPlugin.NAME);
  287. act.invoke(jEdit.getActiveView());
  288. }
  289. }
  290. //}}}
  291. //{{{ Auxiliary function
  292. /**
  293. * Determine the value of the given text field and store in the
  294. * named jedit property. If the valued does not parse as an integer,
  295. * use the specified default instead.
  296. * @param property The name of the jedit property to receive the value
  297. * @param tf Text field containing the string to be parsed as an integer
  298. * @param defaultValue If tf does not contain an integer, use this value instead
  299. */
  300. private void setIntegerPropertyFromTextField(String property, JTextField tf, int defaultValue)
  301. {
  302. int value = defaultValue;
  303. String sValue = tf.getText();
  304. try {
  305. value = Integer.parseInt(sValue);
  306. }
  307. catch (NumberFormatException nfe) {
  308. value = jEdit.getIntegerProperty(property, defaultValue);
  309. }
  310. finally {
  311. jEdit.setIntegerProperty(property, value);
  312. }
  313. }
  314. //}}}
  315. }