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.midp.actions;
042:
043: import org.netbeans.modules.vmd.api.model.DesignComponent;
044: import org.netbeans.modules.vmd.api.model.DesignDocument;
045: import org.netbeans.modules.vmd.api.model.Presenter;
046: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter;
047: import org.netbeans.modules.vmd.api.model.presenters.actions.AddActionItem;
048: import org.netbeans.modules.vmd.api.model.presenters.actions.AddActionPresenter;
049: import org.netbeans.modules.vmd.midp.components.MidpDocumentSupport;
050: import org.openide.util.NbBundle;
051:
052: import javax.swing.*;
053: import java.awt.event.ActionEvent;
054: import java.lang.ref.WeakReference;
055: import java.util.ArrayList;
056: import java.util.Collection;
057: import java.util.Collections;
058: import java.util.List;
059:
060: /**
061: * @author Karol Harezlak
062: */
063: abstract class UnusedCommandsAddActionPresenter extends
064: AddActionPresenter {
065:
066: //TODO Because of havy use of this class created actions should be somehow cashed and reuse!!
067:
068: public static final String DISPLAY_NAME_ADD = NbBundle.getMessage(
069: UnusedCommandsAddActionPresenter.class,
070: "NAME_UnusedCommandsAddActionPresenter"); //NOI18N
071:
072: public static Presenter createForDisplayable(String displayName,
073: int order) {
074: return new UnusedCommandsAddActionPresenter(displayName, order) {
075: private DesignComponent selectedCommandSource;
076:
077: protected synchronized void insideActionPerformed(
078: final DesignComponent unusedCommandComponent) {
079: final DesignDocument document = unusedCommandComponent
080: .getDocument();
081: document.getTransactionManager().writeAccess(
082: new Runnable() {
083: public void run() {
084: selectedCommandSource = MidpDocumentSupport
085: .attachCommandToDisplayable(
086: getComponent(),
087: unusedCommandComponent);
088: }
089: });
090: selectComponent(selectedCommandSource);
091: selectedCommandSource = null;
092: }
093: };
094: }
095:
096: public static Presenter createForItem(String displayName, int order) {
097: return new UnusedCommandsAddActionPresenter(displayName, order) {
098: private DesignComponent selectedCommandSource;
099:
100: protected synchronized void insideActionPerformed(
101: final DesignComponent unusedCommandComponent) {
102: final DesignDocument document = unusedCommandComponent
103: .getDocument();
104: document.getTransactionManager().writeAccess(
105: new Runnable() {
106: public void run() {
107: selectedCommandSource = MidpDocumentSupport
108: .attachCommandToItem(
109: getComponent(),
110: unusedCommandComponent);
111: }
112: });
113: selectComponent(selectedCommandSource);
114: selectedCommandSource = null;
115: }
116: };
117: }
118:
119: private static void selectComponent(
120: final DesignComponent selectedCommandSource) {
121: final DesignDocument document = selectedCommandSource
122: .getDocument();
123: document.getTransactionManager().writeAccess(new Runnable() {
124: public void run() {
125: document.setSelectedComponents(null, Collections
126: .singleton(selectedCommandSource));
127: }
128: });
129: }
130:
131: private Integer order;
132: private String displayName;
133: private List<Action> newAddActions;
134:
135: private UnusedCommandsAddActionPresenter(String displayName,
136: int order) {
137: this .order = order;
138: this .displayName = displayName;
139: }
140:
141: protected abstract void insideActionPerformed(
142: DesignComponent unusedCommandComponent);
143:
144: public String getName() {
145: return displayName;
146: }
147:
148: public Integer getOrder() {
149: return order;
150: }
151:
152: public AddActionItem[] getAddActionItems() {
153: if (newAddActions == null)
154: newAddActions = new ArrayList<Action>();
155: else
156: newAddActions.clear();
157: Collection<DesignComponent> unusedCommands = MidpDocumentSupport
158: .getAvailableCommandsForComponent(getComponent());
159: if (unusedCommands == null)
160: return null;
161: for (final DesignComponent unusedCommand : unusedCommands) {
162: final InfoPresenter infoPresenter = unusedCommand
163: .getPresenter(InfoPresenter.class);
164: if (infoPresenter == null)
165: throw new IllegalStateException(
166: "No Info Presenter for component: "
167: + unusedCommand); //NOI18N
168: AddActionItem item = createUnusedCommandAction(
169: new WeakReference<DesignComponent>(unusedCommand),
170: new WeakReference<InfoPresenter>(infoPresenter),
171: new WeakReference<UnusedCommandsAddActionPresenter>(
172: this ));
173: item.resolveAction(getComponent());
174: newAddActions.add(item);
175: }
176: return newAddActions.toArray(new AddActionItem[newAddActions
177: .size()]);
178: }
179:
180: private static AddActionItem createUnusedCommandAction(
181: final WeakReference<DesignComponent> unusedCommandComponent,
182: final WeakReference<InfoPresenter> infoPresenter,
183: final WeakReference<UnusedCommandsAddActionPresenter> presenter) {
184:
185: return new AddActionItem(unusedCommandComponent.get().getType()) {
186: private ImageIcon icon;
187:
188: public void actionPerformed(ActionEvent event) {
189: presenter.get().insideActionPerformed(
190: unusedCommandComponent.get());
191: }
192:
193: public void resolveAction(DesignComponent component) {
194: InfoPresenter presenter = infoPresenter.get();
195: icon = icon == null ? icon = new ImageIcon(presenter
196: .getIcon(InfoPresenter.IconType.COLOR_16x16))
197: : icon;
198: putValue(Action.NAME, presenter
199: .getDisplayName(InfoPresenter.NameType.PRIMARY));
200: putValue(Action.SMALL_ICON, icon);
201: }
202: };
203: }
204: }
|