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 org.netbeans.modules.vmd.api.model.ComponentProducer;
045: import org.netbeans.modules.vmd.api.model.DesignComponent;
046: import org.netbeans.modules.vmd.api.model.TypeID;
047: import org.netbeans.modules.vmd.api.model.common.AcceptSupport;
048: import org.openide.util.Utilities;
049:
050: import javax.swing.*;
051: import java.awt.*;
052: import java.awt.event.ActionEvent;
053: import java.lang.ref.WeakReference;
054: import java.util.Collections;
055: import java.util.Map;
056: import java.util.WeakHashMap;
057: import org.netbeans.modules.vmd.api.model.DesignDocument;
058:
059: /**
060: *
061: * @author Karol Harezlak
062: */
063:
064: public abstract class AddActionItem extends AbstractAction {
065:
066: public static final String TYPEID_KEY = "typeID"; //NOI18N
067:
068: private static Map<ComponentProducer, AddActionItem> instances = new WeakHashMap<ComponentProducer, AddActionItem>();
069:
070: public static final AddActionItem getInstance(
071: final DesignComponent component,
072: final ComponentProducer producer) {
073: AddActionItem action = instances.get(producer);
074: if (action != null) {
075: action.resolveAction(component);
076: return action;
077: }
078: action = create(component, producer);
079: instances.put(producer, action);
080:
081: return action;
082: }
083:
084: private ImageIcon icon;
085:
086: protected AddActionItem(TypeID typeID) {
087: putValue(TYPEID_KEY, typeID);
088: }
089:
090: private AddActionItem(TypeID typeID, DesignComponent component,
091: ComponentProducer producer) {
092: String smallIcon = producer.getPaletteDescriptor()
093: .getSmallIcon();
094: Image image = smallIcon != null ? Utilities
095: .loadImage(smallIcon) : null;
096: putValue(TYPEID_KEY, typeID);
097: putValue(Action.NAME, producer.getPaletteDescriptor()
098: .getDisplayName());
099: if (image != null)
100: putValue(Action.SMALL_ICON, new ImageIcon(image));
101: resolveAction(component);
102: }
103:
104: public abstract void resolveAction(DesignComponent component);
105:
106: private static AddActionItem create(
107: final DesignComponent component,
108: final ComponentProducer producer) {
109: return new AddActionItem(producer.getMainComponentTypeID(),
110: component, producer) {
111: private WeakReference<DesignComponent> component;
112: //selectedComponent dont have to be weak is reseted right after is used.
113: private DesignComponent selectedComponent;
114:
115: public synchronized void actionPerformed(ActionEvent e) {
116: if (producer == null)
117: throw new IllegalArgumentException(
118: "Null argument typeID"); //NOI18N
119: final DesignDocument document = this .component.get()
120: .getDocument();
121: document.getTransactionManager().writeAccess(
122: new Runnable() {
123: public void run() {
124: ComponentProducer.Result result = AcceptSupport
125: .accept(component.get(),
126: producer, null);
127: selectedComponent = result
128: .getMainComponent();
129: }
130: });
131: document.getTransactionManager().writeAccess(
132: new Runnable() {
133: public void run() {
134: document
135: .setSelectedComponents(
136: null,
137: Collections
138: .singleton(selectedComponent));
139: selectedComponent = null;
140: }
141: });
142: }
143:
144: public void resolveAction(DesignComponent component) {
145: this .component = new WeakReference<DesignComponent>(
146: component);
147: }
148: };
149:
150: }
151:
152: }
|