001: /*
002: * JSHelpOptionPane.java - options panel with some helper functions
003: * Copyright (C) 2000,2001 Dirk Moebius
004: *
005: * jEdit buffer options:
006: * :tabSize=4:indentSize=4:noTabs=false:maxLineLen=0:
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: package org.acm.seguin.ide.common.options;
023:
024: import java.awt.Color;
025: import java.awt.Component;
026: import java.awt.Font;
027: import java.awt.GridBagConstraints;
028: import java.awt.event.MouseAdapter;
029: import java.awt.event.MouseEvent;
030: import javax.swing.JScrollPane;
031: import javax.swing.JTextArea;
032: import javax.swing.JTextPane;
033: import javax.swing.UIManager;
034: import javax.swing.border.EmptyBorder;
035: import javax.swing.*;
036: import javax.swing.text.EditorKit;
037: import java.awt.*;
038: import java.util.Hashtable;
039: import org.acm.seguin.ide.common.options.PropertiesFile;
040: import org.acm.seguin.ide.common.IDEPlugin;
041:
042: /**
043: *@author Mike Atkinson (<a href="mailto:javastyle@ladyshot.demon.co.uk">
044: * Mike@ladyshot.demon.co.uk</a> )
045: *@author Dirk Moebius (<a href="mailto:dmoebius@gmx.net">dmoebius@gmx.net
046: * </a>)
047: *@created 03 September 2003
048: *@version $Version: $
049: *@since 1.0
050: */
051: public abstract class JSHelpOptionPane extends JPanel {
052:
053: public static final String[] NAMES = new String[] {
054: "javastyle.general", "javastyle.indenting",
055: "javastyle.spacing", "javastyle.alignment",
056: "javastyle.sorting", "javastyle.javadoc",
057: "javastyle.stubs", "javastyle.stubs_junit",
058: "javastyle.tags", "javastyle.comments", "javastyle.pmd" };
059:
060: private final static Font helpFont = new Font("DialogInput",
061: Font.PLAIN, 11);
062: private final static Color helpBackground = UIManager
063: .getColor("Label.background");
064: private final static Color helpForeground = UIManager
065: .getColor("Label.foreground");
066:
067: private JTextPane helpArea = null;
068: private EditorKit editorKit = null;
069: private EditorKit htmlEditorKit = new javax.swing.text.html.HTMLEditorKit();
070: private Hashtable helpTexts = new Hashtable();
071: private MouseHandler mh = new MouseHandler();
072: private String project;
073: /**
074: * Description of the Field
075: */
076: protected PropertiesFile props;
077:
078: /**
079: * Gets the IDE Property attribute of the JSHelpOptionPane class
080: *
081: *@param prop Description of the Parameter
082: *@return The IDE Property value
083: */
084: public static String getIdeProperty(String prop) {
085: return IDEPlugin.getProperty(prop);
086: }
087:
088: /**
089: * Gets the IDE Property attribute of the JSHelpOptionPane class
090: *
091: *@param prop Description of the Parameter
092: *@return The IDE Property value
093: */
094: public static String getIdeJavaStyleOption(String prop) {
095: return IDEPlugin.getProperty("options.javastyle." + prop);
096: }
097:
098: /**
099: * Constructor for the JSHelpOptionPane object
100: *
101: *@param key Description of the Parameter
102: *@param project Description of the Parameter
103: */
104: public JSHelpOptionPane(String key, String type, String project) {
105: name = key;
106: this .project = (project == null) ? "default" : project;
107: setLayout(gridBag = new GridBagLayout());
108: props = IDEPlugin.getProperties(type, this .project);
109: }
110:
111: /**
112: * Adds a feature to the HelpFor attribute of the JSHelpOptionPane object
113: *
114: *@param comp The feature to be added to the HelpFor attribute
115: *@param compProperty The feature to be added to the HelpFor attribute
116: */
117: protected void addHelpFor(Component comp, String compProperty) {
118: try {
119: comp.addMouseListener(mh);
120: helpTexts.put(comp, getIdeJavaStyleOption(compProperty
121: + ".tooltip"));
122: } catch (Exception e) {
123: System.out.println("can't find \"" + compProperty
124: + ".tooltip\"");
125: }
126: }
127:
128: /**
129: * Adds a feature to the HelpArea attribute of the JSHelpOptionPane object
130: */
131: protected void addHelpArea() {
132: // "----Help----"
133: addSeparator("helpArea");
134: // help area
135: helpArea = new JTextPane();
136: helpArea.setEditable(false);
137: //helpArea.setLineWrap(true);
138: //helpArea.setWrapStyleWord(true);
139: helpArea.setFont(helpFont);
140: helpArea.setBackground(helpBackground);
141: helpArea.setForeground(helpForeground);
142: helpArea.setText("");
143: //helpArea.setRows(5);
144: editorKit = helpArea.getEditorKit();
145: JScrollPane scrHelpArea = new JScrollPane(helpArea);
146: GridBagConstraints cons = new GridBagConstraints();
147: cons.gridy = y++;
148: cons.gridheight = 1;
149: cons.gridwidth = cons.REMAINDER;
150: cons.fill = GridBagConstraints.BOTH;
151: cons.weightx = 1.0f;
152: cons.weighty = 1.0f;
153: gridBag.setConstraints(scrHelpArea, cons);
154: add(scrHelpArea);
155: }
156:
157: /**
158: * Adds a feature to the Component attribute of the JSHelpOptionPane object
159: *
160: *@param option The feature to be added to the Component attribute
161: *@param label The feature to be added to the Component attribute
162: *@param component The feature to be added to the Component attribute
163: *@return Description of the Return Value
164: */
165: protected SelectedPanel addComponent(String option, String label,
166: javax.swing.JComponent component) {
167: return new SelectedPanel(this , props, project, option, null,
168: label, component);
169: }
170:
171: /**
172: * Adds a feature to the Component attribute of the JSHelpOptionPane object
173: *
174: *@param option The feature to be added to the Component attribute
175: *@param defaultOption The feature to be added to the Component attribute
176: *@param label The feature to be added to the Component attribute
177: *@param component The feature to be added to the Component attribute
178: *@return Description of the Return Value
179: */
180: protected SelectedPanel addComponent(String option,
181: String defaultOption, String label,
182: javax.swing.JComponent component) {
183: return new SelectedPanel(this , props, project, option,
184: defaultOption, label, component);
185: }
186:
187: public void setHelpText(String text) {
188: helpArea
189: .setEditorKit((text.startsWith("<html>")) ? htmlEditorKit
190: : editorKit);
191: helpArea.setText(text);
192: helpArea.setCaretPosition(0);
193: }
194:
195: /**
196: * Description of the Class
197: *
198: *@author Mike
199: *@created 03 September 2003
200: */
201: private class MouseHandler extends MouseAdapter {
202: /**
203: * Description of the Method
204: *
205: *@param evt Description of the Parameter
206: */
207: public void mouseEntered(MouseEvent evt) {
208: setHelpText((String) helpTexts.get(evt.getSource()));
209: }
210: }
211:
212: //{{{ getName() method
213: /**
214: * Returns the internal name of this option pane. The option pane's label
215: * is set to the value of the property named
216: * <code>options.<i>name</i>.label</code>.
217: */
218: public String getName() {
219: return name;
220: } //}}}
221:
222: //{{{ getComponent() method
223: /**
224: * Returns the component that should be displayed for this option pane.
225: * Because this class extends Component, it simply returns "this".
226: */
227: public Component getComponent() {
228: return this ;
229: } //}}}
230:
231: //{{{ init() method
232: /**
233: * Do not override this method, override {@link #_init()} instead.
234: */
235: // final in 4.2
236: public void init() {
237: if (!initialized) {
238: initialized = true;
239: _init();
240: }
241: } //}}}
242:
243: //{{{ save() method
244: /**
245: * Do not override this method, override {@link #_save()} instead.
246: */
247: // final in 4.2
248: public void save() {
249: if (initialized)
250: _save();
251: } //}}}
252:
253: //{{{ addComponent() method
254: /**
255: * Adds a labeled component to the option pane. Components are
256: * added in a vertical fashion, one per row. The label is
257: * displayed to the left of the component.
258: * @param label The label
259: * @param comp The component
260: */
261: public void addComponent(String label, Component comp) {
262: JLabel l = new JLabel(label);
263: l.setBorder(new EmptyBorder(0, 0, 0, 12));
264: addComponent(l, comp, GridBagConstraints.BOTH);
265: } //}}}
266:
267: //{{{ addComponent() method
268: /**
269: * Adds a labeled component to the option pane. Components are
270: * added in a vertical fashion, one per row. The label is
271: * displayed to the left of the component.
272: * @param label The label
273: * @param comp The component
274: * @param fill Fill parameter to GridBagConstraints for the right
275: * component
276: */
277: public void addComponent(String label, Component comp, int fill) {
278: JLabel l = new JLabel(label);
279: l.setBorder(new EmptyBorder(0, 0, 0, 12));
280: addComponent(l, comp, fill);
281: } //}}}
282:
283: //{{{ addComponent() method
284: /**
285: * Adds a labeled component to the option pane. Components are
286: * added in a vertical fashion, one per row. The label is
287: * displayed to the left of the component.
288: * @param comp1 The label
289: * @param comp2 The component
290: *
291: * @since JRefactory 2.8.02
292: */
293: public void addComponent(Component comp1, Component comp2) {
294: addComponent(comp1, comp2, GridBagConstraints.BOTH);
295: } //}}}
296:
297: //{{{ addComponent() method
298: /**
299: * Adds a labeled component to the option pane. Components are
300: * added in a vertical fashion, one per row. The label is
301: * displayed to the left of the component.
302: * @param comp1 The label
303: * @param comp2 The component
304: * @param fill Fill parameter to GridBagConstraints for the right
305: * component
306: *
307: * @since JRefactory 2.8.02
308: */
309: public void addComponent(Component comp1, Component comp2, int fill) {
310: GridBagConstraints cons = new GridBagConstraints();
311: cons.gridy = y++;
312: cons.gridheight = 1;
313: cons.gridwidth = 1;
314: cons.weightx = 0.0f;
315: cons.insets = new Insets(1, 0, 1, 0);
316: cons.fill = GridBagConstraints.BOTH;
317:
318: gridBag.setConstraints(comp1, cons);
319: add(comp1);
320:
321: cons.fill = fill;
322: cons.gridx = 1;
323: cons.weightx = 1.0f;
324: gridBag.setConstraints(comp2, cons);
325: add(comp2);
326: } //}}}
327:
328: //{{{ addComponent() method
329: /**
330: * Adds a component to the option pane. Components are
331: * added in a vertical fashion, one per row.
332: * @param comp The component
333: */
334: public void addComponent(Component comp) {
335: GridBagConstraints cons = new GridBagConstraints();
336: cons.gridy = y++;
337: cons.gridheight = 1;
338: cons.gridwidth = cons.REMAINDER;
339: cons.fill = GridBagConstraints.NONE;
340: cons.anchor = GridBagConstraints.WEST;
341: cons.weightx = 1.0f;
342: cons.insets = new Insets(1, 0, 1, 0);
343:
344: gridBag.setConstraints(comp, cons);
345: add(comp);
346: } //}}}
347:
348: //{{{ addComponent() method
349: /**
350: * Adds a component to the option pane. Components are
351: * added in a vertical fashion, one per row.
352: * @param comp The component
353: * @param fill Fill parameter to GridBagConstraints
354: * @since JRefactory 2.8.02
355: */
356: public void addComponent(Component comp, int fill) {
357: GridBagConstraints cons = new GridBagConstraints();
358: cons.gridy = y++;
359: cons.gridheight = 1;
360: cons.gridwidth = cons.REMAINDER;
361: cons.fill = fill;
362: cons.anchor = GridBagConstraints.WEST;
363: cons.weightx = 1.0f;
364: cons.insets = new Insets(1, 0, 1, 0);
365:
366: gridBag.setConstraints(comp, cons);
367: add(comp);
368: } //}}}
369:
370: //{{{ addSeparator() method
371: /**
372: * Adds a separator component.
373: * @param label The separator label property
374: * @since JRefactory 2.8.02
375: */
376: public void addSeparator() {
377: addComponent(Box.createVerticalStrut(6));
378:
379: JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
380:
381: GridBagConstraints cons = new GridBagConstraints();
382: cons.gridy = y++;
383: cons.gridheight = 1;
384: cons.gridwidth = cons.REMAINDER;
385: cons.fill = GridBagConstraints.BOTH;
386: cons.anchor = GridBagConstraints.WEST;
387: cons.weightx = 1.0f;
388: //cons.insets = new Insets(1,0,1,0);
389:
390: gridBag.setConstraints(sep, cons);
391: add(sep);
392:
393: addComponent(Box.createVerticalStrut(6));
394: } //}}}
395:
396: //{{{ addSeparator() method
397: /**
398: * Adds a separator component.
399: * @param label The separator label property
400: * @since JRefactory 2.8.02
401: */
402: public void addSeparator(String label) {
403: if (y != 0) {
404: addComponent(Box.createVerticalStrut(6));
405: }
406:
407: Box box = new Box(BoxLayout.X_AXIS);
408: Box box2 = new Box(BoxLayout.Y_AXIS);
409: box2.add(Box.createGlue());
410: box2.add(new JSeparator(JSeparator.HORIZONTAL));
411: box2.add(Box.createGlue());
412: box.add(box2);
413: JLabel l = new JLabel(getIdeJavaStyleOption(label));
414: l.setMaximumSize(l.getPreferredSize());
415: box.add(l);
416: Box box3 = new Box(BoxLayout.Y_AXIS);
417: box3.add(Box.createGlue());
418: box3.add(new JSeparator(JSeparator.HORIZONTAL));
419: box3.add(Box.createGlue());
420: box.add(box3);
421:
422: GridBagConstraints cons = new GridBagConstraints();
423: cons.gridy = y++;
424: cons.gridheight = 1;
425: cons.gridwidth = cons.REMAINDER;
426: cons.fill = GridBagConstraints.BOTH;
427: cons.anchor = GridBagConstraints.WEST;
428: cons.weightx = 1.0f;
429: cons.insets = new Insets(1, 0, 1, 0);
430:
431: gridBag.setConstraints(box, cons);
432: add(box);
433: } //}}}
434:
435: //{{{ Protected members
436: /**
437: * Has the option pane been initialized?
438: */
439: protected boolean initialized;
440:
441: /**
442: * The layout manager.
443: */
444: protected GridBagLayout gridBag;
445:
446: /**
447: * The number of components already added to the layout manager.
448: */
449: protected int y;
450:
451: /**
452: * This method should create and arrange the components of the option pane
453: * and initialize the option data displayed to the user. This method
454: * is called when the option pane is first displayed, and is not
455: * called again for the lifetime of the object.
456: */
457: public void _init() {
458: }
459:
460: /**
461: * Called when the options dialog's "ok" button is clicked.
462: * This should save any properties being edited in this option
463: * pane.
464: */
465: public void _save() {
466: }
467:
468: //}}}
469:
470: //{{{ Private members
471: private String name;
472: //}}}
473: }
|