001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.ui.config;
038:
039: import javax.swing.*;
040: import edu.rice.cs.drjava.config.*;
041: import edu.rice.cs.drjava.*;
042: import java.awt.*;
043: import java.awt.event.*;
044: import java.util.Iterator;
045:
046: /**
047: * This component displays all legal choices for a ForcedChoiceOption as a list
048: * of radio buttons. The radio buttons are placed within a framed panel titled
049: * with the OptionComponent's label.
050: * @version $Id: ForcedChoiceOptionComponent.java 4255 2007-08-28 19:17:37Z mgricken $
051: */
052: public class ForcedChoiceOptionComponent extends
053: OptionComponent<String> {
054: private JComboBox _comboBox;
055:
056: /**
057: * Main constructor builds a panel containing a set of radio buttons for the
058: * legal values of the ForcedChoiceOption.
059: */
060: public ForcedChoiceOptionComponent(ForcedChoiceOption option,
061: String labelText, Frame parent) {
062: super (option, labelText, parent);
063:
064: // Build the combo box from the Iterator of legal values
065: Iterator<String> values = option.getLegalValues();
066: _comboBox = new JComboBox();
067:
068: _comboBox.addActionListener(new ActionListener() {
069: public void actionPerformed(ActionEvent e) {
070: notifyChangeListeners();
071: }
072: });
073:
074: while (values.hasNext()) {
075: String currValue = values.next();
076: _comboBox.addItem(currValue);
077: }
078:
079: resetToCurrent(DrJava.getConfig().getSetting(_option));
080: }
081:
082: /**
083: * Constructor that allows for a tooltip description.
084: */
085: public ForcedChoiceOptionComponent(ForcedChoiceOption option,
086: String labelText, Frame parent, String description) {
087: this (option, labelText, parent);
088: setDescription(description);
089: }
090:
091: /**
092: * Sets the tooltip description text for this option.
093: * @param description the tooltip text
094: */
095: public void setDescription(String description) {
096: _comboBox.setToolTipText(description);
097: _label.setToolTipText(description);
098: }
099:
100: /**
101: * Selects the radio button corresponding to the current config options.
102: */
103: public void resetToCurrent(String current) {
104: _comboBox.setSelectedItem(current);
105:
106: // String current = DrJava.getConfig().getSetting(_option);
107: // Iterator values = ((ForcedChoiceOption)_option).getLegalValues();
108: // int i = 0;
109: //
110: // while(values.hasNext()) {
111: // if (current.equals(values.next())) {
112: //
113: // return;
114: // }
115: // i++;
116: // }
117: }
118:
119: /**
120: * Return's this OptionComponent's configurable component.
121: */
122: public JComponent getComponent() {
123: return _comboBox;
124: }
125:
126: /**
127: * Updates the config object with the new setting.
128: * @return true if the new value is set successfully
129: */
130: public boolean updateConfig() {
131: String oldValue = DrJava.getConfig().getSetting(_option);
132: String newValue = _comboBox.getSelectedItem().toString();
133:
134: if (!newValue.equals(oldValue)) {
135: DrJava.getConfig().setSetting(_option, newValue);
136: }
137:
138: return true;
139: }
140:
141: /**
142: * Displays the given value.
143: */
144: public void setValue(String value) {
145: resetToCurrent(value);
146: }
147: }
|