multieditor

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ui.part;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.resource.ColorRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.internal.EditorSite;
import org.eclipse.ui.internal.IWorkbenchThemeConstants;
import org.eclipse.ui.internal.WorkbenchPage;
import org.eclipse.ui.internal.WorkbenchWindow;
import org.eclipse.ui.themes.ITheme;

/**
 * A MultiEditor is a composite of editors.
 * 
 * This class is intended to be subclassed.
 * 		
 */
public abstract class MultiEditor extends EditorPart {

    private int activeEditorIndex;

    private IEditorPart innerEditors[];

    /**
     * Constructor for TileEditor.
     */
    public MultiEditor() {
        super();
    }

    /*
     * @see IEditorPart#doSave(IProgressMonitor)
     */
    public void doSave(IProgressMonitor monitor) {
        for (int i = 0; i < innerEditors.length; i++) {
            IEditorPart e = innerEditors[i];
            e.doSave(monitor);
        }
    }

    /**
     * Create the control of the inner editor.
     * 
     * Must be called by subclass.
     */
    public Composite createInnerPartControl(Composite parent,
            final IEditorPart e) {
        Composite content = new Composite(parent, SWT.NONE);
        content.setLayout(new FillLayout());
        e.createPartControl(content);
        parent.addListener(SWT.Activate, new Listener() {
            public void handleEvent(Event event) {
                if (event.type == SWT.Activate)
                    activateEditor(e);
            }
        });
        return content;
    }

    /**
     * The <code>MultiEditor implementation of this 
     * method extends the <code>EditorPart implementation,
     * and disposes any inner editors.  Subclasses may extend.
     * 
     * @since 3.0
     */
    public void dispose() {
        super.dispose();
        IEditorPart[] editors = getInnerEditors();
        for (int i = 0; i < editors.length; i++) {
            editors[i].dispose();
        }
    }

    /*
     * @see IEditorPart#doSaveAs()
     */
    public void doSaveAs() {
        //no-op
    }

    /*
     * @see IEditorPart#init(IEditorSite, IEditorInput)
     */
    public void init(IEditorSite site, IEditorInput input)
            throws PartInitException {
        init(site, (MultiEditorInput) input);
    }

    /*
     * @see IEditorPart#init(IEditorSite, IEditorInput)
     */
    public void init(IEditorSite site, MultiEditorInput input)
            throws PartInitException {
        setInput(input);
        setSite(site);
        setPartName(input.getName());
        setTitleToolTip(input.getToolTipText());
    }

    /*
     * @see IEditorPart#isDirty()
     */
    public boolean isDirty() {
        for (int i = 0; i < innerEditors.length; i++) {
            IEditorPart e = innerEditors[i];
            if (e.isDirty())
                return true;
        }
        return false;
    }

    /*
     * @see IEditorPart#isSaveAsAllowed()
     */
    public boolean isSaveAsAllowed() {
        return false;
    }

    /*
     * @see IWorkbenchPart#setFocus()
     */
    public void setFocus() {
        innerEditors[activeEditorIndex].setFocus();
        updateGradient(innerEditors[activeEditorIndex]);
    }

    /**
     * Returns the active inner editor.
     */
    public final IEditorPart getActiveEditor() {
        return innerEditors[activeEditorIndex];
    }

    /**
     * Returns an array with all inner editors.
     */
    public final IEditorPart[] getInnerEditors() {
        return innerEditors;
    }

    /**
     * Set the inner editors.
     * 
     * Should not be called by clients.
     */
    public final void setChildren(IEditorPart[] children) {
        innerEditors = children;
        activeEditorIndex = 0;
    }

    /**
     * Activates the given nested editor.
     * 
     * @param part the nested editor
     * @since 3.0
     */
    protected void activateEditor(IEditorPart part) {
        IEditorPart oldEditor = getActiveEditor();
        activeEditorIndex = getIndex(part);
        IEditorPart e = getActiveEditor();
        EditorSite innerSite = (EditorSite) e.getEditorSite();
        ((WorkbenchPage) innerSite.getPage()).requestActivation(e);
        updateGradient(oldEditor);
    }

    /**
     * Returns the index of the given nested editor.
     * 
     * @return the index of the nested editor
     * @since 3.0
     */
    protected int getIndex(IEditorPart editor) {
        for (int i = 0; i < innerEditors.length; i++) {
            if (innerEditors[i] == editor)
                return i;
        }
        return -1;
    }

    /**
     * Updates the gradient in the title bar.
     */
    public void updateGradient(IEditorPart editor) {
        boolean activeEditor = editor == getSite().getPage().getActiveEditor();
        boolean activePart = editor == getSite().getPage().getActivePart();

        ITheme theme = editor.getEditorSite().getWorkbenchWindow()
                .getWorkbench().getThemeManager().getCurrentTheme();
        Gradient g = new Gradient();

        ColorRegistry colorRegistry = theme.getColorRegistry();
        if (activePart) {
            g.fgColor = colorRegistry
                    .get(IWorkbenchThemeConstants.ACTIVE_TAB_TEXT_COLOR);
            g.bgColors = new Color[2];
            g.bgColors[0] = colorRegistry
                    .get(IWorkbenchThemeConstants.ACTIVE_TAB_BG_START);
            g.bgColors[1] = colorRegistry
                    .get(IWorkbenchThemeConstants.ACTIVE_TAB_BG_END);
        } else {
            if (activeEditor) {
                g.fgColor = colorRegistry
                        .get(IWorkbenchThemeConstants.ACTIVE_TAB_TEXT_COLOR);
                g.bgColors = new Color[2];
                g.bgColors[0] = colorRegistry
                        .get(IWorkbenchThemeConstants.ACTIVE_TAB_BG_START);
                g.bgColors[1] = colorRegistry
                        .get(IWorkbenchThemeConstants.ACTIVE_TAB_BG_END);
            } else {
                g.fgColor = colorRegistry
                        .get(IWorkbenchThemeConstants.INACTIVE_TAB_TEXT_COLOR);
                g.bgColors = new Color[2];
                g.bgColors[0] = colorRegistry
                        .get(IWorkbenchThemeConstants.INACTIVE_TAB_BG_START);
                g.bgColors[1] = colorRegistry
                        .get(IWorkbenchThemeConstants.INACTIVE_TAB_BG_END);
            }
        }
        g.bgPercents = new int[] { theme
                .getInt(IWorkbenchThemeConstants.ACTIVE_TAB_PERCENT) };

        drawGradient(editor, g);
    }

    /**
     * Draw the gradient in the title bar.
     */
    protected abstract void drawGradient(IEditorPart innerEditor, Gradient g);

    /**
     * Return true if the shell is activated.
     */
    protected boolean getShellActivated() {
        WorkbenchWindow window = (WorkbenchWindow) getSite().getPage()
                .getWorkbenchWindow();
        return window.getShellActivated();
    }

    /**
     * The colors used to draw the title bar of the inner editors
     */
    public static class Gradient {
        public Color fgColor;

        public Color[] bgColors;

        public int[] bgPercents;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值