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

📄 axiomspanel.java

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

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

/**
 * This class allows to draw an axiom, to move the nodes, to add edges and nodes,
 * to remove edges or nodes, it provides creation and structuration fonctions for
 * all tat can be done on the axiom.
 *
 * @author Fr閐閞ic F黵st
 * @author Nicolas Roy
 * @author Matthieu Legall
 * @author Dounia K閐a
 */
public class AxiomsPanel extends JPanel implements MouseListener, MouseMotionListener{	

	// The mode of interaction : -1 = disabled, 0=concept creation, 1=relation creation, 
	// 2=destruction, 3=linking
	private int mode;
	private MainFrame mf;

	private Node temporaryImage;
	private Concept tempCI;
	private Relation tempRI;
	private Axiom currentAxiomImage;
	private Point pointerPosition;
	private GraphDrawing gd;

	
	public void clear(){
		this.temporaryImage = null;
		this.currentAxiomImage = null;
		this.tempCI = null;
		this.tempRI = null;
		this.repaint();
	}
	
	public void setPrimitiveMode(int mode){
		this.mode = mode;
		if(temporaryImage != null){
			temporaryImage.setSelected(false);
		}
		temporaryImage = null;
		if(tempRI != null){
			tempRI.setSelected(false); 
			tempRI = null;
		}
		if(tempCI != null){
			tempCI.setSelected(false);
			tempCI = null;
		}
		this.repaint();
	}
	
	public Language getOntologyLanguage() {
		return (mf.getOntologyLanguage());
	}

	public void mouseClicked(MouseEvent e){
		if((currentAxiomImage != null) && (!currentAxiomImage.isFinalized())){
			if(e.getButton() == MouseEvent.BUTTON3){
				Concept ci = this.getPointedConcept(e.getX(),e.getY());
				if(ci == null){
					Relation ri = this.getPointedRelation(e.getX(),e.getY());
					if(ri != null) new RelationPropertyFrame(ri,mf,true);
					else{
						if(tempRI != null){
							tempRI.setSelected(false);
							tempRI = null;
						}
						if(tempCI != null){
							tempCI.setSelected(false);
							tempCI = null;
						}
						if(temporaryImage != null){
							temporaryImage.setSelected(false);
							temporaryImage = null; 
						}
					}
				}
				else new ConceptPropertyFrame(ci,mf,true);
			}
			if(e.getButton() == MouseEvent.BUTTON1){
				if(mode == 0){
					Concept c = new Concept(e.getX(),e.getY());
					currentAxiomImage.addHypothesisConcept(c);
				}
				if(mode == 1){
					Relation r = new Relation(e.getX(),e.getY());
					currentAxiomImage.addHypothesisRelation(r);
				}
				if(mode == 2){
					temporaryImage = this.getPointedComponent(e.getX(),e.getY());
					if(temporaryImage != null){
						currentAxiomImage.removeObject(temporaryImage);
					}
					else{
						this.removePointedLink(this.getGraphics(),e.getX(),e.getY());
						this.repaint();
					}
				}
				if(mode == 3){
					if(tempCI == null){
						if(tempRI == null){
							tempCI = this.getPointedConcept(e.getX(),e.getY());
							if(tempCI == null){
								tempRI = this.getPointedRelation(e.getX(),e.getY());
								if(tempRI != null) tempRI.setSelected(true);
							}
							else tempCI.setSelected(true);
						}
						else{
							tempCI = this.getPointedConcept(e.getX(),e.getY());
							if(tempCI != null){
								if(!tempRI.addLinkedConcept(tempCI)){
									JOptionPane.showMessageDialog(this,Constants.THIS_RELATION_CAN_NOT_LINK_MORE_CONCEPT);	
								}
								tempRI.setSelected(false);
								tempRI = null;
								tempCI = null;
							}
						}
					}
					else{
						if(tempRI == null){
							tempRI = this.getPointedRelation(e.getX(),e.getY());
							if(tempRI != null){
								tempRI.addLinkedConcept(tempCI);
								tempCI.setSelected(false);
								tempRI = null;
								tempCI = null;
							}
						}
					}
				}
			}
			this.repaint();
		}
		else{
			JOptionPane.showMessageDialog(this,Constants.FINALIZED_AXIOM_MESSAGE);	
		}
	}
	
	public void mouseEntered(MouseEvent e){
	}
	
	public void mouseExited(MouseEvent e){}
	
	public void mousePressed(MouseEvent e){
		if(e.getButton() == MouseEvent.BUTTON1){
			if((mode == 0) || (mode == 1) || (mode == 3)){
				temporaryImage = this.getPointedComponent(e.getX(),e.getY());
				if (temporaryImage!=null)
					temporaryImage.setFixed(true);
				pointerPosition = new Point(e.getX(),e.getY());
			}
		}
	}
	
	public void mouseReleased(MouseEvent e){
		if((mode == 0) || (mode == 1) || (mode == 3)){
			if (temporaryImage!=null)
				temporaryImage.setFixed(false);
			temporaryImage = null;
			this.repaint();
		}
	}
	
	public void mouseDragged(MouseEvent e){
		if((mode == 0) || (mode == 1) || (mode == 3)){
			if((temporaryImage != null) && (e.getX() > 0) && (e.getX() < this.getWidth()) &&
													(e.getY() > 0) && (e.getY() < this.getHeight())){
				Graphics g = this.getGraphics();
				temporaryImage.erase(g,Constants.AXIOM_FRAME_COLOR,mf.getOntologyLanguage());
				temporaryImage.translate(e.getX() - pointerPosition.x,e.getY() - pointerPosition.y);
				this.repaint();
				pointerPosition.setLocation(e.getX(),e.getY());
			}
		}
	}
	
	public void mouseMoved(MouseEvent e){}
	
	/** Returns the concept image which contains the point (x,y) with the closiest center to the point (x,y). 
	  * Returns null if any concept is pointed. */
	public Concept getPointedConcept(int x, int y){
		Concept result = null;
		double distance = Double.MAX_VALUE;
		if(currentAxiomImage != null){
			for(Iterator i = currentAxiomImage.getAllConcepts().iterator();i.hasNext();){
				Concept ci = (Concept) i.next();
				if(ci.containsPoint(x,y,this.getGraphics(),mf.getOntologyLanguage())){
					double temp = Math.sqrt(((ci.x - x) * (ci.x - x))
												 + ((ci.y - y) * (ci.y - y)));
					if(temp <= distance){
						distance = temp;
						result = ci;
					}
				}
			}
		}
		return result;
	}
	
		
	/** Returns the relation image which contains the point (x,y) with the closiest center to the point (x,y). 
	  * Returns null if any relation is pointed. */
	public Relation getPointedRelation(int x, int y){
		Relation result = null;
		double distance = Double.MAX_VALUE;	
		if(currentAxiomImage != null){
			for(Iterator i = currentAxiomImage.getAllRelations().iterator();i.hasNext();){
				Relation ri = (Relation) i.next();
				if(ri.containsPoint(x,y,this.getGraphics(),mf.getOntologyLanguage())){
					double temp = Math.sqrt(((ri.x - x) * (ri.x - x))
												 + ((ri.y - y) * (ri.y - y)));
					if(temp <= distance){
						distance = temp;
						result = ri;
					}
				}
			}
		}
		return result;
	}
	
	/** Returns the object image which contains the point (x,y) with the closiest center to the point (x,y). 
	  * Returns null if any component is pointed. */
	public Node getPointedComponent(int x, int y){
		Node noi = null;
		noi = this.getPointedConcept(x,y);
		if(noi == null) return this.getPointedRelation(x,y);
		else return noi;
	}
	
	/** Creates a new axiom, add it in the list and display it on the panel. */
	public void newAxiom(){
		String currentAxiomName = JOptionPane.showInputDialog(Constants.NAME_OF_THE_AXIOM_LABEL);
		if(currentAxiomName != null){
			currentAxiomImage = null;
			Terms t = new Terms(currentAxiomName,mf.getOntologyLanguage());
			currentAxiomImage = mf.getOntology().newAxiom(t);
			temporaryImage = null;
			tempCI = null;
			tempRI = null;
			mf.getOntologySummaryFrame().refresh();
		}
	}
	
	/** Load the axiom selected in the list. */
	public void loadAxiom(Axiom a){
		temporaryImage = null;
		tempCI = null;
		tempRI = null;
		//currentAxiomImage = null;
		currentAxiomImage = a;
		this.repaint();
	}
	
	/** Removes the current axiom from the list of axiom, the ontology and the ontology summary frame. */
	public void removeAxiom(){
		if(currentAxiomImage != null){
			int confirm = JOptionPane.showConfirmDialog(this,Constants.DELETE_AXIOM_CONFIRM_MESSAGE,"",JOptionPane.YES_NO_OPTION);
			if(confirm == JOptionPane.YES_OPTION){
				mf.getOntology().removeAxiom(currentAxiomImage);
				mf.getOntologySummaryFrame().refresh();
				currentAxiomImage = null;
				this.repaint();
			} 
		}
	}
	
	public Axiom getCurrentAxiom(){
		return currentAxiomImage;
	}
	
	/** Remove the first encountered link between a concept and a relation which contains the 
	 *  point (x,y). Returns true if such a link exists, false otherwise. */
	public boolean removePointedLink(Graphics g, int x, int y){
		if(currentAxiomImage != null){
			for(Iterator i = currentAxiomImage.getAllRelations().iterator();i.hasNext();){
				Relation r = (Relation) i.next();
				for(int cpt = 1;cpt <= r.getType().getArity();cpt ++){
					if(r.linkContainsPoint(g,mf.getOntologyLanguage(),cpt,new Point(x,y))){
						r.removeLinkedConcept(cpt);
						return true;
					}
				}
			}
		}
		return false;
	}

	public void startDrawingHelp(){
		if (currentAxiomImage!=null){
			gd = new GraphDrawing(this, currentAxiomImage);
		}
	}

	public void stopDrawingHelp() {
		if (gd!=null) gd.stop();
	}


	public void paint(Graphics g){
		g.setColor(Constants.AXIOMS_FRAME_COLOR);
		g.fillRect(0,0,this.getWidth(),this.getHeight());
		if(currentAxiomImage != null) currentAxiomImage.paintObject(g,mf.getOntologyLanguage());
	}
	
	public AxiomsPanel(MainFrame mf){
		super();
		this.mf = mf;
		mode = -1;
		this.setBackground(Constants.AXIOMS_FRAME_COLOR);
		//this.setBackground(new Color(255,0,0));
    	this.setEnabled(true);
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
	}
	
}

⌨️ 快捷键说明

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