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.awt.event.ActionEvent;
045: import java.util.ArrayList;
046: import java.util.Arrays;
047: import java.util.Collection;
048: import java.util.HashSet;
049: import java.util.List;
050: import java.util.Map;
051: import java.util.Set;
052: import java.util.TreeMap;
053:
054: import javax.swing.AbstractAction;
055: import javax.swing.Action;
056:
057: import org.netbeans.modules.vmd.api.model.DescriptorRegistry;
058: import org.netbeans.modules.vmd.api.model.DesignComponent;
059: import org.netbeans.modules.vmd.api.model.DesignDocument;
060: import org.netbeans.modules.vmd.api.model.Presenter;
061: import org.netbeans.modules.vmd.api.model.TypeID;
062: import org.netbeans.modules.vmd.model.actions.ActionsPresenterForwarder;
063:
064: /**
065: *
066: * @author Karol Harezlak
067: */
068: public final class ActionsSupport {
069:
070: static final String SEPERATOR_KEY = "#SEPARATOR#"; //NOI18N
071: static final Set<Action> actionsToDelete = new HashSet<Action>();
072:
073: private static final TypeID[] EMPTY_TYPEID_ARRAY = new TypeID[0];
074:
075: public static synchronized Action[] createAddActionArray(
076: final DesignComponent component) {
077: return createAddActionArray(component, EMPTY_TYPEID_ARRAY);
078: }
079:
080: public static synchronized Action[] createAddActionArray(
081: final DesignComponent component,
082: final TypeID... filtersTypeID) {
083: final List<Action> actions = new ArrayList<Action>();
084:
085: component.getDocument().getTransactionManager().readAccess(
086: new Runnable() {
087: public void run() {
088: if (!component.getPresenters(
089: AddActionPresenter.class).isEmpty()) {
090: Map<Integer, Map<String, List<Action>>> sortedMaps = new TreeMap<Integer, Map<String, List<Action>>>();
091: DescriptorRegistry registry = component
092: .getDocument()
093: .getDescriptorRegistry();
094:
095: //Sorting presenters by the order and name of the AddActionPresenter
096: for (AddActionPresenter presenter : component
097: .getPresenters(AddActionPresenter.class)) {
098: AddActionItem[] addActionItems = presenter
099: .getAddActionItems();
100: if (addActionItems == null
101: || addActionItems.length < 1)
102: continue;
103:
104: Integer order = presenter.getOrder() == null ? Integer.MAX_VALUE
105: : presenter.getOrder();
106:
107: if (sortedMaps.get(order) == null) {
108: sortedMaps
109: .put(
110: order,
111: new TreeMap<String, List<Action>>());
112: sortedMaps
113: .get(order)
114: .put(
115: presenter.getName(),
116: new ArrayList<Action>(
117: Arrays
118: .asList(addActionItems)));
119: } else if (sortedMaps.get(order) != null
120: && sortedMaps.get(order).get(
121: presenter.getName()) == null) {
122: sortedMaps
123: .get(order)
124: .put(
125: presenter.getName(),
126: new ArrayList<Action>(
127: Arrays
128: .asList(addActionItems)));
129: } else
130: sortedMaps
131: .get(order)
132: .get(presenter.getName())
133: .addAll(
134: Arrays
135: .asList(addActionItems));
136: }
137: // Creating of list of AddActionPresenter action list with separators and filtring it is necessary
138: for (Map<String, List<Action>> sortedMap : sortedMaps
139: .values()) {
140: for (String name : sortedMap.keySet()) {
141: if (filtersTypeID.length == 0) {
142: if (name != null)
143: actions
144: .add(new SeperatorAction(
145: name));
146: actions.addAll(sortedMap
147: .get(name));
148: } else {
149: boolean removeCategory = true;
150: if (name != null)
151: actions
152: .add(new SeperatorAction(
153: name));
154: for (Action action : sortedMap
155: .get(name)) {
156: for (TypeID typeID : filtersTypeID) {
157: TypeID actionTypeID = (TypeID) action
158: .getValue(AddActionItem.TYPEID_KEY);
159: if (registry
160: .isInHierarchy(
161: typeID,
162: actionTypeID)) {
163: actions.add(action);
164: removeCategory = false;
165: }
166: }
167: }
168: if (name != null
169: && removeCategory)
170: actions.remove(actions
171: .size() - 1);
172: }
173: }
174: }
175: }
176: }
177: });
178:
179: return actions.toArray(new Action[actions.size()]);
180: }
181:
182: public static synchronized Action[] createActionsArray(
183: final DesignComponent component) {
184: final List<Action> actions = new ArrayList<Action>();
185:
186: component.getDocument().getTransactionManager().readAccess(
187: new Runnable() {
188: public void run() {
189: Map<Integer, List<Action>> sortedLists = new TreeMap<Integer, List<Action>>();
190:
191: DesignDocument document = null;
192: Collection<? extends ActionsPresenter> presenters = component
193: .getPresenters(ActionsPresenter.class);
194: if (presenters == null)
195: return;
196: for (ActionsPresenter presenter : presenters) {
197: List<Action> actions = presenter
198: .getActions();
199: if (actions == null || actions.isEmpty())
200: continue;
201:
202: DesignComponent relatedComponent = presenter
203: .getRelatedComponent();
204: assert relatedComponent != null : "No related component for ActionsPresenter "
205: + presenter;//NOI18N
206: for (Action action : actions) {
207: if (action instanceof ActionContext)
208: ((ActionContext) action)
209: .setComponent(relatedComponent);
210: }
211:
212: if (document != null)
213: assert document == relatedComponent
214: .getDocument();
215: else
216: document = relatedComponent
217: .getDocument();
218:
219: Integer order = presenter.getOrder();
220: if (order == null)
221: order = Integer.MAX_VALUE;
222:
223: List<Action> list = sortedLists.get(order);
224: if (list == null)
225: sortedLists.put(order,
226: new ArrayList<Action>(actions));
227: else
228: list.addAll(actions);
229: }
230: for (List<Action> list : sortedLists.values()) {
231: for (Action action : list) {
232: actions.add(action);
233: }
234: actions.add(null);
235: }
236: }
237: });
238:
239: return actions.toArray(new Action[actions.size()]);
240: }
241:
242: public static synchronized Collection<Presenter> createByReference(
243: final String referencePropertyName,
244: Class... actionsToInherit) {
245: HashSet presenters = new HashSet();
246: for (Class actionClass : actionsToInherit) {
247: Presenter presenter = ActionsPresenterForwarder
248: .byReference(referencePropertyName, actionClass);
249: if (presenter != null)
250: presenters.add(presenter);
251: }
252: return presenters;
253: }
254:
255: public static synchronized Collection<Presenter> createByParent(
256: Class... actionsToInherit) {
257: HashSet presenters = new HashSet();
258: for (Class actionClass : actionsToInherit) {
259: Presenter presenter = ActionsPresenterForwarder
260: .byParent(actionClass);
261: if (presenter != null)
262: presenters.add(presenter);
263: }
264: return presenters;
265: }
266:
267: private static class SeperatorAction extends AbstractAction {
268:
269: public SeperatorAction(String name) {
270: putValue(Action.NAME, name);
271: putValue(SEPERATOR_KEY, SEPERATOR_KEY);
272: }
273:
274: public void actionPerformed(ActionEvent e) {
275: }
276:
277: @Override
278: public boolean isEnabled() {
279: return false;
280: }
281:
282: }
283:
284: }
|