Skip to content

Commit 18b5372

Browse files
committed
Use public API to access Program and URL in ImageDescriptor #174
See: eclipse-platform/eclipse.platform.ui#2991
1 parent f62f151 commit 18b5372

File tree

2 files changed

+150
-98
lines changed
  • runtime
    • tesla/org.eclipse.rcptt.tesla.jface.aspects/src/org/eclipse/rcptt/tesla/jface
    • tests/org.eclipse.rcptt.tesla.jface.aspects.test/src/org/eclipse/rcptt/tesla/jface/aspects/test

2 files changed

+150
-98
lines changed
Lines changed: 99 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2009, 2019 Xored Software Inc and others.
2+
* Copyright (c) 2009 Xored Software Inc and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v2.0
55
* which accompanies this distribution, and is available at
@@ -14,133 +14,107 @@
1414

1515
import java.net.URI;
1616
import java.net.URISyntaxException;
17+
import java.net.URL;
1718
import java.util.regex.Matcher;
1819
import java.util.regex.Pattern;
1920

21+
import org.eclipse.core.runtime.Adapters;
2022
import org.eclipse.core.runtime.Platform;
2123
import org.eclipse.jface.resource.ImageDescriptor;
2224
import org.eclipse.swt.program.Program;
2325
import org.eclipse.ui.internal.misc.ExternalProgramImageDescriptor;
2426
import org.osgi.framework.Bundle;
2527

2628
public enum DescriptorInfo {
27-
BUNDLE_URL() {
28-
public final Pattern pattern = Pattern.compile("URLImageDescriptor\\(((bundleentry|bundleresource).*)\\)");
29-
29+
URL_ADAPTER() {
3030
@Override
31-
public String extract(String descriptorStr) {
32-
Matcher matcher = pattern.matcher(descriptorStr);
33-
if (matcher.matches()) {
34-
String uriStr = matcher.group(1);
35-
URI bundleUri = null;
31+
String extract(ImageDescriptor descriptor) {
32+
URL url = Adapters.adapt(descriptor, URL.class, false);
33+
if (url != null) {
3634
try {
37-
bundleUri = new URI(uriStr);
35+
return extractFromUri(url.toURI());
3836
} catch (URISyntaxException e) {
39-
return "InvalidUri(" + uriStr + ")";
37+
JFaceAspectsActivator.log(e);
38+
return null;
4039
}
41-
42-
String host = bundleUri.getHost();
43-
int bundleIdEndIndex = host.indexOf(".fwk");
44-
if (bundleIdEndIndex == -1) {
45-
return "UnknownBundleId(" + uriStr + ")";
46-
}
47-
48-
int bundleId = -1;
49-
try {
50-
bundleId = Integer.parseInt(host.substring(0, bundleIdEndIndex));
51-
} catch (NumberFormatException e) {
52-
return "UnknownBundleId(" + uriStr + ")";
53-
}
54-
55-
Bundle imageBundle = JFaceAspectsActivator.getDefault().getBundle().getBundleContext()
56-
.getBundle(bundleId);
57-
String bundleName = imageBundle == null ? "unknownBundle" : imageBundle.getSymbolicName();
58-
return String.format("%s%s", bundleName, bundleUri.getPath());
59-
} else {
60-
return null;
6140
}
41+
return null;
42+
}
43+
44+
},
45+
EXTERNAL_PROGRAM_ADAPTER() {
46+
@Override
47+
String extract(ImageDescriptor descriptor) {
48+
Program p = Adapters.adapt(descriptor, Program.class, false);
49+
return extractFromProgram(p);
6250
}
51+
52+
},
53+
BUNDLE_URL() {
54+
private final Pattern pattern = Pattern.compile("URLImageDescriptor\\(((bundleentry|bundleresource).*)\\)");
6355

6456
@Override
6557
String extract(ImageDescriptor descriptor) {
66-
return extract(descriptor.toString());
58+
Matcher matcher = pattern.matcher(descriptor.toString());
59+
if (!matcher.matches()) {
60+
return null;
61+
}
62+
String uriStr = matcher.group(1);
63+
URI bundleUri = null;
64+
try {
65+
bundleUri = new URI(uriStr);
66+
} catch (URISyntaxException e) {
67+
return "InvalidUri(" + uriStr + ")";
68+
}
69+
return extractFromUri(bundleUri);
6770
}
6871
},
6972

7073
ABSOLUTE_URL() {
71-
public final Pattern pattern = Pattern.compile("URLImageDescriptor\\((file:/|platform:/plugin/)(.*)\\)");
72-
74+
private final Pattern pattern = Pattern.compile("URLImageDescriptor\\((file:/|platform:/plugin/)(.*)\\)");
7375
@Override
74-
public String extract(String descriptorStr) {
75-
Matcher matcher = pattern.matcher(descriptorStr);
76+
String extract(ImageDescriptor descriptor) {
77+
Matcher matcher = pattern.matcher(descriptor.toString());
7678
if (matcher.matches()) {
7779
return matcher.group(2);
7880
} else {
7981
return null;
8082
}
8183
}
82-
83-
@Override
84-
String extract(ImageDescriptor descriptor) {
85-
return extract(descriptor.toString());
86-
}
8784
},
8885

8986
FILE_CLASS() {
90-
public final Pattern pattern = Pattern.compile("FileImageDescriptor\\(location=class (.*), name=(.*)\\)");
91-
87+
private final Pattern pattern = Pattern.compile("FileImageDescriptor\\(location=class (.*), name=(.*)\\)");
9288
@Override
93-
public String extract(String descriptorStr) {
94-
Matcher matcher = pattern.matcher(descriptorStr);
89+
String extract(ImageDescriptor descriptor) {
90+
Matcher matcher = pattern.matcher(descriptor.toString());
9591
if (matcher.matches()) {
9692
return String.format("%s%s", matcher.group(1), matcher.group(2));
9793
} else {
9894
return null;
9995
}
10096
}
101-
102-
@Override
103-
String extract(ImageDescriptor descriptor) {
104-
return extract(descriptor.toString());
105-
}
10697
},
10798

10899
/**
109100
* Gets info from program because ExternalProgramImageDescriptor.toString() has no useful information
110101
*/
111102
EXTERNAL_PROGRAM() {
112-
private final boolean isWindows = Platform.getOS().equals(Platform.OS_WIN32);
113-
114103
@Override
115104
String extract(ImageDescriptor descriptor) {
116105
if (descriptor instanceof ExternalProgramImageDescriptor) {
117-
if (isWindows) {
118-
try {
119-
Program p = (Program) getField(descriptor, "program", true);
120-
121-
String extension = (String) getField(p, "extension", true);
122-
if (extension != null && !extension.isEmpty()) {
123-
return extension;
124-
}
125-
126-
String iconName = (String) getField(p, "iconName", true);
127-
if (iconName != null && !iconName.isEmpty()) {
128-
return iconName;
129-
}
130-
} catch (IllegalArgumentException e) {
131-
JFaceAspectsActivator.log(e);
132-
} catch (IllegalAccessException e) {
133-
JFaceAspectsActivator.log(e);
134-
} catch (NoSuchFieldException e) {
135-
JFaceAspectsActivator.log(e);
136-
} catch (SecurityException e) {
137-
JFaceAspectsActivator.log(e);
138-
}
106+
try {
107+
return extractFromProgram( (Program) getField(descriptor, "program", true) );
108+
} catch (NoSuchFieldException | IllegalAccessException e) {
109+
JFaceAspectsActivator.log(e);
110+
return null;
139111
}
140112
}
141113
return null;
142114
}
143115
};
116+
117+
144118

145119
public static String getInfo(ImageDescriptor descriptor) {
146120
for (DescriptorInfo i : DescriptorInfo.values()) {
@@ -152,9 +126,57 @@ public static String getInfo(ImageDescriptor descriptor) {
152126
return null;
153127
}
154128

155-
public String extract(String descriptorStr) {
156-
return null;
129+
abstract String extract(ImageDescriptor descriptor);
130+
131+
private static final boolean isWindows = Platform.getOS().equals(Platform.OS_WIN32);
132+
private static String extractFromProgram(Program p) {
133+
if (p == null) {
134+
return null;
135+
}
136+
try {
137+
if (isWindows) {
138+
String extension = (String) getField(p, "extension", true);
139+
if (extension != null && !extension.isEmpty()) {
140+
return extension;
141+
}
142+
143+
String iconName = (String) getField(p, "iconName", true);
144+
if (iconName != null && !iconName.isEmpty()) {
145+
return iconName;
146+
}
147+
}
148+
} catch (IllegalArgumentException e) {
149+
JFaceAspectsActivator.log(e);
150+
} catch (IllegalAccessException e) {
151+
JFaceAspectsActivator.log(e);
152+
} catch (NoSuchFieldException e) {
153+
JFaceAspectsActivator.log(e);
154+
} catch (SecurityException e) {
155+
JFaceAspectsActivator.log(e);
156+
}
157+
return p.getName().isEmpty() ? null : p.getName();
158+
}
159+
private static String extractFromUri(URI bundleUri) {
160+
String host = bundleUri.getHost();
161+
if (host == null) {
162+
return null;
163+
}
164+
int bundleIdEndIndex = host.indexOf(".fwk");
165+
if (bundleIdEndIndex == -1) {
166+
return "UnknownBundleId(" + bundleUri + ")";
167+
}
168+
169+
int bundleId = -1;
170+
try {
171+
bundleId = Integer.parseInt(host.substring(0, bundleIdEndIndex));
172+
} catch (NumberFormatException e) {
173+
return "UnknownBundleId(" + bundleUri + ")";
174+
}
175+
176+
Bundle imageBundle = JFaceAspectsActivator.getDefault().getBundle().getBundleContext()
177+
.getBundle(bundleId);
178+
String bundleName = imageBundle == null ? "unknownBundle" : imageBundle.getSymbolicName();
179+
return String.format("%s%s", bundleName, bundleUri.getPath());
157180
}
158181

159-
abstract String extract(ImageDescriptor descriptor);
160182
}

runtime/tests/org.eclipse.rcptt.tesla.jface.aspects.test/src/org/eclipse/rcptt/tesla/jface/aspects/test/DescriptorInfoTest.java

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2009, 2019 Xored Software Inc and others.
2+
* Copyright (c) 2009 Xored Software Inc and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v2.0
55
* which accompanies this distribution, and is available at
@@ -10,53 +10,83 @@
1010
*******************************************************************************/
1111
package org.eclipse.rcptt.tesla.jface.aspects.test;
1212

13+
import static org.eclipse.rcptt.tesla.jface.DescriptorInfo.getInfo;
1314
import static org.junit.Assert.assertEquals;
1415
import static org.junit.Assert.assertNull;
16+
import static org.junit.Assert.assertTrue;
17+
18+
import java.net.MalformedURLException;
19+
import java.net.URL;
1520

1621
import org.eclipse.core.runtime.Platform;
17-
import org.eclipse.rcptt.tesla.jface.DescriptorInfo;
22+
import org.eclipse.jface.resource.ImageDescriptor;
23+
import org.eclipse.ui.IEditorDescriptor;
24+
import org.eclipse.ui.PlatformUI;
25+
import org.junit.Assert;
1826
import org.junit.Test;
1927

2028
public class DescriptorInfoTest {
2129

2230
@Test
23-
public void testInvalid() {
24-
String str = "<invalid>";
25-
assertNull(DescriptorInfo.BUNDLE_URL.extract(str));
26-
assertNull(DescriptorInfo.ABSOLUTE_URL.extract(str));
27-
assertNull(DescriptorInfo.FILE_CLASS.extract(str));
31+
public void testInvalid() throws MalformedURLException {
32+
ImageDescriptor descriptor = ImageDescriptor.createFromURL(new URL("http:invalid"));
33+
assertNull(getInfo(descriptor));
2834
}
2935

3036
@Test
31-
public void testBundleResource() {
32-
String str = "URLImageDescriptor(bundleresource://5724.fwk1864311781:1/icons/icon.png)";
33-
assertEquals("unknownBundle/icons/icon.png", DescriptorInfo.BUNDLE_URL.extract(str));
37+
public void testBundleResource() throws MalformedURLException {
38+
ImageDescriptor descriptor = ImageDescriptor.createFromURL(new URL("bundleresource://5724.fwk1864311781:1/icons/icon.png"));
39+
assertEquals("unknownBundle/icons/icon.png", getInfo(descriptor));
3440
}
3541

3642
@Test
37-
public void testBundleEntry() {
43+
public void testBundleEntry() throws MalformedURLException {
3844
long id = Platform.getBundle("org.eclipse.rcptt.tesla.jface.aspects.test").getBundleId();
39-
String str = "URLImageDescriptor(bundleentry://"+id+".fwk1873444284/icons/full/eview16/filenav_nav.gif)";
45+
ImageDescriptor descriptor = ImageDescriptor.createFromURL(new URL("bundleentry://"+id+".fwk1873444284/icons/full/eview16/filenav_nav.gif"));
4046
assertEquals("org.eclipse.rcptt.tesla.jface.aspects.test/icons/full/eview16/filenav_nav.gif",
41-
DescriptorInfo.BUNDLE_URL.extract(str));
47+
getInfo(descriptor));
4248
}
4349

4450
@Test
45-
public void testAbsolutePath() {
46-
String str = "URLImageDescriptor(file:/C:/rcptt/icons/icon.png)";
47-
assertEquals("C:/rcptt/icons/icon.png", DescriptorInfo.ABSOLUTE_URL.extract(str));
51+
public void testAbsolutePath() throws MalformedURLException {
52+
ImageDescriptor descriptor = ImageDescriptor.createFromURL(new URL("file:/C:/rcptt/icons/icon.png"));
53+
assertEquals("C:/rcptt/icons/icon.png", getInfo(descriptor));
4854
}
4955

5056
@Test
51-
public void testPlatformPlugin() {
52-
String str = "URLImageDescriptor(platform:/plugin/org.eclipse.rcptt/icons/scroll_lock.png)";
53-
assertEquals("org.eclipse.rcptt/icons/scroll_lock.png", DescriptorInfo.ABSOLUTE_URL.extract(str));
57+
public void testPlatformPlugin() throws MalformedURLException {
58+
ImageDescriptor descriptor = ImageDescriptor.createFromURL(new URL("platform:/plugin/org.eclipse.rcptt/icons/scroll_lock.png"));
59+
assertEquals("org.eclipse.rcptt/icons/scroll_lock.png", getInfo(descriptor));
5460
}
5561

5662
@Test
5763
public void testFileClass() {
58-
String str = "FileImageDescriptor(location=class org.eclipse.jface.action.StatusLine, name=images/stop.gif)";
59-
assertEquals("org.eclipse.jface.action.StatusLineimages/stop.gif", DescriptorInfo.FILE_CLASS.extract(str));
64+
ImageDescriptor descriptor = ImageDescriptor.createFromFile(org.eclipse.jface.action.Separator.class, "images/stop.gif");
65+
assertEquals("org.eclipse.jface.action.Separatorimages/stop.gif", getInfo(descriptor));
66+
}
67+
68+
@Test
69+
public void testProgram() {
70+
boolean match = false;
71+
match |= assertEditor("Safari");
72+
match |= assertEditor("Python (v3.12)");
73+
match |= assertEditor("Python");
74+
match |= assertEditor("Notepad", "txt"); // blind test for Windows. modify to fit.
75+
assertTrue("No editors suitable for test are found", match);
76+
}
77+
78+
private boolean assertEditor(String editorId) {
79+
return assertEditor(editorId, editorId);
80+
}
81+
private boolean assertEditor(String editorId, String expected) {
82+
IEditorDescriptor editor = PlatformUI.getWorkbench().getEditorRegistry().findEditor(editorId);
83+
if (editor == null) {
84+
return false;
85+
}
86+
ImageDescriptor descriptor = editor.getImageDescriptor();
87+
Assert.assertEquals("org.eclipse.ui.internal.misc.ExternalProgramImageDescriptor", descriptor.getClass().getName());
88+
assertEquals(expected, getInfo(descriptor));
89+
return true;
6090
}
6191

6292

0 commit comments

Comments
 (0)