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: package org.netbeans.modules.xslt.project.nodes;
020:
021: import java.util.ArrayList;
022: import java.util.Collections;
023: import java.util.List;
024: import javax.swing.Action;
025: import org.netbeans.modules.xslt.project.XsltproConstants;
026: import org.netbeans.spi.project.ui.support.CommonProjectActions;
027: import org.openide.filesystems.FileAttributeEvent;
028: import org.openide.filesystems.FileEvent;
029:
030: import org.openide.filesystems.FileObject;
031: import org.openide.filesystems.FileRenameEvent;
032: import org.openide.loaders.DataFolder;
033: import org.openide.loaders.DataObjectNotFoundException;
034: import org.openide.nodes.Children;
035: import org.openide.nodes.FilterNode;
036: import org.openide.nodes.Node;
037: import org.openide.util.NbBundle;
038:
039: import org.netbeans.spi.project.support.ant.AntProjectHelper;
040: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
041:
042: import org.netbeans.modules.compapp.projects.base.ui.customizer.IcanproProjectProperties;
043: import static org.netbeans.modules.xslt.project.XsltproConstants.*;
044: import org.openide.filesystems.FileChangeListener;
045: import org.openide.loaders.DataObject;
046: import java.util.logging.Logger;
047: import javax.swing.event.ChangeEvent;
048: import javax.swing.event.ChangeListener;
049: import javax.swing.event.EventListenerList;
050: import org.netbeans.api.project.FileOwnerQuery;
051: import org.netbeans.api.project.Project;
052: import org.netbeans.api.project.ProjectUtils;
053: import org.netbeans.api.project.SourceGroup;
054: import org.netbeans.api.project.Sources;
055: import org.netbeans.api.queries.VisibilityQuery;
056: import org.openide.loaders.ChangeableDataFilter;
057: import org.openide.loaders.DataFilter;
058:
059: /**
060: *
061: * @author Vitaly Bychkov
062: * @version 1.0
063: */
064: public class IcanproViews {
065: private static Logger logger = Logger.getLogger(IcanproViews.class
066: .getName());
067:
068: private static final DataFilter NO_FOLDERS_FILTER = new NoFoldersDataFilter();
069:
070: private IcanproViews() {
071: }
072:
073: public static final class LogicalViewChildren extends Children.Keys
074: implements FileChangeListener {
075:
076: private static final String KEY_SOURCE_DIR = "srcDir"; // NOI18N
077: private static final String KEY_DOC_BASE = "docBase"; //NOI18N
078: private static final String KEY_EJBS = "ejbKey"; //NOI18N
079: private static final String WEBSERVICES_DIR = "webservicesDir"; // NOI18N
080: private static final String XSLT_TRANSFORM_NODE_KEY = "xsltTransformNodeKey"; // NOI18N
081: private static final String KEY_SETUP_DIR = "setupDir"; //NOI18N
082:
083: private AntProjectHelper helper;
084: private final PropertyEvaluator evaluator;
085: private FileObject projectDir;
086: private Project project;
087:
088: public LogicalViewChildren(AntProjectHelper helper,
089: PropertyEvaluator evaluator, Project project) {
090: assert helper != null;
091: this .helper = helper;
092: projectDir = helper.getProjectDirectory();
093: this .evaluator = evaluator;
094: this .project = project;
095: }
096:
097: @Override
098: protected void addNotify() {
099: super .addNotify();
100: projectDir.addFileChangeListener(this );
101: // TODO r
102: // projectDir.getFileObject("src").addFileChangeListener(this);
103: createNodes();
104: }
105:
106: public void reload() {
107: createNodes();
108: }
109:
110: private void createNodes() {
111: List<Object> l = new ArrayList<Object>();
112: /*
113: l.add(KEY_EJBS);
114: */
115:
116: DataFolder docBaseDir = getFolder(IcanproProjectProperties.META_INF);
117: if (docBaseDir != null) {
118: /*
119: l.add(KEY_DOC_BASE);
120: */
121: }
122:
123: DataFolder srcDir = getFolder(IcanproProjectProperties.SRC_DIR);
124: if (srcDir != null) {
125: l.add(KEY_SOURCE_DIR);
126: }
127:
128: FileObject setupFolder = getSetupFolder();
129: if (setupFolder != null && setupFolder.isFolder()) {
130: l.add(KEY_SETUP_DIR);
131: }
132: /*
133: l.add(WEBSERVICES_DIR);
134: */
135: // TODO r
136: // l.add(XSLT_TRANSFORM_NODE_KEY);
137: // TODO r
138: ////// FileObject xsltMapFo = getXsltmapFO();
139: //////// Lookup projectLookup = project.getLookup();
140: //////// System.out.println("projectLookup: "+projectLookup);
141: ////// if (xsltMapFo != null) {
142: ////// l.add(XSLT_TRANSFORM_NODE_KEY);
143: ////// }
144: if (l.size() > 0) {
145: setKeys(l);
146: }
147:
148: }
149:
150: private FileObject getSetupFolder() {
151: return projectDir.getFileObject("setup"); //NOI18N
152: }
153:
154: protected void removeNotify() {
155: setKeys(Collections.EMPTY_SET);
156: projectDir.removeFileChangeListener(this );
157: // todo r
158: // projectDir.getFileObject("src").removeFileChangeListener(this);
159: super .removeNotify();
160: }
161:
162: protected Node[] createNodes(Object key) {
163: List<Node> nodes = new ArrayList<Node>();
164: Node n = null;
165:
166: if (key == KEY_SOURCE_DIR) {
167: FileObject srcRoot = helper.resolveFileObject(evaluator
168: .getProperty(IcanproProjectProperties.SRC_DIR));
169: Project p = FileOwnerQuery.getOwner(srcRoot);
170: Sources s = ProjectUtils.getSources(p);
171: // TODO r | m
172: SourceGroup sgs[] = ProjectUtils.getSources(p)
173: .getSourceGroups(
174: XsltproConstants.SOURCES_TYPE_ICANPRO);
175: for (int i = 0; i < sgs.length; i++) {
176: if (sgs[i].contains(srcRoot)) {
177: try {
178: FileObject folder = sgs[i].getRootFolder();
179: DataObject dobj = DataObject.find(folder);
180: Children children = ((DataFolder) dobj)
181: .createNodeChildren(NO_FOLDERS_FILTER);
182: // todo m
183: ////// FileObject xsltmapFile = srcRoot.getFileObject(XsltproConstants.XSLTMAP_XML);
184: //////// n = new RootNode(dobj.getNodeDelegate(), (DataFolder) dobj);
185: ////// Node xsltTransformationsNode = NodeFactory.createXsltTransformationsNode(xsltmapFile);
186: ////// if (xsltTransformationsNode != null) {
187: ////// children.add(new Node[] {xsltTransformationsNode});
188: ////// }
189: //////
190: n = new RootNode(srcRoot, dobj
191: .getNodeDelegate(), children);
192: } catch (DataObjectNotFoundException ex) {
193: }
194: break;
195: }
196: }
197: }
198: //// else if (key == XSLT_TRANSFORM_NODE_KEY) {
199: //// FileObject xsltmapFile = getXsltmapFO();
200: //// if (xsltmapFile != null) {
201: //// Project project = FileOwnerQuery.getOwner(xsltmapFile);
202: //// if (project != null ) {
203: //// Children children = new TransformationsChildren(project);
204: ////
205: //// DataObject dObj;
206: //// try {
207: //// dObj = DataObject.find(xsltmapFile);
208: //// if (dObj != null) {
209: //// nodes.add(new XsltTransformationsNode(dObj, children));
210: //// }
211: //// } catch (DataObjectNotFoundException ex) {
212: ////// System.out.println("can't find xsltmap assoc dataobject ");
213: //// }
214: //// }
215: //// }
216: //// }
217:
218: if (n != null) {
219: nodes.add(n);
220: }
221:
222: return nodes.toArray(new Node[nodes.size()]);
223: }
224:
225: private FileObject getTransformmapFO() {
226: DataFolder srcDir = getFolder(IcanproProjectProperties.SRC_DIR);
227: if (srcDir != null) {
228: FileObject srcFO = srcDir.getPrimaryFile();
229: if (srcFO == null) {
230: return null;
231: }
232:
233: // TODO m
234: return srcFO
235: .getFileObject(XsltproConstants.TRANSFORMMAP_XML);
236: }
237: return null;
238: }
239:
240: private DataFolder getFolder(String propName) {
241: String propertyValue = evaluator.getProperty(propName);
242: if (propertyValue != null) {
243: FileObject fo = helper.resolveFileObject(evaluator
244: .getProperty(propName));
245: if (fo != null && fo.isValid()) {
246: try {
247: DataFolder df = DataFolder.findFolder(fo);
248: return df;
249: } catch (Exception ex) {
250: logger.fine(ex.getMessage());
251: }
252: }
253: }
254: return null;
255: }
256:
257: // file change events in the project directory
258: public void fileAttributeChanged(
259: org.openide.filesystems.FileAttributeEvent fe) {
260: }
261:
262: public void fileChanged(org.openide.filesystems.FileEvent fe) {
263: // System.out.println("fileChanged: fe: "+fe+"; file: "+fe.getFile());
264: // new Exception("FILE CHANGED").printStackTrace();
265: // createNodes();
266: }
267:
268: public void fileDataCreated(org.openide.filesystems.FileEvent fe) {
269: }
270:
271: public void fileDeleted(org.openide.filesystems.FileEvent fe) {
272: // if (!projectDir.isValid()){
273: // org.netbeans.api.project.ui.OpenProjects.getDefault().close(new Project[] { project });
274: // }
275: // createNodes();
276: // TODO m
277: // refreshKey(KEY_SOURCE_DIR);
278: }
279:
280: public void fileFolderCreated(
281: org.openide.filesystems.FileEvent fe) {
282: // setup folder could be created
283: createNodes();
284: // TODO m
285: // refreshKey(KEY_SOURCE_DIR);
286: }
287:
288: public void fileRenamed(
289: org.openide.filesystems.FileRenameEvent fe) {
290: // setup folder could be renamed
291: createNodes();
292: // TODO m
293: // refreshKey(KEY_SOURCE_DIR);
294: }
295: }
296:
297: private static final class RootNode extends FilterNode {
298: public RootNode(FileObject sourceFolder, Node n,
299: org.openide.nodes.Children children) {
300: super (n, children);
301: assert sourceFolder != null;
302: disableDelegation(DELEGATE_GET_DISPLAY_NAME
303: | DELEGATE_SET_DISPLAY_NAME
304: | DELEGATE_GET_SHORT_DESCRIPTION
305: | DELEGATE_GET_ACTIONS);
306: setDisplayName(NbBundle.getMessage(IcanproViews.class,
307: "LBL_ProcessFiles"));
308: }
309:
310: // TODO r
311: // public RootNode(Node n, DataFolder dataFolder) {
312: // super(n, dataFolder.createNodeChildren( NO_FOLDERS_FILTER));
313: // disableDelegation(DELEGATE_GET_DISPLAY_NAME|
314: // DELEGATE_SET_DISPLAY_NAME|DELEGATE_GET_SHORT_DESCRIPTION|
315: // DELEGATE_GET_ACTIONS);
316: // setDisplayName(
317: // NbBundle.getMessage(IcanproViews.class, "LBL_ProcessFiles"));
318: //
319: // }
320:
321: @Override
322: public Action[] getActions(boolean context) {
323: return new Action[] {
324: CommonProjectActions.newFileAction(),
325: null,
326: org.openide.util.actions.SystemAction
327: .get(org.openide.actions.FileSystemAction.class),
328: null,
329: org.openide.util.actions.SystemAction
330: .get(org.openide.actions.FindAction.class),
331: null,
332: org.openide.util.actions.SystemAction
333: .get(org.openide.actions.PasteAction.class),
334: null,
335: org.openide.util.actions.SystemAction
336: .get(org.openide.actions.ToolsAction.class), };
337: }
338:
339: @Override
340: public boolean canDestroy() {
341: return false;
342: }
343:
344: @Override
345: public boolean canRename() {
346: return false;
347: }
348:
349: @Override
350: public boolean canCopy() {
351: return false;
352: }
353:
354: @Override
355: public boolean canCut() {
356: return false;
357: }
358: }
359:
360: static final class NoFoldersDataFilter implements ChangeListener,
361: ChangeableDataFilter {
362:
363: EventListenerList ell = new EventListenerList();
364:
365: public NoFoldersDataFilter() {
366: VisibilityQuery.getDefault().addChangeListener(this );
367: }
368:
369: public boolean acceptDataObject(DataObject obj) {
370: FileObject fo = obj.getPrimaryFile();
371:
372: // TODO r | m
373: ////// if (isTransformmapFile(fo)) {
374: ////// return false;
375: ////// }
376:
377: return VisibilityQuery.getDefault().isVisible(fo);
378: }
379:
380: public void stateChanged(ChangeEvent e) {
381: Object[] listeners = ell.getListenerList();
382: ChangeEvent event = null;
383: for (int i = listeners.length - 2; i >= 0; i -= 2) {
384: if (listeners[i] == ChangeListener.class) {
385: if (event == null) {
386: event = new ChangeEvent(this );
387: }
388: ((ChangeListener) listeners[i + 1])
389: .stateChanged(event);
390: }
391: }
392: }
393:
394: public void addChangeListener(ChangeListener listener) {
395: ell.add(ChangeListener.class, listener);
396: }
397:
398: public void removeChangeListener(ChangeListener listener) {
399: ell.remove(ChangeListener.class, listener);
400: }
401:
402: }
403:
404: // TODO m
405: // doesn't show xsltmap file
406: public static boolean isTransformmapFile(FileObject fo) {
407: if (fo == null) {
408: return false;
409: }
410: Project project = FileOwnerQuery.getOwner(fo);
411: FileObject srcFolder = null;
412: if (project != null) {
413: srcFolder = org.netbeans.modules.xslt.tmap.util.Util
414: .getSrcFolder(project);
415: }
416:
417: // TODO m
418: return XsltproConstants.TRANSFORMMAP_XML
419: .equals(fo.getNameExt())
420: && srcFolder != null
421: && srcFolder.equals(fo.getParent());
422: }
423:
424: }
|