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.vmd.api.model.presenters.actions;
043:
044: import java.util.ArrayList;
045: import java.util.Collections;
046: import java.util.Comparator;
047: import java.util.List;
048:
049: import javax.swing.Action;
050:
051: import org.netbeans.modules.vmd.api.model.ComponentDescriptor;
052: import org.netbeans.modules.vmd.api.model.ComponentProducer;
053: import org.netbeans.modules.vmd.api.model.DesignDocument;
054: import org.netbeans.modules.vmd.api.model.Presenter;
055: import org.netbeans.modules.vmd.api.model.TypeID;
056: import org.netbeans.modules.vmd.api.model.common.AcceptSupport;
057: import org.openide.util.NbBundle;
058:
059: /**
060: *
061: * @author Karol Harezlak
062: */
063: public abstract class AddActionPresenter extends Presenter {
064:
065: public static final String ADD_ACTION = NbBundle.getMessage(
066: AddActionPresenter.class, "NAME_AddActionPresenter"); //NOI18N
067:
068: // TODO - move this method to a ActionsPresenterFactory class and hide this class
069: public static final Presenter create(Integer order, TypeID... types) {
070: return create(null, order, types);
071: }
072:
073: // TODO - move this method to a ActionsPresenterFactory class and hide this class
074: public final static Presenter create(final String name,
075: final int order, final TypeID... types) {
076:
077: return new AddActionPresenter() {
078: public Integer getOrder() {
079: return order;
080: }
081:
082: public String getName() {
083: return name;
084: }
085:
086: public AddActionItem[] getAddActionItems() {
087: List<Action> actions = new ArrayList<Action>();
088: DesignDocument document = getComponent().getDocument();
089: for (TypeID type : types) {
090: for (ComponentDescriptor descriptor : document
091: .getDescriptorRegistry()
092: .getComponentDescriptors()) {
093: if (getComponent().getDocument()
094: .getDescriptorRegistry().isInHierarchy(
095: type,
096: descriptor.getTypeDescriptor()
097: .getThisType())) {
098: for (ComponentProducer producer : document
099: .getDescriptorRegistry()
100: .getComponentProducers()) {
101: if (producer
102: .getMainComponentTypeID()
103: .equals(
104: descriptor
105: .getTypeDescriptor()
106: .getThisType())
107: && AcceptSupport.isAcceptable(
108: getComponent(),
109: producer, null)) {
110: Boolean isValid = producer
111: .checkValidity(document,
112: true);
113: if (isValid != null && isValid)
114: actions.add(AddActionItem
115: .getInstance(
116: getComponent(),
117: producer));
118: }
119: }
120: }
121: }
122: }
123: Collections.sort(actions, ACTIONS_COMPARATOR);
124: return actions
125: .toArray(new AddActionItem[actions.size()]);
126: }
127:
128: };
129: }
130:
131: public abstract Integer getOrder();
132:
133: public abstract String getName();
134:
135: public abstract AddActionItem[] getAddActionItems();
136:
137: // TODO - move to ActionsPresenterFactory or ActionsSupport class
138: protected static final Comparator<Action> ACTIONS_COMPARATOR = new Comparator<Action>() {
139: public int compare(Action s1, Action s2) {
140: String name1 = s1.getValue(Action.NAME).toString();
141: String name2 = s2.getValue(Action.NAME).toString();
142:
143: return name1.compareTo(name2);
144: }
145: };
146:
147: }
|