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

📄 grapheditionpanel.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.*;
import java.awt.geom.*;

/** 
 * This class represents the panel into which a graph can be drawed by the user. Such a panel 
 * must be included in a JScrollPane to enable all drawing fonctionalities.
 *
 * @author Fr閐閞ic F黵st
 */
public class GraphEditionPanel extends JPanel implements ActionListener, MouseListener, MouseMotionListener{

	protected MainFrame mf;
	protected Graph g;
	protected Color bgColor;
	// The mode of interaction : 0=concept creation, 1=relation creation, 2=destruction, 3=linking
	protected int mode;
	protected NamedGraphicalObject pickedObject;
	protected Point pointerPosition;
	protected Point dragBegin;
	protected LinkedList selectedObjects;
	protected LinkedList buffer;
	
	public Graph getGraph(){
		return this.g;
	}
	
	/** Erase the previous graph and paint the given one on the panel. */
	public void setGraph(Graph g){
		this.clearGraph();
		this.g = g;
		this.reSize(this.getGraphics());
		this.repaint();
	}
	
	public void clearGraph(){
		this.pickedObject = null;
		this.g = new Graph();
		this.dragBegin = null;
		this.pointerPosition = null;
		this.selectedObjects.clear();
		this.repaint();
	}
	
	public void setPrimitiveMode(int mode){
		this.mode = mode;
		this.repaint();
	}
	
	public void mouseClicked(MouseEvent e){
		if(g != null){
			if(e.getButton() == MouseEvent.BUTTON3){
				Concept ci = this.getPointedConcept(e.getX(),e.getY());
				if(ci != null) new ConceptPropertyFrame(ci,mf,false);	
				else{
					Relation ri = this.getPointedRelation(e.getX(),e.getY());
					if(ri != null) new RelationPropertyFrame(ri,mf,false);
					else this.cancelSelection();
				}
			}
			if(e.getButton() == MouseEvent.BUTTON1){
				NamedGraphicalObject tempObject = this.getPointedObject(e.getX(),e.getY());
				if(tempObject == null){
					if(mode == 0){
						Concept c = new Concept(e.getX(),e.getY());
						g.addConcept(c);
					}
					if(mode == 1){
						Relation r = new Relation(e.getX(),e.getY());
						g.addRelation(r);
					}
					if(mode == 2){
						for(Iterator i = this.g.getRelations().iterator();i.hasNext();){
							Relation r = (Relation) i.next();
							for(int cpt = 1;cpt <= r.getType().getArity();cpt ++){
								if(r.linkContainsPoint(this.getGraphics(),mf.getOntologyLanguage(),cpt,new Point(e.getX(),e.getY())))
									r.removeLinkedConcept(cpt);
							}
						}
					}
				}
				else{
					if(mode == 2){
						this.selectedObjects.remove(tempObject);
						g.removeObject(tempObject);
					}
					if(mode == 3){
						if(this.selectedObjects.size() == 0){
							this.selectedObjects.add(tempObject);
							tempObject.setSelected(true);
						}
						else{
							if(this.selectedObjects.size() == 1){
								NamedGraphicalObject selection = (NamedGraphicalObject) this.selectedObjects.getFirst();
								if((selection instanceof Concept) &&
									(tempObject instanceof Relation)){
										((Relation) tempObject).addLinkedConcept((Concept) selection);
										this.cancelSelection();
								}
								else{
									if((selection instanceof Relation) &&
										(tempObject instanceof Concept)){
											((Relation) selection).addLinkedConcept((Concept) tempObject);
											this.cancelSelection();
									}
									else{
										this.cancelSelection();
										this.selectedObjects.add(selection);
										selection.setSelected(true);
									}
								}
							}
						}
					}
					if((mode != 2) && (mode != 3)){
						if(!tempObject.isSelected()){
							if(!e.isShiftDown()){
								this.cancelSelection();
								this.selectedObjects.add(tempObject);
								tempObject.setSelected(true);
							}
							else{
								this.selectedObjects.add(tempObject);
								tempObject.setSelected(true);
							}
						}
						else{
							if(e.isShiftDown()){
								this.selectedObjects.remove(tempObject);
								tempObject.setSelected(false);
							}
							else{
								this.cancelSelection();
								this.selectedObjects.add(tempObject);
								tempObject.setSelected(true);
							}
						}
					}
				}
			}
			this.repaint();
		}
	}
	
	public void mouseEntered(MouseEvent e){}
	
	public void mouseExited(MouseEvent e){}
	
	public void mousePressed(MouseEvent e){
		if(e.getButton() == MouseEvent.BUTTON1){
			pickedObject = this.getPointedObject(e.getX(),e.getY());
			pointerPosition = new Point(e.getX(),e.getY());
			if(pickedObject == null) dragBegin = new Point(e.getX(),e.getY());
		}
	}
	
	public void mouseReleased(MouseEvent e){
		if((pickedObject == null) && (dragBegin != null)){
			this.cancelSelection();
			this.selectedObjects = this.getSelectedObjects(Math.min(dragBegin.x,e.getX()),Math.min(dragBegin.y,e.getY()),Math.abs(dragBegin.x - e.getX()),Math.abs(dragBegin.y - e.getY()));
			for(Iterator i = this.selectedObjects.iterator();i.hasNext();){
				NamedGraphicalObject tempObject = (NamedGraphicalObject) i.next();
				tempObject.setSelected(true);
			}
		}
		pickedObject = null;
		dragBegin = null;
		pointerPosition = null;
		this.repaint();
	}
	
	public void mouseDragged(MouseEvent e){
		if((pickedObject != null) && (e.getX() > 0) && (e.getX() < this.getWidth()) &&
			(e.getY() > 0) && (e.getY() < this.getHeight())){
			if(this.selectedObjects.contains(pickedObject)){
				for(Iterator i = this.selectedObjects.iterator();i.hasNext();){
					((NamedGraphicalObject) i.next()).translate(e.getX() - pointerPosition.x,e.getY() - pointerPosition.y);
				}
			}
			else pickedObject.translate(e.getX() - pointerPosition.x,e.getY() - pointerPosition.y);
			pointerPosition.setLocation(e.getX(),e.getY());
		}
		if((pickedObject == null) && (pointerPosition != null)){
			pointerPosition.setLocation(e.getX(),e.getY());
		}
		this.reSize(this.getGraphics());
		this.repaint();
	}
	
	public void mouseMoved(MouseEvent e){}
	
	public void actionPerformed(ActionEvent event){
		if(event.getActionCommand().equals("Copy")){
			this.buffer.clear();
			Hashtable table = new Hashtable();
			for(Iterator i = this.selectedObjects.iterator();i.hasNext();){
				NamedGraphicalObject tempObject = (NamedGraphicalObject) i.next();
				if(tempObject instanceof Concept){
					Concept c = ((Concept) tempObject).cloneConcept();
					c.translate(Constants.PASTE_MOVE_X,Constants.PASTE_MOVE_Y);
					this.buffer.add(c);
					table.put(tempObject,c);
				}
			}
			for(Iterator i = this.selectedObjects.iterator();i.hasNext();){
				NamedGraphicalObject tempObject = (NamedGraphicalObject) i.next();
				if(tempObject instanceof Relation){
					Relation r = ((Relation) tempObject).cloneRelation();
					for(int index = 1;index <= ((Relation) tempObject).getArity();index ++){
						Concept linked = ((Relation) tempObject).getLinkedConcept(index);
						if(linked != null) r.setLinkedConcept((Concept) table.get(linked),index);
					}
					r.translate(Constants.PASTE_MOVE_X,Constants.PASTE_MOVE_Y);
					this.buffer.add(r);
				}
			}
		}
		if(event.getActionCommand().equals("Cut")){
			this.buffer.clear();
			Hashtable table = new Hashtable();
			for(Iterator i = this.selectedObjects.iterator();i.hasNext();){
				NamedGraphicalObject tempObject = (NamedGraphicalObject) i.next();
				if(tempObject instanceof Concept){
					Concept c = ((Concept) tempObject).cloneConcept();
					c.translate(Constants.PASTE_MOVE_X,Constants.PASTE_MOVE_Y);
					this.buffer.add(c);
					table.put(tempObject,c);
				}
			}
			for(Iterator i = this.selectedObjects.iterator();i.hasNext();){
				NamedGraphicalObject tempObject = (NamedGraphicalObject) i.next();
				if(tempObject instanceof Relation){
					Relation r = ((Relation) tempObject).cloneRelation();
					for(int index = 1;index <= ((Relation) tempObject).getArity();index ++){
						Concept linked = ((Relation) tempObject).getLinkedConcept(index);
						if(linked != null) r.setLinkedConcept((Concept) table.get(linked),index);
					}
					r.translate(Constants.PASTE_MOVE_X,Constants.PASTE_MOVE_Y);
					this.buffer.add(r);
				}
			}
			for(Iterator i = this.selectedObjects.iterator();i.hasNext();){
				g.removeObject((NamedGraphicalObject) i.next());
			}
			this.cancelSelection();
			this.repaint();
		}
		if(event.getActionCommand().equals("Paste")){
			for(Iterator i = this.buffer.iterator();i.hasNext();){
				NamedGraphicalObject tempObject = (NamedGraphicalObject) i.next();
				if(tempObject instanceof Concept)
					this.g.addConcept((Concept) tempObject);
				else this.g.addRelation((Relation) tempObject);
			}
			this.repaint();
		}
	}
	
	/** 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){
		if(g != null){
			Concept result = null;
			double distance = Double.MAX_VALUE;
			for(Iterator i = g.getConcepts().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;
		}
		else return null;
	}
	
		
	/** 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){
		if(g != null){
			Relation result = null;
			double distance = Double.MAX_VALUE;	
			for(Iterator i = g.getRelations().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;
		}
		else return null;
	}
	
	/** 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 NamedGraphicalObject getPointedObject(int x, int y){
		NamedGraphicalObject noi = null;
		noi = this.getPointedConcept(x,y);
		if(noi == null) return this.getPointedRelation(x,y);
		else return noi;
	}
	
	/** Returns the list of objects that are embedded into the specified rectangle. */
	public LinkedList getSelectedObjects(int x, int y, int width, int height){
		LinkedList result = new LinkedList();
		if(g != null){
			Rectangle rect = new Rectangle(x,y,width,height);
			for(Iterator i = g.getConcepts().iterator();i.hasNext();){
				Concept c = (Concept) i.next();
				if(c.isIncludedInto(this.getGraphics(),mf.getOntologyLanguage(),rect)) result.add(c);
			}
			for(Iterator i = g.getRelations().iterator();i.hasNext();){
				Relation r = (Relation) i.next();
				if(r.isIncludedInto(this.getGraphics(),mf.getOntologyLanguage(),rect)) result.add(r);
			}
		}
		return result;
	}
	
	public void cancelSelection(){
		for(Iterator i = this.selectedObjects.iterator();i.hasNext();){
			((NamedGraphicalObject) i.next()).setSelected(false);
		}
		this.selectedObjects.clear();
	}
	
	protected void reSize(Graphics gr){
		RectangularShape rs = this.g.getBounds(gr,mf.getOntologyLanguage());
		Point p = new Point((int) rs.getX(),(int) rs.getY());
		if(p.x < 0){
			if(p.y < 0){
				this.g.translate(-1*p.x,-1*p.y);
				p = new Point(0,0);
			}
			else{
				this.g.translate(-1*p.x,0);
				p = new Point(0,p.y);
			}
		}
		else{
			if(p.y < 0){
				this.g.translate(0,-1*p.y);
				p = new Point(p.x,0);
			}
		}
		Graph selectPart = this.g.getSelectedPart();
		if(!selectPart.isEmpty()){
			RectangularShape select = selectPart.getBounds(gr,mf.getOntologyLanguage());
			this.scrollRectToVisible(new Rectangle((int) select.getX(),(int) select.getY(),(int) select.getWidth(),(int) select.getHeight()));
		}
		this.setPreferredSize(new Dimension((int) rs.getWidth() + p.x,(int) rs.getHeight() + p.y));
		this.revalidate();
	}
	
	public void paint(Graphics gr){
		gr.setColor(bgColor);
		gr.fillRect(0,0,this.getWidth(),this.getHeight());
		if(g != null) g.paintObject(gr,mf.getOntologyLanguage());
		if((dragBegin != null) && (pointerPosition != null)){
			gr.setColor(Constants.SELECT_RECT_COLOR);
			gr.drawRect(Math.min(dragBegin.x,pointerPosition.x),Math.min(dragBegin.y,pointerPosition.y),Math.abs(dragBegin.x - pointerPosition.x),Math.abs(dragBegin.y - pointerPosition.y));
		} 
	}
	
	public GraphEditionPanel(MainFrame mf,Color bgColor){
		this.mf = mf;
		this.bgColor = bgColor;
		this.g = new Graph();
		this.mode = 0;
		this.selectedObjects = new LinkedList();
		this.buffer = new LinkedList();
		this.setBackground(bgColor);
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		this.registerKeyboardAction(this,"Copy",KeyStroke.getKeyStroke(Constants.COPY_KEY),JComponent.WHEN_IN_FOCUSED_WINDOW);
		this.registerKeyboardAction(this,"Cut",KeyStroke.getKeyStroke(Constants.CUT_KEY),JComponent.WHEN_IN_FOCUSED_WINDOW);
		this.registerKeyboardAction(this,"Paste",KeyStroke.getKeyStroke(Constants.PASTE_KEY),JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
}
	

⌨️ 快捷键说明

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