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-2006 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xml.wsdl.ui.api.property;
020:
021: import java.io.File;
022: import java.io.FileFilter;
023: import java.util.ArrayList;
024: import java.util.Arrays;
025: import java.util.List;
026: import java.util.regex.Matcher;
027: import java.util.regex.Pattern;
028:
029: import org.netbeans.modules.xml.xam.Component;
030: import org.openide.nodes.FilterNode;
031: import org.openide.nodes.Node;
032:
033: public abstract class ChooserHelper<T extends Component> {
034:
035: public abstract void populateNodes(Node parentNode);
036:
037: public abstract Node selectNode(T comp);
038:
039: /*
040: * Filternode to make the nodes look enabled.
041: */
042: public static class EnabledNode extends FilterNode {
043: private static Pattern pattern = Pattern
044: .compile("(^<font.*>)(.*)(<.*>$)");
045:
046: public EnabledNode(Node node) {
047: super (node, new EnabledChildren(node));
048: }
049:
050: @Override
051: public String getHtmlDisplayName() {
052: //strips off font tag, to make it not grey. IZ
053: String retValue = super .getHtmlDisplayName();
054: if (retValue == null)
055: retValue = getDisplayName();
056:
057: if (retValue != null) {
058: Matcher matcher = pattern.matcher(retValue);
059: if (matcher.find()) {
060: return matcher.group(2);
061: }
062: }
063: return retValue;
064: }
065:
066: }
067:
068: private static class EnabledChildren extends FilterNode.Children {
069:
070: public EnabledChildren(Node node) {
071: super (node);
072: }
073:
074: @Override
075: protected Node copyNode(Node node) {
076: return new EnabledNode(node);
077: }
078:
079: }
080:
081: static class DirFileFilter implements FileFilter {
082:
083: public boolean accept(File pathname) {
084: return pathname.isDirectory();
085: }
086: }
087:
088: class FileNode extends FilterNode {
089:
090: String displayName;
091:
092: public FileNode(Node original, String path, int level) {
093: super (original, new FileNodeChildren(original, level));
094: displayName = path;
095: }
096:
097: public FileNode(Node original, String path) {
098: this (original, path, 1);
099: }
100:
101: @Override
102: public String getDisplayName() {
103: return displayName;
104: }
105:
106: }
107:
108: static class FileNodeChildren extends FilterNode.Children {
109:
110: int level = 1;
111:
112: public FileNodeChildren(Node node) {
113: super (node);
114: }
115:
116: public FileNodeChildren(Node node, int level) {
117: super (node);
118: this .level = level;
119: }
120:
121: @Override
122: protected Node copyNode(Node key) {
123: return new CategoryFilterNode(key, level - 1);
124: }
125:
126: }
127:
128: static class CategoryFilterNode extends FilterNode {
129:
130: public CategoryFilterNode(Node node, int level) {
131: super (node, new CategoryFilterChildren(node, level));
132: }
133:
134: }
135:
136: static class CategoryFilterChildren extends FilterNode.Children {
137:
138: private final int level;
139:
140: public CategoryFilterChildren(Node node, int level) {
141: super (node);
142: this .level = level;
143: }
144:
145: @Override
146: protected Node copyNode(Node key) {
147: if (level <= 0) {
148: return new ChildLessNode(key);
149: }
150: return new CategoryFilterNode(key, level - 1);
151: }
152:
153: }
154:
155: static class ChildLessNode extends FilterNode {
156:
157: public ChildLessNode(Node node) {
158: super (node, Children.LEAF);
159: }
160:
161: }
162:
163: protected File[] recursiveListFiles(File file, FileFilter filter) {
164: List<File> files = new ArrayList<File>();
165: File[] filesArr = file.listFiles(filter);
166: files.addAll(Arrays.asList(filesArr));
167: File[] dirs = file.listFiles(new DirFileFilter());
168: for (File dir : dirs) {
169: files
170: .addAll(Arrays.asList(recursiveListFiles(dir,
171: filter)));
172: }
173: return files.toArray(new File[files.size()]);
174: }
175:
176: }
|