1 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 ; 43 import java.util.Vector ; 44 45 46 51 public abstract class VectorOptionComponent<T> extends OptionComponent<Vector <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; 59 protected JButton _moveDownButton; 60 protected DefaultListModel _listModel; 61 protected static final int NUM_ROWS = 5; 62 protected static final int PIXELS_PER_ROW = 18; 63 64 70 public VectorOptionComponent(VectorOption<T> opt, String text, Frame parent) { 71 super(opt, text, parent); 72 73 _listModel = new DefaultListModel(); 75 _list = new JList(_listModel); 76 _list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 77 78 resetToCurrent(); 79 80 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()) { if (index > 0) _list.setSelectedIndex(index - 1); 99 notifyChangeListeners(); 100 } 101 else { 102 _list.setSelectedIndex(index); 103 notifyChangeListeners(); 104 } 105 } 106 } 107 }); 108 109 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 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 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 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 (5,5,5,5)); 144 _buttonPanel.setLayout(new BoxLayout(_buttonPanel, BoxLayout.X_AXIS)); 145 146 _buttonPanel.add(Box.createHorizontalGlue()); 147 _addButtons(); _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 160 protected void _addButtons() { 161 _buttonPanel.add(_addButton); 162 _buttonPanel.add(_removeButton); 163 } 164 165 168 public VectorOptionComponent(VectorOption<T> opt, String text, 169 Frame parent, String description) { 170 this(opt, text, parent); 171 setDescription(description); 172 } 173 174 178 public void setDescription(String description) { 179 _listScrollPane.setToolTipText(description); 180 _list.setToolTipText(description); 181 _label.setToolTipText(description); 182 } 183 184 188 public boolean updateConfig() { 189 Vector <T> current = getValue(); 190 DrJava.getConfig().setSetting(_option, current); 191 resetToCurrent(); 192 return true; 193 } 194 195 200 public Vector <T> getValue() { 201 Vector <T> current = new Vector <T>(); 202 for (int i = 0; i < _listModel.getSize(); i++) { 203 204 @SuppressWarnings ("unchecked") T element = (T) _listModel.getElementAt(i); 205 current.add(element); 206 } 207 return current; 208 } 209 210 211 public void setValue(Vector <T> value) { 212 _listModel.clear(); 213 for (int i = 0; i < value.size(); i++) { 214 _listModel.addElement(value.elementAt(i)); 215 } 216 } 217 218 219 public JComponent getComponent() { return _panel; } 220 221 222 protected abstract Action _getAddAction(); 223 } 224 | Popular Tags |