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 org.netbeans.api.project.Project; |
59 |
import org.netbeans.spi.project.ProjectIconAnnotator; |
60 |
import org.netbeans.junit.MockServices; |
61 |
import org.netbeans.junit.NbTestCase; |
62 |
import org.netbeans.modules.project.ui.actions.TestSupport.TestProject; |
63 |
import org.openide.filesystems.FileObject; |
64 |
import org.openide.filesystems.FileStatusEvent; |
65 |
import org.openide.filesystems.FileSystem; |
66 |
import org.openide.filesystems.FileSystem.HtmlStatus; |
67 |
import org.openide.filesystems.FileUtil; |
68 |
import org.openide.filesystems.MultiFileSystem; |
69 |
import org.openide.nodes.AbstractNode; |
70 |
import org.openide.nodes.Children; |
71 |
import org.openide.util.ImageUtilities; |
72 |
import org.openide.util.Lookup; |
73 |
import org.openide.util.lookup.Lookups; |
74 |
|
75 |
/** |
76 |
* |
77 |
* @author petrdvorak |
78 |
*/ |
79 |
public class ProjectRootNodeIconTest extends NbTestCase { |
80 |
|
81 |
public ProjectRootNodeIconTest() { |
82 |
super("ProjectRootNodeIconTest"); |
83 |
} |
84 |
|
85 |
final static Image icon1 = ImageUtilities.loadImage("org/netbeans/modules/project/ui/resources/icon-1.png"); |
86 |
final static Image icon2 = ImageUtilities.loadImage("org/netbeans/modules/project/ui/resources/icon-2.png"); |
87 |
final static Image icon3 = ImageUtilities.loadImage("org/netbeans/modules/project/ui/resources/icon-3.png"); |
88 |
|
89 |
@Override |
90 |
protected void setUp() throws Exception { |
91 |
} |
92 |
|
93 |
@Override |
94 |
protected void tearDown() throws Exception { |
95 |
super.tearDown(); |
96 |
} |
97 |
|
98 |
public void testIconAnnotated() throws IOException, Exception { |
99 |
class BadgedImage extends Image { |
100 |
final Image original; |
101 |
BadgedImage(Image original) {this.original = original;} |
102 |
public @Override int getWidth(ImageObserver observer) {return original.getWidth(observer);} |
103 |
public @Override int getHeight(ImageObserver observer) {return original.getHeight(observer);} |
104 |
public @Override ImageProducer getSource() {return original.getSource();} |
105 |
public @Override Graphics getGraphics() {return original.getGraphics();} |
106 |
public @Override Object getProperty(String name, ImageObserver observer) {return original.getProperty(name, observer);} |
107 |
public @Override void flush() {original.flush();} |
108 |
} |
109 |
class TestFS extends MultiFileSystem { |
110 |
String nameBadge = ""; |
111 |
String htmlBadge = ""; |
112 |
boolean badging; |
113 |
Set<? extends FileObject> badgedFiles; |
114 |
TestFS() throws Exception { |
115 |
super(new FileSystem[] {FileUtil.createMemoryFileSystem()}); |
116 |
} |
117 |
public @Override Status getStatus() { |
118 |
return new HtmlStatus() { |
119 |
public String annotateName(String name, Set<? extends FileObject> files) { |
120 |
badgedFiles = files; |
121 |
return name + nameBadge; |
122 |
} |
123 |
public String annotateNameHtml(String name, Set<? extends FileObject> files) { |
124 |
badgedFiles = files; |
125 |
return name + htmlBadge; |
126 |
} |
127 |
public Image annotateIcon(Image icon, int iconType, Set<? extends FileObject> files) { |
128 |
badgedFiles = files; |
129 |
return badging ? new BadgedImage(icon) : icon; |
130 |
} |
131 |
}; |
132 |
} |
133 |
void fireChange(boolean icon, boolean name, FileObject... files) { |
134 |
fireFileStatusChanged(new FileStatusEvent(this, new HashSet<FileObject>(Arrays.asList(files)), icon, name)); |
135 |
} |
136 |
} |
137 |
TestFS fs = new TestFS(); |
138 |
FileObject root = fs.getRoot(); |
139 |
Project prj = new TestProject(root, null); |
140 |
assertNotNull(prj); |
141 |
MockServices.setServices(ProjectIconAnnotatorImpl.class); |
142 |
Collection<? extends ProjectIconAnnotator> lookupAll = Lookup.getDefault().lookupAll(ProjectIconAnnotator.class); |
143 |
System.setProperty("test.nodelay", "true"); |
144 |
ProjectsRootNode.BadgingNode node = new ProjectsRootNode.BadgingNode(null, new ProjectsRootNode.ProjectChildren.Pair(prj), |
145 |
new AbstractNode(Children.LEAF, Lookups.singleton(prj)) { |
146 |
public @Override String getDisplayName() {return "Prj";} |
147 |
public @Override String getHtmlDisplayName() {return "Prj";} |
148 |
}, false, true); |
149 |
Set<FileObject> fos = new TreeSet<FileObject>(); |
150 |
fos.add(prj.getProjectDirectory()); |
151 |
node.setFiles(fos); |
152 |
Image img = node.getIcon(BeanInfo.ICON_COLOR_16x16); |
153 |
assertTrue("Icon is not the same as 'icon3'", img.equals(icon3)); |
154 |
assertFalse("Icon is the same as 'icon2'", img.equals(icon2)); |
155 |
img = node.getOpenedIcon(BeanInfo.ICON_COLOR_16x16); |
156 |
assertTrue("Icon is not the same as 'icon2'", img.equals(icon2)); |
157 |
assertFalse("Icon is the same as 'icon3'", img.equals(icon3)); |
158 |
} |
159 |
|
160 |
public static final class ProjectIconAnnotatorImpl implements ProjectIconAnnotator { |
161 |
|
162 |
public ProjectIconAnnotatorImpl() { |
163 |
} |
164 |
|
165 |
public Image annotateIcon(Project p, Image original, boolean openedNode) { |
166 |
if (openedNode) |
167 |
return icon2; |
168 |
else |
169 |
return icon3; |
170 |
} |
171 |
} |
172 |
|
173 |
} |