PageRenderTime 59ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 347 lines | 142 code | 32 blank | 173 comment | 4 complexity | 8c1804becf752c94ec015dc4c2495833 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. * 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 4720 2003-05-22 23:43:16Z 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. * @param label The separator label property
  232. * @since jEdit 4.1pre7
  233. */
  234. public void addSeparator()
  235. {
  236. addComponent(Box.createVerticalStrut(6));
  237. JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
  238. GridBagConstraints cons = new GridBagConstraints();
  239. cons.gridy = y++;
  240. cons.gridheight = 1;
  241. cons.gridwidth = cons.REMAINDER;
  242. cons.fill = GridBagConstraints.BOTH;
  243. cons.anchor = GridBagConstraints.WEST;
  244. cons.weightx = 1.0f;
  245. //cons.insets = new Insets(1,0,1,0);
  246. gridBag.setConstraints(sep,cons);
  247. add(sep);
  248. addComponent(Box.createVerticalStrut(6));
  249. } //}}}
  250. //{{{ addSeparator() method
  251. /**
  252. * Adds a separator component.
  253. * @param label The separator label property
  254. * @since jEdit 2.6pre2
  255. */
  256. public void addSeparator(String label)
  257. {
  258. if(y != 0)
  259. addComponent(Box.createVerticalStrut(6));
  260. Box box = new Box(BoxLayout.X_AXIS);
  261. Box box2 = new Box(BoxLayout.Y_AXIS);
  262. box2.add(Box.createGlue());
  263. box2.add(new JSeparator(JSeparator.HORIZONTAL));
  264. box2.add(Box.createGlue());
  265. box.add(box2);
  266. JLabel l = new JLabel(jEdit.getProperty(label));
  267. l.setMaximumSize(l.getPreferredSize());
  268. box.add(l);
  269. Box box3 = new Box(BoxLayout.Y_AXIS);
  270. box3.add(Box.createGlue());
  271. box3.add(new JSeparator(JSeparator.HORIZONTAL));
  272. box3.add(Box.createGlue());
  273. box.add(box3);
  274. GridBagConstraints cons = new GridBagConstraints();
  275. cons.gridy = y++;
  276. cons.gridheight = 1;
  277. cons.gridwidth = cons.REMAINDER;
  278. cons.fill = GridBagConstraints.BOTH;
  279. cons.anchor = GridBagConstraints.WEST;
  280. cons.weightx = 1.0f;
  281. cons.insets = new Insets(1,0,1,0);
  282. gridBag.setConstraints(box,cons);
  283. add(box);
  284. } //}}}
  285. //{{{ Protected members
  286. /**
  287. * Has the option pane been initialized?
  288. */
  289. protected boolean initialized;
  290. /**
  291. * The layout manager.
  292. */
  293. protected GridBagLayout gridBag;
  294. /**
  295. * The number of components already added to the layout manager.
  296. */
  297. protected int y;
  298. /**
  299. * This method should create and arrange the components of the option pane
  300. * and initialize the option data displayed to the user. This method
  301. * is called when the option pane is first displayed, and is not
  302. * called again for the lifetime of the object.
  303. */
  304. protected void _init() {}
  305. /**
  306. * Called when the options dialog's "ok" button is clicked.
  307. * This should save any properties being edited in this option
  308. * pane.
  309. */
  310. protected void _save() {}
  311. //}}}
  312. //{{{ Private members
  313. private String name;
  314. //}}}
  315. }