PageRenderTime 71ms CodeModel.GetById 61ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 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 5004 2004-03-28 00:07:27Z 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	 * @since jEdit 4.1pre7
250	 */
251	public void addSeparator()
252	{
253		addComponent(Box.createVerticalStrut(6));
254
255		JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
256
257		GridBagConstraints cons = new GridBagConstraints();
258		cons.gridy = y++;
259		cons.gridheight = 1;
260		cons.gridwidth = cons.REMAINDER;
261		cons.fill = GridBagConstraints.BOTH;
262		cons.anchor = GridBagConstraints.WEST;
263		cons.weightx = 1.0f;
264		//cons.insets = new Insets(1,0,1,0);
265
266		gridBag.setConstraints(sep,cons);
267		add(sep);
268
269		addComponent(Box.createVerticalStrut(6));
270	} //}}}
271
272	//{{{ addSeparator() method
273	/**
274	 * Adds a separator component.
275	 * @param label The separator label property
276	 * @since jEdit 2.6pre2
277	 */
278	public void addSeparator(String label)
279	{
280		if(y != 0)
281			addComponent(Box.createVerticalStrut(6));
282
283		Box box = new Box(BoxLayout.X_AXIS);
284		Box box2 = new Box(BoxLayout.Y_AXIS);
285		box2.add(Box.createGlue());
286		box2.add(new JSeparator(JSeparator.HORIZONTAL));
287		box2.add(Box.createGlue());
288		box.add(box2);
289		JLabel l = new JLabel(jEdit.getProperty(label));
290		l.setMaximumSize(l.getPreferredSize());
291		box.add(l);
292		Box box3 = new Box(BoxLayout.Y_AXIS);
293		box3.add(Box.createGlue());
294		box3.add(new JSeparator(JSeparator.HORIZONTAL));
295		box3.add(Box.createGlue());
296		box.add(box3);
297
298		GridBagConstraints cons = new GridBagConstraints();
299		cons.gridy = y++;
300		cons.gridheight = 1;
301		cons.gridwidth = cons.REMAINDER;
302		cons.fill = GridBagConstraints.BOTH;
303		cons.anchor = GridBagConstraints.WEST;
304		cons.weightx = 1.0f;
305		cons.insets = new Insets(1,0,1,0);
306
307		gridBag.setConstraints(box,cons);
308		add(box);
309	} //}}}
310
311	//{{{ Protected members
312	/**
313	 * Has the option pane been initialized?
314	 */
315	protected boolean initialized;
316
317	/**
318	 * The layout manager.
319	 */
320	protected GridBagLayout gridBag;
321
322	/**
323	 * The number of components already added to the layout manager.
324	 */
325	protected int y;
326
327	/**
328	 * This method should create and arrange the components of the option pane
329	 * and initialize the option data displayed to the user. This method
330	 * is called when the option pane is first displayed, and is not
331	 * called again for the lifetime of the object.
332	 */
333	protected void _init() {}
334
335	/**
336	 * Called when the options dialog's "ok" button is clicked.
337	 * This should save any properties being edited in this option
338	 * pane.
339	 */
340	protected void _save() {}
341	//}}}
342
343	//{{{ Private members
344	private String name;
345	//}}}
346}