⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractclosable.java

📁 eclise rcp 项目,是非常好的学习源码
💻 JAVA
字号:
/*******************************************************************************
 * Copyright (c) 2007 Siemens AG
 * 
 * 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:
 *    Kai T鰀ter - initial API and implementation
 *******************************************************************************/

package com.siemens.ct.mp3m.ui.presentation;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.ui.presentations.IPresentablePart;

public abstract class AbstractClosable extends Canvas implements PaintListener {

    protected static Image closeUnselectedImage;

    protected static Image closeSelectedImage;

    protected boolean isCloseSelected;

    protected boolean isMouseOver;

    protected IPresentablePart part;

    private MouseMoveListener mouseMoveListener = new MouseMoveListener() {
        public void mouseMove(MouseEvent e) {
            boolean oldState = isCloseSelected;
            Rectangle clientArea = getBounds();
            clientArea.x = clientArea.width - 20;
            clientArea.width = closeSelectedImage.getBounds().width;
            clientArea.y = 2;
            clientArea.height = closeSelectedImage.getBounds().height;
            if (clientArea.contains(e.x, e.y)) {
                isCloseSelected = true;
            } else {
                isCloseSelected = false;
            }

            if (oldState != isCloseSelected) {
                redraw();
            }
        }
    };

    protected MouseTrackAdapter mouseTrackAdapter = new MouseTrackAdapter() {
        public void mouseEnter(MouseEvent e) {
            isMouseOver = true;
            redraw();
        }

        public void mouseExit(MouseEvent e) {
            isMouseOver = false;
            isCloseSelected = false;
            redraw();
        }
    };

    static {
        ImageDescriptor imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin(
                "com.siemens.ct.mp3m.ui.presentation", "images/close.gif");
        if (imageDescriptor != null) {
            closeUnselectedImage = imageDescriptor.createImage();
        }
        imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin(
                "com.siemens.ct.mp3m.ui.presentation", "images/closeSelected.gif");
        if (imageDescriptor != null) {
            closeSelectedImage = imageDescriptor.createImage();
        }
    }

    public AbstractClosable(Composite parent, int style) {
        super(parent, style);
        addMouseMoveListener(mouseMoveListener);
        addMouseTrackListener(mouseTrackAdapter);
    }

    public void setPresentablePart(IPresentablePart part) {
        this.part = part;
        setToolTipText(part.getTitleToolTip());
        layout();
        redraw();
    }

    public int getHeight() {
        return 21;
    }

    public void removePart(IPresentablePart oldPart) {
        if (part == oldPart) {
            part = null;
        }
    }

    protected String shortenText(GC gc, String text, int width) {
        if (text == null) {
            return null;
        }
        if(gc.textExtent(text, 0).x <= width) {
            return text;
        }
        String elipse = "...";
        int elipseWidth = gc.textExtent(elipse, 0).x;
        int textLength = text.length();

        while (textLength >= 0) {
            String s1 = text.substring(0, textLength);
            int textWidth = gc.textExtent(s1, 0).x;

            if (textWidth + elipseWidth < width) {
                text = s1 + elipse;
                break;
            }
            textLength--;

        }
        return text;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -