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

[51] Add custom icon to OpenAPI 3.0 YAML and JSON files #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions dev/org.eclipse.codewind.openapi.ui/plugin.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ OPENAPI_TOOLS=OpenAPI Tools
CLIENT_API_STUB=Client API stub...
SERVER_API_STUB=Server API stub...
HTML_DOCUMENTATION=HTML documentation...

DECORATOR=OpenAPI Decorator
DECORATOR_DESCRIPTION=Codewind OpenAPI Document Decorators
14 changes: 14 additions & 0 deletions dev/org.eclipse.codewind.openapi.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -472,5 +472,19 @@
disabledIcon="icons/dtool16/html_generate.gif"
icon="icons/etool16/html_generate.gif" />
</extension>

<extension point="org.eclipse.ui.decorators" id="org.eclipse.codewind.openapi.ui.decorators" name="OpenAPI decorators">
<decorator
id="org.eclipse.codewind.openapi.ui.decorator"
label="%DECORATOR"
state="true"
class="org.eclipse.codewind.openapi.ui.CustomLabelDecorator"
lightweight="false">
<description>%DECORATOR_DESCRIPTION</description>
<enablement>
<objectClass name="org.eclipse.core.resources.IFile"/>
</enablement>
</decorator>
</extension>
</plugin>

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.osgi.service.debug.DebugOptionsListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

Expand Down Expand Up @@ -80,6 +81,21 @@ public static ImageDescriptor getImageDescriptor(String path) {
return descriptor;
}

public static Image getImage(String path) {
ImageRegistry imageRegistry = plugin.getImageRegistry();
ImageDescriptor descriptor = imageRegistry.getDescriptor(path);
if (descriptor == null) {
descriptor = imageDescriptorFromPlugin(PLUGIN_ID, path);
imageRegistry.put(path, descriptor);
}
Image image = imageRegistry.get(path + "_IMG");
if (image == null) {
image = descriptor.createImage();
imageRegistry.put(path + "_IMG", image);
}
return image;
}

public static void log(int severity, String message) {
plugin.getLog().log(new Status(severity, PLUGIN_ID, message));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public Constants() {
public static String CODEWIND_EXPLORER_VIEW = "org.eclipse.codewind.ui.explorerView"; //$NON-NLS-1$
public static String HTML_DOCUMENTATION_FILE = "index.html"; //$NON-NLS-1$

public static String IMG_CLIENT_WIZBAN = "icons/wizban/select_client_wiz.png";
public static String IMG_SERVER_WIZBAN = "icons/wizban/select_server_wiz.png";
public static String IMG_HTML_WIZBAN = "icons/wizban/html_generator_wiz.png";
public static String IMG_CLIENT_WIZBAN = "icons/wizban/select_client_wiz.png"; //$NON-NLS-1$
public static String IMG_SERVER_WIZBAN = "icons/wizban/select_server_wiz.png"; //$NON-NLS-1$
public static String IMG_HTML_WIZBAN = "icons/wizban/html_generator_wiz.png"; //$NON-NLS-1$
public static String IMG_OPENAPI = "icons/OpenAPI.png"; //$NON-NLS-1$


public static String ALL_CLIENT_LANGUAGES[][] = new String[][] {
{ "Go", "go"},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*******************************************************************************
* Copyright (c) 2019 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.codewind.openapi.ui;

import java.io.IOException;

import org.eclipse.codewind.openapi.core.IOpenApiConstants;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.jface.viewers.ILabelDecorator;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;

public class CustomLabelDecorator implements ILabelDecorator {

public CustomLabelDecorator() {
// Empty
}

@Override
public void addListener(ILabelProviderListener arg0) {
// Empty

}

@Override
public void dispose() {
// Empty

}

@Override
public boolean isLabelProperty(Object arg0, String arg1) {
return false;
}

@Override
public void removeListener(ILabelProviderListener arg0) {
// Empty
}

@Override
public Image decorateImage(Image arg0, Object resource) {
if (resource instanceof IFile) {
IFile iFile = (IFile) resource;
try {
IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
IContentType contentType = contentTypeManager.findContentTypeFor(iFile.getContents(), iFile.getName());
if (contentType != null) {
if (IOpenApiConstants.CONTENTTYPE_JSON.equals(contentType.getId())
|| IOpenApiConstants.CONTENTTYPE_YAML.contentEquals(contentType.getId())) {
Image image = Activator.getImage(Constants.IMG_OPENAPI);
return image;
}
}
} catch (IOException e) {
// ignore
} catch (CoreException e) {
// ignore
}
}
return arg0;
}

@Override
public String decorateText(String arg0, Object arg1) {
return null;
}

}