KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > AbstractOptionPane


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
23 package org.gjt.sp.jedit;
24
25 //{{{ Imports
26
import javax.swing.border.EmptyBorder JavaDoc;
27 import javax.swing.*;
28 import 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 5503 2006-06-27 04:12:36Z ezust $
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.
63
public class AbstractOptionPane extends JPanel implements OptionPane
64 {
65     //{{{ AbstractOptionPane constructor
66
/**
67      * Creates a new option pane.
68      * @param internalName 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 JavaDoc internalName)
72     {
73         this.name = internalName;
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 JavaDoc 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     // {{{ newLabel()
124
/**
125      *@return a label which has the same tooltiptext as the Component
126      * that it is a label for. This is used to create labels from inside
127      * AbstractOptionPane.
128      * @since jEdit 4.3pre4
129      */

130     public JLabel newLabel(String JavaDoc label, Component comp)
131     {
132         JLabel retval = new JLabel(label);
133         try /* to get the tooltip of the component */
134         {
135             JComponent jc = (JComponent) comp;
136             String JavaDoc tttext = jc.getToolTipText();
137             retval.setToolTipText(tttext);
138         }
139         catch (Exception JavaDoc e)
140         {
141             /* There probably wasn't a tooltip,
142              * or it wasn't a JComponent.
143                We don't care. */

144         }
145         return retval;
146     }// }}}
147

148     //{{{ addComponent() method
149
/**
150      * Adds a labeled component to the option pane. Components are
151      * added in a vertical fashion, one per row. The label is
152      * displayed to the left of the component.
153      * @param label The label
154      * @param comp The component
155      */

156     public void addComponent(String JavaDoc label, Component comp)
157     {
158         JLabel l = newLabel(label, comp);
159         l.setBorder(new EmptyBorder JavaDoc(0,0,0,12));
160         addComponent(l,comp,GridBagConstraints.BOTH);
161     } //}}}
162

163     //{{{ addComponent() method
164
/**
165      * Adds a labeled component to the option pane. Components are
166      * added in a vertical fashion, one per row. The label is
167      * displayed to the left of the component.
168      * @param label The label
169      * @param comp The component
170      * @param fill Fill parameter to GridBagConstraints for the right
171      * component
172      */

173     public void addComponent(String JavaDoc label, Component comp, int fill)
174     {
175         JLabel l = newLabel(label, comp);
176         l.setBorder(new EmptyBorder JavaDoc(0,0,0,12));
177         addComponent(l,comp,fill);
178     } //}}}
179

180     //{{{ addComponent() method
181
/**
182      * Adds a labeled component to the option pane. Components are
183      * added in a vertical fashion, one per row. The label is
184      * displayed to the left of the component.
185      * @param comp1 The label
186      * @param comp2 The component
187      *
188      * @since jEdit 4.1pre3
189      */

190     public void addComponent(Component comp1, Component comp2)
191     {
192         addComponent(comp1,comp2,GridBagConstraints.BOTH);
193     } //}}}
194

195     //{{{ addComponent() method
196
/**
197      * Adds a labeled component to the option pane. Components are
198      * added in a vertical fashion, one per row. The label is
199      * displayed to the left of the component.
200      * @param comp1 The label
201      * @param comp2 The component
202      * @param fill Fill parameter to GridBagConstraints for the right
203      * component
204      *
205      * @since jEdit 4.1pre3
206      */

207     public void addComponent(Component comp1, Component comp2, int fill)
208     {
209         copyToolTips(comp1, comp2);
210         GridBagConstraints cons = new GridBagConstraints();
211         cons.gridy = y++;
212         cons.gridheight = 1;
213         cons.gridwidth = 1;
214         cons.weightx = 0.0f;
215         cons.insets = new Insets(1,0,1,0);
216         cons.fill = GridBagConstraints.BOTH;
217
218         gridBag.setConstraints(comp1,cons);
219         add(comp1);
220
221         cons.fill = fill;
222         cons.gridx = 1;
223         cons.weightx = 1.0f;
224         gridBag.setConstraints(comp2,cons);
225         add(comp2);
226     } //}}}
227

228     //{{{ addComponent() method
229
/**
230      * Adds a component to the option pane. Components are
231      * added in a vertical fashion, one per row.
232      * @param comp The component
233      */

234     public void addComponent(Component comp)
235     {
236         GridBagConstraints cons = new GridBagConstraints();
237         cons.gridy = y++;
238         cons.gridheight = 1;
239         cons.gridwidth = cons.REMAINDER;
240         cons.fill = GridBagConstraints.NONE;
241         cons.anchor = GridBagConstraints.WEST;
242         cons.weightx = 1.0f;
243         cons.insets = new Insets(1,0,1,0);
244
245         gridBag.setConstraints(comp,cons);
246         add(comp);
247     } //}}}
248

249     //{{{ addComponent() method
250
/**
251      * Adds a component to the option pane. Components are
252      * added in a vertical fashion, one per row.
253      * @param comp The component
254      * @param fill Fill parameter to GridBagConstraints
255      * @since jEdit 4.2pre2
256      */

257     public void addComponent(Component comp, int fill)
258     {
259         GridBagConstraints cons = new GridBagConstraints();
260         cons.gridy = y++;
261         cons.gridheight = 1;
262         cons.gridwidth = cons.REMAINDER;
263         cons.fill = fill;
264         cons.anchor = GridBagConstraints.WEST;
265         cons.weightx = 1.0f;
266         cons.insets = new Insets(1,0,1,0);
267
268         gridBag.setConstraints(comp,cons);
269         add(comp);
270     } //}}}
271

272     private void copyToolTips (Component c1, Component c2) {
273         int tooltips = 0;
274         int jc=0;
275         String JavaDoc text = null;
276         JComponent jc1 = null, jc2 = null;
277         try {
278             jc1 = (JComponent) c1;
279             text = jc1.getToolTipText();
280             ++jc;
281             if (text != null && text.length() > 0) tooltips++;
282         }
283         catch (Exception JavaDoc e) {}
284         try {
285             jc2 = (JComponent) c2;
286             String JavaDoc text2 = jc2.getToolTipText();
287             ++jc;
288             if (text2 != null && text2.length() > 0) {
289                 text = text2;
290                 tooltips++;
291             }
292         }
293         catch (Exception JavaDoc e) {}
294         if (tooltips == 1 && jc == 2) {
295             jc1.setToolTipText(text);
296             jc2.setToolTipText(text);
297         }
298         
299     }
300     //{{{ addSeparator() method
301
/**
302      * Adds a separator component.
303      * @since jEdit 4.1pre7
304      */

305     public void addSeparator()
306     {
307         addComponent(Box.createVerticalStrut(6));
308
309         JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
310
311         GridBagConstraints cons = new GridBagConstraints();
312         cons.gridy = y++;
313         cons.gridheight = 1;
314         cons.gridwidth = cons.REMAINDER;
315         cons.fill = GridBagConstraints.BOTH;
316         cons.anchor = GridBagConstraints.WEST;
317         cons.weightx = 1.0f;
318         //cons.insets = new Insets(1,0,1,0);
319

320         gridBag.setConstraints(sep,cons);
321         add(sep);
322
323         addComponent(Box.createVerticalStrut(6));
324     } //}}}
325

326     //{{{ addSeparator() method
327
/**
328      * Adds a separator component.
329      * @param label The separator label property
330      * @since jEdit 2.6pre2
331      */

332     public void addSeparator(String JavaDoc label)
333     {
334         if(y != 0)
335             addComponent(Box.createVerticalStrut(6));
336
337         Box box = new Box(BoxLayout.X_AXIS);
338         Box box2 = new Box(BoxLayout.Y_AXIS);
339         box2.add(Box.createGlue());
340         box2.add(new JSeparator(JSeparator.HORIZONTAL));
341         box2.add(Box.createGlue());
342         box.add(box2);
343         JLabel l = new JLabel(jEdit.getProperty(label));
344         l.setMaximumSize(l.getPreferredSize());
345         box.add(l);
346         Box box3 = new Box(BoxLayout.Y_AXIS);
347         box3.add(Box.createGlue());
348         box3.add(new JSeparator(JSeparator.HORIZONTAL));
349         box3.add(Box.createGlue());
350         box.add(box3);
351
352         GridBagConstraints cons = new GridBagConstraints();
353         cons.gridy = y++;
354         cons.gridheight = 1;
355         cons.gridwidth = cons.REMAINDER;
356         cons.fill = GridBagConstraints.BOTH;
357         cons.anchor = GridBagConstraints.WEST;
358         cons.weightx = 1.0f;
359         cons.insets = new Insets(1,0,1,0);
360
361         gridBag.setConstraints(box,cons);
362         add(box);
363     } //}}}
364

365     //{{{ Protected members
366
/**
367      * Has the option pane been initialized?
368      */

369     protected boolean initialized;
370
371     /**
372      * The layout manager.
373      */

374     protected GridBagLayout gridBag;
375
376     /**
377      * The number of components already added to the layout manager.
378      */

379     protected int y;
380
381     /**
382      * This method should create and arrange the components of the option pane
383      * and initialize the option data displayed to the user. This method
384      * is called when the option pane is first displayed, and is not
385      * called again for the lifetime of the object.
386      */

387     protected void _init() {}
388
389     /**
390      * Called when the options dialog's "ok" button is clicked.
391      * This should save any properties being edited in this option
392      * pane.
393      */

394     protected void _save() {}
395     //}}}
396

397     //{{{ Private members
398
private String JavaDoc name;
399     //}}}
400
}
401
Popular Tags