001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: /*
021: * Sun Public License Notice
022: *
023: * The contents of this file are subject to the Sun Public License
024: * Version 1.0 (the "License"). You may not use this file except in
025: * compliance with the License. A copy of the License is available at
026: * http://www.sun.com/
027: *
028: * The Original Code is NetBeans. The Initial Developer of the Original
029: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
030: * Microsystems, Inc. All Rights Reserved.
031: */
032:
033: package org.netbeans.modules.sql.project.ui;
034:
035: import java.awt.event.ActionEvent;
036: import java.beans.PropertyChangeEvent;
037: import java.beans.PropertyChangeListener;
038: import java.util.ArrayList;
039: import java.util.Arrays;
040: import java.lang.reflect.Method;
041: import java.util.List;
042: import java.util.ResourceBundle;
043: import java.util.Collections;
044: import java.util.StringTokenizer;
045:
046: import javax.swing.AbstractAction;
047: import javax.swing.Action;
048: import javax.swing.JSeparator;
049: import org.netbeans.modules.sql.project.SQLproProject;
050:
051: import org.openide.nodes.*;
052: import org.openide.util.*;
053: import org.openide.util.actions.SystemAction;
054:
055: import org.netbeans.api.project.Project;
056: import org.netbeans.api.project.ProjectUtils;
057: import org.netbeans.api.project.SourceGroup;
058: import org.netbeans.api.project.Sources;
059: import org.netbeans.spi.project.ui.support.CommonProjectActions;
060: import org.netbeans.spi.project.ActionProvider;
061: import org.netbeans.spi.project.SubprojectProvider;
062: import org.netbeans.spi.project.support.ant.AntProjectHelper;
063: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
064: import org.netbeans.spi.project.support.ant.ReferenceHelper;
065: import org.netbeans.spi.project.ui.LogicalViewProvider;
066: import org.netbeans.spi.project.ui.support.ProjectSensitiveActions;
067: import org.netbeans.spi.java.project.support.ui.BrokenReferencesSupport;
068: import org.netbeans.modules.compapp.projects.base.ui.customizer.IcanproProjectProperties;
069: import org.netbeans.modules.compapp.projects.base.ui.IcanproLogicalViewProvider;
070: import org.netbeans.modules.sql.project.wsdl.GenFiles;
071: import org.netbeans.modules.sql.project.wsdl.ActionImpl;
072: import org.netbeans.modules.compapp.projects.base.IcanproConstants;
073: import org.openide.loaders.DataFolder;
074: import org.openide.loaders.DataObject;
075: import org.openide.util.lookup.Lookups;
076: import org.openide.util.Lookup;
077:
078: /**
079: * Support for creating logical views.
080: * @author Petr Hrebejk
081: */
082: public class SQLproLogicalViewProvider implements LogicalViewProvider {
083:
084: private final Project project;
085: private final AntProjectHelper helper;
086: private final PropertyEvaluator evaluator;
087: private final SubprojectProvider spp;
088: private final ReferenceHelper resolver;
089:
090: public SQLproLogicalViewProvider(Project project,
091: AntProjectHelper helper, PropertyEvaluator evaluator,
092: SubprojectProvider spp, ReferenceHelper resolver) {
093: this .project = project;
094: assert project != null;
095: this .helper = helper;
096: assert helper != null;
097: this .evaluator = evaluator;
098: assert evaluator != null;
099: this .spp = spp;
100: assert spp != null;
101: this .resolver = resolver;
102: }
103:
104: public Node createLogicalView() {
105: return new DtelLogicalViewRootNode();
106: }
107:
108: public Node findPath(Node root, Object target) {
109: // XXX implement
110: return null;
111: }
112:
113: private static Lookup createLookup(Project project) {
114: DataFolder rootFolder = DataFolder.findFolder(project
115: .getProjectDirectory());
116: Sources sources = ProjectUtils.getSources(project);
117: List<SourceGroup> roots = new ArrayList<SourceGroup>();
118: SourceGroup[] javaRoots = sources
119: .getSourceGroups(SQLproProject.SOURCES_TYPE_ICANPRO);
120: roots.addAll(Arrays.asList(javaRoots));
121: if (roots.isEmpty()) {
122: SourceGroup[] sourceGroups = sources
123: .getSourceGroups(Sources.TYPE_GENERIC);
124: roots.addAll(Arrays.asList(sourceGroups));
125: }
126: DataFolder folder = DataFolder.findFolder(roots.get(0)
127: .getRootFolder());
128: rootFolder = folder != null ? folder : rootFolder;
129: // XXX Remove root folder after FindAction rewrite
130: return Lookups.fixed(new Object[] { project, rootFolder });
131: }
132:
133: // Private innerclasses ----------------------------------------------------
134:
135: private static final String[] BREAKABLE_PROPERTIES = new String[] {
136: IcanproProjectProperties.JAVAC_CLASSPATH,
137: IcanproProjectProperties.DEBUG_CLASSPATH,
138: IcanproProjectProperties.SRC_DIR, };
139:
140: public static boolean hasBrokenLinks(AntProjectHelper helper,
141: ReferenceHelper resolver) {
142: // return BrokenReferencesSupport.isBroken(helper, resolver, BREAKABLE_PROPERTIES,
143: // new String[] {IcanproProjectProperties.JAVA_PLATFORM});
144: return false;
145: }
146:
147: /** Filter node containin additional features for the J2SE physical
148: */
149: private final class DtelLogicalViewRootNode extends AbstractNode {
150:
151: private Action brokenLinksAction;
152: private boolean broken;
153:
154: public DtelLogicalViewRootNode() {
155: super (
156: new SQLproViews.LogicalViewChildren(helper,
157: evaluator), createLookup(project));
158: setIconBaseWithExtension("org/netbeans/modules/sql/project/ui/resources/sqlproProjectIcon.gif"); // NOI18N
159: setName(ProjectUtils.getInformation(project)
160: .getDisplayName());
161: if (hasBrokenLinks(helper, resolver)) {
162: broken = true;
163: brokenLinksAction = new BrokenLinksAction();
164: }
165: }
166:
167: public Action[] getActions(boolean context) {
168: if (context)
169: return super .getActions(true);
170: else
171: return getAdditionalActions();
172: }
173:
174: public boolean canRename() {
175: return false;
176: }
177:
178: // Private methods -------------------------------------------------
179:
180: private Action[] getAdditionalActions() {
181:
182: ResourceBundle bundle = NbBundle
183: .getBundle(SQLproLogicalViewProvider.class);
184:
185: List<Action> actions = new ArrayList<Action>();
186:
187: actions.add(CommonProjectActions.newFileAction());
188: actions.add(null);
189: actions.add(ProjectSensitiveActions.projectCommandAction(
190: ActionProvider.COMMAND_BUILD, bundle
191: .getString("LBL_BuildAction_Name"), null)); // NOI18N
192: actions
193: .add(ProjectSensitiveActions.projectCommandAction(
194: ActionProvider.COMMAND_REBUILD,
195: bundle.getString("LBL_RebuildAction_Name"),
196: null)); // NOI18N
197: actions.add(ProjectSensitiveActions.projectCommandAction(
198: ActionProvider.COMMAND_CLEAN, bundle
199: .getString("LBL_CleanAction_Name"), null)); // NOI18N
200: // null,
201: // ProjectSensitiveActions.projectCommandAction( IcanproConstants.COMMAND_REDEPLOY, bundle.getString( "LBL_RedeployAction_Name" ), null ), // NOI18N
202: // ProjectSensitiveActions.projectCommandAction( IcanproConstants.COMMAND_DEPLOY, bundle.getString( "LBL_DeployAction_Name" ), null ), // NOI18N
203: // actions.add(ProjectSensitiveActions.projectCommandAction( IcanproConstants.POPULATE_CATALOG, bundle.getString( "LBL_Populate_Catalog" ), null )); // NOI18N
204: actions.add(null);
205: actions.add(CommonProjectActions.setAsMainProjectAction());
206: actions.add(CommonProjectActions.openSubprojectsAction());
207: actions.add(CommonProjectActions.closeProjectAction());
208: actions.add(null);
209: actions.add(CommonProjectActions.renameProjectAction());
210: actions.add(CommonProjectActions.moveProjectAction());
211: actions.add(CommonProjectActions.copyProjectAction());
212: actions.add(CommonProjectActions.deleteProjectAction());
213: actions.add(null);
214: generateWSDL(actions);
215: actions.add(null);
216: actions.add(SystemAction
217: .get(org.openide.actions.FindAction.class));
218: addFromLayers(actions, "Projects/Actions");
219: actions.add(null);
220: actions.add(brokenLinksAction);
221: actions.add(CommonProjectActions.customizeProjectAction());
222:
223: return actions.toArray(new Action[actions.size()]);
224:
225: /* return new Action[] {
226: // disable new action at the top...
227: CommonProjectActions.newFileAction(),
228: null,
229: ProjectSensitiveActions.projectCommandAction( ActionProvider.COMMAND_BUILD, bundle.getString( "LBL_BuildAction_Name" ), null ), // NOI18N
230: ProjectSensitiveActions.projectCommandAction( ActionProvider.COMMAND_REBUILD, bundle.getString( "LBL_RebuildAction_Name" ), null ), // NOI18N
231: ProjectSensitiveActions.projectCommandAction( ActionProvider.COMMAND_CLEAN, bundle.getString( "LBL_CleanAction_Name" ), null ), // NOI18N
232: null,
233: CommonProjectActions.setAsMainProjectAction(),
234: CommonProjectActions.openSubprojectsAction(),
235: CommonProjectActions.closeProjectAction(),
236: null,
237: CommonProjectActions.renameProjectAction(),
238: CommonProjectActions.moveProjectAction(),
239: CommonProjectActions.copyProjectAction(),
240: CommonProjectActions.deleteProjectAction(),
241: null,
242: generateWSDL(),
243: null,
244: actions.add(SystemAction.get(org.openide.actions.FindAction.class),
245: // add versioning support
246: addFromLayers(actions, "Projects/Actions"),
247: actions.add(null),
248: actions.add(brokenLinksAction),
249: actions.add(CommonProjectActions.customizeProjectAction())
250: )};*/
251: }
252:
253: private void addFromLayers(List<Action> actions, String path) {
254: Lookup look = Lookups.forPath(path);
255: for (Object next : look.lookupAll(Object.class)) {
256: if (next instanceof Action) {
257: actions.add((Action) next);
258: } else if (next instanceof JSeparator) {
259: actions.add(null);
260: }
261: }
262: }
263:
264: private void generateWSDL(List<Action> actions) {
265: Object genFiles = SystemAction.findObject(GenFiles.class,
266: true);
267: try {
268: Object[] params = new Object[1];
269: params[0] = project;
270: Class[] cls = new Class[1];
271: cls[0] = Project.class;
272: params[0] = project;
273: Method meth = GenFiles.class.getMethod("setProject",
274: cls);
275: meth.invoke(genFiles, params);
276: } catch (Exception e) {
277: // TODO Auto-generated catch block
278: e.printStackTrace();
279: }
280: actions.add((Action) genFiles);
281: //genFiles.setProject(project);
282: // return (Action)genFiles;
283: }
284:
285: /** This action is created only when project has broken references.
286: * Once these are resolved the action is disabled.
287: */
288: private class BrokenLinksAction extends AbstractAction
289: implements PropertyChangeListener {
290:
291: public BrokenLinksAction() {
292: evaluator.addPropertyChangeListener(this );
293: putValue(Action.NAME, NbBundle.getMessage(
294: IcanproLogicalViewProvider.class,
295: "LBL_Fix_Broken_Links_Action"));
296: }
297:
298: public void actionPerformed(ActionEvent e) {
299: /* BrokenReferencesSupport.showCustomizer(helper, resolver, BREAKABLE_PROPERTIES, new String[]{IcanproProjectProperties.JAVA_PLATFORM});
300: if (!hasBrokenLinks(helper, resolver)) {
301: disable();
302: }*/
303: }
304:
305: public void propertyChange(PropertyChangeEvent evt) {
306: if (!broken) {
307: disable();
308: return;
309: }
310: broken = hasBrokenLinks(helper, resolver);
311: if (!broken) {
312: disable();
313: }
314: }
315:
316: private void disable() {
317: broken = false;
318: setEnabled(false);
319: evaluator.removePropertyChangeListener(this );
320: fireIconChange();
321: fireOpenedIconChange();
322: }
323:
324: }
325:
326: }
327:
328: /** Factory for project actions.<BR>
329: * XXX This class is a candidate for move to org.netbeans.spi.project.ui.support
330: */
331: public static class Actions {
332:
333: private Actions() {
334: } // This is a factory
335:
336: public static Action createAction(String key, String name,
337: boolean global) {
338: return new ActionImpl(key, name, global ? Utilities
339: .actionsGlobalContext() : null);
340: }
341:
342: private static class ActionImpl extends AbstractAction
343: implements ContextAwareAction {
344:
345: Lookup context;
346: String name;
347: String command;
348:
349: public ActionImpl(String command, String name,
350: Lookup context) {
351: super (name);
352: this .context = context;
353: this .command = command;
354: this .name = name;
355: }
356:
357: public void actionPerformed(ActionEvent e) {
358:
359: Project project = (Project) context
360: .lookup(Project.class);
361: ActionProvider ap = (ActionProvider) project
362: .getLookup().lookup(ActionProvider.class);
363:
364: ap.invokeAction(command, context);
365:
366: }
367:
368: public Action createContextAwareInstance(Lookup lookup) {
369: return new ActionImpl(command, name, lookup);
370: }
371: }
372:
373: }
374:
375: }
|