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