1 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 ; 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"); JPanel pane = new JPanel(); 50 pane.setBorder(new EmptyBorder (new Insets(5,5,5,5))); 51 GridBagLayout layout = new GridBagLayout(); 52 GridBagConstraints con = new GridBagConstraints (); 53 pane.setLayout (layout); 54 55 57 JLabel label = new JLabel(); 58 Mnemonics.setLocalizedText(label, bundle.getString("AddIndexName")); label.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_AddIndexNameA11yDesc")); 60 con.anchor = GridBagConstraints.WEST; 61 con.insets = new java.awt.Insets (2, 2, 2, 2); 62 con.gridx = 0; 63 con.gridy = 0; 64 layout.setConstraints(label, con); 65 pane.add(label); 66 67 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 (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 83 JLabel label_uq = new JLabel(bundle.getString("AddUniqueIndex")); label.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_AddUniqueIndexA11yDesc")); 85 con.weightx = 0.0; 86 con.anchor = GridBagConstraints.WEST; 87 con.insets = new java.awt.Insets (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 (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 107 label = new JLabel(bundle.getString("AddIndexLabel")); label.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_AddIndexLabelA11yDesc")); 109 con.weightx = 0.0; 110 con.anchor = GridBagConstraints.WEST; 111 con.insets = new java.awt.Insets (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 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 (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 colname = (String )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 (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")); pane.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_AddIndexDialogA11yDesc")); 152 final String tablename = (String )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 )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 )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 } catch (DDLException e) { 178 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(e.getMessage(), NotifyDescriptor.ERROR_MESSAGE)); } catch (ClassNotFoundException e) { 180 } catch (IllegalAccessException e) { 182 } catch (InstantiationException e) { 184 } 186 } 187 } 188 }; 189 190 DialogDescriptor descriptor = new DialogDescriptor(pane, bundle.getString("AddIndexTitle"), true, listener); Object [] 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 name) 214 { 215 namefld.setText(name); 216 } 217 218 public String getIndexName() 219 { 220 return namefld.getText(); 221 } 222 223 public String getIndexType() 224 { 225 return (cbx_uq.isSelected())?ColumnItem.UNIQUE:""; } 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 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 |