001: /* ====================================================================
002: * Copyright (c) 1998 - 2003 David F. Glasser. All rights
003: * reserved.
004: *
005: * This file is part of the QueryForm Database Tool.
006: *
007: * The QueryForm Database Tool is free software; you can redistribute it
008: * and/or modify it under the terms of the GNU General Public License as
009: * published by the Free Software Foundation; either version 2 of the
010: * License, or (at your option) any later version.
011: *
012: * The QueryForm Database Tool is distributed in the hope that it will
013: * be useful, but WITHOUT ANY WARRANTY; without even the implied
014: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
015: * See the GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with the QueryForm Database Tool; if not, write to:
019: *
020: * The Free Software Foundation, Inc.,
021: * 59 Temple Place, Suite 330
022: * Boston, MA 02111-1307 USA
023: *
024: * or visit http://www.gnu.org.
025: *
026: * ====================================================================
027: *
028: * This product includes software developed by the
029: * Apache Software Foundation (http://www.apache.org/).
030: *
031: * ====================================================================
032: */
033: package org.glasser.qform;
034:
035: import javax.swing.*;
036: import java.awt.*;
037: import java.awt.event.*;
038: import javax.swing.border.*;
039: import javax.swing.event.*;
040:
041: import org.glasser.swing.*;
042: import org.glasser.util.*;
043:
044: public class ColumnMapDialog extends JDialog implements ActionListener {
045:
046: JList list = new JList();
047:
048: JButton btnSelectAll = new JButton("Select All");
049: JButton btnOK = new JButton("OK");
050: JButton btnCancel = new JButton("Cancel");
051:
052: private int[] selections = null;
053:
054: Object[][] buttonConfig = {
055: { btnSelectAll, "A", "SELECT_ALL",
056: "Select all listed columns." },
057: { btnOK, "O", "OK", "Accept selections." },
058: { btnCancel, "C", "CANCEL", "Discard selections." } };
059:
060: public ColumnMapDialog(Frame parent) {
061:
062: super (parent);
063: setTitle("Select Visible Columns");
064:
065: JPanel cp = new JPanel();
066: cp.setLayout(new BorderLayout());
067:
068: list.setToolTipText("Ctrl-click to select/deselect columns.");
069:
070: cp.setBorder(new EmptyBorder(10, 10, 10, 10));
071:
072: JPanel centerPanel = new JPanel();
073: centerPanel.setLayout(new BorderLayout());
074: JLabel prompt = new JLabel(
075: "Ctrl-click to select/deselect columns.");
076: prompt.setBorder(new EmptyBorder(10, 0, 0, 0));
077: centerPanel.add(prompt, BorderLayout.SOUTH);
078:
079: centerPanel.add(new JScrollPane(list), BorderLayout.CENTER);
080: cp.add(centerPanel, BorderLayout.CENTER);
081:
082: JPanel buttonPanel = new JPanel();
083: buttonPanel.setBorder(new EmptyBorder(10, 0, 0, 0));
084: buttonPanel.setLayout(new FlowLayout());
085: for (int j = 0; j < buttonConfig.length; j++) {
086: JButton button = (JButton) buttonConfig[j][0];
087: button.setMnemonic(((String) buttonConfig[j][1]).charAt(0));
088: button.setActionCommand((String) buttonConfig[j][2]);
089: button.setToolTipText((String) buttonConfig[j][3]);
090: button.addActionListener(this );
091: buttonPanel.add(button);
092: }
093: cp.add(buttonPanel, BorderLayout.SOUTH);
094:
095: addKeyListener(new EnterEscapeKeyHandler(btnOK, btnCancel));
096:
097: setContentPane(cp);
098:
099: list.setVisibleRowCount(20);
100:
101: setModal(true);
102: pack();
103: }
104:
105: public void openDialog(String[] columnNames, int[] selections) {
106:
107: list.setListData(columnNames);
108:
109: if (selections != null)
110: list.setSelectedIndices(selections);
111:
112: setModal(true);
113: selections = null;
114: super .setVisible(true);
115:
116: }
117:
118: public int[] getSelections() {
119: return selections;
120: }
121:
122: public void actionPerformed(ActionEvent e) {
123: String command = e.getActionCommand();
124: if (command.equals("OK")) {
125: selections = list.getSelectedIndices();
126: if (selections.length == 0)
127: selections = null;
128: setVisible(false);
129: } else if (command.equals("CANCEL")) {
130: setVisible(false);
131: } else if (command.equals("SELECT_ALL")) {
132: int size = list.getModel().getSize();
133: if (size > 0) {
134: list.setSelectionInterval(0, size - 1);
135: }
136: }
137: }
138:
139: }
|