org.reuseware.sokan.resource.ui.GenDecorator.java Source code

Java tutorial

Introduction

Here is the source code for org.reuseware.sokan.resource.ui.GenDecorator.java

Source

/*******************************************************************************
 * Copyright (c) 2006-2012
 * Software Technology Group, Dresden University of Technology
 * DevBoost GmbH, Berlin, Amtsgericht Charlottenburg, HRB 140026
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *   Software Technology Group - TU Dresden, Germany;
 *   DevBoost GmbH - Berlin, Germany
 *      - initial API and implementation
 ******************************************************************************/
package org.reuseware.sokan.resource.ui;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IDecoration;
import org.eclipse.jface.viewers.ILightweightLabelDecorator;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.LabelProviderChangedEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IDecoratorManager;
import org.eclipse.ui.PlatformUI;
import org.reuseware.sokan.index.util.IndexUtil;
import org.reuseware.sokan.index.util.SokanConst;
import org.reuseware.sokan.resource.SokanResourcePlugin;

/**
 * Decorates files generated by Sokan with a special icon.
 */
public class GenDecorator extends LabelProvider implements ILightweightLabelDecorator {
    private static final String ID = SokanConst.ID_GEN_DECORATOR;

    private static GenDecorator decorator;
    private static String iconPath = "icons/genIcon.png";

    private static final int QUADRANT = IDecoration.BOTTOM_RIGHT;

    /**
     * Calculates decorations based on element. 
     * 
     * @param element the element to decorate
     * @param decoration the decoration to set
     */
    public void decorate(Object element, IDecoration decoration) {
        IFile file = null;
        if (element instanceof IFile) {
            file = (IFile) element;
        } else if (element instanceof IAdaptable) {
            file = (IFile) ((IAdaptable) element).getAdapter(IFile.class);
        }

        if (file == null) {
            return;
        }
        if (isGenerated(file)) {
            ImageDescriptor descriptor = SokanResourcePlugin.getImageDescriptor(iconPath);
            if (descriptor == null) {
                return;
            }
            decoration.addOverlay(descriptor, QUADRANT);
        }
    }

    private boolean isGenerated(IFile file) {
        return IndexUtil.INSTANCE.isGenerated(file);
    }

    /**
     * Activates the gen decorator in the running platform using
     * the platform's decorator manager.
     */
    public static void activate() {
        IDecoratorManager decoratorManager = PlatformUI.getWorkbench().getDecoratorManager();

        if (!decoratorManager.getEnabled(ID)) {
            try {
                decoratorManager.setEnabled(ID, true);
            } catch (CoreException e) {
                //should not happen
            }
        }
    }

    /**
     * Refreshes the decorator for the given container.
     * 
     * @param container the container (file or project)
     */
    public static void refresh(IContainer container) {
        activate();

        final GenDecorator genDecorator = getDecorator();
        if (genDecorator == null) {
            return;
        }
        // Create a label changed event
        final LabelProviderChangedEvent event = new LabelProviderChangedEvent(genDecorator, container);

        // Re-Decorate using current UI thread
        Display.getDefault().asyncExec(new Runnable() {
            public void run() {
                // Fire a LabelProviderChangedEvent to notify eclipse views
                // that label provider has been changed for the resources
                genDecorator.fireLabelProviderChanged(event);
            }
        });
    }

    private static GenDecorator getDecorator() {
        if (decorator != null) {
            return decorator;
        }
        IDecoratorManager decoratorManager = SokanResourcePlugin.getDefault().getWorkbench().getDecoratorManager();

        // If the decorator is disabled, a null value is returned
        decorator = (GenDecorator) decoratorManager.getBaseLabelProvider(ID);
        return decorator;
    }
}