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.BorderLayout;
045:
046: import java.awt.event.ActionEvent;
047: import java.lang.ref.WeakReference;
048: import java.util.Arrays;
049: import java.util.Collection;
050: import java.util.HashMap;
051: import java.util.Map;
052:
053: import javax.swing.AbstractAction;
054: import javax.swing.Action;
055: import javax.swing.JComponent;
056: import javax.swing.JLabel;
057: import javax.swing.JMenu;
058: import javax.swing.JPanel;
059: import javax.swing.SwingConstants;
060:
061: import org.netbeans.modules.vmd.api.model.DesignComponent;
062: import org.netbeans.modules.vmd.api.model.TypeID;
063: import org.openide.util.HelpCtx;
064: import org.openide.util.NbBundle;
065: import org.openide.util.actions.Presenter;
066:
067: /**
068: *
069: * @author Karol Harezlak
070: */
071:
072: public final class AddAction extends AbstractAction implements
073: ActionContext, Presenter.Popup {
074:
075: public static final String DISPLAY_NAME = NbBundle.getMessage(
076: AddAction.class, "Name_AddAction"); //NOI18N
077:
078: private static Map<Collection<TypeID>, AddAction> instances = new HashMap<Collection<TypeID>, AddAction>();
079:
080: public static final AddAction getInstance(TypeID... filters) {
081: Collection<TypeID> filtersList = Arrays.asList(filters);
082:
083: for (Collection<TypeID> key : instances.keySet()) {
084: if (key.size() == filtersList.size()
085: && key.containsAll(filtersList))
086: return instances.get(key);
087: }
088: instances.put(filtersList, new AddAction(filters));
089: return instances.get(filtersList);
090: }
091:
092: private JMenu menu;
093: private WeakReference<DesignComponent> component;
094: private boolean enabled;
095: private TypeID[] filtersTypeID = new TypeID[0];
096:
097: private AddAction(TypeID... filtersTypeID) {
098: this .putValue(Action.NAME, DISPLAY_NAME);
099: if (filtersTypeID != null)
100: this .filtersTypeID = filtersTypeID;
101: }
102:
103: public void actionPerformed(ActionEvent e) {
104: }
105:
106: public HelpCtx getHelpCtx() {
107: return HelpCtx.DEFAULT_HELP;
108: }
109:
110: public JMenu getPopupPresenter() {
111: return getMenu();
112: }
113:
114: private JMenu getMenu() {
115: if (component == null || component.get() == null)
116: throw new IllegalStateException(
117: "This action has to be attached to exisitng DesignComponent");//NOI18N
118:
119: if (menu == null)
120: menu = new JMenu(DISPLAY_NAME);
121: else
122: menu.removeAll();
123:
124: component.get().getDocument().getTransactionManager()
125: .readAccess(new Runnable() {
126: public void run() {
127: menu.setEnabled(isEnabled());
128: }
129: });
130:
131: for (Action addAction : ActionsSupport.createAddActionArray(
132: component.get(), filtersTypeID)) {
133: if (addAction.getValue(ActionsSupport.SEPERATOR_KEY) == null)
134: menu.add(addAction);
135: else
136: menu.add(getSeparator((String) addAction
137: .getValue(Action.NAME)));
138: }
139: return menu;
140: }
141:
142: @Override
143: public boolean isEnabled() {
144: if (component == null || component.get() == null)
145: throw new IllegalStateException(
146: "This action has to be attache to DesignComponent, component can not be null");//NOI18N
147:
148: if (component.get().getDocument().getSelectedComponents()
149: .size() > 1
150: || component.get().getPresenters(
151: AddActionPresenter.class).isEmpty())
152: enabled = false;
153: for (AddActionPresenter presenter : component.get()
154: .getPresenters(AddActionPresenter.class)) {
155: AddActionItem[] addActionItems = presenter
156: .getAddActionItems();
157: if (addActionItems != null && addActionItems.length > 0)
158: enabled = true;
159: }
160: return enabled;
161: }
162:
163: private JComponent getSeparator(String name) {
164: JPanel panel = new JPanel(new BorderLayout());
165: JLabel label = new JLabel(name);
166:
167: //panel.setBackground(new Color(230,230,230));
168: label.setHorizontalAlignment(SwingConstants.CENTER);
169: //label.setFont(label.getFont().deriveFont(Font.PLAIN, 12));
170: panel.add(label, BorderLayout.CENTER);
171: return panel;
172: }
173:
174: public void setComponent(DesignComponent component) {
175: this .component = new WeakReference<DesignComponent>(component);
176: }
177:
178: }
|