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

📄 legendpainter.java

📁 JPowerGraph is a Java library for creating directed graphs for SWT. It supports graph movement, sele
💻 JAVA
字号:
package net.sourceforge.jpowergraph.painters;

import java.awt.event.ActionEvent;
import java.util.Iterator;

import javax.swing.AbstractAction;

import net.sourceforge.jpowergraph.Legend;
import net.sourceforge.jpowergraph.pane.JGraphPane;

import org.eclipse.swt.SWT;
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.Display;

/**
 * @author Mick Kerrigan
 *
 * Created on 07-Aug-2005
 * Committed by $Author: morcen $
 *
 * $Source: /cvsroot/jpowergraph/swt/src/net/sourceforge/jpowergraph/painters/LegendPainter.java,v $,
 * @version $Revision: 1.3 $ $Date: 2005/08/18 16:08:45 $
 */

public class LegendPainter {
    
    private Color white;
    private Color black;
    private Color enabledButtonColor;
    private Color disabledButtonColor;
    
    private boolean disposed = false;

    public LegendPainter(Display theDisplay) {
        white = theDisplay.getSystemColor(SWT.COLOR_WHITE);
        black = theDisplay.getSystemColor(SWT.COLOR_BLACK);
        this.enabledButtonColor = theDisplay.getSystemColor(SWT.COLOR_BLACK);
        this.disabledButtonColor = theDisplay.getSystemColor(SWT.COLOR_GRAY);
    }

    public Rectangle paintLegend(JGraphPane graphPane, GC g, final Legend theLegend){
        int padding = 2;
        int widthPad = 20;
        int heightPad = 10;
        int width=widthPad;
        int height=heightPad;
        int buttonWidth = 10;
        for (Iterator i = theLegend.getNodePainters().iterator(); i.hasNext();) {
            NodePainter nodePainter = (NodePainter) i.next();
            String label = theLegend.getDescriptionForPainter(nodePainter);
            Point d = nodePainter.getLegendItemSize(graphPane, label);
            width = Math.max(width, d.x + widthPad);
            height += d.y; 
        }
        
        if (theLegend.isNodeFilterInUse()){
            width += buttonWidth + (padding*2);
        }
        
        int x = 10;
        int y = graphPane.getSize().y - (height + 10);
        
        Color oldColor = g.getBackground();
        g.setBackground(white);
        g.fillRoundRectangle(x, y, width, height, 10, 10);
        g.setBackground(black);
        g.drawRoundRectangle(x, y, width, height, 10, 10);
        g.setBackground(oldColor);
        
        int itemX = x + widthPad/2;
        int itemY = y + heightPad;
        
        for (Iterator i = theLegend.getNodePainters().iterator(); i.hasNext();) {
            NodePainter nodePainter = (NodePainter) i.next();
            String label = theLegend.getDescriptionForPainter(nodePainter);
            final Class theClass = theLegend.getNodeClassForPainter(nodePainter);
            Point d = nodePainter.getLegendItemSize(graphPane, label);
            nodePainter.paintLegendItem(graphPane, g, new Point(itemX, itemY), label);
            
            int buttonX = x + (width - (buttonWidth + (padding*2)));
            int buttonY = itemY - (padding/2);
            int buttonHeight = (int) (d.y - (padding * 2.5));
            
            if (theLegend.isNodeFilterInUse()){
                boolean canFilterNode = theLegend.getNodeFilterLens().canFilterNode(theClass);
                Rectangle r = new Rectangle(buttonX, buttonY, buttonWidth, buttonHeight);
                if (!canFilterNode){
                    g.setForeground(disabledButtonColor);
                }
                else{
                    g.setForeground(enabledButtonColor);
                }
                g.drawRectangle(r.x, r.y, r.width, r.height);
                if (theLegend.getNodeFilterLens().getFilterValue(theClass)){
                    g.drawLine(r.x + 3, r.y + 3, r.x + r.width - 3, r.y + r.height - 3);
                    g.drawLine(r.x + 3, r.y + r.height - 3, r.x + r.width - 3, r.y + 3);
                }
                if (canFilterNode){
                    theLegend.addActionRectangle(r, new AbstractAction() {
                        public void actionPerformed(ActionEvent e) {
                            theLegend.getNodeFilterLens().setFilterValue(theClass, !theLegend.getNodeFilterLens().getFilterValue(theClass));
                        }
                    });
                }
            }
            
            
            itemY += (int) d.y;
        }
        return new Rectangle(x, y, width, height);
    }
    
    public void dispose(){
        if (!white.isDisposed()){
            white.dispose();
        }
        if (!black.isDisposed()){
            black.dispose();
        }
        if (!enabledButtonColor.isDisposed()){
            enabledButtonColor.dispose();
        }
        if (!disabledButtonColor.isDisposed()){
            disabledButtonColor.dispose();
        }
        disposed = true;
    }
    
    public boolean isDisposed(){
        return disposed;
    }
}

⌨️ 快捷键说明

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