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 edu.rice.cs.drjava.config.*;
040: import edu.rice.cs.drjava.*;
041:
042: import java.awt.*;
043: import java.awt.event.*;
044: import javax.swing.*;
045: import javax.swing.border.EmptyBorder;
046: import java.util.Vector;
047:
048: /** Graphical form of a VectorOption for the Extra Classpath option. Uses a file chooser for each String element.
049: * TODO: define a static make method that adds buttons so that moveUp and moveDown button definitions can be moved
050: * to subclass
051: * @version $Id: VectorOptionComponent.java 4255 2007-08-28 19:17:37Z mgricken $
052: */
053: public abstract class VectorOptionComponent<T> extends
054: OptionComponent<Vector<T>> implements OptionConstants {
055: protected JScrollPane _listScrollPane;
056: protected JPanel _panel;
057: protected JList _list;
058: protected JPanel _buttonPanel;
059: protected JButton _addButton;
060: protected JButton _removeButton;
061: protected JButton _moveUpButton; /* Only used in VectorFileOptionComponent subclass. */
062: protected JButton _moveDownButton; /* Only used in VectorFileOptionComponent subclass. */
063: protected DefaultListModel _listModel;
064: protected static final int NUM_ROWS = 5;
065: protected static final int PIXELS_PER_ROW = 18;
066:
067: /**
068: * Builds a new VectorOptionComponent.
069: * @param opt the option
070: * @param text the label to display
071: * @param parent the parent frame
072: */
073: public VectorOptionComponent(VectorOption<T> opt, String text,
074: Frame parent) {
075: super (opt, text, parent);
076:
077: //set up list
078: _listModel = new DefaultListModel();
079: _list = new JList(_listModel);
080: _list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
081:
082: resetToCurrent();
083:
084: /*
085: Vector v = DrJava.getConfig().getSetting(_option);
086: String[] array = new String[v.size()];
087: v.copyInto(array);
088: //_list.setListData(array);
089: for (int i = 0; i < array.length; i++) {
090: _listModel.add(array[i]);
091: }
092: */
093:
094: _addButton = new JButton(_getAddAction());
095: _removeButton = new JButton(new AbstractAction("Remove") {
096: public void actionPerformed(ActionEvent ae) {
097: if (!_list.isSelectionEmpty()) {
098: int index = _list.getSelectedIndex();
099: _listModel.remove(index);
100: if (index == _listModel.getSize()) { // we removed the last element
101: if (index > 0) // and there's more than one element in the list
102: _list.setSelectedIndex(index - 1);
103: notifyChangeListeners();
104: } else {
105: _list.setSelectedIndex(index);
106: notifyChangeListeners();
107: }
108: }
109: }
110: });
111:
112: /* Only used in VectorFileOptionComponent subclass */
113: _moveUpButton = new JButton(new AbstractAction("Move Up") {
114: public void actionPerformed(ActionEvent ae) {
115: if (!_list.isSelectionEmpty()) {
116: int index = _list.getSelectedIndex();
117: if (index > 0) {
118: Object o = _listModel.getElementAt(index);
119: _listModel.remove(index);
120: _listModel.insertElementAt(o, index - 1);
121: _list.setSelectedIndex(index - 1);
122: notifyChangeListeners();
123: }
124: }
125: }
126: });
127:
128: /* Only used in VectorFileOptionComponent subclass */
129: _moveDownButton = new JButton(new AbstractAction("Move Down") {
130: public void actionPerformed(ActionEvent ae) {
131: if (!_list.isSelectionEmpty()) {
132: int index = _list.getSelectedIndex();
133: if (index < _listModel.getSize() - 1) {
134: Object o = _listModel.getElementAt(index);
135: _listModel.remove(index);
136: _listModel.insertElementAt(o, index + 1);
137: _list.setSelectedIndex(index + 1);
138: notifyChangeListeners();
139: }
140: }
141: }
142: });
143:
144: _buttonPanel = new JPanel();
145: _buttonPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
146: _buttonPanel.setLayout(new BoxLayout(_buttonPanel,
147: BoxLayout.X_AXIS));
148:
149: _buttonPanel.add(Box.createHorizontalGlue());
150: _addButtons(); // all buttons needs to be added consecutively as a group for glue to work properly
151: _buttonPanel.add(Box.createHorizontalGlue());
152:
153: _listScrollPane = new JScrollPane(_list,
154: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
155: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
156: _panel = new JPanel(new BorderLayout());
157: _panel.add(_listScrollPane, BorderLayout.CENTER);
158: _panel.add(_buttonPanel, BorderLayout.SOUTH);
159:
160: _listScrollPane.setPreferredSize(new Dimension(0, NUM_ROWS
161: * PIXELS_PER_ROW));
162: }
163:
164: /** Adds buttons to _buttonPanel */
165: protected void _addButtons() {
166: _buttonPanel.add(_addButton);
167: _buttonPanel.add(_removeButton);
168: }
169:
170: /**
171: * Constructor that allows for a tooltip description.
172: */
173: public VectorOptionComponent(VectorOption<T> opt, String text,
174: Frame parent, String description) {
175: this (opt, text, parent);
176: setDescription(description);
177: }
178:
179: /**
180: * Sets the tooltip description text for this option.
181: * @param description the tooltip text
182: */
183: public void setDescription(String description) {
184: _listScrollPane.setToolTipText(description);
185: _list.setToolTipText(description);
186: _label.setToolTipText(description);
187: }
188:
189: /**
190: * Updates the config object with the new setting.
191: * @return true if the new value is set successfully
192: */
193: public boolean updateConfig() {
194: Vector<T> current = getValue();
195: DrJava.getConfig().setSetting(_option, current);
196: resetToCurrent();
197: return true;
198: }
199:
200: /**
201: * Accessor to the current contents of the list.
202: * @return The contents of the list in this component
203: * in the form of a Vector.
204: */
205: public Vector<T> getValue() {
206: Vector<T> current = new Vector<T>();
207: for (int i = 0; i < _listModel.getSize(); i++) {
208: /* javax.swing.DefaultListModel should be generified! */
209: @SuppressWarnings("unchecked")
210: T element = (T) _listModel.getElementAt(i);
211: current.add(element);
212: }
213: return current;
214: }
215:
216: /** Displays the given value. */
217: public void setValue(Vector<T> value) {
218: _listModel.clear();
219: for (int i = 0; i < value.size(); i++) {
220: _listModel.addElement(value.elementAt(i));
221: }
222: }
223:
224: /** Return's this OptionComponent's configurable component. */
225: public JComponent getComponent() {
226: return _panel;
227: }
228:
229: /** Gets an action that adds a component to the set of options. */
230: protected abstract Action _getAddAction();
231: }
|