001: /*
002: *JSIndentOptionPane.java - JavaStyle indenting options panel
003: *Copyright (C) 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.Dimension;
025: import java.awt.BorderLayout;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import javax.swing.JCheckBox;
029: import javax.swing.JLabel;
030: import javax.swing.JPanel;
031: import javax.swing.JTextArea;
032: import javax.swing.JTextPane;
033: import javax.swing.JTextField;
034: import javax.swing.JComponent;
035: import javax.swing.JRadioButton;
036: import javax.swing.AbstractButton;
037: import javax.swing.BoxLayout;
038: import javax.swing.event.ChangeListener;
039: import javax.swing.event.ChangeEvent;
040: import org.acm.seguin.ide.common.options.PropertiesFile;
041: import org.acm.seguin.ide.common.IDEPlugin;
042:
043: /**
044: * @author Mike Atkinson (<a
045: * href="mailto:javastyle@ladyshot.demon.co.uk">
046: * Mike@ladyshot.demon.co.uk</a> )
047: * @created 02 September 2003
048: * @version $Version: $
049: * @since 1.5
050: */
051: public class SelectedPanel extends JPanel {
052: private JComponent component;
053: private JLabel optionLabel = null;
054: private PropertiesFile props;
055: private String option;
056: private JCheckBox cb = null;
057:
058: /**
059: * Constructor for the SelectedPanel object
060: *
061: * @param optionPane Description of the Parameter
062: * @param props Description of the Parameter
063: * @param project Description of the Parameter
064: * @param option Description of the Parameter
065: * @param defaultOption Description of the Parameter
066: * @param label Description of the Parameter
067: * @param comp Description of the Parameter
068: */
069: public SelectedPanel(JSHelpOptionPane optionPane,
070: PropertiesFile props, String project, String option,
071: String defaultOption, String label, JComponent comp) {
072: this .props = props;
073: this .option = option;
074:
075: String fullLabel = (label == null) ? null
076: : ("options.javastyle." + label);
077: boolean selected = false;
078:
079: String value = null;
080:
081: if (option != null) {
082: if (defaultOption != null) {
083: value = props.getString(option, defaultOption);
084: } else {
085: value = props.getString(option);
086: }
087: if (value == null) {
088: System.out.println("WARNING: Option Not Found option="
089: + option);
090: }
091: }
092: if (comp instanceof JTextField && value != null
093: && value.length() > 50) {
094: component = new JTextArea(1, 50);
095: ((JTextArea) component).setLineWrap(true);
096: component.setBorder(comp.getBorder());
097: component.setFont(comp.getFont());
098: //value = value.substring(0,50);
099: //component = comp;
100: } else {
101: component = comp;
102: }
103: setLayout(new BoxLayout(this , BoxLayout.X_AXIS));
104: if (!"default".equals(project)) {
105: selected = props.isLocalProperty(option);
106: cb = new JCheckBox(" ");
107: cb.setSelected(selected);
108: add(cb);
109: cb.addChangeListener(new ChangeListener() {
110: public void stateChanged(ChangeEvent e) {
111: component.setEnabled(cb.isSelected());
112: if (optionLabel != null) {
113: optionLabel.setEnabled(cb.isSelected());
114: }
115: }
116: });
117: optionPane.addHelpFor(cb, "project.local");
118: } else {
119: selected = true;
120: }
121: component.setEnabled(selected);
122:
123: if (component instanceof AbstractButton) {
124: String text = IDEPlugin.getProperty(fullLabel);
125:
126: if (text == null) {
127: System.out
128: .println("WARNING: IDEPlugin.getProperty() not found fullLabel="
129: + fullLabel);
130: }
131: ((AbstractButton) component).setText(text);
132: if (option != null) {
133: ((AbstractButton) component).setSelected(props
134: .getBoolean(option));
135: }
136: } else if (component instanceof JMouseComboBox) {
137: ((JMouseComboBox) component).setEditable(false);
138: if (value != null) {
139: ((JMouseComboBox) component).setSelectedItem(value);
140: }
141: } else if (component instanceof JTextField) {
142: if (value != null) {
143: ((JTextField) component).setText(value);
144: }
145: } else if (component instanceof JTextArea) {
146: if (value != null) {
147: ((JTextArea) component).setText(value);
148: }
149: }
150: if (label != null && component != null) {
151: optionPane.addHelpFor(component, label);
152: }
153: if (component instanceof AbstractButton) {
154: add(component);
155: optionPane.addComponent(this );
156: } else if (fullLabel != null) {
157: optionLabel = new JLabel(IDEPlugin.getProperty(fullLabel));
158: optionLabel.setEnabled(selected);
159: add(optionLabel);
160: optionPane.addComponent(this , component);
161: } else {
162: optionPane.addComponent(this , component);
163: }
164: }
165:
166: /**
167: * Gets the PropertiesFile attribute of the SelectedPanel
168: * object
169: *
170: * @return The PropertiesFile value
171: */
172: public PropertiesFile getPropertiesFile() {
173: return props;
174: }
175:
176: /** Description of the Method */
177: public void save() {
178: if (localAvailable()) {
179: if (component instanceof AbstractButton) {
180: props.setString(option, ((AbstractButton) component)
181: .isSelected() ? "true" : "false");
182: } else if (component instanceof JTextField) {
183: props.setString(option, ((JTextField) component)
184: .getText());
185: } else if (component instanceof JTextArea) {
186: props.setString(option, ((JTextArea) component)
187: .getText());
188: } else if (component instanceof JMouseComboBox) {
189: props.setString(option, ((JMouseComboBox) component)
190: .getSelectedItem().toString());
191: } else {
192: }
193: } else if (localDelete()) {
194: props.deleteKey(option);
195: }
196: }
197:
198: /**
199: * Description of the Method
200: *
201: * @param value Description of the Parameter
202: */
203: public void save(String value) {
204: if (localAvailable()) {
205: System.out.println("SelectedPanel.save(" + value + ")");
206: System.out.println(" -props.setString(" + option + ","
207: + value + ")");
208: props.setString(option, value);
209: } else if (localDelete()) {
210: System.out.println(" -props.deleteKey(" + option + ")");
211: props.deleteKey(option);
212: }
213: }
214:
215: /**
216: * Description of the Method
217: *
218: * @param defaultValue Description of Parameter
219: * @param minValue Description of Parameter
220: */
221: public void saveInt(int defaultValue, int minValue) {
222: int val = defaultValue;
223:
224: try {
225: val = Integer.parseInt(((JTextField) component).getText());
226: } catch (NumberFormatException nfex) {
227: }
228: if (val < minValue) {
229: val = minValue;
230: }
231: save(Integer.toString(val));
232: }
233:
234: /**
235: * Description of the Method
236: *
237: * @return Description of the Returned Value
238: */
239: public boolean localAvailable() {
240: return (cb == null || cb.isSelected());
241: }
242:
243: /**
244: * Description of the Method
245: *
246: * @return Description of the Returned Value
247: */
248: public boolean localDelete() {
249: return (props.isLocalProperty(option) && !cb.isSelected());
250: }
251: }
|