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: package org.netbeans.modules.form.j2ee;
042:
043: import java.awt.dnd.DropTargetDragEvent;
044: import java.util.logging.Level;
045: import java.util.logging.Logger;
046: import javax.swing.ImageIcon;
047: import org.netbeans.api.db.explorer.DatabaseMetaDataTransfer;
048: import org.netbeans.api.project.FileOwnerQuery;
049: import org.netbeans.api.project.Project;
050: import org.netbeans.api.project.libraries.LibraryManager;
051: import org.netbeans.modules.form.FormEditor;
052: import org.netbeans.modules.form.FormModel;
053: import org.netbeans.modules.form.NewComponentDrop;
054: import org.netbeans.modules.form.RADComponent;
055: import org.netbeans.modules.form.palette.PaletteItem;
056: import org.netbeans.modules.form.project.ClassSource;
057: import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
058: import org.openide.filesystems.FileObject;
059: import org.openide.util.Utilities;
060:
061: /**
062: * Result of DB connection drop.
063: *
064: * @author Jan Stola
065: */
066: public class DBConnectionDrop implements NewComponentDrop {
067: /** Dropped connection. */
068: private DatabaseMetaDataTransfer.Connection connection;
069: /** Determines whether to drag just the binding or also a component. */
070: private boolean bindingOnly;
071: /** Form model. */
072: FormModel model;
073: /** Enclosing project. */
074: Project project;
075: /** Determines whether the assistant context messages were initialized. */
076: boolean assistantInitialized;
077:
078: /**
079: * Creates new <code>DBConnectionDrop</code>.
080: *
081: * @param model form model.
082: * @param connection dropped connection.
083: */
084: public DBConnectionDrop(FormModel model,
085: DatabaseMetaDataTransfer.Connection connection) {
086: this .model = model;
087: this .connection = connection;
088: }
089:
090: /**
091: * Returns <code>EntityManager</code> palette item.
092: *
093: * @param dtde corresponding drop target drag event.
094: * @return <code>EntityManager</code> palette item.
095: */
096: public PaletteItem getPaletteItem(DropTargetDragEvent dtde) {
097: PaletteItem pItem = new PaletteItem(new ClassSource(
098: "javax.persistence.EntityManager", // NOI18N
099: new ClassSource.LibraryEntry(LibraryManager
100: .getDefault().getLibrary("toplink"))), // NOI18N
101: null);
102: pItem
103: .setIcon(new ImageIcon(
104: Utilities
105: .loadImage("org/netbeans/modules/form/j2ee/resources/EntityManager.png"))
106: .getImage()); // NOI18N
107: return pItem;
108: }
109:
110: /**
111: * Post-processing after placement of the dragged connection.
112: *
113: * @param componentId ID of the corresponding component.
114: * @param droppedOverId ID of a component the new component has been dropped over.
115: */
116: public void componentAdded(String componentId, String droppedOverId) {
117: try {
118: FileObject formFile = FormEditor.getFormDataObject(model)
119: .getFormFile();
120: project = FileOwnerQuery.getOwner(formFile);
121:
122: // Make sure persistence.xml file exists
123: FileObject persistenceXML = J2EEUtils.getPersistenceXML(
124: project, true);
125:
126: // Initializes persistence unit and persistence descriptor
127: PersistenceUnit unit = J2EEUtils.initPersistenceUnit(
128: persistenceXML, connection.getDatabaseConnection());
129:
130: // Initializes project's classpath
131: J2EEUtils.updateProjectForUnit(persistenceXML, unit,
132: connection.getJDBCDriver());
133:
134: RADComponent entityManager = model
135: .getMetaComponent(componentId);
136: entityManager.getPropertyByName("persistenceUnit")
137: .setValue(unit.getName()); // NOI18N
138: J2EEUtils.renameComponent(entityManager, true, unit
139: .getName()
140: + "EntityManager", "entityManager"); // NOI18N
141: } catch (Exception ex) {
142: Logger.getLogger(getClass().getName()).log(Level.INFO,
143: ex.getMessage(), ex);
144: }
145: }
146:
147: /**
148: * Ensures existence of an entity manager "bean" that corresponds to the given persistence unit.
149: *
150: * @param unit persistence unit.
151: * @return RAD component encapsulating entity manager corresponding to the given persistence unit.
152: * @throws Exception when something goes wrong.
153: */
154: protected RADComponent initEntityManagerBean(PersistenceUnit unit)
155: throws Exception {
156: String puName = unit.getName();
157: RADComponent entityManager = J2EEUtils.findEntityManager(model,
158: puName);
159: if (entityManager == null) {
160: entityManager = J2EEUtils
161: .createEntityManager(model, puName);
162: }
163: return entityManager;
164: }
165:
166: /**
167: * Sets <code>bindingOnly</code> property.
168: *
169: * @param bindingOnly new value of the property.
170: */
171: void setBindingOnly(boolean bindingOnly) {
172: this .bindingOnly = bindingOnly;
173: }
174:
175: /**
176: * Returns value of <code>bindingOnly</code> property.
177: *
178: * @return value of <code>bindingOnly</code> property.
179: */
180: boolean isBindingOnly() {
181: return bindingOnly;
182: }
183:
184: }
|