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

📄 presentation.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.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IPropertyListener;
import org.eclipse.ui.presentations.IPresentablePart;
import org.eclipse.ui.presentations.IStackPresentationSite;
import org.eclipse.ui.presentations.StackDropResult;
import org.eclipse.ui.presentations.StackPresentation;

public class Presentation extends StackPresentation {
    private Composite presentationControl;

    Color colorBorder;

    private Title titleBar;

    private TabContainer tabContainer;

    private IPresentablePart current;

    /**
     * Listener attached to all child parts. It responds to changes in part
     * properties
     */
    private IPropertyListener partPropertyChangeListener = new IPropertyListener() {
        public void propertyChanged(Object source, int property) {

            if (source instanceof IPresentablePart) {
                redraw();
            }
        }
    };

    protected Presentation(IStackPresentationSite stackSite) {
        super(stackSite);
        // TODO Auto-generated constructor stub
    }

    public Presentation(Composite parent, IStackPresentationSite site) {
        super(site);
        // Create a top-level control for the presentation.
        presentationControl = new Composite(parent, SWT.NONE);
        colorBorder = new Color(presentationControl.getDisplay(), 50, 50, 50);
        presentationControl.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                Rectangle clientArea = presentationControl.getClientArea();
                // Image img = new Image(e.display, clientArea.width,
                // clientArea.height);
                // GC gc = new GC(img);
                GC gc = e.gc;

                int border = 1;
                gc.setLineWidth(border);
                gc.setForeground(colorBorder);
                gc.drawRectangle(clientArea.x, clientArea.y, clientArea.width - border,
                        clientArea.height - border);

                // e.gc.drawImage(img, 0, 0);
                // gc.dispose();
                // img.dispose();

            }
        });

        titleBar = new Title(presentationControl, SWT.NONE, getSite());
        tabContainer = new TabContainer(presentationControl, SWT.NONE);

        /*
         * Add a dispose listener. Important because dispose() may // not always
         * be called.
         */
        presentationControl.addDisposeListener(new DisposeListener() {
            public void widgetDisposed(DisposeEvent e) {
                presentationDisposed();
            }
        });

    }

    @Override
    public void setBounds(Rectangle bounds) {
        // Set the bounds of the presentation widget
        presentationControl.setBounds(bounds);

        int titlebarHeight = titleBar.getHeight();
        Rectangle clientArea = presentationControl.getClientArea();
        titleBar
                .setBounds(clientArea.x + 1, clientArea.y + 1, clientArea.width - 2, titlebarHeight);
        int tabPaneHeight = tabContainer.getHeight();
        tabContainer.setBounds(clientArea.x, clientArea.y + clientArea.height - tabPaneHeight,
                clientArea.width, tabPaneHeight);
        if (current != null) {
            Rectangle contentArea = presentationControl.getBounds();
            contentArea.x += 1;
            contentArea.y += titlebarHeight;
            contentArea.width -= 2;
            contentArea.height -= titlebarHeight + tabPaneHeight;
            if (tabPaneHeight == 0) {
                contentArea.height -= 1;
            }
            current.setBounds(contentArea);
        }
    }

    protected void presentationDisposed() {
        // Remove any listeners that were attached to any
        // global Eclipse resources. This is necessary in order to prevent
        // memory leaks.
        colorBorder.dispose();
        presentationControl = null;
    }

    @Override
    public void dispose() {
        presentationDisposed();
    }

    @Override
    public void setActive(int newState) {
        // TODO Auto-generated method stub

    }

    @Override
    public void setVisible(boolean isVisible) {
        // Make the presentation widget visible
        presentationControl.setVisible(isVisible);
        titleBar.setVisible(isVisible);

        // Make the currently visible part visible
        if (current != null) {
            current.setVisible(isVisible);

            if (isVisible) {
                // Restore the bounds of the currently visible part.
                // IPartPresentations can be used by multiple
                // StackPresentations,
                // although only one such presentation is ever visible at a
                // time.
                // It is possible that some other presentation has changed the
                // bounds of the part since it was last visible, so we need to
                // update the part's bounds when the presentation becomes
                // visible.

                setBounds(presentationControl.getBounds());
            }
        }
    }

    @Override
    public void setState(int state) {
        // TODO Auto-generated method stub

    }

    @Override
    public Control getControl() {
        return presentationControl;
    }

    @Override
    public void addPart(IPresentablePart newPart, Object cookie) {
        newPart.addPropertyListener(partPropertyChangeListener);
        tabContainer.addPart(newPart, this, getSite());
    }

    @Override
    public void removePart(IPresentablePart oldPart) {
        oldPart.removePropertyListener(partPropertyChangeListener);
        tabContainer.removePart(oldPart);
        titleBar.removePart(oldPart);
        if (current == oldPart) {
            current = null;
        }
        redraw();
    }

    @Override
    public void selectPart(IPresentablePart toSelect) {
        // Ignore redundant selections
        if (toSelect == current) {
            return;
        }

        // If there was an existing part selected, make it invisible
        if (current != null) {
            current.setVisible(false);
        }
        // Select the new part
        current = toSelect;

        // Make the part visible before setBounds, or the call to setBounds
        // may be ignored.
        if (current != null) {
            current.setVisible(true);
            setBounds(presentationControl.getBounds());
            titleBar.setPresentablePart(current);
            tabContainer.setPresentablePart(current);
        }
    }

    protected void redraw() {
        if (presentationControl != null) {
            presentationControl.redraw();
            titleBar.redraw();
            tabContainer.redraw();
        }
    }

    @Override
    public StackDropResult dragOver(Control currentControl, Point location) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void showSystemMenu() {
        // TODO Auto-generated method stub

    }

    @Override
    public void showPaneMenu() {
        // TODO Auto-generated method stub

    }

    @Override
    public Control[] getTabList(IPresentablePart part) {
        return new Control[] { part.getControl() };
    }

    /**
     * Gets the colorBorder.
     * 
     * @return Returns the colorBorder.
     */
    public Color getColorBorder() {
        return colorBorder;
    }
    
    @Override
    public int computePreferredSize(boolean width, int availableParallel, int availablePerpendicular, int preferredResult) {
        if(width) {
            return Math.max(preferredResult, 100);
        } else {
            return tabContainer.getHeight() + titleBar.getHeight();
        }
    }
}

⌨️ 快捷键说明

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