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.dataconnectivity.customizers;
042:
043: import javax.naming.NamingException;
044: import org.netbeans.api.db.explorer.ConnectionManager;
045: import org.netbeans.api.db.explorer.DatabaseConnection;
046: import org.netbeans.modules.visualweb.dataconnectivity.Log;
047: import org.netbeans.modules.db.sql.visualeditor.api.VisualSQLEditor;
048: import org.netbeans.modules.db.sql.visualeditor.api.VisualSQLEditorFactory;
049: import org.netbeans.modules.db.sql.visualeditor.api.VisualSQLEditorMetaData;
050: import org.netbeans.modules.visualweb.dataconnectivity.sql.DesignTimeDataSource;
051:
052: import com.sun.rave.designtime.DesignBean;
053: import com.sun.rave.designtime.DesignContext;
054: import com.sun.rave.designtime.impl.BasicCustomizer2;
055:
056: import java.awt.Component;
057: import java.beans.PropertyChangeEvent;
058: import java.beans.PropertyChangeListener;
059: import java.util.ArrayList;
060: import java.util.Arrays;
061: import java.util.HashMap;
062: import java.util.LinkedHashSet;
063: import java.util.List;
064: import java.util.Set;
065:
066: import org.netbeans.api.project.Project;
067: import org.netbeans.api.project.ui.OpenProjects;
068: import org.netbeans.modules.visualweb.insync.live.LiveUnit;
069: import org.netbeans.modules.visualweb.insync.models.FacesModel;
070: import org.netbeans.modules.visualweb.insync.models.FacesModelSet;
071:
072: import org.openide.util.HelpCtx;
073: import org.openide.util.NbBundle;
074: import org.openide.windows.TopComponent;
075:
076: /**
077: * This customizer is instantiated once with the name of each class that it will customize
078: * (currently only CachedRowSetX); the instance is then registered with insync
079: * When a customizer is needed, getCustomizerPanel() is called.
080: */
081: public class SqlCommandCustomizer extends BasicCustomizer2 {
082:
083: public String customizerClassName = ""; // NOI18N
084:
085: private OpenProjectsListener openProjectsListener = new OpenProjectsListener();
086:
087: // Maps for tracking open Query Editors
088:
089: private static HashMap<DesignBean, QBPair> queryEditors = new HashMap<DesignBean, QBPair>();
090:
091: private static HashMap<Project, ArrayList<DesignBean>> projectBeans = new HashMap<Project, ArrayList<DesignBean>>();
092:
093: // Constructor
094:
095: public SqlCommandCustomizer(String customizerClassName) {
096: super (null, NbBundle.getMessage(SqlCommandCustomizer.class,
097: "EDIT_QUERY")); // NOI18N
098: this .customizerClassName = customizerClassName;
099: OpenProjects.getDefault().addPropertyChangeListener(
100: openProjectsListener);
101: Log.err.log("Customizer for " + customizerClassName);
102: }
103:
104: // Factory method. Returns QueryBuilder, which is a TopComponent
105:
106: public Component getCustomizerPanel(DesignBean srcBean) {
107:
108: Log.err.log("Customizer panel requested for "
109: + srcBean.getInstanceName());
110:
111: // Check the map for an existing query editor for this bean, and reuse it if so
112: QBPair qbp = queryEditors.get(srcBean);
113: if (qbp != null) {
114: TopComponent qb = qbp.qb;
115: if (!qb.isOpened()) {
116: qb.open();
117: }
118: qb.requestActive();
119: return qb;
120: }
121:
122: Project project = null;
123: DesignContext dc = srcBean.getDesignContext();
124: if (dc instanceof LiveUnit) {
125: FacesModel fm = ((LiveUnit) dc).getModel();
126: FacesModelSet fms = fm.getFacesModelSet();
127: project = fms.getProject();
128: }
129: if (project != null) {
130:
131: ArrayList<DesignBean> beans = projectBeans.get(project);
132: if (beans == null) {
133: beans = new ArrayList<DesignBean>();
134: projectBeans.put(project, beans);
135: }
136: beans.add(srcBean);
137: }
138:
139: /****
140: * get the dataSourceName
141: */
142: // Object o = designBean.getInstance();
143: String dsName = null;
144: dsName = (String) srcBean.getProperty("dataSourceName")
145: .getValue();
146: VisualSQLEditorMetaData metadata = null;
147: try {
148: metadata = VisualSQLEditorMetaDataImpl
149: .getDataSourceCache(dsName);
150: } catch (java.sql.SQLException e) {
151: // JDTODO
152: }
153:
154: // Get the DatabaseConnection, to be passed to the Visual SQL Editor
155: DatabaseConnection dbconn = null;
156: try {
157: // First, get the DesignTimeDataSource
158: DesignTimeDataSource dtds = lookupDataSource(dsName);
159:
160: // Get the list of DatabaseConnections
161: DatabaseConnection[] dbconns = ConnectionManager
162: .getDefault().getConnections();
163: // Find the one we want
164: for (int i = 0; i < dbconns.length; i++) {
165: if ((dbconns[i]).getDatabaseURL().equals(dtds.getUrl())) {
166: dbconn = dbconns[i];
167: break;
168: }
169: }
170: } catch (NamingException ex) {
171: }
172:
173: String command = (String) srcBean.getProperty("command")
174: .getValue();
175: VisualSQLEditor vse = VisualSQLEditorFactory
176: .createVisualSQLEditor(dbconn, command, metadata);
177:
178: vse.addPropertyChangeListener(vseListener);
179: Component retComp = vse.open();
180: if (retComp instanceof TopComponent) {
181: queryEditors.put(srcBean, new QBPair(
182: (TopComponent) retComp, vse));
183: }
184:
185: return retComp;
186: }
187:
188: public HelpCtx getHelpCtx() {
189: return new HelpCtx(
190: "projrave_ui_elements_editors_about_query_editor"); // NOI18N
191: }
192:
193: /****
194: * convenience method for looking up the datasource in the current
195: * context. Copied from SqlStatementImpl
196: */
197: private DesignTimeDataSource lookupDataSource(String dataSourceName)
198: throws NamingException {
199: String dsName;
200: if (dataSourceName == null) {
201: // we should never be here, but just in case ...
202: NamingException ne = new NamingException(
203: "Data Source Name is required: none provided."); // NOI18N
204: throw ne;
205: }
206:
207: javax.naming.Context ctx = new javax.naming.InitialContext();
208: if (!dataSourceName.startsWith("java:comp/env/jdbc/")) {
209: dsName = "java:comp/env/jdbc/" + dataSourceName;
210: } else {
211: dsName = dataSourceName;
212: }
213:
214: DesignTimeDataSource ds = (DesignTimeDataSource) ctx
215: .lookup(dsName);
216: return ds;
217: }
218:
219: // Listen for changes to statement property; update design bean
220:
221: private PropertyChangeListener vseListener =
222:
223: new PropertyChangeListener() {
224: public void propertyChange(PropertyChangeEvent evt) {
225: // what property?
226: String propName = evt.getPropertyName();
227: Log.log("VSE property change: " + propName);
228: if (propName.equals(VisualSQLEditor.PROP_STATEMENT)) {
229:
230: // Get the VSE that raised the event
231: VisualSQLEditor vse = (VisualSQLEditor) evt.getSource();
232:
233: // Find the bean that is associated with this VSE in the map
234: for (DesignBean bean : queryEditors.keySet()) {
235: QBPair qbp = queryEditors.get(bean);
236: if ((qbp != null) && (qbp.vse == vse)) {
237: Log.err.log(" newValue=" + vse.getStatement());
238: // Found it. Update the bean property.
239: bean.getProperty("command").setValue(
240: vse.getStatement());
241: break;
242: }
243: }
244: }
245: }
246: };
247:
248: // Listen for project close events; update Maps
249:
250: private class OpenProjectsListener implements
251: PropertyChangeListener {
252:
253: public void propertyChange(PropertyChangeEvent event) {
254:
255: // The list of open projects has changed; clean up any old projects we may be holding on to.
256: if (OpenProjects.PROPERTY_OPEN_PROJECTS.equals(event
257: .getPropertyName())) {
258:
259: List<Project> oldOpenProjectsList = Arrays
260: .asList((Project[]) event.getOldValue());
261: List<Project> newOpenProjectsList = Arrays
262: .asList((Project[]) event.getNewValue());
263: Set<Project> closedProjectsSet = new LinkedHashSet<Project>(
264: oldOpenProjectsList);
265: closedProjectsSet.removeAll(newOpenProjectsList);
266: for (Project project : closedProjectsSet) {
267:
268: // Project has been closed; close any open QueryEditors, then remove it from map
269: ArrayList<DesignBean> beans = projectBeans
270: .get(project);
271: if (beans != null) {
272: for (DesignBean bean : beans) {
273: QBPair qbp = queryEditors.get(bean);
274: if (qbp != null) {
275: qbp.qb.close();
276: }
277: queryEditors.remove(bean);
278: }
279: }
280: projectBeans.remove(project);
281: }
282: }
283: }
284: }
285:
286: // Data class representing a <QueryBuilder, VisualSQLEditor> pair. Used as the value in the HashMap
287: private class QBPair {
288: TopComponent qb;
289: VisualSQLEditor vse;
290:
291: QBPair(TopComponent qb, VisualSQLEditor vse) {
292: this.qb = qb;
293: this.vse = vse;
294: }
295: }
296: }
|