001: package jimm.datavision.gui;
002:
003: import jimm.datavision.Report;
004: import jimm.util.I18N;
005: import java.awt.BorderLayout;
006: import java.awt.event.*;
007: import javax.swing.*;
008: import javax.swing.event.ListSelectionListener;
009: import javax.swing.event.ListSelectionEvent;
010:
011: /* ================================================================ */
012:
013: /**
014: * An abstract superclass for edit windows that manipulate a list of
015: * sortable, orderable items.
016: *
017: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
018: */
019: public abstract class TwoListWin extends EditWin implements
020: ActionListener, ListSelectionListener {
021:
022: protected static final String PROTOTYPE_CELL_VALUE = "table_name.column_name";
023:
024: protected Report report;
025: protected SortedListModel leftModel;
026: protected DefaultListModel rightModel;
027: protected JList leftList;
028: protected JList rightList;
029: protected JButton addOne, addAll, removeOne, removeAll;
030: protected JButton moveUp, moveDown;
031: protected JRadioButton ascendingRButton, descendingRButton;
032:
033: /**
034: * Constructor.
035: *
036: * @param designer the window to which this dialog belongs
037: * @param title the window title
038: * @param commandNameKey the {@link I18N} command name lookup key
039: * @param rightBoxTitleKey the I18N lookup key for the right box title
040: * @param report the...um...I forgot
041: */
042: public TwoListWin(Designer designer, String title,
043: String commandNameKey, String rightBoxTitleKey,
044: Report report) {
045: super (designer, title, commandNameKey);
046: this .report = report;
047:
048: leftModel = new SortedListModel();
049: rightModel = new DefaultListModel();
050: fillListModels();
051:
052: buildWindow(rightBoxTitleKey);
053: pack();
054: setVisible(true);
055: }
056:
057: /**
058: * Builds the window contents.
059: *
060: * @param rightBoxTitleKey the I18N lookup key for the right box title
061: */
062: protected void buildWindow(String rightBoxTitleKey) {
063: // Movement buttons
064: Box box = Box.createVerticalBox();
065: box.add(Box.createVerticalStrut(32));
066:
067: addOne = new JButton(">");
068: addOne.addActionListener(this );
069: box.add(addOne);
070:
071: addAll = new JButton(">>>");
072: addAll.addActionListener(this );
073: box.add(addAll);
074:
075: removeOne = new JButton("<");
076: removeOne.addActionListener(this );
077: box.add(removeOne);
078:
079: removeAll = new JButton("<<<");
080: removeAll.addActionListener(this );
081: box.add(removeAll);
082:
083: JPanel moveButtonPanel = new JPanel();
084: moveButtonPanel.add(box);
085:
086: // "Move Up", "Move Down", "Ascending" and "Descending" radio buttons
087: box = Box.createVerticalBox();
088: box.add(Box.createVerticalStrut(32));
089:
090: moveUp = new JButton(I18N.get("TwoListWin.move_up"));
091: moveUp.addActionListener(this );
092: box.add(moveUp);
093:
094: moveDown = new JButton(I18N.get("TwoListWin.move_down"));
095: moveDown.addActionListener(this );
096: box.add(moveDown);
097:
098: box.add(Box.createVerticalStrut(32));
099:
100: ButtonGroup bg = new ButtonGroup();
101:
102: ascendingRButton = new JRadioButton(I18N.get("GUI.ascending"));
103: ascendingRButton.addActionListener(this );
104: box.add(ascendingRButton);
105: bg.add(ascendingRButton);
106:
107: descendingRButton = new JRadioButton(I18N.get("GUI.descending"));
108: descendingRButton.addActionListener(this );
109: box.add(descendingRButton);
110: bg.add(descendingRButton);
111:
112: JPanel orderButtonPanel = new JPanel();
113: orderButtonPanel.add(box);
114:
115: // Left list and label in panel
116: leftList = new JList(leftModel);
117: leftList.addListSelectionListener(this );
118: leftList.setPrototypeCellValue(PROTOTYPE_CELL_VALUE);
119: leftList.addMouseListener(new MouseAdapter() {
120: public void mouseClicked(MouseEvent e) {
121: if (e.getClickCount() == 2)
122: moveToRight(leftList.locationToIndex(e.getPoint()));
123: }
124: });
125: Box leftBox = Box.createVerticalBox();
126: leftBox.add(new JLabel(I18N.get("TwoListWin.columns")));
127: leftBox.add(new JScrollPane(leftList));
128:
129: // Right list and label in panel
130: rightList = new JList(rightModel);
131: rightList.addListSelectionListener(this );
132: rightList.setPrototypeCellValue(PROTOTYPE_CELL_VALUE);
133: rightList.addMouseListener(new MouseAdapter() {
134: public void mouseClicked(MouseEvent e) {
135: if (e.getClickCount() == 2)
136: moveToLeft(rightList.locationToIndex(e.getPoint()));
137: }
138: });
139: Box rightBox = Box.createVerticalBox();
140: rightBox.add(new JLabel(I18N.get(rightBoxTitleKey)));
141: rightBox.add(new JScrollPane(rightList));
142:
143: // Panel containing left, arrows, right, and radio buttons
144: Box center = Box.createHorizontalBox();
145: center.add(Box.createHorizontalStrut(12));
146: center.add(leftBox);
147: center.add(moveButtonPanel);
148: center.add(rightBox);
149: center.add(orderButtonPanel);
150: center.add(Box.createHorizontalStrut(12));
151:
152: // OK, Apply, Revert, and Cancel Buttons
153: JPanel buttonPanel = closeButtonPanel();
154:
155: // Add tables and buttons to window
156: getContentPane().add(center, BorderLayout.CENTER);
157: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
158:
159: // Update button states
160: adjustButtons();
161: }
162:
163: protected abstract void fillListModels();
164:
165: /**
166: * Moves the specified item number from the left to the right list
167: * and calls {@link #adjustButtons}.
168: *
169: * @param index the item number to move
170: */
171: protected void moveToRight(int index) {
172: moveToRight(index, true);
173: }
174:
175: /**
176: * Moves the specified item number from the left to the right list
177: * and optionally calls {@link #adjustButtons}.
178: *
179: * @param index the item number to move
180: * @param callAdjustButtons if <code>true</code>, make it so
181: */
182: protected void moveToRight(int index, boolean callAdjustButtons) {
183: TLWListItem item = (TLWListItem) leftModel.getElementAt(index);
184: leftModel.remove(index);
185: rightModel.addElement(item);
186: if (callAdjustButtons)
187: adjustButtons();
188: }
189:
190: /**
191: * Moves the specified item number from the right to the left list
192: * and calls {@link #adjustButtons}.
193: *
194: * @param index the item number to move
195: */
196: protected void moveToLeft(int index) {
197: moveToLeft(index, true);
198: }
199:
200: /**
201: * Moves the specified item number from the right to the left list
202: * and optionally calls {@link #adjustButtons}.
203: *
204: * @param index the item number to move
205: * @param callAdjustButtons if <code>true</code>, make it so
206: */
207: protected void moveToLeft(int index, boolean callAdjustButtons) {
208: TLWListItem item = (TLWListItem) rightModel.elementAt(index);
209: rightModel.removeElementAt(index);
210: leftModel.add(item);
211: if (callAdjustButtons)
212: adjustButtons();
213: }
214:
215: /**
216: * Handles all buttons except ascending and descending sort order.
217: */
218: public void actionPerformed(ActionEvent e) {
219: SortedListModel leftModel = (SortedListModel) leftList
220: .getModel();
221: DefaultListModel rightModel = (DefaultListModel) rightList
222: .getModel();
223:
224: String cmd = e.getActionCommand();
225: if (cmd.equals(">")) {
226: int[] indices = leftList.getSelectedIndices();
227: for (int i = 0; i < indices.length; ++i)
228: moveToRight(indices[i], false);
229: adjustButtons();
230: } else if (cmd.equals(">>>")) {
231: Object[] items = leftModel.toArray();
232: for (int i = 0; i < items.length; ++i)
233: rightModel.addElement(items[i]);
234: leftModel.removeAllElements();
235: adjustButtons();
236: } else if (cmd.equals("<")) {
237: int[] indices = rightList.getSelectedIndices();
238: for (int i = 0; i < indices.length; ++i)
239: moveToLeft(indices[i], false);
240: adjustButtons();
241: } else if (cmd.equals("<<<")) {
242: Object[] items = rightModel.toArray();
243: for (int i = 0; i < items.length; ++i)
244: leftModel.add((TLWListItem) items[i]);
245: rightModel.removeAllElements();
246: adjustButtons();
247: } else if (cmd.equals(I18N.get("TwoListWin.move_up"))) {
248: int index = rightList.getSelectedIndex();
249: Object item = rightModel.remove(index);
250: rightModel.add(index - 1, item);
251: rightList.setSelectedIndex(index - 1);
252: } else if (cmd.equals(I18N.get("TwoListWin.move_down"))) {
253: int index = rightList.getSelectedIndex();
254: Object item = rightModel.remove(index);
255: rightModel.add(index + 1, item);
256: rightList.setSelectedIndex(index + 1);
257: } else
258: super .actionPerformed(e);
259: }
260:
261: public void valueChanged(ListSelectionEvent e) {
262: adjustButtons();
263: }
264:
265: protected void adjustButtons() {
266: boolean selEmpty;
267:
268: addOne.setEnabled(!leftList.isSelectionEmpty());
269: addAll.setEnabled(leftList.getModel().getSize() > 0);
270:
271: selEmpty = rightList.isSelectionEmpty();
272: if (selEmpty) {
273: removeOne.setEnabled(false);
274: moveUp.setEnabled(false);
275: moveDown.setEnabled(false);
276: ascendingRButton.setEnabled(false);
277: descendingRButton.setEnabled(false);
278: ascendingRButton.setSelected(false);
279: descendingRButton.setSelected(false);
280: } else {
281: removeOne.setEnabled(true);
282:
283: int[] selectionIndices = rightList.getSelectedIndices();
284: if (selectionIndices.length == 1) {
285: int numGroups = rightModel.size();
286: if (numGroups > 1) {
287: int index = selectionIndices[0];
288: moveUp.setEnabled(index != 0);
289: moveDown.setEnabled(index != numGroups - 1);
290: } else {
291: moveUp.setEnabled(false);
292: moveDown.setEnabled(false);
293: }
294:
295: ascendingRButton.setEnabled(true);
296: descendingRButton.setEnabled(true);
297:
298: TLWListItem item = (TLWListItem) rightList
299: .getSelectedValue();
300: if (item.sortsAscending())
301: ascendingRButton.setSelected(true);
302: else
303: descendingRButton.setSelected(true);
304: } else {
305: moveUp.setEnabled(false);
306: moveDown.setEnabled(false);
307: ascendingRButton.setEnabled(false);
308: descendingRButton.setEnabled(false);
309: ascendingRButton.setSelected(false);
310: descendingRButton.setSelected(false);
311: }
312: }
313: removeAll.setEnabled(rightList.getModel().getSize() > 0);
314: }
315:
316: }
|