/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/AbstractOptionPane.java

# · Java · 346 lines · 142 code · 32 blank · 172 comment · 4 complexity · 477b06a49b22890e7f34db45048f2422 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 5004 2004-03-28 00:07:27Z spestov $
  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 name 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 name)
  69. {
  70. this.name = name;
  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. public String getName()
  80. {
  81. return name;
  82. } //}}}
  83. //{{{ getComponent() method
  84. /**
  85. * Returns the component that should be displayed for this option pane.
  86. * Because this class extends Component, it simply returns "this".
  87. */
  88. public Component getComponent()
  89. {
  90. return this;
  91. } //}}}
  92. //{{{ init() method
  93. /**
  94. * Do not override this method, override {@link #_init()} instead.
  95. */
  96. // final in 4.2
  97. public void init()
  98. {
  99. if(!initialized)
  100. {
  101. initialized = true;
  102. _init();
  103. }
  104. } //}}}
  105. //{{{ save() method
  106. /**
  107. * Do not override this method, override {@link #_save()} instead.
  108. */
  109. // final in 4.2
  110. public void save()
  111. {
  112. if(initialized)
  113. _save();
  114. } //}}}
  115. //{{{ addComponent() method
  116. /**
  117. * Adds a labeled component to the option pane. Components are
  118. * added in a vertical fashion, one per row. The label is
  119. * displayed to the left of the component.
  120. * @param label The label
  121. * @param comp The component
  122. */
  123. public void addComponent(String label, Component comp)
  124. {
  125. JLabel l = new JLabel(label);
  126. l.setBorder(new EmptyBorder(0,0,0,12));
  127. addComponent(l,comp,GridBagConstraints.BOTH);
  128. } //}}}
  129. //{{{ addComponent() method
  130. /**
  131. * Adds a labeled component to the option pane. Components are
  132. * added in a vertical fashion, one per row. The label is
  133. * displayed to the left of the component.
  134. * @param label The label
  135. * @param comp The component
  136. * @param fill Fill parameter to GridBagConstraints for the right
  137. * component
  138. */
  139. public void addComponent(String label, Component comp, int fill)
  140. {
  141. JLabel l = new JLabel(label);
  142. l.setBorder(new EmptyBorder(0,0,0,12));
  143. addComponent(l,comp,fill);
  144. } //}}}
  145. //{{{ addComponent() method
  146. /**
  147. * Adds a labeled component to the option pane. Components are
  148. * added in a vertical fashion, one per row. The label is
  149. * displayed to the left of the component.
  150. * @param comp1 The label
  151. * @param comp2 The component
  152. *
  153. * @since jEdit 4.1pre3
  154. */
  155. public void addComponent(Component comp1, Component comp2)
  156. {
  157. addComponent(comp1,comp2,GridBagConstraints.BOTH);
  158. } //}}}
  159. //{{{ addComponent() method
  160. /**
  161. * Adds a labeled component to the option pane. Components are
  162. * added in a vertical fashion, one per row. The label is
  163. * displayed to the left of the component.
  164. * @param comp1 The label
  165. * @param comp2 The component
  166. * @param fill Fill parameter to GridBagConstraints for the right
  167. * component
  168. *
  169. * @since jEdit 4.1pre3
  170. */
  171. public void addComponent(Component comp1, Component comp2, int fill)
  172. {
  173. GridBagConstraints cons = new GridBagConstraints();
  174. cons.gridy = y++;
  175. cons.gridheight = 1;
  176. cons.gridwidth = 1;
  177. cons.weightx = 0.0f;
  178. cons.insets = new Insets(1,0,1,0);
  179. cons.fill = GridBagConstraints.BOTH;
  180. gridBag.setConstraints(comp1,cons);
  181. add(comp1);
  182. cons.fill = fill;
  183. cons.gridx = 1;
  184. cons.weightx = 1.0f;
  185. gridBag.setConstraints(comp2,cons);
  186. add(comp2);
  187. } //}}}
  188. //{{{ addComponent() method
  189. /**
  190. * Adds a component to the option pane. Components are
  191. * added in a vertical fashion, one per row.
  192. * @param comp The component
  193. */
  194. public void addComponent(Component comp)
  195. {
  196. GridBagConstraints cons = new GridBagConstraints();
  197. cons.gridy = y++;
  198. cons.gridheight = 1;
  199. cons.gridwidth = cons.REMAINDER;
  200. cons.fill = GridBagConstraints.NONE;
  201. cons.anchor = GridBagConstraints.WEST;
  202. cons.weightx = 1.0f;
  203. cons.insets = new Insets(1,0,1,0);
  204. gridBag.setConstraints(comp,cons);
  205. add(comp);
  206. } //}}}
  207. //{{{ addComponent() method
  208. /**
  209. * Adds a component to the option pane. Components are
  210. * added in a vertical fashion, one per row.
  211. * @param comp The component
  212. * @param fill Fill parameter to GridBagConstraints
  213. * @since jEdit 4.2pre2
  214. */
  215. public void addComponent(Component comp, int fill)
  216. {
  217. GridBagConstraints cons = new GridBagConstraints();
  218. cons.gridy = y++;
  219. cons.gridheight = 1;
  220. cons.gridwidth = cons.REMAINDER;
  221. cons.fill = fill;
  222. cons.anchor = GridBagConstraints.WEST;
  223. cons.weightx = 1.0f;
  224. cons.insets = new Insets(1,0,1,0);
  225. gridBag.setConstraints(comp,cons);
  226. add(comp);
  227. } //}}}
  228. //{{{ addSeparator() method
  229. /**
  230. * Adds a separator component.
  231. * @since jEdit 4.1pre7
  232. */
  233. public void addSeparator()
  234. {
  235. addComponent(Box.createVerticalStrut(6));
  236. JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
  237. GridBagConstraints cons = new GridBagConstraints();
  238. cons.gridy = y++;
  239. cons.gridheight = 1;
  240. cons.gridwidth = cons.REMAINDER;
  241. cons.fill = GridBagConstraints.BOTH;
  242. cons.anchor = GridBagConstraints.WEST;
  243. cons.weightx = 1.0f;
  244. //cons.insets = new Insets(1,0,1,0);
  245. gridBag.setConstraints(sep,cons);
  246. add(sep);
  247. addComponent(Box.createVerticalStrut(6));
  248. } //}}}
  249. //{{{ addSeparator() method
  250. /**
  251. * Adds a separator component.
  252. * @param label The separator label property
  253. * @since jEdit 2.6pre2
  254. */
  255. public void addSeparator(String label)
  256. {
  257. if(y != 0)
  258. addComponent(Box.createVerticalStrut(6));
  259. Box box = new Box(BoxLayout.X_AXIS);
  260. Box box2 = new Box(BoxLayout.Y_AXIS);
  261. box2.add(Box.createGlue());
  262. box2.add(new JSeparator(JSeparator.HORIZONTAL));
  263. box2.add(Box.createGlue());
  264. box.add(box2);
  265. JLabel l = new JLabel(jEdit.getProperty(label));
  266. l.setMaximumSize(l.getPreferredSize());
  267. box.add(l);
  268. Box box3 = new Box(BoxLayout.Y_AXIS);
  269. box3.add(Box.createGlue());
  270. box3.add(new JSeparator(JSeparator.HORIZONTAL));
  271. box3.add(Box.createGlue());
  272. box.add(box3);
  273. GridBagConstraints cons = new GridBagConstraints();
  274. cons.gridy = y++;
  275. cons.gridheight = 1;
  276. cons.gridwidth = cons.REMAINDER;
  277. cons.fill = GridBagConstraints.BOTH;
  278. cons.anchor = GridBagConstraints.WEST;
  279. cons.weightx = 1.0f;
  280. cons.insets = new Insets(1,0,1,0);
  281. gridBag.setConstraints(box,cons);
  282. add(box);
  283. } //}}}
  284. //{{{ Protected members
  285. /**
  286. * Has the option pane been initialized?
  287. */
  288. protected boolean initialized;
  289. /**
  290. * The layout manager.
  291. */
  292. protected GridBagLayout gridBag;
  293. /**
  294. * The number of components already added to the layout manager.
  295. */
  296. protected int y;
  297. /**
  298. * This method should create and arrange the components of the option pane
  299. * and initialize the option data displayed to the user. This method
  300. * is called when the option pane is first displayed, and is not
  301. * called again for the lifetime of the object.
  302. */
  303. protected void _init() {}
  304. /**
  305. * Called when the options dialog's "ok" button is clicked.
  306. * This should save any properties being edited in this option
  307. * pane.
  308. */
  309. protected void _save() {}
  310. //}}}
  311. //{{{ Private members
  312. private String name;
  313. //}}}
  314. }