KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > dlg > AddIndexDialog


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.explorer.dlg;
21
22 import java.awt.*;
23 import java.awt.event.*;
24 import java.util.*;
25 import javax.swing.*;
26 import javax.swing.border.EmptyBorder JavaDoc;
27 import org.openide.DialogDescriptor;
28 import org.openide.NotifyDescriptor;
29 import org.openide.DialogDisplayer;
30 import org.openide.util.NbBundle;
31 import org.netbeans.lib.ddl.impl.CreateIndex;
32 import org.netbeans.lib.ddl.impl.Specification;
33 import org.netbeans.lib.ddl.*;
34 import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
35 import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
36 import org.netbeans.modules.db.explorer.*;
37 import org.openide.awt.Mnemonics;
38
39 public class AddIndexDialog {
40     boolean result = false;
41     Dialog dialog = null;
42     JTextField namefld;
43     CheckBoxListener cbxlistener;
44     JCheckBox cbx_uq;
45     
46     public AddIndexDialog(Collection columns, final Specification spec, final DatabaseNodeInfo info) {
47         try {
48             ResourceBundle bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle"); //NOI18N
49
JPanel pane = new JPanel();
50             pane.setBorder(new EmptyBorder JavaDoc(new Insets(5,5,5,5)));
51             GridBagLayout layout = new GridBagLayout();
52             GridBagConstraints con = new GridBagConstraints ();
53             pane.setLayout (layout);
54
55             // Index name
56

57             JLabel label = new JLabel();
58             Mnemonics.setLocalizedText(label, bundle.getString("AddIndexName")); //NOI18N
59
label.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_AddIndexNameA11yDesc"));
60             con.anchor = GridBagConstraints.WEST;
61             con.insets = new java.awt.Insets JavaDoc (2, 2, 2, 2);
62             con.gridx = 0;
63             con.gridy = 0;
64             layout.setConstraints(label, con);
65             pane.add(label);
66
67             // Index name field
68

69             con.fill = GridBagConstraints.HORIZONTAL;
70             con.weightx = 1.0;
71             con.gridx = 1;
72             con.gridy = 0;
73             con.insets = new java.awt.Insets JavaDoc (2, 2, 2, 2);
74             namefld = new JTextField(35);
75             namefld.setToolTipText(bundle.getString("ACS_AddIndexNameTextFieldA11yDesc"));
76             namefld.getAccessibleContext().setAccessibleName(bundle.getString("ACS_AddIndexNameTextFieldA11yName"));
77             label.setLabelFor(namefld);
78             layout.setConstraints(namefld, con);
79             pane.add(namefld);
80
81             // Unique/Non-unique
82

83             JLabel label_uq = new JLabel(bundle.getString("AddUniqueIndex")); //NOI18N
84
label.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_AddUniqueIndexA11yDesc"));
85             con.weightx = 0.0;
86             con.anchor = GridBagConstraints.WEST;
87             con.insets = new java.awt.Insets JavaDoc (2, 2, 2, 2);
88             con.gridx = 0;
89             con.gridy = 1;
90             layout.setConstraints(label_uq, con);
91             pane.add(label_uq);
92
93             con.fill = GridBagConstraints.HORIZONTAL;
94             con.weightx = 1.0;
95             con.gridx = 1;
96             con.gridy = 1;
97             con.insets = new java.awt.Insets JavaDoc (2, 2, 2, 2);
98             cbx_uq = new JCheckBox();
99             Mnemonics.setLocalizedText(cbx_uq, bundle.getString("Unique"));
100             cbx_uq.setToolTipText(bundle.getString("ACS_UniqueA11yDesc"));
101             label_uq.setLabelFor(cbx_uq);
102             layout.setConstraints(cbx_uq, con);
103             pane.add(cbx_uq);
104
105             // Items list title
106

107             label = new JLabel(bundle.getString("AddIndexLabel")); //NOI18N
108
label.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_AddIndexLabelA11yDesc"));
109             con.weightx = 0.0;
110             con.anchor = GridBagConstraints.WEST;
111             con.insets = new java.awt.Insets JavaDoc (2, 2, 2, 2);
112             con.gridx = 0;
113             con.gridy = 2;
114             con.gridwidth = 2;
115             layout.setConstraints(label, con);
116             pane.add(label);
117
118             // Items list
119

120             JPanel subpane = new JPanel();
121             label.setLabelFor(subpane);
122             int colcount = columns.size();
123             colcount = (colcount%2==0?colcount/2:colcount/2+1);
124             GridLayout sublayout = new GridLayout(colcount,2);
125             subpane.setBorder(new EmptyBorder JavaDoc(new Insets(5,5,5,5)));
126             subpane.setLayout(sublayout);
127
128             cbxlistener = new CheckBoxListener(columns);
129             Iterator iter = columns.iterator();
130             while(iter.hasNext()) {
131                 String JavaDoc colname = (String JavaDoc)iter.next();
132                 JCheckBox cbx = new JCheckBox(colname);
133                 cbx.setName(colname);
134                 cbx.setToolTipText(colname);
135                 cbx.addActionListener(cbxlistener);
136                 subpane.add(cbx);
137             }
138
139             con.weightx = 1.0;
140             con.weighty = 1.0;
141             con.gridwidth = 2;
142             con.fill = GridBagConstraints.BOTH;
143             con.insets = new java.awt.Insets JavaDoc (0, 0, 0, 0);
144             con.gridx = 0;
145             con.gridy = 3;
146             JScrollPane spane = new JScrollPane(subpane);
147             layout.setConstraints(spane, con);
148             pane.add(spane);
149             pane.getAccessibleContext().setAccessibleName(bundle.getString("ACS_AddIndexDialogA11yName")); // NOI18N
150
pane.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_AddIndexDialogA11yDesc")); // NOI18N
151

152             final String JavaDoc tablename = (String JavaDoc)info.get(DatabaseNode.TABLE);
153
154             ActionListener listener = new ActionListener() {
155                 public void actionPerformed(ActionEvent event) {
156                     
157                     if (event.getSource() == DialogDescriptor.OK_OPTION) {
158                         
159                         try {
160                             result = false;
161                             CreateIndex icmd = spec.createCommandCreateIndex(tablename);
162                             icmd.setObjectOwner((String JavaDoc)info.get(DatabaseNodeInfo.SCHEMA));
163                             icmd.setIndexName(getIndexName());
164                             icmd.setIndexType(getIndexType());
165                             Iterator enu = getSelectedColumns().iterator();
166                             while (enu.hasNext())
167                                 icmd.specifyColumn((String JavaDoc)enu.next());
168                             icmd.execute();
169
170                             if (!icmd.wasException()) {
171                                 dialog.setVisible(false);
172                                 dialog.dispose();
173                             }
174                             result = true;
175                         } catch (CommandNotSupportedException e) {
176                             //PENDING
177
} catch (DDLException e) {
178                             DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(e.getMessage(), NotifyDescriptor.ERROR_MESSAGE)); // NOI18N
179
} catch (ClassNotFoundException JavaDoc e) {
180                             //PENDING
181
} catch (IllegalAccessException JavaDoc e) {
182                             //PENDING
183
} catch (InstantiationException JavaDoc e) {
184                             //PENDING
185
}
186                     }
187                 }
188             };
189
190             DialogDescriptor descriptor = new DialogDescriptor(pane, bundle.getString("AddIndexTitle"), true, listener); //NOI18N
191
// inbuilt close of the dialog is only after CANCEL button click
192
// after OK button is dialog closed by hand
193
Object JavaDoc [] closingOptions = {DialogDescriptor.CANCEL_OPTION};
194             descriptor.setClosingOptions(closingOptions);
195             dialog = DialogDisplayer.getDefault().createDialog(descriptor);
196             dialog.setResizable(true);
197         } catch (MissingResourceException e) {
198             e.printStackTrace();
199         }
200     }
201
202     public boolean run()
203     {
204         if (dialog != null) dialog.setVisible(true);
205         return result;
206     }
207
208     public Set getSelectedColumns()
209     {
210         return cbxlistener.getSelectedColumns();
211     }
212
213     public void setIndexName(String JavaDoc name)
214     {
215         namefld.setText(name);
216     }
217
218     public String JavaDoc getIndexName()
219     {
220         return namefld.getText();
221     }
222
223     public String JavaDoc getIndexType()
224     {
225         return (cbx_uq.isSelected())?ColumnItem.UNIQUE:""; // NOI18N
226
}
227
228     class CheckBoxListener implements ActionListener
229     {
230         private HashSet set;
231
232         CheckBoxListener(Collection columns)
233         {
234             set = new HashSet();
235         }
236
237         public void actionPerformed(ActionEvent event)
238         {
239             JCheckBox cbx = (JCheckBox)event.getSource();
240             String JavaDoc name = cbx.getName();
241             if (cbx.isSelected()) set.add(name);
242             else set.remove(name);
243         }
244
245         public Set getSelectedColumns()
246         {
247             return set;
248         }
249     }
250 }
251
Popular Tags