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-2007 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: package org.netbeans.modules.visualweb.propertyeditors.binding.data;
042:
043: import java.awt.BorderLayout;
044: import java.awt.Cursor;
045: import java.awt.Point;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.ActionListener;
048: import java.beans.PropertyChangeEvent;
049: import java.beans.PropertyChangeListener;
050: import java.io.IOException;
051: import java.util.ArrayList;
052: import java.util.List;
053: import javax.swing.Action;
054: import javax.swing.JButton;
055: import javax.swing.JDialog;
056: import javax.swing.JPanel;
057: import org.openide.DialogDescriptor;
058: import org.openide.DialogDisplayer;
059: import org.openide.ErrorManager;
060: import org.openide.loaders.DataFolder;
061: import org.openide.loaders.FolderInstance;
062: import org.openide.nodes.AbstractNode;
063: import org.openide.nodes.FilterNode;
064: import org.openide.nodes.Node;
065: import org.openide.util.HelpCtx;
066: import org.openide.util.NbBundle;
067: import org.openide.util.actions.NodeAction;
068: import org.openide.windows.TopComponent;
069: import org.openide.windows.WindowManager;
070:
071: /**
072: *
073: * @author Winston Prakash
074: */
075: public class AddDataProviderDialog extends JPanel implements
076: PropertyChangeListener {
077:
078: private String addString = NbBundle.getMessage(
079: AddDataProviderDialog.class, "ADD");
080: private String cancelString = NbBundle.getMessage(
081: AddDataProviderDialog.class, "CANCEL");
082: private JDialog dialog;
083: private DialogDescriptor dlg = null;
084:
085: private JButton addButton = new JButton(addString);
086: private JButton cancelButton = new JButton(cancelString);
087:
088: private static DataProviderExplorerPanel dataProviderExplorerPanel = null;
089:
090: boolean result = false;
091:
092: TopComponent tc = WindowManager.getDefault().findTopComponent(
093: "serverNavigator");
094:
095: /** Creates new form AddDataProviderDialog */
096: public AddDataProviderDialog() {
097: initComponents();
098: dataProviderExplorerPanel = new DataProviderExplorerPanel();
099: dataProviderExplorerPanel.getExplorerManager()
100: .addPropertyChangeListener(this );
101: add(dataProviderExplorerPanel, BorderLayout.CENTER);
102: // This is a pure hack for not to show the Query view as it screws up the Add Data Provider
103: // functionality. We need better way to add data provider, some kind of service offered by Server Navigator.
104: System.setProperty("AddDataProviderMode", "true");
105: }
106:
107: public void propertyChange(PropertyChangeEvent evt) {
108:
109: Node[] nodes = dataProviderExplorerPanel.getExplorerManager()
110: .getSelectedNodes();
111: tc.requestActive();
112: tc.setActivatedNodes(nodes);
113: boolean canAdd = false;
114: if ((nodes != null) && (nodes.length > 0)) {
115: Action[] actions = nodes[0].getActions(true);
116: for (int i = 0; i < actions.length; i++) {
117: if (actions[i] instanceof NodeAction) {
118: final NodeAction nodeAction = (NodeAction) actions[i];
119: if (nodeAction.getName().equals(
120: NbBundle.getMessage(
121: AddDataProviderDialog.class,
122: "Add_to_Form"))) {
123: canAdd = true;
124: break;
125: }
126: }
127: }
128: }
129: addButton.setEnabled(canAdd);
130: }
131:
132: /**
133: * Show the Add data provider dialog
134: */
135: public boolean showDialog() {
136: // Add a listener to the dialog's buttons
137: ActionListener listener = new ActionListener() {
138: public void actionPerformed(ActionEvent evt) {
139: //Remove the AddDataProviderMode property as it is not needed any more.
140: System.getProperties().remove("AddDataProviderMode");
141: Object o = evt.getSource();
142:
143: Object[] option = dlg.getOptions();
144:
145: if (o == option[1]) {
146: // cancel button or escape
147: } else if (o == option[0]) {
148: // Add the Data Provider
149: Node[] nodes = dataProviderExplorerPanel
150: .getExplorerManager().getSelectedNodes();
151: tc.requestActive();
152: tc.setActivatedNodes(nodes);
153: Action[] actions = nodes[0].getActions(true);
154: for (int i = 0; i < actions.length; i++) {
155: if (actions[i] instanceof NodeAction) {
156: final NodeAction nodeAction = (NodeAction) actions[i];
157: if (nodeAction
158: .getName()
159: .equals(
160: NbBundle
161: .getMessage(
162: AddDataProviderDialog.class,
163: "Add_to_Form"))) {
164: nodeAction.performAction();
165: result = true;
166: break;
167: }
168: }
169: }
170: dialog.dispose();
171: }
172: }
173: };
174:
175: dlg = new DialogDescriptor(this , NbBundle.getMessage(
176: AddDataProviderDialog.class, "ADD_DATA_PROVIDER"),
177: true, listener);
178: dlg.setOptions(new Object[] { addButton, cancelButton });
179: dlg.setClosingOptions(new Object[] { cancelButton });
180: //dlg.setHelpCtx(new HelpCtx("projrave_ui_elements_server_nav_add_datasourcedb")); // NOI18N
181:
182: dialog = (JDialog) DialogDisplayer.getDefault().createDialog(
183: dlg);
184: dialog.setResizable(true);
185: dialog.pack();
186: addButton.setEnabled(false);
187: Point loc = dialog.getLocation();
188: dialog
189: .setLocation((int) loc.getX() + 50,
190: (int) loc.getY() + 50);
191: dialog.show();
192: return result;
193: }
194:
195: /** This method is called from within the constructor to
196: * initialize the form.
197: * WARNING: Do NOT modify this code. The content of this method is
198: * always regenerated by the Form Editor.
199: */
200: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
201: private void initComponents() {
202: jLabel1 = new javax.swing.JLabel();
203:
204: setLayout(new java.awt.BorderLayout(10, 10));
205:
206: setBorder(new javax.swing.border.EmptyBorder(
207: new java.awt.Insets(10, 10, 10, 10)));
208: jLabel1.setText(org.openide.util.NbBundle.getMessage(
209: AddDataProviderDialog.class,
210: "ADD_DATA_PROVIDER_MESSAGE"));
211: add(jLabel1, java.awt.BorderLayout.NORTH);
212:
213: }
214:
215: // </editor-fold>//GEN-END:initComponents
216:
217: // Variables declaration - do not modify//GEN-BEGIN:variables
218: private javax.swing.JLabel jLabel1;
219: // End of variables declaration//GEN-END:variables
220:
221: }
|