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.DesignComponent;
044: import org.netbeans.modules.vmd.api.model.VersionDescriptor;
045: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter;
046: import org.openide.util.NbBundle;
047:
048: import java.util.*;
049:
050: /**
051: * This class is a default implementation of the version descriptor interface. This implementation deletes the component
052: * when it is incompatible with new abilities. The class can be extended and convertion methods could be overriden.
053: *
054: * @author David Kaspar
055: */
056: public abstract class DefaultVersionDescriptor implements
057: VersionDescriptor {
058:
059: private static final VersionDescriptor FOREVER_COMPATIBLE = new VersionDescriptor() {
060:
061: public boolean isCompatibleWith(VersionDescriptor descriptor) {
062: return true;
063: }
064:
065: public boolean isCompatibleWith(Collection<String> abilities) {
066: return true;
067: }
068:
069: public Set<String> getPreliminaryConvertMessages(
070: DesignComponent component,
071: Collection<String> oldAbilities,
072: Collection<String> newAbilities) {
073: return null;
074: }
075:
076: public void convertComponent(DesignComponent component,
077: Collection<String> oldAbilities,
078: Collection<String> newAbilities) {
079: }
080: };
081:
082: private Kind kind;
083: private String[] abilities;
084:
085: /**
086: * The kind of the abilities checking.
087: */
088: public enum Kind {
089:
090: /**
091: * If any ability of the version descriptor exists then version is resolved as compatible.
092: */
093: ANY,
094:
095: /**
096: * Only when all abilities of the version descriptor exist then version is resolved as compatible.
097: */
098: ALL
099: }
100:
101: /**
102: * Creates a abstract implementation of default version descriptor.
103: * @param kind the kind used for checking abilities
104: * @param abilities the abilities of the version descriptor
105: */
106: public DefaultVersionDescriptor(Kind kind, String[] abilities) {
107: assert kind != null && abilities != null;
108: this .kind = kind;
109: this .abilities = abilities;
110: }
111:
112: /**
113: * Returns whether this version descriptor is compatible with abilities based on the kind and the abilities of the version descriptor.
114: * @param abilities the collection of abilities
115: * @return true, if compatible
116: */
117: public final boolean isCompatibleWith(String[] abilities) {
118: if (kind == Kind.ALL) {
119: return Arrays.asList(abilities).containsAll(
120: Arrays.asList(this .abilities));
121: } else {
122: List<String> abilitiesList = Arrays.asList(abilities);
123: for (String ability : this .abilities) {
124: if (abilitiesList.contains(ability))
125: return true;
126: }
127: return false;
128: }
129: }
130:
131: /**
132: * Return a set of warning/error messages for notifying an user about conversion changes.
133: * The check is based on isCompatibleWith method and in the case that it is not compatible, it returns a message
134: * about a component removing.
135: * <p>
136: * This method is called before convertComponent method is called on any component in a document.
137: * @param component the component
138: * @param oldAbilities the collection of old abilities
139: * @param newAbilities the collection of new abilities
140: * @return a set of messages
141: */
142: // TODO - change this signature
143: public Set<String> getPreliminaryConvertMessages(
144: DesignComponent component, Collection<String> oldAbilities,
145: Collection<String> newAbilities) {
146: if (isCompatibleWith(newAbilities))
147: return null;
148: String htmlDisplayName = InfoPresenter
149: .getHtmlDisplayName(component);
150: if (htmlDisplayName == null)
151: htmlDisplayName = "#" + component.getComponentID(); // NOI18N
152: return Collections.singleton(NbBundle.getMessage(
153: DefaultVersionDescriptor.class,
154: "MSG_Convertion_RemoveComponent", htmlDisplayName)); // NOI18N
155: }
156:
157: /**
158: * Creates a version descriptor that is is always compatible.
159: * @return the "forever" version descriptor
160: */
161: public static VersionDescriptor createForeverCompatibleVersionDescriptor() {
162: return FOREVER_COMPATIBLE;
163: }
164:
165: }
|