KickJava   Java API By Example, From Geeks To Geeks.

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


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.DialogDisplayer;
29 import org.openide.util.NbBundle;
30 import org.openide.NotifyDescriptor;
31 import org.netbeans.lib.ddl.impl.CreateView;
32 import org.netbeans.lib.ddl.impl.Specification;
33 import org.netbeans.lib.ddl.*;
34 import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
35 import org.netbeans.modules.db.explorer.*;
36 import org.openide.awt.Mnemonics;
37
38 public class AddViewDialog {
39     boolean result = false;
40     Dialog dialog = null;
41     JTextField namefld;
42     JTextArea tarea;
43
44     public AddViewDialog(final Specification spec, final DatabaseNodeInfo info) {
45         try {
46             ResourceBundle bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle"); //NOI18N
47
JPanel pane = new JPanel();
48             pane.setBorder(new EmptyBorder JavaDoc(new Insets(5,5,5,5)));
49             GridBagLayout layout = new GridBagLayout();
50             GridBagConstraints con = new GridBagConstraints ();
51             pane.setLayout (layout);
52
53             // Index name
54

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

67             con.fill = GridBagConstraints.HORIZONTAL;
68             con.weightx = 1.0;
69             con.gridx = 1;
70             con.gridy = 0;
71             con.insets = new java.awt.Insets JavaDoc (2, 2, 2, 2);
72             namefld = new JTextField(35);
73             namefld.setToolTipText(bundle.getString("ACS_AddViewNameTextFieldA11yDesc"));
74             namefld.getAccessibleContext().setAccessibleName(bundle.getString("ACS_AddViewNameTextFieldA11yName"));
75             label.setLabelFor(namefld);
76             layout.setConstraints(namefld, con);
77             pane.add(namefld);
78
79             // Items list title
80

81             label = new JLabel();
82             Mnemonics.setLocalizedText(label, bundle.getString("AddViewLabel"));
83             label.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_AddViewLabelA11yDesc"));
84             con.weightx = 0.0;
85             con.anchor = GridBagConstraints.WEST;
86             con.insets = new java.awt.Insets JavaDoc (2, 2, 2, 2);
87             con.gridx = 0;
88             con.gridy = 1;
89             con.gridwidth = 2;
90             layout.setConstraints(label, con);
91             pane.add(label);
92
93             // Editor list
94

95             tarea = new JTextArea(5,50);
96             tarea.setToolTipText(bundle.getString("ACS_AddViewTextAreaA11yDesc"));
97             tarea.getAccessibleContext().setAccessibleName(bundle.getString("ACS_AddViewTextAreaA11yName"));
98             label.setLabelFor(tarea);
99
100             con.weightx = 1.0;
101             con.weighty = 1.0;
102             con.gridwidth = 2;
103             con.fill = GridBagConstraints.BOTH;
104             con.insets = new java.awt.Insets JavaDoc (0, 0, 0, 0);
105             con.gridx = 0;
106             con.gridy = 2;
107             JScrollPane spane = new JScrollPane(tarea);
108             layout.setConstraints(spane, con);
109             pane.add(spane);
110
111             ActionListener listener = new ActionListener() {
112                 public void actionPerformed(ActionEvent event) {
113                     
114                     if (event.getSource() == DialogDescriptor.OK_OPTION) {
115                         
116                         try {
117                             CreateView cmd = spec.createCommandCreateView(getViewName());
118                             cmd.setQuery(getViewCode());
119                             cmd.setObjectOwner((String JavaDoc)info.get(DatabaseNodeInfo.SCHEMA));
120                             cmd.execute();
121                             result = !cmd.wasException();
122                             
123                             if (!cmd.wasException()) {
124                                 dialog.setVisible(false);
125                                 dialog.dispose();
126                             }
127                         } catch (CommandNotSupportedException e) {
128                             DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(e.getMessage(), NotifyDescriptor.ERROR_MESSAGE));
129                         } catch (DDLException e) {
130                             DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(e.getMessage(), NotifyDescriptor.ERROR_MESSAGE));
131                         } catch (Exception JavaDoc e) {
132                             DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(e.getMessage(), NotifyDescriptor.ERROR_MESSAGE));
133                         }
134                     }
135                 }
136             };
137
138             pane.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_AddViewDialogA11yDesc")); //NOI18N
139

140             DialogDescriptor descriptor = new DialogDescriptor(pane, bundle.getString("AddViewTitle"), true, listener); //NOI18N
141
// inbuilt close of the dialog is only after CANCEL button click
142
// after OK button is dialog closed by hand
143
Object JavaDoc [] closingOptions = {DialogDescriptor.CANCEL_OPTION};
144             descriptor.setClosingOptions(closingOptions);
145             dialog = DialogDisplayer.getDefault().createDialog(descriptor);
146             dialog.setResizable(true);
147         } catch (MissingResourceException e) {
148             e.printStackTrace();
149         }
150     }
151
152     public boolean run()
153     {
154         if (dialog != null) dialog.setVisible(true);
155         return result;
156     }
157
158     public void setViewName(String JavaDoc name)
159     {
160         namefld.setText(name);
161     }
162
163     public String JavaDoc getViewName()
164     {
165         return namefld.getText();
166     }
167
168     public String JavaDoc getViewCode()
169     {
170         return tarea.getText();
171     }
172 }
173
Popular Tags