PageRenderTime 59ms CodeModel.GetById 45ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 1ms

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

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