Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* Contributor(s): |
28 |
* |
29 |
* The Original Software is NetBeans. The Initial Developer of the Original |
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
31 |
* Microsystems, Inc. All Rights Reserved. |
32 |
* |
33 |
* If you wish your version of this file to be governed by only the CDDL |
34 |
* or only the GPL Version 2, indicate your decision by adding |
35 |
* "[Contributor] elects to include this software in this distribution |
36 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
37 |
* single choice of license, a recipient has the option to distribute |
38 |
* your version of this file under either the CDDL, the GPL Version 2 or |
39 |
* to extend the choice of license to its licensees as provided above. |
40 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
41 |
* Version 2 license, then the option applies only if the new code is |
42 |
* made subject to such option by the copyright holder. |
43 |
*/ |
44 |
|
45 |
package org.netbeans.modules.project.ui; |
46 |
|
47 |
import java.awt.Graphics; |
48 |
import java.awt.Image; |
49 |
import java.awt.image.ImageObserver; |
50 |
import java.awt.image.ImageProducer; |
51 |
import java.beans.BeanInfo; |
52 |
import java.io.IOException; |
53 |
import java.util.Arrays; |
54 |
import java.util.Collection; |
55 |
import java.util.HashSet; |
56 |
import java.util.Set; |
57 |
import java.util.TreeSet; |
58 |
import javax.swing.event.ChangeListener; |
59 |
import org.netbeans.api.project.Project; |
60 |
import org.netbeans.spi.project.ProjectIconAnnotator; |
61 |
import org.netbeans.junit.MockServices; |
62 |
import org.netbeans.junit.NbTestCase; |
63 |
import org.netbeans.modules.project.ui.actions.TestSupport.TestProject; |
64 |
import org.openide.filesystems.FileObject; |
65 |
import org.openide.filesystems.FileStatusEvent; |
66 |
import org.openide.filesystems.FileSystem; |
67 |
import org.openide.filesystems.FileSystem.HtmlStatus; |
68 |
import org.openide.filesystems.FileUtil; |
69 |
import org.openide.filesystems.MultiFileSystem; |
70 |
import org.openide.nodes.AbstractNode; |
71 |
import org.openide.nodes.Children; |
72 |
import org.openide.util.ImageUtilities; |
73 |
import org.openide.util.Lookup; |
74 |
import org.openide.util.lookup.Lookups; |
75 |
|
76 |
/** |
77 |
* |
78 |
* @author petrdvorak |
79 |
*/ |
80 |
public class ProjectRootNodeIconTest extends NbTestCase { |
81 |
|
82 |
public ProjectRootNodeIconTest(String name) { |
83 |
super(name); |
84 |
} |
85 |
|
86 |
final static Image icon1 = ImageUtilities.loadImage("org/netbeans/modules/project/ui/resources/icon-1.png"); |
87 |
final static Image icon2 = ImageUtilities.loadImage("org/netbeans/modules/project/ui/resources/icon-2.png"); |
88 |
final static Image icon3 = ImageUtilities.loadImage("org/netbeans/modules/project/ui/resources/icon-3.png"); |
89 |
|
90 |
public void testIconAnnotated() throws IOException, Exception { |
91 |
class BadgedImage extends Image { |
92 |
final Image original; |
93 |
BadgedImage(Image original) {this.original = original;} |
94 |
public @Override int getWidth(ImageObserver observer) {return original.getWidth(observer);} |
95 |
public @Override int getHeight(ImageObserver observer) {return original.getHeight(observer);} |
96 |
public @Override ImageProducer getSource() {return original.getSource();} |
97 |
public @Override Graphics getGraphics() {return original.getGraphics();} |
98 |
public @Override Object getProperty(String name, ImageObserver observer) {return original.getProperty(name, observer);} |
99 |
public @Override void flush() {original.flush();} |
100 |
} |
101 |
class TestFS extends MultiFileSystem { |
102 |
String nameBadge = ""; |
103 |
String htmlBadge = ""; |
104 |
boolean badging; |
105 |
Set<? extends FileObject> badgedFiles; |
106 |
TestFS() throws Exception { |
107 |
super(new FileSystem[] {FileUtil.createMemoryFileSystem()}); |
108 |
} |
109 |
public @Override Status getStatus() { |
110 |
return new HtmlStatus() { |
111 |
public String annotateName(String name, Set<? extends FileObject> files) { |
112 |
badgedFiles = files; |
113 |
return name + nameBadge; |
114 |
} |
115 |
public String annotateNameHtml(String name, Set<? extends FileObject> files) { |
116 |
badgedFiles = files; |
117 |
return name + htmlBadge; |
118 |
} |
119 |
public Image annotateIcon(Image icon, int iconType, Set<? extends FileObject> files) { |
120 |
badgedFiles = files; |
121 |
return badging ? new BadgedImage(icon) : icon; |
122 |
} |
123 |
}; |
124 |
} |
125 |
void fireChange(boolean icon, boolean name, FileObject... files) { |
126 |
fireFileStatusChanged(new FileStatusEvent(this, new HashSet<FileObject>(Arrays.asList(files)), icon, name)); |
127 |
} |
128 |
} |
129 |
TestFS fs = new TestFS(); |
130 |
FileObject root = fs.getRoot(); |
131 |
Project prj = new TestProject(root, null); |
132 |
assertNotNull(prj); |
133 |
MockServices.setServices(ProjectIconAnnotatorImpl.class); |
134 |
Collection<? extends ProjectIconAnnotator> lookupAll = Lookup.getDefault().lookupAll(ProjectIconAnnotator.class); |
135 |
System.setProperty("test.nodelay", "true"); |
136 |
ProjectsRootNode.BadgingNode node = new ProjectsRootNode.BadgingNode(null, new ProjectsRootNode.ProjectChildren.Pair(prj), |
137 |
new AbstractNode(Children.LEAF, Lookups.singleton(prj)) { |
138 |
public @Override String getDisplayName() {return "Prj";} |
139 |
public @Override String getHtmlDisplayName() {return "Prj";} |
140 |
}, false, true); |
141 |
Set<FileObject> fos = new TreeSet<FileObject>(); |
142 |
fos.add(prj.getProjectDirectory()); |
143 |
node.setFiles(fos); |
144 |
Image img = node.getIcon(BeanInfo.ICON_COLOR_16x16); |
145 |
assertTrue("Icon is not the same as 'icon3'", img.equals(icon3)); |
146 |
assertFalse("Icon is the same as 'icon2'", img.equals(icon2)); |
147 |
img = node.getOpenedIcon(BeanInfo.ICON_COLOR_16x16); |
148 |
assertTrue("Icon is not the same as 'icon2'", img.equals(icon2)); |
149 |
assertFalse("Icon is the same as 'icon3'", img.equals(icon3)); |
150 |
} |
151 |
|
152 |
public static final class ProjectIconAnnotatorImpl implements ProjectIconAnnotator { |
153 |
|
154 |
public ProjectIconAnnotatorImpl() { |
155 |
} |
156 |
|
157 |
public Image annotateIcon(Project p, Image original, boolean openedNode) { |
158 |
if (openedNode) |
159 |
return icon2; |
160 |
else |
161 |
return icon3; |
162 |
} |
163 |
|
164 |
@Override |
165 |
public void addChangeListener(ChangeListener listener) { |
166 |
} |
167 |
|
168 |
@Override |
169 |
public void removeChangeListener(ChangeListener listener) { |
170 |
} |
171 |
|
172 |
} |
173 |
|
174 |
} |