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.collab.channel.filesharing.ui.actions;
042:
043: import org.openide.ErrorManager;
044: import org.openide.nodes.Node;
045: import org.openide.util.*;
046:
047: import java.awt.event.*;
048:
049: import java.beans.PropertyChangeListener;
050:
051: import java.util.ArrayList;
052: import java.util.HashMap;
053: import java.util.List;
054:
055: import javax.swing.Action;
056:
057: import org.netbeans.api.project.Project;
058: import org.netbeans.api.project.ProjectUtils;
059:
060: import org.netbeans.modules.collab.channel.filesharing.FilesharingConstants;
061: import org.netbeans.modules.collab.channel.filesharing.FilesharingContext;
062: import org.netbeans.modules.collab.channel.filesharing.context.ProjectContext;
063: import org.netbeans.modules.collab.channel.filesharing.event.ProjectPerformActionEvent;
064: import org.netbeans.modules.collab.channel.filesharing.eventlistener.FilesharingTimerTask;
065: import org.netbeans.modules.collab.channel.filesharing.eventlistener.ProjectPerformActionTimerTask;
066: import org.netbeans.modules.collab.channel.filesharing.mdc.EventContext;
067: import org.netbeans.modules.collab.channel.filesharing.ui.ProjectsRootNode.ProjectNode;
068: import org.netbeans.modules.collab.core.Debug;
069:
070: import org.netbeans.spi.project.ui.LogicalViewProvider;
071:
072: /**
073: * SyncAction
074: *
075: * @author Ayub Khan
076: * @version 1.0
077: */
078: public class CollabProjectAction //extends SystemAction
079: implements Action, FilesharingConstants {
080: ////////////////////////////////////////////////////////////////////////////
081: // Class variables
082: ////////////////////////////////////////////////////////////////////////////
083: public static String COMMAND_BUILD = "build";
084: public static String COMMAND_INSTALL = "install";
085: public static String COMMAND_REBUILD = "rebuild";
086: public static String COMMAND_RUN = "run";
087: private String name;
088: private Action projectAction;
089: private Project project;
090: private FilesharingContext context;
091: private Node projectNode;
092:
093: ////////////////////////////////////////////////////////////////////////////
094: // Instant variables
095: ////////////////////////////////////////////////////////////////////////////
096: private HashMap prop = new HashMap();
097: private List listeners = new ArrayList();
098: private boolean enabled;
099:
100: public CollabProjectAction(String name, Project project,
101: Action projectAction, FilesharingContext context,
102: Node projectNode) {
103: this .name = name;
104: this .project = project;
105: this .projectAction = projectAction;
106: this .context = context;
107: this .projectNode = projectNode;
108: putValue(Action.NAME, name);
109: }
110:
111: public String getName() {
112: return name;
113: }
114:
115: protected String iconResource() {
116: return "org/openide/resources/actions/empty.gif";
117: }
118:
119: public HelpCtx getHelpCtx() {
120: return HelpCtx.DEFAULT_HELP;
121: }
122:
123: protected boolean asynchronous() {
124: return true;
125: }
126:
127: private FilesharingContext getContext() {
128: return this .context;
129: }
130:
131: public String getProjectName() {
132: if (this .projectNode != null) {
133: return ((ProjectNode) this .projectNode).getName();
134: } else {
135: return null;
136: }
137: }
138:
139: public String getProjectOwner() {
140: if (this .projectNode != null) {
141: return ((ProjectNode) this .projectNode).getParentNode()
142: .getName();
143: } else {
144: return null;
145: }
146: }
147:
148: public void actionPerformed(ActionEvent actionEvent) {
149: Debug.out.println(name + ", actionPerformed"); //NoI18n
150:
151: if ((project != null) && (projectAction != null)) {
152: LogicalViewProvider lvp = (LogicalViewProvider) project
153: .getLookup().lookup(LogicalViewProvider.class);
154: Node[] nodes = new Node[] { lvp.createLogicalView() };
155: Debug.log("ProjectsRootNode",
156: "ProjectsRootNode, PRN createNodes length: "
157: + nodes.length);
158:
159: if (nodes[0].getLookup().lookup(Project.class) != project) {
160: // Various actions, badging, etc. are not going to work.
161: ErrorManager
162: .getDefault()
163: .log(
164: ErrorManager.WARNING,
165: "Warning - project1 "
166: + ProjectUtils.getInformation(
167: project).getName()
168: + " failed to supply itself in the lookup of the "
169: + "root node of its own logical view"); // NOI18N
170: }
171:
172: if (projectAction instanceof ContextAwareAction) {
173: projectAction = ((ContextAwareAction) projectAction)
174: .createContextAwareInstance(nodes[0]
175: .getLookup());
176: }
177:
178: if (projectAction != null) {
179: projectAction.actionPerformed(new ActionEvent(nodes[0],
180: ActionEvent.ACTION_PERFORMED, "")); // NOI18N
181: }
182: } else //remote action
183: {
184: sendMessageProjectPerformAction();
185: }
186: }
187:
188: private boolean sendMessageProjectPerformAction() {
189: EventContext evContext = new ProjectContext(
190: ProjectPerformActionEvent.getEventID(),
191: getProjectOwner(), getProjectName(),
192: new Action[] { this });
193:
194: /* send projectActionList message after a delay */
195: ProjectPerformActionTimerTask sendProjectPerformActionTimerTask = new ProjectPerformActionTimerTask(
196: getContext().getChannelEventNotifier(),
197: new ProjectPerformActionEvent(evContext), getContext());
198: getContext().addTimerTask(SEND_PROJECTPERFORMACTION_TIMER_TASK,
199: sendProjectPerformActionTimerTask);
200: sendProjectPerformActionTimerTask
201: .schedule(FilesharingTimerTask.PERIOD);
202:
203: return true;
204: }
205:
206: /**
207: * Adds a listener
208: *
209: * @param listener a listener to add
210: *
211: */
212: public void addPropertyChangeListener(
213: PropertyChangeListener listener) {
214: listeners.add(listener);
215: }
216:
217: /**
218: * Removes a listener.
219: * @param listener a listener to remove
220: */
221: public void removePropertyChangeListener(
222: PropertyChangeListener listener) {
223: listeners.remove(listener);
224: }
225:
226: public boolean isEnabled() {
227: return enabled;
228: }
229:
230: public void putValue(String key, Object value) {
231: prop.put(key, value);
232: }
233:
234: public Object getValue(String key) {
235: return prop.get(key);
236: }
237:
238: public void setEnabled(boolean b) {
239: enabled = b;
240: }
241: }
|