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.dbschema.jdbcimpl;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046:
047: import org.openide.filesystems.FileObject;
048: import org.openide.loaders.DataObjectExistsException;
049: import org.openide.loaders.MultiDataObject;
050: import org.openide.nodes.CookieSet;
051: import org.openide.nodes.Children;
052: import org.openide.nodes.Node;
053: import org.openide.util.HelpCtx;
054:
055: import org.netbeans.modules.dbschema.DBElementProvider;
056: import org.netbeans.modules.dbschema.SchemaElement;
057: import org.netbeans.modules.dbschema.SchemaElementUtil;
058: import org.netbeans.modules.dbschema.nodes.SchemaRootChildren;
059:
060: public class DBschemaDataObject extends MultiDataObject {
061:
062: transient protected SchemaElement schemaElement;
063: transient SchemaElementImpl schemaElementImpl;
064:
065: public DBschemaDataObject(FileObject pf, DBschemaDataLoader loader)
066: throws DataObjectExistsException {
067: super (pf, loader);
068: init();
069: }
070:
071: private void init() {
072: CookieSet cookies = getCookieSet();
073:
074: cookies.add(new DBElementProvider());
075:
076: PropertyChangeListener listener = new PropertyChangeListener() {
077: public void propertyChange(PropertyChangeEvent event) {
078: if (event.getPropertyName().equals("valid")) //NOI18N
079: if (!isValid())
080: if (schemaElement == null) {
081: schemaElement = SchemaElementUtil
082: .forName(getPrimaryFile());
083: if (schemaElement != null) {
084: SchemaElement
085: .removeFromCache(schemaElement
086: .getName()
087: .getFullName());
088: try {
089: SchemaElement
090: .removeFromCache(schemaElement
091: .getName()
092: .getFullName()
093: + "#"
094: + getPrimaryFile()
095: .getURL()
096: .toString()); //NOI18N
097: } catch (Exception exc) {
098: if (Boolean
099: .getBoolean("netbeans.debug.exceptions")) //NOI18N
100: exc.printStackTrace();
101: }
102: schemaElement = null;
103: }
104: return;
105: } else {
106: SchemaElement.removeFromCache(schemaElement
107: .getName().getFullName());
108: try {
109: SchemaElement
110: .removeFromCache(schemaElement
111: .getName()
112: .getFullName()
113: + "#"
114: + getPrimaryFile()
115: .getURL()
116: .toString()); //NOI18N
117: } catch (Exception exc) {
118: if (Boolean
119: .getBoolean("netbeans.debug.exceptions")) //NOI18N
120: exc.printStackTrace();
121: }
122: schemaElement = null;
123: return;
124: }
125:
126: if (event.getPropertyName().equals("primaryFile")) //NOI18N
127: if (schemaElement == null)
128: return;
129: else {
130: SchemaElement.removeFromCache(schemaElement
131: .getName().getFullName());
132: try {
133: SchemaElement.removeFromCache(schemaElement
134: .getName().getFullName()
135: + "#"
136: + getPrimaryFile().getURL()
137: .toString()); //NOI18N
138: } catch (Exception exc) {
139: if (Boolean
140: .getBoolean("netbeans.debug.exceptions")) //NOI18N
141: exc.printStackTrace();
142: }
143: schemaElement = null;
144: getSchema();
145: return;
146: }
147: }
148: };
149:
150: addPropertyChangeListener(listener);
151: }
152:
153: public Node.Cookie getCookie(Class c) {
154: // Looks like a bug - why is it done this way? This inevitable leads to a ClassCastException
155: if (SchemaElement.class.isAssignableFrom(c))
156: return getCookie(DBElementProvider.class);
157: return super .getCookie(c);
158: }
159:
160: public SchemaElement getSchema() {
161: if (schemaElement == null)
162: setSchema(SchemaElementUtil.forName(getPrimaryFile()));
163:
164: return schemaElement;
165: }
166:
167: public void setSchema(SchemaElement schema) {
168: schemaElement = schema;
169: Node n = getNodeDelegate();
170: Children ch = n.getChildren();
171: ((SchemaRootChildren) ch).setElement(schemaElement);
172: }
173:
174: public SchemaElementImpl getSchemaElementImpl() {
175: return schemaElementImpl;
176: }
177:
178: public void setSchemaElementImpl(SchemaElementImpl schemaImpl) {
179: schemaElementImpl = schemaImpl;
180: }
181:
182: public HelpCtx getHelpCtx() {
183: return new HelpCtx("dbschema_ctxhelp_wizard"); //NOI18N
184: }
185:
186: protected Node createNodeDelegate() {
187: Node nodeDelegate = new DBschemaDataNode(this);
188:
189: return nodeDelegate;
190: }
191: }
|