nescfilefigure.java

来自「plugin for eclipse」· Java 代码 · 共 222 行

JAVA
222
字号
package isis.tinydt.editors.nesceditor;
import isis.anp.nesc.ot.Component;
import isis.anp.nesc.ot.Port;

import java.util.Iterator;
import java.util.Vector;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;





/*
 * Created on Jul 31, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Sebo
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class NesCFileFigure extends RectangleFigure{
	
	private String name_;
	private String type_;
	private Vector provided_ = new Vector();
	private Vector used_  = new Vector();
	private static int provided_X_coord = 5;
	private static int used_X_coord = 120;
	
	private static int port_distance_from_conf = 10;
	private static int port_distance_from_port = 5;
	private static int width_of_conf = used_X_coord + port_distance_from_conf + NesCPortFigure.getPortSize(); 
	private static int min_higth_of_conf = port_distance_from_conf*2 + NesCPortFigure.getPortSize() * 2 + port_distance_from_port;
	private static int port_label_size = 45;
	private static int main_label_size = 15;
	private static int height_of_main_label = main_label_size + port_distance_from_conf;
	

	private Object myObj = null;
	
	
	
	
	public NesCFileFigure(String name, String type)
	{
		super();
		name_ = name;
		type_ = type;
		setSize(countActualSize());
		
		initialize();
	}        
	
	public void setObject(Object obj)
	{
		myObj = obj;
	}
    
    public Object getObject()
    {
        return myObj;
    }
	
	
	private void initialize()
	{
		Label label = new Label(name_);
	    label.setOpaque(true);
	    label.setFont(new Font(null, "Times new roman", 14, SWT.BOLD));
	    label.setSize(width_of_conf - 2*port_distance_from_conf, 15);
	    label.setLocation(new Point(port_distance_from_conf,port_distance_from_conf));
	    add(label);
	    setToolTip(new Label(name_));
	    
	    
	    RectangleFigure  separator = new RectangleFigure();
	    separator.setSize(new Dimension(width_of_conf - 2 *port_distance_from_conf, 2));
	    separator.setLocation(new Point(port_distance_from_conf, height_of_main_label));
	    separator.setBorder(new LineBorder(ColorConstants.black, 2));
	    add(separator);
	    
	    
	    if(type_ == "configuration")
	    {
	    	setBackgroundColor(new Color(null, 149, 191, 138));
	    }
	    else if(type_ == "module")
	    {
	    	setBackgroundColor(new Color(null, 216, 185, 114));
	    }
	    else if(type_ == "interface")
	    {
	    	setBackgroundColor(new Color(null, 239, 254, 180));
	    }
	    else if(type_ == "function")
	    {
	    	setBackgroundColor(new Color(null, 204, 228, 230));
	    }
	    
	    setBorder(new LineBorder(ColorConstants.black, 2));

	    
	    
	    
	}
	
	//add interface port to the configuartion 
	public void addInterface(Port port)
	{
		NesCPortFigure  new_port = new NesCPortFigure(port);
		
		Label label = new Label(port.getName());
	    label.setOpaque(true);
	    label.setBackgroundColor(this.getBackgroundColor());
	    label.setFont(new Font(null, "Times new roman", 8, SWT.NORMAL));
	    label.setSize(port_label_size, 10);
	    
		
		if (port.getDirection() == Port.Direction.PROVIDES)
		{
			new_port.setLocation(new Point(provided_X_coord, getNextPortLocationY(provided_.size())));
			label.setLocation(new Point(provided_X_coord + NesCPortFigure.getPortSize(), getNextPortLocationY(provided_.size()) ));
			provided_.add(port.getName());

		}
		else
		{
			new_port.setLocation(new Point(used_X_coord, getNextPortLocationY(used_.size())));
			label.setLocation(new Point(used_X_coord - port_label_size , getNextPortLocationY(used_.size())));
			used_.add(port.getName());
		}
		
		
		add(new_port);
		add(label);
		
		setSize(countActualSize());
	
	}
	
	//return with the next port's y coordinate
	private int getNextPortLocationY(int count)
	{
		return height_of_main_label + port_distance_from_conf + count * ( NesCPortFigure.getPortSize() + port_distance_from_port);
	}
	
	// get the actual size
	private Dimension countActualSize()
	{
		int size = provided_.size();
		if (provided_.size() < used_.size())
			size=used_.size();
		
		Dimension ret_dim = new  Dimension(width_of_conf, height_of_main_label+ size*(NesCPortFigure.getPortSize()+port_distance_from_port)  - port_distance_from_port+ 2*port_distance_from_conf);
		
		if( ret_dim.height < min_higth_of_conf)
			ret_dim.height = min_higth_of_conf;
		
		return ret_dim; 
	}

	public  RectangleFigure getInterface(Port port)
	{
		
		
		Iterator children_it = getChildren().iterator();
		while( children_it.hasNext() )
		{
			try
			{
			Figure figu = (Figure)children_it.next();
			
			if(figu instanceof NesCPortFigure)
			{
				NesCPortFigure figur = (NesCPortFigure)figu;
				if(figur.isSame(port))
					return figur;
			}
			}
			catch(Exception ex)
			{
			}
		}
		
		return this;
	}
	
	private boolean comparePoints(Point first, Point second)
	{
		return (first.x == second.x && first.y == second.y);
	}
  
    
    public String getName()
    {
    	return name_;
    }
    
    public boolean isSame(Object obj)
    {
    	return (myObj == obj);
    	
    }

}

⌨️ 快捷键说明

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