/jEdit/branches/4.3.x/org/gjt/sp/jedit/AbstractOptionPane.java

# · Java · 416 lines · 196 code · 37 blank · 183 comment · 14 complexity · 0f77c74fc446ade0b6b2526778fb16e3 MD5 · raw file

  1. /*
  2. * AbstractOptionPane.java - Abstract option pane
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1998, 1999, 2000, 2001, 2002 Slava Pestov
  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 org.gjt.sp.jedit;
  23. //{{{ Imports
  24. import javax.swing.border.EmptyBorder;
  25. import javax.swing.*;
  26. import java.awt.*;
  27. //}}}
  28. /**
  29. * The default implementation of the option pane interface.<p>
  30. *
  31. * See {@link EditPlugin} for information on how jEdit obtains and constructs
  32. * option pane instances.<p>
  33. *
  34. * Most option panes extend this implementation of {@link OptionPane}, instead
  35. * of implementing {@link OptionPane} directly. This class provides a convenient
  36. * default framework for laying out configuration options.<p>
  37. *
  38. * It is derived from Java's <code>JPanel</code> class and uses a
  39. * <code>GridBagLayout</code> object for component management. Since
  40. * <code>GridBagLayout</code> can be a bit cumbersome to use, this class
  41. * contains shortcut methods to simplify layout:
  42. *
  43. * <ul>
  44. * <li>{@link #addComponent(Component)}</li>
  45. * <li>{@link #addComponent(String,Component)}</li>
  46. * <li>{@link #addComponent(String,Component,int)}</li>
  47. * <li>{@link #addComponent(Component,Component)}</li>
  48. * <li>{@link #addComponent(Component,Component,int)}</li>
  49. * <li>{@link #addSeparator()}</li>
  50. * <li>{@link #addSeparator(String)}</li>
  51. * </ul>
  52. *
  53. * @author Slava Pestov
  54. * @author John Gellene (API documentation)
  55. * @version $Id: AbstractOptionPane.java 13806 2008-09-26 08:45:42Z kpouer $
  56. */
  57. // even though this class is called AbstractOptionPane, it is not really
  58. // abstract, since BufferOptions uses an instance of it to lay out its
  59. // components.
  60. public class AbstractOptionPane extends JPanel implements OptionPane
  61. {
  62. //{{{ AbstractOptionPane constructor
  63. /**
  64. * Creates a new option pane.
  65. * @param internalName The internal name. The option pane's label is set to the
  66. * value of the property named <code>options.<i>name</i>.label</code>.
  67. */
  68. public AbstractOptionPane(String internalName)
  69. {
  70. this.name = internalName;
  71. setLayout(gridBag = new GridBagLayout());
  72. } //}}}
  73. //{{{ getName() method
  74. /**
  75. * Returns the internal name of this option pane. The option pane's label
  76. * is set to the value of the property named
  77. * <code>options.<i>name</i>.label</code>.
  78. */
  79. @Override
  80. public String getName()
  81. {
  82. return name;
  83. } //}}}
  84. //{{{ getComponent() method
  85. /**
  86. * Returns the component that should be displayed for this option pane.
  87. * Because this class extends Component, it simply returns "this".
  88. */
  89. public Component getComponent()
  90. {
  91. return this;
  92. } //}}}
  93. //{{{ init() method
  94. /**
  95. * Do not override this method, override {@link #_init()} instead.
  96. */
  97. // final in 4.2
  98. public void init()
  99. {
  100. if(!initialized)
  101. {
  102. initialized = true;
  103. _init();
  104. }
  105. } //}}}
  106. //{{{ save() method
  107. /**
  108. * Do not override this method, override {@link #_save()} instead.
  109. */
  110. // final in 4.2
  111. public void save()
  112. {
  113. if(initialized)
  114. _save();
  115. } //}}}
  116. //{{{ newLabel()
  117. /**
  118. * @return a label which has the same tooltiptext as the Component
  119. * that it is a label for. This is used to create labels from inside
  120. * AbstractOptionPane.
  121. * @since jEdit 4.3pre4
  122. */
  123. public JLabel newLabel(String label, Component comp)
  124. {
  125. JLabel retval = new JLabel(label);
  126. try /* to get the tooltip of the component */
  127. {
  128. JComponent jc = (JComponent) comp;
  129. String tttext = jc.getToolTipText();
  130. retval.setToolTipText(tttext);
  131. }
  132. catch (Exception e)
  133. {
  134. /* There probably wasn't a tooltip,
  135. * or it wasn't a JComponent.
  136. We don't care. */
  137. }
  138. return retval;
  139. }// }}}
  140. //{{{ addComponent() method
  141. /**
  142. * Adds a labeled component to the option pane. Components are
  143. * added in a vertical fashion, one per row. The label is
  144. * displayed to the left of the component.
  145. * @param label The label
  146. * @param comp The component
  147. */
  148. public void addComponent(String label, Component comp)
  149. {
  150. JLabel l = newLabel(label, comp);
  151. l.setBorder(new EmptyBorder(0,0,0,12));
  152. addComponent(l,comp,GridBagConstraints.BOTH);
  153. } //}}}
  154. //{{{ addComponent() method
  155. /**
  156. * Adds a labeled component to the option pane. Components are
  157. * added in a vertical fashion, one per row. The label is
  158. * displayed to the left of the component.
  159. * @param label The label
  160. * @param comp The component
  161. * @param fill Fill parameter to GridBagConstraints for the right
  162. * component
  163. */
  164. public void addComponent(String label, Component comp, int fill)
  165. {
  166. JLabel l = newLabel(label, comp);
  167. l.setBorder(new EmptyBorder(0,0,0,12));
  168. addComponent(l,comp,fill);
  169. } //}}}
  170. //{{{ addComponent() method
  171. /**
  172. * Adds a labeled component to the option pane. Components are
  173. * added in a vertical fashion, one per row. The label is
  174. * displayed to the left of the component.
  175. * @param comp1 The label
  176. * @param comp2 The component
  177. *
  178. * @since jEdit 4.1pre3
  179. */
  180. public void addComponent(Component comp1, Component comp2)
  181. {
  182. addComponent(comp1,comp2,GridBagConstraints.BOTH);
  183. } //}}}
  184. //{{{ addComponent() method
  185. /**
  186. * Adds a labeled component to the option pane. Components are
  187. * added in a vertical fashion, one per row. The label is
  188. * displayed to the left of the component.
  189. * @param comp1 The label
  190. * @param comp2 The component
  191. * @param fill Fill parameter to GridBagConstraints for the right
  192. * component
  193. *
  194. * @since jEdit 4.1pre3
  195. */
  196. public void addComponent(Component comp1, Component comp2, int fill)
  197. {
  198. copyToolTips(comp1, comp2);
  199. GridBagConstraints cons = new GridBagConstraints();
  200. cons.gridy = y++;
  201. cons.gridheight = 1;
  202. cons.gridwidth = 1;
  203. cons.weightx = 0.0f;
  204. cons.insets = new Insets(1,0,1,0);
  205. cons.fill = GridBagConstraints.BOTH;
  206. gridBag.setConstraints(comp1,cons);
  207. add(comp1);
  208. cons.fill = fill;
  209. cons.gridx = 1;
  210. cons.weightx = 1.0f;
  211. gridBag.setConstraints(comp2,cons);
  212. add(comp2);
  213. } //}}}
  214. //{{{ addComponent() method
  215. /**
  216. * Adds a component to the option pane. Components are
  217. * added in a vertical fashion, one per row.
  218. * @param comp The component
  219. */
  220. public void addComponent(Component comp)
  221. {
  222. GridBagConstraints cons = new GridBagConstraints();
  223. cons.gridy = y++;
  224. cons.gridheight = 1;
  225. cons.gridwidth = GridBagConstraints.REMAINDER;
  226. cons.fill = GridBagConstraints.NONE;
  227. cons.anchor = GridBagConstraints.WEST;
  228. cons.weightx = 1.0f;
  229. cons.insets = new Insets(1,0,1,0);
  230. gridBag.setConstraints(comp,cons);
  231. add(comp);
  232. } //}}}
  233. //{{{ addComponent() method
  234. /**
  235. * Adds a component to the option pane. Components are
  236. * added in a vertical fashion, one per row.
  237. * @param comp The component
  238. * @param fill Fill parameter to GridBagConstraints
  239. * @since jEdit 4.2pre2
  240. */
  241. public void addComponent(Component comp, int fill)
  242. {
  243. GridBagConstraints cons = new GridBagConstraints();
  244. cons.gridy = y++;
  245. cons.gridheight = 1;
  246. cons.gridwidth = GridBagConstraints.REMAINDER;
  247. cons.fill = fill;
  248. cons.anchor = GridBagConstraints.WEST;
  249. cons.weightx = 1.0f;
  250. cons.insets = new Insets(1,0,1,0);
  251. gridBag.setConstraints(comp,cons);
  252. add(comp);
  253. } //}}}
  254. //{{{ copyToolTips() method
  255. private static void copyToolTips(Component c1, Component c2)
  256. {
  257. int tooltips = 0;
  258. int jc = 0;
  259. String text = null;
  260. JComponent jc1 = null;
  261. try
  262. {
  263. jc1 = (JComponent) c1;
  264. text = jc1.getToolTipText();
  265. ++jc;
  266. if (text != null && text.length() > 0)
  267. tooltips++;
  268. }
  269. catch (Exception e)
  270. {
  271. }
  272. JComponent jc2 = null;
  273. try
  274. {
  275. jc2 = (JComponent) c2;
  276. String text2 = jc2.getToolTipText();
  277. ++jc;
  278. if (text2 != null && text2.length() > 0)
  279. {
  280. text = text2;
  281. tooltips++;
  282. }
  283. }
  284. catch (Exception e)
  285. {
  286. }
  287. if (tooltips == 1 && jc == 2)
  288. {
  289. jc1.setToolTipText(text);
  290. jc2.setToolTipText(text);
  291. }
  292. } //}}}
  293. //{{{ addSeparator() method
  294. /**
  295. * Adds a separator component.
  296. * @since jEdit 4.1pre7
  297. */
  298. public void addSeparator()
  299. {
  300. addComponent(Box.createVerticalStrut(6));
  301. JSeparator sep = new JSeparator(SwingConstants.HORIZONTAL);
  302. GridBagConstraints cons = new GridBagConstraints();
  303. cons.gridy = y++;
  304. cons.gridheight = 1;
  305. cons.gridwidth = GridBagConstraints.REMAINDER;
  306. cons.fill = GridBagConstraints.BOTH;
  307. cons.anchor = GridBagConstraints.WEST;
  308. cons.weightx = 1.0f;
  309. //cons.insets = new Insets(1,0,1,0);
  310. gridBag.setConstraints(sep,cons);
  311. add(sep);
  312. addComponent(Box.createVerticalStrut(6));
  313. } //}}}
  314. //{{{ addSeparator() method
  315. /**
  316. * Adds a separator component.
  317. * @param label The separator label property
  318. * @since jEdit 2.6pre2
  319. */
  320. public void addSeparator(String label)
  321. {
  322. if(y != 0)
  323. addComponent(Box.createVerticalStrut(6));
  324. Box box = new Box(BoxLayout.X_AXIS);
  325. Box box2 = new Box(BoxLayout.Y_AXIS);
  326. box2.add(Box.createGlue());
  327. box2.add(new JSeparator(SwingConstants.HORIZONTAL));
  328. box2.add(Box.createGlue());
  329. box.add(box2);
  330. JLabel l = new JLabel(jEdit.getProperty(label));
  331. l.setMaximumSize(l.getPreferredSize());
  332. box.add(l);
  333. Box box3 = new Box(BoxLayout.Y_AXIS);
  334. box3.add(Box.createGlue());
  335. box3.add(new JSeparator(SwingConstants.HORIZONTAL));
  336. box3.add(Box.createGlue());
  337. box.add(box3);
  338. GridBagConstraints cons = new GridBagConstraints();
  339. cons.gridy = y++;
  340. cons.gridheight = 1;
  341. cons.gridwidth = GridBagConstraints.REMAINDER;
  342. cons.fill = GridBagConstraints.BOTH;
  343. cons.anchor = GridBagConstraints.WEST;
  344. cons.weightx = 1.0f;
  345. cons.insets = new Insets(1,0,1,0);
  346. gridBag.setConstraints(box,cons);
  347. add(box);
  348. } //}}}
  349. //{{{ Protected members
  350. /**
  351. * Has the option pane been initialized?
  352. */
  353. protected boolean initialized;
  354. /**
  355. * The layout manager.
  356. */
  357. protected GridBagLayout gridBag;
  358. /**
  359. * The number of components already added to the layout manager.
  360. */
  361. protected int y;
  362. /**
  363. * This method should create and arrange the components of the option pane
  364. * and initialize the option data displayed to the user. This method
  365. * is called when the option pane is first displayed, and is not
  366. * called again for the lifetime of the object.
  367. */
  368. protected void _init() {}
  369. /**
  370. * Called when the options dialog's "ok" button is clicked.
  371. * This should save any properties being edited in this option
  372. * pane.
  373. */
  374. protected void _save() {}
  375. //}}}
  376. //{{{ Private members
  377. private String name;
  378. //}}}
  379. }