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.inspector.common;
043:
044: import java.awt.event.ActionEvent;
045: import java.lang.ref.WeakReference;
046: import javax.swing.SwingUtilities;
047: import org.netbeans.modules.vmd.api.inspector.InspectorFolderPresenter;
048: import org.netbeans.modules.vmd.api.model.Debug;
049: import org.netbeans.modules.vmd.api.model.DesignComponent;
050: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter;
051: import org.netbeans.modules.vmd.api.model.presenters.actions.ActionContext;
052: import org.openide.DialogDisplayer;
053: import org.openide.NotifyDescriptor;
054: import org.openide.util.HelpCtx;
055: import org.openide.util.NbBundle;
056: import org.openide.util.actions.SystemAction;
057:
058: /**
059: *
060: * @author Karol Harezlak
061: */
062:
063: /**
064: * This class provides GUI to change DesignComponent display name and DesignComponent's
065: * property which keeps information about DesignCompoent name. It needs to be attache to the DesignComponent
066: * through ActionsPresenter.
067: */
068: public final class RenameAction extends SystemAction implements
069: ActionContext {
070:
071: public static final String DISPLAY_NAME = NbBundle.getMessage(
072: RenameAction.class, "NAME_RenameAction"); //NOI18N
073:
074: private NotifyDescriptor.InputLine descriptor;
075: private boolean canRename;
076: private WeakReference<DesignComponent> component;
077:
078: public void actionPerformed(ActionEvent e) {
079: if (component == null)
080: return;
081:
082: SwingUtilities.invokeLater(new Runnable() {
083: public void run() {
084: if (component == null || component.get() == null)
085: throw new IllegalArgumentException(
086: "No DesignComponent attached to DeleteAction"); //NOI18N
087: component.get().getDocument().getTransactionManager()
088: .writeAccess(new Runnable() {
089: public void run() {
090: InfoPresenter presenter = component
091: .get().getPresenter(
092: InfoPresenter.class);
093: if (presenter == null) {
094: Debug
095: .warning("No necessary presenter for this operation - component: "
096: + component); //NOI18N
097: return;
098: }
099: getDialogDescriptor().setInputText(
100: presenter.getEditableName());
101: DialogDisplayer.getDefault().notify(
102: getDialogDescriptor());
103: if (((Integer) descriptor.getValue()) == 0
104: && descriptor.getInputText()
105: .trim().length() > 0) {
106: presenter
107: .setEditableName(descriptor
108: .getInputText()
109: .trim());
110: }
111: }
112: });
113: }
114: });
115: }
116:
117: private NotifyDescriptor.InputLine getDialogDescriptor() {
118: if (descriptor != null)
119: return descriptor;
120:
121: descriptor = new NotifyDescriptor.InputLine(
122: NbBundle.getMessage(RenameAction.class,
123: "TITLE_RenameQuestion"), NbBundle.getMessage(
124: RenameAction.class, "TITLE_RenameDialog")); //NOI18N
125:
126: return descriptor;
127: }
128:
129: public HelpCtx getHelpCtx() {
130: return HelpCtx.DEFAULT_HELP;
131: }
132:
133: @Override
134: public boolean isEnabled() {
135: if (component == null)
136: return false;
137:
138: component.get().getDocument().getTransactionManager()
139: .readAccess(new Runnable() {
140: public void run() {
141: if (component.get().getDocument()
142: .getSelectedComponents().size() > 1) {
143: canRename = false;
144: return;
145: }
146: InspectorFolderPresenter presenter = component
147: .get().getPresenter(
148: InspectorFolderPresenter.class);
149: if (presenter != null)
150: canRename = presenter.getFolder()
151: .canRename();
152: else
153: canRename = false;
154: }
155: });
156:
157: return canRename;
158: }
159:
160: public String getName() {
161: return DISPLAY_NAME;
162: }
163:
164: public void setComponent(DesignComponent component) {
165: this .component = new WeakReference<DesignComponent>(component);
166: }
167:
168: }
|