PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/Sessions/sessions/SessionPropertyPane.java

#
Java | 256 lines | 111 code | 44 blank | 101 comment | 7 complexity | 2ba4f464119c7d5fe986cb043c35b0a6 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. * SessionPropertyPane.java - option pane for session properties
  3. * Copyright (C) 2001 Dirk Moebius
  4. *
  5. * Loosely modelled after AbstractOptionPane.java
  6. * Copyright (C) 1998, 1999, 2000, 2001 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 sessions;
  23. import java.awt.*;
  24. import java.util.Enumeration;
  25. import javax.swing.border.EmptyBorder;
  26. import javax.swing.*;
  27. import javax.swing.tree.TreeNode;
  28. import org.gjt.sp.jedit.jEdit;
  29. /**
  30. * An abstract option pane for session properties.
  31. * All session option panes must inherit from this class.
  32. */
  33. public abstract class SessionPropertyPane extends JPanel
  34. {
  35. /**
  36. * Creates a new session property pane.
  37. * @param session the session.
  38. */
  39. public SessionPropertyPane(Session session)
  40. {
  41. this.session = session;
  42. setLayout(gridBag = new GridBagLayout());
  43. }
  44. /**
  45. * Return a unique identifier for this pane.
  46. * Typically this identifier should include your plugin's name,
  47. * eg. "myplugin.sessionproperties.firstPane".
  48. */
  49. public abstract String getIdentifier();
  50. /**
  51. * Return the visible label of this property pane.
  52. */
  53. public abstract String getLabel();
  54. /**
  55. * This method should create the property pane's GUI.
  56. * Use <code>session.getProperty(String key)</code> to retrieve properties
  57. * of the session belonging to this option pane.
  58. * Use <code>setLayout(LayoutManager)</code> at the start of the
  59. * method, if you want a different LayoutManager than the default,
  60. * GridBayLayout.
  61. */
  62. public abstract void _init();
  63. /**
  64. * Called when the options dialog's "ok" button is clicked.
  65. * This should save any properties being edited in this option
  66. * pane.
  67. * Save properties to the session belonging to this instance with
  68. * <code>session.setProperty(String key, String value)</code>.
  69. */
  70. public abstract void _save();
  71. public final String toString()
  72. {
  73. return getLabel();
  74. }
  75. /**
  76. * The session instance for this option pane.
  77. * Save properties to this session belonging to this instance with
  78. * <code>session.setProperty(String key, String value)</code>,
  79. * retrieve session properties with
  80. * <code>session.getProperty(String key)</code>.
  81. */
  82. protected Session session;
  83. /** The default layout manager. */
  84. protected GridBagLayout gridBag;
  85. /** The number of components already added to the layout manager. */
  86. protected int y;
  87. /**
  88. * Adds two components to the pane. The two components are
  89. * aligned left to right, forming a row. With each invocation of
  90. * addComponent(), a new row is created.
  91. *
  92. * @param leftComp The left component
  93. * @param rightComp The right component
  94. * @throws IllegalStateException if the layout manager of this panel
  95. * has been changed to something other than GridBagLayout.
  96. * You cannot use this method if the layout is not gridbag.
  97. */
  98. protected void addComponent(Component leftComp, Component rightComp)
  99. {
  100. if(!(getLayout() instanceof GridBagLayout))
  101. throw new IllegalStateException("current layout manager is no GridBagLayout, current is: "
  102. + getLayout().getClass().getName());
  103. GridBagConstraints cons = new GridBagConstraints();
  104. cons.gridy = y++;
  105. cons.gridheight = 1;
  106. cons.gridwidth = 1;
  107. cons.weightx = 0.0f;
  108. cons.fill = GridBagConstraints.BOTH;
  109. gridBag.setConstraints(leftComp,cons);
  110. add(leftComp);
  111. cons.gridx = 1;
  112. cons.weightx = 1.0f;
  113. gridBag.setConstraints(rightComp,cons);
  114. add(rightComp);
  115. }
  116. /**
  117. * Adds a labeled component to the pane. Components are
  118. * added in a vertical fashion, one per row. The label is
  119. * displayed to the left of the component.
  120. *
  121. * @param label The jEdit property for the label text, NOT the label
  122. * text itself!
  123. * @param comp The component on the right.
  124. * @throws IllegalStateException if the layout manager of this panel
  125. * has been changed to something other than GridBagLayout.
  126. * You cannot use this method if the layout is not gridbag.
  127. */
  128. protected void addComponent(String labelProperty, Component comp)
  129. {
  130. String label = jEdit.getProperty(labelProperty);
  131. if(label == null)
  132. label = labelProperty;
  133. JLabel l = new JLabel(label, SwingConstants.RIGHT);
  134. l.setBorder(new EmptyBorder(0,0,0,12));
  135. addComponent(l, comp);
  136. }
  137. /**
  138. * Adds a component to the option pane. Components are
  139. * added in a vertical fashion, one per row.
  140. *
  141. * @param comp The component
  142. * @throws IllegalStateException if the layout manager of this panel
  143. * has been changed to something other than GridBagLayout.
  144. * You cannot use this method if the layout is not gridbag.
  145. */
  146. protected void addComponent(Component comp)
  147. {
  148. if(!(getLayout() instanceof GridBagLayout))
  149. throw new IllegalStateException("current layout manager is no GridBagLayout, current is: "
  150. + getLayout().getClass().getName());
  151. GridBagConstraints cons = new GridBagConstraints();
  152. cons.gridy = y++;
  153. cons.gridheight = 1;
  154. cons.gridwidth = cons.REMAINDER;
  155. cons.fill = GridBagConstraints.NONE;
  156. cons.anchor = GridBagConstraints.WEST;
  157. cons.weightx = 1.0f;
  158. gridBag.setConstraints(comp,cons);
  159. add(comp);
  160. }
  161. /**
  162. * Adds a separator component.
  163. *
  164. * @param label The separator label property
  165. * @throws IllegalStateException if the layout manager of this panel
  166. * has been changed to something other than GridBagLayout.
  167. * You cannot use this method if the layout is not gridbag.
  168. */
  169. protected void addSeparator(String label)
  170. {
  171. if(!(getLayout() instanceof GridBagLayout))
  172. throw new IllegalStateException("current layout manager is no GridBagLayout, current is: "
  173. + getLayout().getClass().getName());
  174. Box box = new Box(BoxLayout.X_AXIS);
  175. Box box2 = new Box(BoxLayout.Y_AXIS);
  176. box2.add(Box.createGlue());
  177. box2.add(new JSeparator(JSeparator.HORIZONTAL));
  178. box2.add(Box.createGlue());
  179. box.add(box2);
  180. JLabel l = new JLabel(" " + jEdit.getProperty(label) + " ");
  181. l.setMaximumSize(l.getPreferredSize());
  182. box.add(l);
  183. Box box3 = new Box(BoxLayout.Y_AXIS);
  184. box3.add(Box.createGlue());
  185. box3.add(new JSeparator(JSeparator.HORIZONTAL));
  186. box3.add(Box.createGlue());
  187. box.add(box3);
  188. GridBagConstraints cons = new GridBagConstraints();
  189. cons.gridy = y++;
  190. cons.gridheight = 1;
  191. cons.gridwidth = cons.REMAINDER;
  192. cons.fill = GridBagConstraints.BOTH;
  193. cons.anchor = GridBagConstraints.WEST;
  194. cons.weightx = 1.0f;
  195. gridBag.setConstraints(box,cons);
  196. add(box);
  197. }
  198. void init()
  199. {
  200. if(!initialized)
  201. {
  202. initialized = true;
  203. _init();
  204. }
  205. }
  206. void save()
  207. {
  208. if(initialized)
  209. _save();
  210. }
  211. private boolean initialized;
  212. }