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.etl.project.ui;
020:
021: import java.beans.PropertyChangeEvent;
022: import java.beans.PropertyChangeListener;
023: import java.util.ArrayList;
024: import java.util.Collections;
025: import java.util.List;
026:
027: import javax.swing.event.ChangeEvent;
028: import javax.swing.event.ChangeListener;
029: import javax.swing.event.EventListenerList;
030: import org.netbeans.api.project.Project;
031:
032: import org.netbeans.api.queries.VisibilityQuery;
033: import org.openide.filesystems.FileObject;
034: import org.openide.loaders.DataFolder;
035: import org.openide.nodes.Children;
036: import org.openide.nodes.FilterNode;
037: import org.openide.nodes.Node;
038: import net.java.hulp.i18n.Logger;
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 org.netbeans.modules.etl.logger.Localizer;
044: import org.netbeans.modules.etl.logger.LogUtil;
045: import org.openide.filesystems.FileChangeListener;
046: import org.openide.loaders.ChangeableDataFilter;
047: import org.openide.loaders.DataFilter;
048: import org.openide.loaders.DataObject;
049: import org.openide.loaders.DataObjectNotFoundException;
050:
051: class EtlproViews {
052:
053: private EtlproViews() {
054: }
055:
056: static final class LogicalViewChildren extends Children.Keys
057: implements FileChangeListener {
058:
059: private static final String KEY_SOURCE_DIR = "srcDir"; // NOI18N
060: private static final String KEY_SETUP_DIR = "setupDir"; //NOI18N
061: private AntProjectHelper helper;
062: private final PropertyEvaluator evaluator;
063: private FileObject projectDir;
064: private Project project;
065: private static transient final Logger mLogger = LogUtil
066: .getLogger(EtlproViews.class.getName());
067: private static transient final Localizer mLoc = Localizer.get();
068:
069: public LogicalViewChildren(AntProjectHelper helper,
070: PropertyEvaluator evaluator, Project project) {
071: assert helper != null;
072: this .helper = helper;
073: projectDir = helper.getProjectDirectory();
074: this .evaluator = evaluator;
075: this .project = project;
076: }
077:
078: protected void addNotify() {
079: super .addNotify();
080: projectDir.addFileChangeListener(this );
081: createNodes();
082: }
083:
084: private void createNodes() {
085: List l = new ArrayList();
086:
087: DataFolder srcDir = getFolder(IcanproProjectProperties.SRC_DIR);
088: if (srcDir != null) {
089: l.add(KEY_SOURCE_DIR);
090: }
091:
092: FileObject setupFolder = getSetupFolder();
093: if (setupFolder != null && setupFolder.isFolder()) {
094: l.add(KEY_SETUP_DIR);
095: }
096: setKeys(l);
097: }
098:
099: private FileObject getSetupFolder() {
100: return projectDir.getFileObject("setup"); //NOI18N
101: }
102:
103: @Override
104: protected void removeNotify() {
105: setKeys(Collections.EMPTY_SET);
106: projectDir.removeFileChangeListener(this );
107: super .removeNotify();
108: }
109:
110: protected Node[] createNodes(Object key) {
111: Node n = null;
112: if (key == KEY_SOURCE_DIR) {
113: FileObject srcRoot = helper.resolveFileObject(evaluator
114: .getProperty(IcanproProjectProperties.SRC_DIR));
115: DataObject fileDO;
116: try {
117: fileDO = DataObject.find(srcRoot);
118: n = new ViewItemNode((DataFolder) fileDO,
119: "collaborations", "collaborations");
120: } catch (DataObjectNotFoundException e) {
121: throw new AssertionError(e);
122: }
123:
124: }
125: return n == null ? new Node[0] : new Node[] { n };
126: }
127:
128: private DataFolder getFolder(String propName) {
129: try {
130: FileObject fo = helper.resolveFileObject(evaluator
131: .getProperty(propName));
132: if (fo != null) {
133: DataFolder df = DataFolder.findFolder(fo);
134: return df;
135: }
136: } catch (Exception ex) {
137: mLogger.errorNoloc(mLoc.t("PRSR021: Exception :{0}", ex
138: .getMessage()), ex);
139: }
140: return null;
141: }
142:
143: // file change events in the project directory
144: public void fileAttributeChanged(
145: org.openide.filesystems.FileAttributeEvent fe) {
146: }
147:
148: public void fileChanged(org.openide.filesystems.FileEvent fe) {
149: }
150:
151: public void fileDataCreated(org.openide.filesystems.FileEvent fe) {
152: }
153:
154: public void fileDeleted(org.openide.filesystems.FileEvent fe) {
155: createNodes();
156: }
157:
158: public void fileFolderCreated(
159: org.openide.filesystems.FileEvent fe) {
160: createNodes();
161: }
162:
163: public void fileRenamed(
164: org.openide.filesystems.FileRenameEvent fe) {
165: createNodes();
166: }
167: }
168:
169: private static final DataFilter FILTER = new CollabDataFilter();
170:
171: private static final class ViewItemNode extends FilterNode
172: implements PropertyChangeListener {
173:
174: private final String name;
175: private final String displayName;
176:
177: public ViewItemNode(DataFolder folder, String name,
178: String displayName) {
179: super (folder.getNodeDelegate(), folder
180: .createNodeChildren(FILTER));
181: this .name = name;
182: this .displayName = displayName;
183: }
184:
185: public String getName() {
186: return name;
187: }
188:
189: public String getDisplayName() {
190: return displayName;
191: }
192:
193: public boolean canRename() {
194: return true;
195: }
196:
197: public boolean canDestroy() {
198: return true;
199: }
200:
201: public boolean canCut() {
202: return true;
203: }
204:
205: @Override
206: public void setName(String arg0) {
207: super .setName(arg0);
208: }
209:
210: public void propertyChange(PropertyChangeEvent evt) {
211: fireNameChange(null, null);
212: fireDisplayNameChange(null, null);
213: fireIconChange();
214: fireOpenedIconChange();
215: }
216: }
217:
218: static final class CollabDataFilter implements ChangeListener,
219: ChangeableDataFilter {
220:
221: EventListenerList ell = new EventListenerList();
222:
223: public CollabDataFilter() {
224: VisibilityQuery.getDefault().addChangeListener(this );
225: }
226:
227: public boolean acceptDataObject(DataObject obj) {
228: FileObject fo = obj.getPrimaryFile();
229: return VisibilityQuery.getDefault().isVisible(fo);
230: }
231:
232: public void stateChanged(ChangeEvent e) {
233: Object[] listeners = ell.getListenerList();
234: ChangeEvent event = null;
235: for (int i = listeners.length - 2; i >= 0; i -= 2) {
236: if (listeners[i] == ChangeListener.class) {
237: if (event == null) {
238: event = new ChangeEvent(this );
239: }
240: ((ChangeListener) listeners[i + 1])
241: .stateChanged(event);
242: }
243: }
244: }
245:
246: public void addChangeListener(ChangeListener listener) {
247: ell.add(ChangeListener.class, listener);
248: }
249:
250: public void removeChangeListener(ChangeListener listener) {
251: ell.remove(ChangeListener.class, listener);
252: }
253: }
254: }
|