KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > config > VectorOptionComponent


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui.config;
35
36 import edu.rice.cs.drjava.config.*;
37 import edu.rice.cs.drjava.*;
38
39 import java.awt.*;
40 import java.awt.event.*;
41 import javax.swing.*;
42 import javax.swing.border.EmptyBorder JavaDoc;
43 import java.util.Vector JavaDoc;
44
45
46 /** Graphical form of a VectorOption for the Extra Classpath option. Uses a file chooser for each String element.
47  * TODO: define a static make method that adds buttons so that moveUp and moveDown button definitions can be moved
48  * to subclass
49  * @version $Id: VectorOptionComponent.java 4026 2006-10-31 15:50:16Z rcartwright $
50  */

51 public abstract class VectorOptionComponent<T> extends OptionComponent<Vector JavaDoc<T>> implements OptionConstants {
52   protected JScrollPane _listScrollPane;
53   protected JPanel _panel;
54   protected JList _list;
55   protected JPanel _buttonPanel;
56   protected JButton _addButton;
57   protected JButton _removeButton;
58   protected JButton _moveUpButton; /* Only used in VectorFileOptionComponent subclass. */
59   protected JButton _moveDownButton; /* Only used in VectorFileOptionComponent subclass. */
60   protected DefaultListModel _listModel;
61   protected static final int NUM_ROWS = 5;
62   protected static final int PIXELS_PER_ROW = 18;
63
64   /**
65    * Builds a new VectorOptionComponent.
66    * @param opt the option
67    * @param text the label to display
68    * @param parent the parent frame
69    */

70   public VectorOptionComponent(VectorOption<T> opt, String JavaDoc text, Frame parent) {
71     super(opt, text, parent);
72
73     //set up list
74
_listModel = new DefaultListModel();
75     _list = new JList(_listModel);
76     _list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
77
78     resetToCurrent();
79
80     /*
81     Vector v = DrJava.getConfig().getSetting(_option);
82     String[] array = new String[v.size()];
83     v.copyInto(array);
84     //_list.setListData(array);
85     for (int i = 0; i < array.length; i++) {
86       _listModel.add(array[i]);
87     }
88     */

89
90     _addButton = new JButton(_getAddAction());
91     _removeButton = new JButton(new AbstractAction("Remove") {
92       public void actionPerformed(ActionEvent ae) {
93         if (!_list.isSelectionEmpty()) {
94           int index = _list.getSelectedIndex();
95           _listModel.remove(index);
96           if (index == _listModel.getSize()) { // we removed the last element
97
if (index > 0) // and there's more than one element in the list
98
_list.setSelectedIndex(index - 1);
99             notifyChangeListeners();
100           }
101           else {
102             _list.setSelectedIndex(index);
103             notifyChangeListeners();
104           }
105         }
106       }
107     });
108     
109     /* Only used in VectorFileOptionComponent subclass */
110     _moveUpButton = new JButton(new AbstractAction("Move Up") {
111       public void actionPerformed(ActionEvent ae) {
112         if (!_list.isSelectionEmpty()) {
113           int index = _list.getSelectedIndex();
114           if (index > 0) {
115             Object JavaDoc o = _listModel.getElementAt(index);
116             _listModel.remove(index);
117             _listModel.insertElementAt(o, index - 1);
118             _list.setSelectedIndex(index - 1);
119             notifyChangeListeners();
120           }
121         }
122       }
123     });
124
125     /* Only used in VectorFileOptionComponent subclass */
126     _moveDownButton = new JButton(new AbstractAction("Move Down") {
127       public void actionPerformed(ActionEvent ae) {
128         if (!_list.isSelectionEmpty()) {
129           int index = _list.getSelectedIndex();
130           if (index < _listModel.getSize() - 1) {
131             Object JavaDoc o = _listModel.getElementAt(index);
132             _listModel.remove(index);
133             _listModel.insertElementAt(o, index + 1);
134             _list.setSelectedIndex(index + 1);
135             notifyChangeListeners();
136           }
137         }
138       }
139     });
140     
141     
142     _buttonPanel = new JPanel();
143     _buttonPanel.setBorder(new EmptyBorder JavaDoc(5,5,5,5));
144     _buttonPanel.setLayout(new BoxLayout(_buttonPanel, BoxLayout.X_AXIS));
145     
146     _buttonPanel.add(Box.createHorizontalGlue());
147     _addButtons(); // all buttons needs to be added consecutively as a group for glue to work properly
148
_buttonPanel.add(Box.createHorizontalGlue());
149
150     _listScrollPane = new JScrollPane(_list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
151                                       JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
152     _panel = new JPanel(new BorderLayout());
153     _panel.add(_listScrollPane, BorderLayout.CENTER);
154     _panel.add(_buttonPanel, BorderLayout.SOUTH);
155
156     _listScrollPane.setPreferredSize(new Dimension(0, NUM_ROWS * PIXELS_PER_ROW));
157   }
158
159   /** Adds buttons to _buttonPanel */
160   protected void _addButtons() {
161     _buttonPanel.add(_addButton);
162     _buttonPanel.add(_removeButton);
163   }
164   
165   /**
166    * Constructor that allows for a tooltip description.
167    */

168   public VectorOptionComponent(VectorOption<T> opt, String JavaDoc text,
169                                Frame parent, String JavaDoc description) {
170     this(opt, text, parent);
171     setDescription(description);
172   }
173
174   /**
175    * Sets the tooltip description text for this option.
176    * @param description the tooltip text
177    */

178   public void setDescription(String JavaDoc description) {
179     _listScrollPane.setToolTipText(description);
180     _list.setToolTipText(description);
181     _label.setToolTipText(description);
182   }
183
184   /**
185    * Updates the config object with the new setting.
186    * @return true if the new value is set successfully
187    */

188   public boolean updateConfig() {
189     Vector JavaDoc<T> current = getValue();
190     DrJava.getConfig().setSetting(_option, current);
191     resetToCurrent();
192     return true;
193   }
194   
195   /**
196    * Accessor to the current contents of the list.
197    * @return The contents of the list in this component
198    * in the form of a Vector.
199    */

200   public Vector JavaDoc<T> getValue() {
201     Vector JavaDoc<T> current = new Vector JavaDoc<T>();
202     for (int i = 0; i < _listModel.getSize(); i++) {
203       /* javax.swing.DefaultListModel should be generified! */
204       @SuppressWarnings JavaDoc("unchecked") T element = (T) _listModel.getElementAt(i);
205       current.add(element);
206     }
207     return current;
208   }
209
210   /** Displays the given value. */
211   public void setValue(Vector JavaDoc<T> value) {
212     _listModel.clear();
213     for (int i = 0; i < value.size(); i++) {
214       _listModel.addElement(value.elementAt(i));
215     }
216   }
217
218   /** Return's this OptionComponent's configurable component. */
219   public JComponent getComponent() { return _panel; }
220
221   /** Gets an action that adds a component to the set of options. */
222   protected abstract Action _getAddAction();
223 }
224
Popular Tags