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.vmd.api.model.common;
042:
043: import org.netbeans.modules.vmd.api.model.*;
044:
045: import java.util.*;
046:
047: /**
048: * @author David Kaspar
049: */
050: public class ValidatorPresenter extends DynamicPresenter {
051:
052: // WARNING - "inside-main-tree" validator test is not covered completely because Validator is checked on changed components only
053: // for proper work - it requires that all components (at least in main-tree) will be checked at the end of transaction
054:
055: private boolean mustHaveDescriptor;
056: private boolean referencesFromMainTreeOnly;
057: private boolean allChildrenMustBeInProperties;
058: private ArrayList<String> propertiesUsingChildrenOnly = new ArrayList<String>();
059: private List<String> propertiesUsingChildrenOnlyUm = Collections
060: .unmodifiableList(propertiesUsingChildrenOnly);
061: private ArrayList<TypeID> validChildrenTypeIDs = new ArrayList<TypeID>();
062: private List<TypeID> validChildrenTypeIDsUm = Collections
063: .unmodifiableList(validChildrenTypeIDs);
064:
065: public final boolean isMustHaveDescriptor() {
066: return mustHaveDescriptor;
067: }
068:
069: public final ValidatorPresenter setMustHaveDescriptor(
070: boolean mustHaveDescriptor) {
071: this .mustHaveDescriptor = mustHaveDescriptor;
072: return this ;
073: }
074:
075: public final boolean hasReferencesFromMainTreeOnly() {
076: return referencesFromMainTreeOnly;
077: }
078:
079: public final ValidatorPresenter setReferencesFromMainTreeOnly(
080: boolean referencesFromMainTreeOnly) {
081: this .referencesFromMainTreeOnly = referencesFromMainTreeOnly;
082: return this ;
083: }
084:
085: public final boolean hasAllChildrenMustBeInProperties() {
086: return allChildrenMustBeInProperties;
087: }
088:
089: public final ValidatorPresenter setAllChildrenMustBeInProperties(
090: boolean allChildrenMustBeInProperties) {
091: this .allChildrenMustBeInProperties = allChildrenMustBeInProperties;
092: return this ;
093: }
094:
095: public final List<String> getPropertiesUsingChildrenOnly() {
096: return propertiesUsingChildrenOnlyUm;
097: }
098:
099: public final ValidatorPresenter addPropertiesUsingChildrenOnly(
100: String... propertyNames) {
101: for (String propertyName : propertyNames)
102: propertiesUsingChildrenOnly.add(propertyName);
103: return this ;
104: }
105:
106: public final List<TypeID> getValidChildrenTypeIDs() {
107: return validChildrenTypeIDsUm;
108: }
109:
110: public final ValidatorPresenter addValidChildrenTypeID(
111: TypeID... typeIDs) {
112: for (TypeID typeID : typeIDs)
113: validChildrenTypeIDs.add(typeID);
114: return this ;
115: }
116:
117: public final void checkValidity() {
118: assert checkValidatyAssert();
119:
120: try {
121: checkCustomValidity();
122: } catch (ThreadDeath td) {
123: throw td;
124: } catch (Throwable th) {
125: Debug.error(th);
126: }
127: }
128:
129: private boolean checkValidatyAssert() {
130: DesignComponent component = getComponent();
131: assert component != null;
132:
133: if (mustHaveDescriptor)
134: assert component.getComponentDescriptor() != null;
135:
136: if (validChildrenTypeIDs.size() > 0) {
137: DescriptorRegistry registry = component.getDocument()
138: .getDescriptorRegistry();
139: Collection<DesignComponent> children = component
140: .getComponents();
141: mainloop: for (DesignComponent child : children) {
142: for (TypeID typeID : validChildrenTypeIDs) {
143: if (registry.isInHierarchy(typeID, child.getType()))
144: continue mainloop;
145: }
146: assert false;
147: }
148: }
149:
150: HashSet<DesignComponent> references = new HashSet<DesignComponent>();
151: if (hasReferencesFromMainTreeOnly()
152: || hasAllChildrenMustBeInProperties()) {
153: ComponentDescriptor descriptor = component
154: .getComponentDescriptor();
155:
156: Collection<PropertyDescriptor> propertyDescriptors = descriptor
157: .getPropertyDescriptors();
158: for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
159: PropertyValue value = component
160: .readProperty(propertyDescriptor.getName());
161: Debug.collectAllComponentReferences(value, references);
162: }
163: }
164:
165: if (hasReferencesFromMainTreeOnly()) {
166: DesignComponent rootComponent = component.getDocument()
167: .getRootComponent();
168: for (DesignComponent reference : references) {
169: for (;;) {
170: DesignComponent parentComponent = reference
171: .getParentComponent();
172: if (parentComponent == null)
173: break;
174: reference = parentComponent;
175: }
176: assert reference == rootComponent;
177: }
178: }
179:
180: if (hasAllChildrenMustBeInProperties()) {
181: Collection<DesignComponent> children = component
182: .getComponents();
183: for (DesignComponent child : children)
184: assert references.contains(child);
185: }
186:
187: if (!propertiesUsingChildrenOnly.isEmpty()) {
188: references = new HashSet<DesignComponent>();
189: for (String propertyName : propertiesUsingChildrenOnly)
190: Debug.collectAllComponentReferences(component
191: .readProperty(propertyName), references);
192: HashSet<DesignComponent> children = new HashSet<DesignComponent>(
193: component.getComponents());
194: for (DesignComponent reference : references)
195: assert children.contains(reference);
196: }
197:
198: return true;
199: }
200:
201: public final void notifyAttached(DesignComponent component) {
202: }
203:
204: public final void notifyDetached(DesignComponent component) {
205: }
206:
207: public final DesignEventFilter getEventFilter() {
208: return null;
209: }
210:
211: public void designChanged(DesignEvent event) {
212: }
213:
214: public void presenterChanged(PresenterEvent event) {
215: }
216:
217: protected void checkCustomValidity() {
218: }
219:
220: }
|