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

📄 relationtypespanel.java

📁 toocom源代码,主要应用在本体匹配方面!
💻 JAVA
字号:
package toocom.ui;

import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.lang.*;
import toocom.ocgl.*;

/**
 * This class represents the graphical window used for building the relation types hierarchy.
 * It provides creation and structuration fonctions by using its own toolbar or right
 * mouse click. The buttons of the toolbar allow to create, destroy or link relations with 
 * a is-a link. Right-clicking on a relation open a window with the properties of the relation.
 *
 * @author Fr閐閞ic F黵st
 */
public class RelationTypesPanel extends JPanel implements MouseListener, MouseMotionListener{
	
	// The mode of interaction : -1 = disabled, 0=creation, 1=destruction, 2=linking
	private int mode;
	private MainFrame mf;
	
	private int maxUnitIncrement=1;

	private RelationType temporaryRTI;
	private RelationType tempRTI;
	private Point pointerPosition;
	
	public void clear(){
		this.temporaryRTI = null;
		this.tempRTI = null;
		this.repaint();
	}
		
	public void setRelationTypeMode(int mode){
		this.mode = mode;
		if(temporaryRTI != null){
			temporaryRTI.setSelected(false);
			this.repaint();
		}
		temporaryRTI = null;
	}
	
	public void mouseClicked(MouseEvent e){
		if(mode == 0){
			if(temporaryRTI == null){
				if(e.getModifiers() == InputEvent.BUTTON1_MASK){
					if((mf != null) && (this.getPointedComponent(e.getX(),e.getY()) == null)){
						RelationType ct = mf.getOntology().newRelationType(e.getX(),e.getY());
						this.repaint();
						mf.getOntologySummaryFrame().refresh();
					}
				}	
			}
		}
		if(mode == 2){
			if(e.getModifiers() == InputEvent.BUTTON1_MASK){
				if(temporaryRTI == null){
					temporaryRTI = this.getPointedComponent(e.getX(),e.getY());
					if(temporaryRTI != null) temporaryRTI.setSelected(true);
					this.repaint();
				}
				else{
					RelationType cti = this.getPointedComponent(e.getX(),e.getY());
					if((cti != null) && (!cti.equals(temporaryRTI))){
						cti.addChild(temporaryRTI);
						temporaryRTI.setSelected(false);
						temporaryRTI = null;
						this.repaint();
					}
				}
			}
		}
		if(mode == 1){
			if(e.getModifiers() == InputEvent.BUTTON1_MASK){
				temporaryRTI = this.getPointedComponent(e.getX(),e.getY());
				if(temporaryRTI != null){
					if(JOptionPane.showConfirmDialog(this,Constants.REMOVE_RELATION_TYPE_CONFIRM_MESSAGE,Constants.CONFIRMATION_BOX,JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION){
						temporaryRTI.destroy(mf.getOntology());
						this.repaint();
						mf.getOntologySummaryFrame().refresh();
					}
				}
				else{
					this.removePointedLink(e.getX(),e.getY());
					this.repaint();
				}
			}
		}
	}
	
	public void mouseEntered(MouseEvent e){
		//if(mode != -1) this.setrelationTypeMode(this.mode);
	}
	
	public void mouseExited(MouseEvent e){}
	
	public void mousePressed(MouseEvent e){
		if(mode != -1){
			if(e.getButton() == MouseEvent.BUTTON3){
				if(temporaryRTI != null){
					temporaryRTI.setSelected(false);
					temporaryRTI = null;
					this.repaint();
				}
				temporaryRTI = this.getPointedComponent(e.getX(),e.getY());
				if((temporaryRTI != null) && (!mf.getOntology().getDifferenceRelationType().equals(temporaryRTI))) new RelationTypePropertyFrame(temporaryRTI,mf);
				temporaryRTI = null;
			}
			else{
				if((mode == 0) || (mode == 2)){
					tempRTI = this.getPointedComponent(e.getX(),e.getY());
					pointerPosition = new Point(e.getX(),e.getY());
				}
			}
		}
	}
	
	public void mouseReleased(MouseEvent e){
		if((mode == 0) || (mode == 2)){
			tempRTI = null;
			this.repaint();
		}
	}
	
	public void mouseDragged(MouseEvent e){
		if((mode == 0) || (mode == 2)){
			if((tempRTI != null) && (e.getX() > 0) && (e.getX() < this.getWidth()) &&
													(e.getY() > 0) && (e.getY() < this.getHeight())){
				Graphics g = this.getGraphics();
				tempRTI.erase(g,Constants.RELATION_TYPES_FRAME_COLOR,mf.getOntologyLanguage());
				tempRTI.translate(e.getX() - pointerPosition.x,e.getY() - pointerPosition.y);
				this.repaint();
				pointerPosition.setLocation(e.getX(),e.getY());
			}
		}
	}
	
	public void mouseMoved(MouseEvent e){}
	
	/** Returns the component which contains the point (x,y) with the closiest center to the point (x,y). 
	  * Returns null if any component is pointed. */
	public RelationType getPointedComponent(int x, int y){
		RelationType result = null;
		double distance = Double.MAX_VALUE;
		for(Iterator i = mf.getOntology().getRelationTypes().iterator();i.hasNext();){
			RelationType rt = (RelationType) i.next();
			if(rt.containsPoint(x,y,this.getGraphics(),mf.getOntologyLanguage())){
				double temp = Math.sqrt(((rt.getCenter().x - x) * (rt.getCenter().x - x))
											 + ((rt.getCenter().y - y) * (rt.getCenter().y - y)));
				if(temp <= distance){
					distance = temp;
					result = rt;
				}
			}
		}
		return result;
	}
		
	/** Remove the first encountered is-a link which contains the point (x,y). Returns true if
	 *  such a link exists, false otherwise. */
	public boolean removePointedLink(int x, int y){
		if((mf.getOntology() != null) && (mf.getOntology().getRelationTypes() != null)){
			for(Iterator i = mf.getOntology().getRelationTypes().iterator();i.hasNext();){
				RelationType rt = (RelationType) i.next();
				for(Iterator j = rt.getChildren().iterator();j.hasNext();){
					RelationType child = (RelationType) j.next();
					if(ConceptualPrimitive.isALinkContainsPoint(this.getGraphics(),new Point(x,y),rt,child,mf.getOntologyLanguage())){
						rt.removeChild(child);
						return true;
					}
				}
			}
		}
		return false;
	}
	
	public void paint(Graphics g){
		g.setColor(Constants.RELATION_TYPES_FRAME_COLOR);
		g.fillRect(0,0,this.getWidth(),this.getHeight());
		if(mf.getOntology() != null){
			for(Iterator i = mf.getOntology().getRelationTypes().iterator();i.hasNext();){
				RelationType rt = (RelationType) i.next();
				rt.paintChildrenLinks(g,mf.getOntologyLanguage());
			}
			for(Iterator i = mf.getOntology().getRelationTypes().iterator();i.hasNext();){
				RelationType rt = (RelationType) i.next();
				rt.paintObject(g,mf.getOntologyLanguage());
			}
		}
	}

	public RelationTypesPanel(MainFrame mf){
		super();
		this.mf = mf;
		mode = -1;
		this.setBackground(Constants.RELATION_TYPES_FRAME_COLOR);
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		this.setEnabled(true);
	}	
}

⌨️ 快捷键说明

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