001: /*
002: * AbstractOptionPane.java - Abstract option pane
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1998, 1999, 2000, 2001, 2002 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit;
024:
025: //{{{ Imports
026: import javax.swing.border.EmptyBorder;
027: import javax.swing.*;
028: import java.awt.*;
029:
030: //}}}
031:
032: /**
033: * The default implementation of the option pane interface.<p>
034: *
035: * See {@link EditPlugin} for information on how jEdit obtains and constructs
036: * option pane instances.<p>
037: *
038: * Most option panes extend this implementation of {@link OptionPane}, instead
039: * of implementing {@link OptionPane} directly. This class provides a convenient
040: * default framework for laying out configuration options.<p>
041: *
042: * It is derived from Java's <code>JPanel</code> class and uses a
043: * <code>GridBagLayout</code> object for component management. Since
044: * <code>GridBagLayout</code> can be a bit cumbersome to use, this class
045: * contains shortcut methods to simplify layout:
046: *
047: * <ul>
048: * <li>{@link #addComponent(Component)}</li>
049: * <li>{@link #addComponent(String,Component)}</li>
050: * <li>{@link #addComponent(String,Component,int)}</li>
051: * <li>{@link #addComponent(Component,Component)}</li>
052: * <li>{@link #addComponent(Component,Component,int)}</li>
053: * <li>{@link #addSeparator()}</li>
054: * <li>{@link #addSeparator(String)}</li>
055: * </ul>
056: *
057: * @author Slava Pestov
058: * @author John Gellene (API documentation)
059: * @version $Id: AbstractOptionPane.java 9998 2007-07-11 10:25:00Z Vampire0 $
060: */
061: // even though this class is called AbstractOptionPane, it is not really
062: // abstract, since BufferOptions uses an instance of it to lay out its
063: // components.
064: public class AbstractOptionPane extends JPanel implements OptionPane {
065: //{{{ AbstractOptionPane constructor
066: /**
067: * Creates a new option pane.
068: * @param internalName The internal name. The option pane's label is set to the
069: * value of the property named <code>options.<i>name</i>.label</code>.
070: */
071: public AbstractOptionPane(String internalName) {
072: this .name = internalName;
073: setLayout(gridBag = new GridBagLayout());
074: } //}}}
075:
076: //{{{ getName() method
077: /**
078: * Returns the internal name of this option pane. The option pane's label
079: * is set to the value of the property named
080: * <code>options.<i>name</i>.label</code>.
081: */
082: public String getName() {
083: return name;
084: } //}}}
085:
086: //{{{ getComponent() method
087: /**
088: * Returns the component that should be displayed for this option pane.
089: * Because this class extends Component, it simply returns "this".
090: */
091: public Component getComponent() {
092: return this ;
093: } //}}}
094:
095: //{{{ init() method
096: /**
097: * Do not override this method, override {@link #_init()} instead.
098: */
099: // final in 4.2
100: public void init() {
101: if (!initialized) {
102: initialized = true;
103: _init();
104: }
105: } //}}}
106:
107: //{{{ save() method
108: /**
109: * Do not override this method, override {@link #_save()} instead.
110: */
111: // final in 4.2
112: public void save() {
113: if (initialized)
114: _save();
115: } //}}}
116:
117: //{{{ newLabel()
118: /**
119: *@return a label which has the same tooltiptext as the Component
120: * that it is a label for. This is used to create labels from inside
121: * AbstractOptionPane.
122: * @since jEdit 4.3pre4
123: */
124: public JLabel newLabel(String label, Component comp) {
125: JLabel retval = new JLabel(label);
126: try /* to get the tooltip of the component */
127: {
128: JComponent jc = (JComponent) comp;
129: String tttext = jc.getToolTipText();
130: retval.setToolTipText(tttext);
131: } catch (Exception e) {
132: /* There probably wasn't a tooltip,
133: * or it wasn't a JComponent.
134: We don't care. */
135: }
136: return retval;
137: }// }}}
138:
139: //{{{ addComponent() method
140: /**
141: * Adds a labeled component to the option pane. Components are
142: * added in a vertical fashion, one per row. The label is
143: * displayed to the left of the component.
144: * @param label The label
145: * @param comp The component
146: */
147: public void addComponent(String label, Component comp) {
148: JLabel l = newLabel(label, comp);
149: l.setBorder(new EmptyBorder(0, 0, 0, 12));
150: addComponent(l, comp, GridBagConstraints.BOTH);
151: } //}}}
152:
153: //{{{ addComponent() method
154: /**
155: * Adds a labeled component to the option pane. Components are
156: * added in a vertical fashion, one per row. The label is
157: * displayed to the left of the component.
158: * @param label The label
159: * @param comp The component
160: * @param fill Fill parameter to GridBagConstraints for the right
161: * component
162: */
163: public void addComponent(String label, Component comp, int fill) {
164: JLabel l = newLabel(label, comp);
165: l.setBorder(new EmptyBorder(0, 0, 0, 12));
166: addComponent(l, comp, fill);
167: } //}}}
168:
169: //{{{ addComponent() method
170: /**
171: * Adds a labeled component to the option pane. Components are
172: * added in a vertical fashion, one per row. The label is
173: * displayed to the left of the component.
174: * @param comp1 The label
175: * @param comp2 The component
176: *
177: * @since jEdit 4.1pre3
178: */
179: public void addComponent(Component comp1, Component comp2) {
180: addComponent(comp1, comp2, GridBagConstraints.BOTH);
181: } //}}}
182:
183: //{{{ addComponent() method
184: /**
185: * Adds a labeled component to the option pane. Components are
186: * added in a vertical fashion, one per row. The label is
187: * displayed to the left of the component.
188: * @param comp1 The label
189: * @param comp2 The component
190: * @param fill Fill parameter to GridBagConstraints for the right
191: * component
192: *
193: * @since jEdit 4.1pre3
194: */
195: public void addComponent(Component comp1, Component comp2, int fill) {
196: copyToolTips(comp1, comp2);
197: GridBagConstraints cons = new GridBagConstraints();
198: cons.gridy = y++;
199: cons.gridheight = 1;
200: cons.gridwidth = 1;
201: cons.weightx = 0.0f;
202: cons.insets = new Insets(1, 0, 1, 0);
203: cons.fill = GridBagConstraints.BOTH;
204:
205: gridBag.setConstraints(comp1, cons);
206: add(comp1);
207:
208: cons.fill = fill;
209: cons.gridx = 1;
210: cons.weightx = 1.0f;
211: gridBag.setConstraints(comp2, cons);
212: add(comp2);
213: } //}}}
214:
215: //{{{ addComponent() method
216: /**
217: * Adds a component to the option pane. Components are
218: * added in a vertical fashion, one per row.
219: * @param comp The component
220: */
221: public void addComponent(Component comp) {
222: GridBagConstraints cons = new GridBagConstraints();
223: cons.gridy = y++;
224: cons.gridheight = 1;
225: cons.gridwidth = cons.REMAINDER;
226: cons.fill = GridBagConstraints.NONE;
227: cons.anchor = GridBagConstraints.WEST;
228: cons.weightx = 1.0f;
229: cons.insets = new Insets(1, 0, 1, 0);
230:
231: gridBag.setConstraints(comp, cons);
232: add(comp);
233: } //}}}
234:
235: //{{{ addComponent() method
236: /**
237: * Adds a component to the option pane. Components are
238: * added in a vertical fashion, one per row.
239: * @param comp The component
240: * @param fill Fill parameter to GridBagConstraints
241: * @since jEdit 4.2pre2
242: */
243: public void addComponent(Component comp, int fill) {
244: GridBagConstraints cons = new GridBagConstraints();
245: cons.gridy = y++;
246: cons.gridheight = 1;
247: cons.gridwidth = cons.REMAINDER;
248: cons.fill = fill;
249: cons.anchor = GridBagConstraints.WEST;
250: cons.weightx = 1.0f;
251: cons.insets = new Insets(1, 0, 1, 0);
252:
253: gridBag.setConstraints(comp, cons);
254: add(comp);
255: } //}}}
256:
257: //{{{ copyToolTips() method
258: private void copyToolTips(Component c1, Component c2) {
259: int tooltips = 0;
260: int jc = 0;
261: String text = null;
262: JComponent jc1 = null, jc2 = null;
263: try {
264: jc1 = (JComponent) c1;
265: text = jc1.getToolTipText();
266: ++jc;
267: if (text != null && text.length() > 0)
268: tooltips++;
269: } catch (Exception e) {
270: }
271: try {
272: jc2 = (JComponent) c2;
273: String text2 = jc2.getToolTipText();
274: ++jc;
275: if (text2 != null && text2.length() > 0) {
276: text = text2;
277: tooltips++;
278: }
279: } catch (Exception e) {
280: }
281: if (tooltips == 1 && jc == 2) {
282: jc1.setToolTipText(text);
283: jc2.setToolTipText(text);
284: }
285:
286: } //}}}
287:
288: //{{{ addSeparator() method
289: /**
290: * Adds a separator component.
291: * @since jEdit 4.1pre7
292: */
293: public void addSeparator() {
294: addComponent(Box.createVerticalStrut(6));
295:
296: JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
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(sep, cons);
308: add(sep);
309:
310: addComponent(Box.createVerticalStrut(6));
311: } //}}}
312:
313: //{{{ addSeparator() method
314: /**
315: * Adds a separator component.
316: * @param label The separator label property
317: * @since jEdit 2.6pre2
318: */
319: public void addSeparator(String label) {
320: if (y != 0)
321: addComponent(Box.createVerticalStrut(6));
322:
323: Box box = new Box(BoxLayout.X_AXIS);
324: Box box2 = new Box(BoxLayout.Y_AXIS);
325: box2.add(Box.createGlue());
326: box2.add(new JSeparator(JSeparator.HORIZONTAL));
327: box2.add(Box.createGlue());
328: box.add(box2);
329: JLabel l = new JLabel(jEdit.getProperty(label));
330: l.setMaximumSize(l.getPreferredSize());
331: box.add(l);
332: Box box3 = new Box(BoxLayout.Y_AXIS);
333: box3.add(Box.createGlue());
334: box3.add(new JSeparator(JSeparator.HORIZONTAL));
335: box3.add(Box.createGlue());
336: box.add(box3);
337:
338: GridBagConstraints cons = new GridBagConstraints();
339: cons.gridy = y++;
340: cons.gridheight = 1;
341: cons.gridwidth = cons.REMAINDER;
342: cons.fill = GridBagConstraints.BOTH;
343: cons.anchor = GridBagConstraints.WEST;
344: cons.weightx = 1.0f;
345: cons.insets = new Insets(1, 0, 1, 0);
346:
347: gridBag.setConstraints(box, cons);
348: add(box);
349: } //}}}
350:
351: //{{{ Protected members
352: /**
353: * Has the option pane been initialized?
354: */
355: protected boolean initialized;
356:
357: /**
358: * The layout manager.
359: */
360: protected GridBagLayout gridBag;
361:
362: /**
363: * The number of components already added to the layout manager.
364: */
365: protected int y;
366:
367: /**
368: * This method should create and arrange the components of the option pane
369: * and initialize the option data displayed to the user. This method
370: * is called when the option pane is first displayed, and is not
371: * called again for the lifetime of the object.
372: */
373: protected void _init() {
374: }
375:
376: /**
377: * Called when the options dialog's "ok" button is clicked.
378: * This should save any properties being edited in this option
379: * pane.
380: */
381: protected void _save() {
382: }
383:
384: //}}}
385:
386: //{{{ Private members
387: private String name;
388: //}}}
389: }
|