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.xml.xam;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.util.HashSet;
047: import java.util.List;
048: import java.util.Set;
049: import javax.swing.text.Document;
050: import javax.xml.namespace.QName;
051: import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
052: import org.netbeans.modules.xml.xam.dom.ComponentFactory;
053: import org.netbeans.modules.xml.xam.dom.DocumentComponent;
054: import org.netbeans.modules.xml.xam.dom.DocumentModel;
055: import org.netbeans.modules.xml.xam.dom.ElementIdentity;
056: import org.openide.util.Lookup;
057: import org.openide.util.lookup.Lookups;
058: import org.w3c.dom.Element;
059:
060: /**
061: *
062: * @author Nam Nguyen
063: */
064: public class TestModel extends AbstractDocumentModel<TestComponent>
065: implements DocumentModel<TestComponent> {
066: TestComponent testRoot;
067:
068: /** Creates a new instance of TestModel */
069: public TestModel(Document doc) {
070: super (createModelSource(doc));
071: try {
072: super .sync();
073: } catch (Exception e) {
074: throw new RuntimeException(e);
075: }
076: this .addPropertyChangeListener(new FaultGenerator());
077: }
078:
079: protected void setIdentifyingAttributes() {
080: ElementIdentity eid = getAccess().getElementIdentity();
081: eid.addIdentifier("id");
082: eid.addIdentifier("index");
083: eid.addIdentifier("name");
084: eid.addIdentifier("ref");
085: }
086:
087: public TestComponent createRootComponent(org.w3c.dom.Element root) {
088: if (TestComponent.NS_URI.equals(root.getNamespaceURI())
089: && "test".equals(root.getLocalName())) {
090: testRoot = new TestComponent(this , root);
091: } else {
092: testRoot = null;
093: }
094: return testRoot;
095: }
096:
097: public TestComponent createComponent(TestComponent parent,
098: org.w3c.dom.Element element) {
099: return TestComponent.createComponent(this , parent, element);
100: }
101:
102: public TestComponent getRootComponent() {
103: return testRoot;
104: }
105:
106: private boolean faultInSyncUpdater = false;
107:
108: public void injectFaultInSyncUpdater() {
109: faultInSyncUpdater = true;
110: }
111:
112: protected ComponentUpdater<TestComponent> getComponentUpdater() {
113: if (faultInSyncUpdater) {
114: faultInSyncUpdater = false;
115: Object npe = null;
116: npe.getClass();
117: }
118: return new TestComponentUpdater();
119: }
120:
121: private boolean faultInFindComponent = false;
122:
123: public void injectFaultInFindComponent() {
124: faultInFindComponent = true;
125: }
126:
127: public DocumentComponent findComponent(List<Element> pathFromRoot) {
128: if (faultInFindComponent) {
129: faultInFindComponent = false;
130: Object npe = null;
131: npe.getClass();
132: }
133: return super .findComponent(pathFromRoot);
134: }
135:
136: private boolean faultInEventFiring = false;
137:
138: public void injectFaultInEventFiring() {
139: faultInEventFiring = true;
140: }
141:
142: private class FaultGenerator implements PropertyChangeListener {
143: public void propertyChange(PropertyChangeEvent evt) {
144: if (faultInEventFiring) {
145: faultInEventFiring = false;
146: Object foo = null;
147: foo.getClass();
148: }
149: }
150: }
151:
152: public static ModelSource createModelSource(Document doc) {
153: Lookup lookup = Lookups.fixed(new Object[] { doc }); //maybe later a simple catalog
154: return new ModelSource(lookup, true);
155: }
156:
157: private static Set<QName> qnames = null;
158:
159: public Set<QName> getQNames() {
160: if (qnames == null) {
161: qnames = new HashSet<QName>();
162: qnames.add(TestComponent.A.QNAME);
163: qnames.add(TestComponent.Aa.QNAME);
164: qnames.add(TestComponent.B.QNAME);
165: qnames.add(TestComponent.C.QNAME);
166: qnames.add(TestComponent.D.QNAME);
167: qnames.add(TestComponent.E.QNAME);
168: qnames.add(new QName(TestComponent.NS_URI, "test"));
169: }
170: return qnames;
171: }
172:
173: public ComponentFactory<TestComponent> getFactory() {
174: return new ComponentFactory<TestComponent>() {
175: public TestComponent create(Element child,
176: TestComponent parent) {
177: return TestModel.this .createComponent(parent, child);
178: }
179: };
180: }
181:
182: public TestComponent.A createA(TestComponent parent) {
183: QName q = TestComponent.A.QNAME;
184: Element e = this .getDocument().createElementNS(
185: q.getNamespaceURI(), q.getLocalPart());
186: return (TestComponent.A) TestComponent.createComponent(this ,
187: parent, e);
188: }
189:
190: public TestComponent.Aa createAa(TestComponent parent) {
191: QName q = TestComponent.Aa.QNAME;
192: Element e = this .getDocument().createElementNS(
193: q.getNamespaceURI(), q.getLocalPart());
194: return (TestComponent.Aa) TestComponent.createComponent(this ,
195: parent, e);
196: }
197:
198: public TestComponent.B createB(TestComponent parent) {
199: QName q = TestComponent.B.QNAME;
200: Element e = this .getDocument().createElementNS(
201: q.getNamespaceURI(), q.getLocalPart());
202: return (TestComponent.B) TestComponent.createComponent(this ,
203: parent, e);
204: }
205:
206: public TestComponent.C createC(TestComponent parent) {
207: QName q = TestComponent.C.QNAME;
208: Element e = this .getDocument().createElementNS(
209: q.getNamespaceURI(), q.getLocalPart());
210: return (TestComponent.C) TestComponent.createComponent(this ,
211: parent, e);
212: }
213:
214: public TestComponent.D createD(TestComponent parent) {
215: QName q = TestComponent.D.QNAME;
216: Element e = this .getDocument().createElementNS(
217: q.getNamespaceURI(), q.getLocalPart());
218: return (TestComponent.D) TestComponent.createComponent(this,
219: parent, e);
220: }
221: }
|