com.android.tools.idea.rendering.GutterIconRenderer.java Source code

Java tutorial

Introduction

Here is the source code for com.android.tools.idea.rendering.GutterIconRenderer.java

Source

/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.tools.idea.rendering;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import icons.AndroidIcons;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.io.File;

public class GutterIconRenderer extends com.intellij.openapi.editor.markup.GutterIconRenderer {
    private final PsiElement myElement;
    private final File myFile;
    private Icon myIcon;

    public GutterIconRenderer(@NotNull PsiElement element, @NotNull File file) {
        myElement = element;
        myFile = file;
    }

    @NotNull
    @Override
    public Icon getIcon() {
        if (myIcon == null) {
            myIcon = GutterIconCache.getInstance().getIcon(myFile.getPath());
            if (myIcon == null) {
                // Broken icon -- what do I show here?
                myIcon = AndroidIcons.Android;
            }
        }

        return myIcon;
    }

    @Nullable
    @Override
    public AnAction getClickAction() {
        return new AnAction() {
            @Override
            public void actionPerformed(AnActionEvent e) {
                final Editor editor = PlatformDataKeys.EDITOR.getData(e.getDataContext());
                if (editor != null) {
                    Project project = editor.getProject();
                    VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByIoFile(myFile);
                    if (project != null && virtualFile != null) {
                        OpenFileDescriptor descriptor = new OpenFileDescriptor(project, virtualFile, -1);
                        FileEditorManager.getInstance(project).openEditor(descriptor, true);
                    }
                }
            }
        };
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;

        GutterIconRenderer that = (GutterIconRenderer) o;

        if (!myElement.equals(that.myElement))
            return false;
        if (!myFile.equals(that.myFile))
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = myElement.hashCode();
        result = 31 * result + myFile.hashCode();
        return result;
    }
}