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

📄 concepttypespanel.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 concept 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 concepts with 
 * a is-a link. Right-clicking on a concept open a window with the properties of the concept.
 *
 * @author Fr閐閞ic F黵st
 */
public class ConceptTypesPanel extends JPanel implements MouseListener, MouseMotionListener{
	
	// The mode of interaction : -1 = disabled, 0=creation, 1=destruction, 2=linking
	private int mode;
	private MainFrame mf;

	private ConceptType temporaryCTI;
	private ConceptType tempCTI;
	private Point pointerPosition;
	
	public void clear(){
		this.temporaryCTI = null;
		this.tempCTI = null;
		this.repaint();
	}

	public void setConceptTypeMode(int mode){
		this.mode = mode;
		if(temporaryCTI != null){
			temporaryCTI.setSelected(false);
			this.repaint();
		}
		temporaryCTI = null;
	}
	
	public void mouseClicked(MouseEvent e){
		if(mf.getOntology() != null){
			if(mode == 0){
				if((temporaryCTI == null)){
					if(e.getModifiers() == InputEvent.BUTTON1_MASK){
						if((mf != null) && (this.getPointedComponent(e.getX(),e.getY()) == null)){
							ConceptType ct = mf.getOntology().newConceptType(e.getX(),e.getY());
							this.repaint();
							mf.getOntologySummaryFrame().refresh();
						}
					}	
				}
			}
			if(mode == 2){
				if(e.getModifiers() == InputEvent.BUTTON1_MASK){
					if(temporaryCTI == null){
						temporaryCTI = this.getPointedComponent(e.getX(),e.getY());
						if(temporaryCTI != null){
							temporaryCTI.setSelected(true);
							this.repaint();
						}
					}
					else{
						ConceptType ct = this.getPointedComponent(e.getX(),e.getY());
						if((ct != null) && (!ct.equals(temporaryCTI))){
							if(temporaryCTI.equals(mf.getOntology().getUniversalConceptType())){
								JOptionPane.showMessageDialog(mf,Constants.UNIVERSAL_HAS_NO_PARENT_MESSAGE);
							}
							else{
								ct.addChild(temporaryCTI);
								//temporaryCTI.addParent(ct);
								temporaryCTI.setSelected(false);
								temporaryCTI = null;
								this.repaint();
							}
						}
					}
				}
			}
			if(mode == 1){
				if(e.getModifiers() == InputEvent.BUTTON1_MASK){
					temporaryCTI = this.getPointedComponent(e.getX(),e.getY());
					if(temporaryCTI != null){
						if(JOptionPane.showConfirmDialog(this,Constants.REMOVE_CONCEPT_TYPE_CONFIRM_MESSAGE,Constants.CONFIRMATION_BOX,JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION){
							temporaryCTI.destroy(mf.getOntology());
							this.mf.repaint();
							this.mf.getOntologySummaryFrame().refresh();
						}
					}
					else{
						this.removePointedLink(e.getX(),e.getY());
						this.repaint();
					}
				}
			}
		}
	}
	
	public void mouseEntered(MouseEvent e){
		//if(mode != -1) this.setConceptTypeMode(this.mode);
	}
	
	public void mouseExited(MouseEvent e){}
	
	public void mousePressed(MouseEvent e){
		if(mode != -1){
			if(e.getButton() == MouseEvent.BUTTON3){
				if(temporaryCTI != null) temporaryCTI.setSelected(false);
				temporaryCTI = this.getPointedComponent(e.getX(),e.getY());
				if((temporaryCTI != null) && (!mf.getOntology().getUniversalConceptType().equals(temporaryCTI))) new ConceptTypePropertyFrame(temporaryCTI,mf);
				temporaryCTI = null;
				this.repaint();
			}
			else{
				if((mode == 0) || (mode == 2)){
					tempCTI = this.getPointedComponent(e.getX(),e.getY());
					pointerPosition = new Point(e.getX(),e.getY());
				}
			}
		}
	}
	
	public void mouseReleased(MouseEvent e){
		if((mode == 0) || (mode == 2)){
			tempCTI = null;
			this.repaint();
		}
	}
	
	public void mouseDragged(MouseEvent e){
		if((mode == 0) || (mode == 2)){
			if((tempCTI != null) && (e.getX() > 0) && (e.getX() < this.getWidth()) &&
													(e.getY() > 0) && (e.getY() < this.getHeight())){
				Graphics g = this.getGraphics();
				tempCTI.erase(g,Constants.CONCEPT_TYPES_FRAME_COLOR,mf.getOntologyLanguage());
				tempCTI.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 ConceptType getPointedComponent(int x, int y){
		ConceptType result = null;
		double distance = Double.MAX_VALUE;
		if((mf.getOntology() != null) && (mf.getOntology().getConceptTypes() != null)){
			for(Iterator i = mf.getOntology().getConceptTypes().iterator();i.hasNext();){
				ConceptType ct = (ConceptType) i.next();
				if(ct.containsPoint(x,y,this.getGraphics(),mf.getOntologyLanguage())){
					double temp = Math.sqrt(((ct.getCenter().x - x) * (ct.getCenter().x - x))
												 + ((ct.getCenter().y - y) * (ct.getCenter().y - y)));
					if(temp <= distance){
						distance = temp;
						result = ct;
					}
				}
			}
		}
		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().getConceptTypes() != null)){
			for(Iterator i = mf.getOntology().getConceptTypes().iterator();i.hasNext();){
				ConceptType ct = (ConceptType) i.next();
				for(Iterator j = ct.getChildren().iterator();j.hasNext();){
					ConceptType child = (ConceptType) j.next();
					if(ConceptualPrimitive.isALinkContainsPoint(this.getGraphics(),new Point(x,y),ct,child,mf.getOntologyLanguage())){
						ct.removeChild(child);
						return true;
					}
				}
			}
		}
		return false;
	}
	
	public void paint(Graphics g){
		g.setColor(Constants.CONCEPT_TYPES_FRAME_COLOR);
		g.fillRect(0,0,this.getWidth(),this.getHeight());
		if(mf.getOntology() != null){
			for(Iterator i = mf.getOntology().getConceptTypes().iterator();i.hasNext();){
				ConceptType ct = (ConceptType) i.next();
				ct.paintChildrenLinks(g,mf.getOntologyLanguage());
			}
			for(Iterator i = mf.getOntology().getConceptTypes().iterator();i.hasNext();){
				ConceptType ct = (ConceptType) i.next();
				ct.paintObject(g,mf.getOntologyLanguage());
			}
		}
	}
	
	public ConceptTypesPanel(MainFrame mf){
		super();
		this.mf = mf;
		mode = -1;
		this.setBackground(Constants.CONCEPT_TYPES_FRAME_COLOR);
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		this.setEnabled(true);
	}	
}

⌨️ 快捷键说明

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