View Javadoc

1   /*   Copyright (C) 2003 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   */
17  
18  package com.finalist.jaggenerator;
19  
20  import java.util.*;
21  import java.util.List;
22  import java.awt.*;
23  import java.awt.event.WindowAdapter;
24  import java.awt.event.WindowEvent;
25  import java.awt.event.ActionListener;
26  import java.awt.event.ActionEvent;
27  import javax.swing.*;
28  
29  /***
30   *
31   * @author  hillie
32   */
33  public class SelectTablesDialog extends JDialog {
34     private DefaultListModel listModel = new DefaultListModel();
35  
36     private static final ArrayList tableList = new ArrayList();
37     private static final ArrayList alreadySelected = new ArrayList();
38  
39  
40     /*** Creates new form SelectTablesDialog */
41     public SelectTablesDialog(JagGenerator parent) {
42        super(parent, true);
43        initComponents();
44        this.setTitle("Select tables..");
45        setBounds(50, 10, 200, 700);
46  
47        ArrayList tables = DatabaseUtils.getTables();
48        if (tables != null) {
49           for (Iterator it = tables.iterator(); it.hasNext();) {
50              String table = (String) it.next();
51              if (!alreadySelected.contains(table)) {
52                 listModel.addElement(table);
53              }
54           }
55           list.setModel(listModel);
56        }
57     }
58  
59     /***
60      * Once the user has selected tables using this dialog, this method returns the selected tables.
61      * @return the ArrayList of selected table names (String), never <code>null</code>.
62      */
63     public static ArrayList getTablelist() {
64        return tableList;
65     }
66  
67     /***
68      * By adding table names to this list, those tables are excluded from appearing in future dialogues.
69      *
70      * @return
71      */
72     public static List getAlreadyselected() {
73        return alreadySelected;
74     }
75  
76     /***
77      * clears all lists.
78      */
79     public static void clear() {
80        tableList.clear();
81        alreadySelected.clear();
82     }
83  
84     /*** This method is called from within the constructor to
85      * initialize the form.
86      * WARNING: Do NOT modify this code. The content of this method is
87      * always regenerated by the Form Editor.
88      */
89      private void initComponents() {//GEN-BEGIN:initComponents
90          java.awt.GridBagConstraints gridBagConstraints;
91  
92          jScrollPane1 = new javax.swing.JScrollPane();
93          list = new javax.swing.JList();
94          jPanel1 = new javax.swing.JPanel();
95          selectButton = new javax.swing.JButton();
96          cancelButton = new javax.swing.JButton();
97  
98          getContentPane().setLayout(new java.awt.GridBagLayout());
99  
100         addWindowListener(new java.awt.event.WindowAdapter() {
101             public void windowClosing(java.awt.event.WindowEvent evt) {
102                 closeDialog(evt);
103             }
104         });
105 
106         list.addMouseListener(new java.awt.event.MouseAdapter() {
107             public void mouseClicked(java.awt.event.MouseEvent evt) {
108                 listMouseClicked(evt);
109             }
110         });
111 
112         jScrollPane1.setViewportView(list);
113 
114         gridBagConstraints = new java.awt.GridBagConstraints();
115         gridBagConstraints.gridx = 0;
116         gridBagConstraints.gridy = 0;
117         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
118         gridBagConstraints.weightx = 1.0;
119         gridBagConstraints.weighty = 1.0;
120         getContentPane().add(jScrollPane1, gridBagConstraints);
121 
122         selectButton.setText("Select");
123         selectButton.setSelected(true);
124         selectButton.addActionListener(new java.awt.event.ActionListener() {
125             public void actionPerformed(java.awt.event.ActionEvent evt) {
126                 selectButtonActionPerformed(evt);
127             }
128         });
129 
130         jPanel1.add(selectButton);
131 
132         cancelButton.setText("Cancel");
133         cancelButton.addActionListener(new java.awt.event.ActionListener() {
134             public void actionPerformed(java.awt.event.ActionEvent evt) {
135                 cancelButtonActionPerformed(evt);
136             }
137         });
138 
139         jPanel1.add(cancelButton);
140 
141         gridBagConstraints = new java.awt.GridBagConstraints();
142         gridBagConstraints.gridx = 0;
143         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
144         getContentPane().add(jPanel1, gridBagConstraints);
145 
146         pack();
147     }//GEN-END:initComponents
148 
149     private void listMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listMouseClicked
150        if (evt.getClickCount() > 1) {
151           selectButtonActionPerformed(null);
152        }
153     }//GEN-LAST:event_listMouseClicked
154 
155     private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
156         closeDialog(null);
157     }//GEN-LAST:event_cancelButtonActionPerformed
158 
159 
160    private void selectButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_selectButtonActionPerformed
161       Object[] tables = list.getSelectedValues();
162       evt = null;
163       tableList.clear();
164       tableList.addAll(Arrays.asList(tables));
165       dispose();
166    }//GEN-LAST:event_selectButtonActionPerformed
167 
168 
169    /*** Closes the dialog */
170    private void closeDialog(WindowEvent evt) {//GEN-FIRST:event_closeDialog
171       tableList.clear();
172       evt = null;
173       this.dispose();
174    }//GEN-LAST:event_closeDialog
175 
176     // Variables declaration - do not modify//GEN-BEGIN:variables
177     private javax.swing.JButton cancelButton;
178     private javax.swing.JPanel jPanel1;
179     private javax.swing.JScrollPane jScrollPane1;
180     private javax.swing.JList list;
181     private javax.swing.JButton selectButton;
182    // End of variables declaration//GEN-END:variables
183 
184 
185 }