Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit d4985d7

Browse files
committed
[51] Add custom icon to OpenAPI 3.0 YAML and JSON files
Signed-off-by: Keith Chong <[email protected]>
1 parent 31b680f commit d4985d7

File tree

6 files changed

+122
-3
lines changed

6 files changed

+122
-3
lines changed
Loading

dev/org.eclipse.codewind.openapi.ui/plugin.properties

+3
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ OPENAPI_TOOLS=OpenAPI Tools
1919
CLIENT_API_STUB=Client API stub...
2020
SERVER_API_STUB=Server API stub...
2121
HTML_DOCUMENTATION=HTML documentation...
22+
23+
DECORATOR=OpenAPI Decorator
24+
DECORATOR_DESCRIPTION=Codewind OpenAPI Document Decorators

dev/org.eclipse.codewind.openapi.ui/plugin.xml

+14
Original file line numberDiff line numberDiff line change
@@ -472,5 +472,19 @@
472472
disabledIcon="icons/dtool16/html_generate.gif"
473473
icon="icons/etool16/html_generate.gif" />
474474
</extension>
475+
476+
<extension point="org.eclipse.ui.decorators" id="org.eclipse.codewind.openapi.ui.decorators" name="OpenAPI decorators">
477+
<decorator
478+
id="org.eclipse.codewind.openapi.ui.decorator"
479+
label="%DECORATOR"
480+
state="true"
481+
class="org.eclipse.codewind.openapi.ui.CustomLabelDecorator"
482+
lightweight="false">
483+
<description>%DECORATOR_DESCRIPTION</description>
484+
<enablement>
485+
<objectClass name="org.eclipse.core.resources.IFile"/>
486+
</enablement>
487+
</decorator>
488+
</extension>
475489
</plugin>
476490

dev/org.eclipse.codewind.openapi.ui/src/org/eclipse/codewind/openapi/ui/Activator.java

+16
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.eclipse.jface.resource.ImageDescriptor;
2121
import org.eclipse.jface.resource.ImageRegistry;
2222
import org.eclipse.osgi.service.debug.DebugOptionsListener;
23+
import org.eclipse.swt.graphics.Image;
2324
import org.eclipse.ui.plugin.AbstractUIPlugin;
2425
import org.osgi.framework.BundleContext;
2526

@@ -80,6 +81,21 @@ public static ImageDescriptor getImageDescriptor(String path) {
8081
return descriptor;
8182
}
8283

84+
public static Image getImage(String path) {
85+
ImageRegistry imageRegistry = plugin.getImageRegistry();
86+
ImageDescriptor descriptor = imageRegistry.getDescriptor(path);
87+
if (descriptor == null) {
88+
descriptor = imageDescriptorFromPlugin(PLUGIN_ID, path);
89+
imageRegistry.put(path, descriptor);
90+
}
91+
Image image = imageRegistry.get(path + "_IMG");
92+
if (image == null) {
93+
image = descriptor.createImage();
94+
imageRegistry.put(path + "_IMG", image);
95+
}
96+
return image;
97+
}
98+
8399
public static void log(int severity, String message) {
84100
plugin.getLog().log(new Status(severity, PLUGIN_ID, message));
85101
}

dev/org.eclipse.codewind.openapi.ui/src/org/eclipse/codewind/openapi/ui/Constants.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ public Constants() {
2424
public static String CODEWIND_EXPLORER_VIEW = "org.eclipse.codewind.ui.explorerView"; //$NON-NLS-1$
2525
public static String HTML_DOCUMENTATION_FILE = "index.html"; //$NON-NLS-1$
2626

27-
public static String IMG_CLIENT_WIZBAN = "icons/wizban/select_client_wiz.png";
28-
public static String IMG_SERVER_WIZBAN = "icons/wizban/select_server_wiz.png";
29-
public static String IMG_HTML_WIZBAN = "icons/wizban/html_generator_wiz.png";
27+
public static String IMG_CLIENT_WIZBAN = "icons/wizban/select_client_wiz.png"; //$NON-NLS-1$
28+
public static String IMG_SERVER_WIZBAN = "icons/wizban/select_server_wiz.png"; //$NON-NLS-1$
29+
public static String IMG_HTML_WIZBAN = "icons/wizban/html_generator_wiz.png"; //$NON-NLS-1$
30+
public static String IMG_OPENAPI = "icons/OpenAPI.png"; //$NON-NLS-1$
31+
3032

3133
public static String ALL_CLIENT_LANGUAGES[][] = new String[][] {
3234
{ "Go", "go"},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2019 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* IBM Corporation - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.codewind.openapi.ui;
14+
15+
import java.io.IOException;
16+
17+
import org.eclipse.codewind.openapi.core.IOpenApiConstants;
18+
import org.eclipse.core.resources.IFile;
19+
import org.eclipse.core.runtime.CoreException;
20+
import org.eclipse.core.runtime.Platform;
21+
import org.eclipse.core.runtime.content.IContentType;
22+
import org.eclipse.core.runtime.content.IContentTypeManager;
23+
import org.eclipse.jface.viewers.ILabelDecorator;
24+
import org.eclipse.jface.viewers.ILabelProviderListener;
25+
import org.eclipse.swt.graphics.Image;
26+
27+
public class CustomLabelDecorator implements ILabelDecorator {
28+
29+
public CustomLabelDecorator() {
30+
// Empty
31+
}
32+
33+
@Override
34+
public void addListener(ILabelProviderListener arg0) {
35+
// Empty
36+
37+
}
38+
39+
@Override
40+
public void dispose() {
41+
// Empty
42+
43+
}
44+
45+
@Override
46+
public boolean isLabelProperty(Object arg0, String arg1) {
47+
return false;
48+
}
49+
50+
@Override
51+
public void removeListener(ILabelProviderListener arg0) {
52+
// Empty
53+
}
54+
55+
@Override
56+
public Image decorateImage(Image arg0, Object resource) {
57+
if (resource instanceof IFile) {
58+
IFile iFile = (IFile) resource;
59+
try {
60+
IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
61+
IContentType contentType = contentTypeManager.findContentTypeFor(iFile.getContents(), iFile.getName());
62+
if (contentType != null) {
63+
System.out.println(contentType.getId());
64+
if (IOpenApiConstants.CONTENTTYPE_JSON.equals(contentType.getId())
65+
|| IOpenApiConstants.CONTENTTYPE_YAML.contentEquals(contentType.getId())) {
66+
Image image = Activator.getImage(Constants.IMG_OPENAPI);
67+
return image;
68+
}
69+
}
70+
} catch (IOException e) {
71+
// ignore
72+
} catch (CoreException e) {
73+
// ignore
74+
}
75+
}
76+
return arg0;
77+
}
78+
79+
@Override
80+
public String decorateText(String arg0, Object arg1) {
81+
return null;
82+
}
83+
84+
}

0 commit comments

Comments
 (0)