001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.explorer.dlg;
043:
044: import java.awt.*;
045: import java.awt.event.*;
046: import java.util.*;
047: import java.util.logging.Level;
048: import java.util.logging.Logger;
049: import javax.swing.*;
050: import javax.swing.border.EmptyBorder;
051: import org.openide.DialogDescriptor;
052: import org.openide.DialogDisplayer;
053: import org.openide.util.NbBundle;
054: import org.netbeans.lib.ddl.impl.Specification;
055: import org.netbeans.lib.ddl.*;
056: import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
057: import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
058: import org.netbeans.modules.db.explorer.*;
059: import org.openide.awt.Mnemonics;
060:
061: public class AddIndexDialog {
062: boolean result = false;
063: Dialog dialog = null;
064: JTextField namefld;
065: CheckBoxListener cbxlistener;
066: JCheckBox cbx_uq;
067: private static Logger LOGGER = Logger
068: .getLogger(AddIndexDialog.class.getName());
069: private final ResourceBundle bundle = NbBundle
070: .getBundle("org.netbeans.modules.db.resources.Bundle"); //NOI18N
071:
072: public AddIndexDialog(Collection columns, final Specification spec,
073: final DatabaseNodeInfo info) {
074: try {
075: JPanel pane = new JPanel();
076: pane.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
077: GridBagLayout layout = new GridBagLayout();
078: GridBagConstraints con = new GridBagConstraints();
079: pane.setLayout(layout);
080:
081: // Index name
082:
083: JLabel label = new JLabel();
084: Mnemonics.setLocalizedText(label, bundle
085: .getString("AddIndexName")); //NOI18N
086: label.getAccessibleContext().setAccessibleDescription(
087: bundle.getString("ACS_AddIndexNameA11yDesc"));
088: con.anchor = GridBagConstraints.WEST;
089: con.insets = new java.awt.Insets(2, 2, 2, 2);
090: con.gridx = 0;
091: con.gridy = 0;
092: layout.setConstraints(label, con);
093: pane.add(label);
094:
095: // Index name field
096:
097: con.fill = GridBagConstraints.HORIZONTAL;
098: con.weightx = 1.0;
099: con.gridx = 1;
100: con.gridy = 0;
101: con.insets = new java.awt.Insets(2, 2, 2, 2);
102: namefld = new JTextField(35);
103: namefld.setToolTipText(bundle
104: .getString("ACS_AddIndexNameTextFieldA11yDesc"));
105: namefld
106: .getAccessibleContext()
107: .setAccessibleName(
108: bundle
109: .getString("ACS_AddIndexNameTextFieldA11yName"));
110: label.setLabelFor(namefld);
111: layout.setConstraints(namefld, con);
112: pane.add(namefld);
113:
114: // Unique/Non-unique
115:
116: JLabel label_uq = new JLabel(bundle
117: .getString("AddUniqueIndex")); //NOI18N
118: label.getAccessibleContext().setAccessibleDescription(
119: bundle.getString("ACS_AddUniqueIndexA11yDesc"));
120: con.weightx = 0.0;
121: con.anchor = GridBagConstraints.WEST;
122: con.insets = new java.awt.Insets(2, 2, 2, 2);
123: con.gridx = 0;
124: con.gridy = 1;
125: layout.setConstraints(label_uq, con);
126: pane.add(label_uq);
127:
128: con.fill = GridBagConstraints.HORIZONTAL;
129: con.weightx = 1.0;
130: con.gridx = 1;
131: con.gridy = 1;
132: con.insets = new java.awt.Insets(2, 2, 2, 2);
133: cbx_uq = new JCheckBox();
134: Mnemonics.setLocalizedText(cbx_uq, bundle
135: .getString("Unique"));
136: cbx_uq.setToolTipText(bundle
137: .getString("ACS_UniqueA11yDesc"));
138: label_uq.setLabelFor(cbx_uq);
139: layout.setConstraints(cbx_uq, con);
140: pane.add(cbx_uq);
141:
142: // Items list title
143:
144: label = new JLabel(bundle.getString("AddIndexLabel")); //NOI18N
145: label.getAccessibleContext().setAccessibleDescription(
146: bundle.getString("ACS_AddIndexLabelA11yDesc"));
147: con.weightx = 0.0;
148: con.anchor = GridBagConstraints.WEST;
149: con.insets = new java.awt.Insets(2, 2, 2, 2);
150: con.gridx = 0;
151: con.gridy = 2;
152: con.gridwidth = 2;
153: layout.setConstraints(label, con);
154: pane.add(label);
155:
156: // Items list
157:
158: JPanel subpane = new JPanel();
159: label.setLabelFor(subpane);
160: int colcount = columns.size();
161: colcount = (colcount % 2 == 0 ? colcount / 2
162: : colcount / 2 + 1);
163: GridLayout sublayout = new GridLayout(colcount, 2);
164: subpane.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
165: subpane.setLayout(sublayout);
166:
167: cbxlistener = new CheckBoxListener(columns);
168: Iterator iter = columns.iterator();
169: while (iter.hasNext()) {
170: String colname = (String) iter.next();
171: JCheckBox cbx = new JCheckBox(colname);
172: cbx.setName(colname);
173: cbx.setToolTipText(colname);
174: cbx.addActionListener(cbxlistener);
175: subpane.add(cbx);
176: }
177:
178: con.weightx = 1.0;
179: con.weighty = 1.0;
180: con.gridwidth = 2;
181: con.fill = GridBagConstraints.BOTH;
182: con.insets = new java.awt.Insets(0, 0, 0, 0);
183: con.gridx = 0;
184: con.gridy = 3;
185: JScrollPane spane = new JScrollPane(subpane);
186: layout.setConstraints(spane, con);
187: pane.add(spane);
188: pane.getAccessibleContext().setAccessibleName(
189: bundle.getString("ACS_AddIndexDialogA11yName")); // NOI18N
190: pane.getAccessibleContext().setAccessibleDescription(
191: bundle.getString("ACS_AddIndexDialogA11yDesc")); // NOI18N
192:
193: final String tablename = (String) info
194: .get(DatabaseNode.TABLE);
195:
196: ActionListener listener = new ActionListener() {
197: public void actionPerformed(ActionEvent event) {
198:
199: if (event.getSource() == DialogDescriptor.OK_OPTION) {
200:
201: try {
202: result = false;
203: AddIndexDDL ddl = new AddIndexDDL(
204: spec,
205: ((String) info
206: .get(DatabaseNodeInfo.SCHEMA)),
207: tablename);
208:
209: boolean wasException = ddl.execute(
210: getIndexName(),
211: cbx_uq.isSelected(),
212: getSelectedColumns());
213:
214: if (!wasException) {
215: dialog.setVisible(false);
216: dialog.dispose();
217: }
218: result = true;
219: } catch (Exception e) {
220: LOGGER.log(Level.WARNING,
221: "Unable to create index", e);
222:
223: DbUtilities.reportError(bundle
224: .getString("ERR_UnableToAddIndex"),
225: e.getMessage());
226:
227: return;
228: }
229: }
230: }
231: };
232:
233: DialogDescriptor descriptor = new DialogDescriptor(pane,
234: bundle.getString("AddIndexTitle"), true, listener); //NOI18N
235: // inbuilt close of the dialog is only after CANCEL button click
236: // after OK button is dialog closed by hand
237: Object[] closingOptions = { DialogDescriptor.CANCEL_OPTION };
238: descriptor.setClosingOptions(closingOptions);
239: dialog = DialogDisplayer.getDefault().createDialog(
240: descriptor);
241: dialog.setResizable(true);
242: } catch (MissingResourceException e) {
243: e.printStackTrace();
244: }
245: }
246:
247: public boolean run() {
248: if (dialog != null)
249: dialog.setVisible(true);
250: return result;
251: }
252:
253: public Set getSelectedColumns() {
254: return cbxlistener.getSelectedColumns();
255: }
256:
257: public void setIndexName(String name) {
258: namefld.setText(name);
259: }
260:
261: public String getIndexName() {
262: return namefld.getText();
263: }
264:
265: class CheckBoxListener implements ActionListener {
266: private HashSet set;
267:
268: CheckBoxListener(Collection columns) {
269: set = new HashSet();
270: }
271:
272: public void actionPerformed(ActionEvent event) {
273: JCheckBox cbx = (JCheckBox) event.getSource();
274: String name = cbx.getName();
275: if (cbx.isSelected())
276: set.add(name);
277: else
278: set.remove(name);
279: }
280:
281: public Set getSelectedColumns() {
282: return set;
283: }
284: }
285: }
|