ontologywithclasshierarchygraph.java

来自「Semantic Web Ontology Editor」· Java 代码 · 共 1,285 行 · 第 1/3 页

JAVA
1,285
字号
	protected void searchForNodes(String subText) {
		subText = subText.trim();
		subText = subText.toLowerCase();

		if (subText.equals(""))
			return;

		HashSet treeNodes = new HashSet();
		Set vertexSet = graph.getVertices();

		for (Iterator it = vertexSet.iterator(); it.hasNext();) {
			OntologyGraphNode graphNode = (OntologyGraphNode) (((Vertex) it
					.next()).getUserDatum(OntologyWithClassHierarchyGraph.DATA));
			Set s = graphNode.matchNodeWithShortName(subText);
			treeNodes.addAll(s);
		}
		vv.setHighlightedNode(treeNodes, subText);
		mySearchField.setText("");
	}

	public OntologyVisualizationViewer getVV() {
		return vv;
	}

	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		//class history managing
		if (e.getSource() instanceof JButton) {
			JButton source = (JButton) e.getSource();
			if (source == myBackListButton) {
				backClassListHistory();
			} else if (source == myForwardListButton) {
				forwardClassListHistory();
			} 
			else if (source == myViewButton) 
			{				
				if (currentView == NORMAL_VIEW)
					this.setColorMode(OVERLAY_GRAPH_COLOR);
				else
					this.setColorMode(BASIC_COLOR);
			}
			else if (source == myFocusButton) 
			{
				ClassTreeNode node = vv.getCurrentSelectedNode();
				vv.panZoomToFitNode( node, node.getOntologyNode().getTreeNode() );
			}
			else if (source == mySearchButton) 
			{
				searchForNodes(mySearchField.getText());
			}
			return;
		}

		// menu item (and popup menu items)
		if (e.getSource() instanceof JMenuItem) {
			JMenuItem src = (JMenuItem) e.getSource();
			SwoopOntologyVertex picked = vv.getSelectedVertex();
			ClassTreeNode node = vv.getRightSelectedNode();

			// Vertex popup menu items
			if (src == myShowSubclassAxioms) {
				// ontain subclass axioms (Vector of SubclassAxiomContainer
				OntologyGraphAxiomWalker walker = new OntologyGraphAxiomWalker(
						picked, myModel);
				Vector subAxioms = walker.getSubclassAxioms();
				ListSelectionPopup popup = new ListSelectionPopup(this,
						SUBCLASS_AXIOM_LIST, subAxioms);
			} else if (src == myShowDomainAxioms) {
			} else if (src == myShowRangeAxioms) {
			} else if (src == myShowIndividualAxioms) {
			}
			// Class popup menu items
			else if (src == myShowRef) {
				System.out.println(node.getURI());
				myOverlayGraph.addNode(node);
				this.setColorMode(OVERLAY_GRAPH_COLOR);
			}
			return;
		}

		// controlpanel stuff
		if (cmd.equals("label")) {
			VertexStringer vs = ((JCheckBox) e.getSource()).isSelected() ? SHORT_LABEL
					: NO_LABEL;
			pr.setVertexStringer(vs);
		} else if (cmd.equals("inverseEdge")) {
			EdgeShapeFunction es = ((JCheckBox) e.getSource()).isSelected() ? CURVED_LINE
					: LINE;
			pr.setEdgeShapeFunction(es);
		} else if (cmd.equals("scale")) {
			vSize.setScaling(((JCheckBox) e.getSource()).isSelected());
			if (vSize.getScaling() == false) {
				pr.setIsDrawContent(false);
				v_content.setSelected(false);
			}
		} else if (cmd.equals("font")) {
			vFont.setBold(((JCheckBox) e.getSource()).isSelected());
		} else if (cmd.equals("content")) {
			pr.setIsDrawContent(((JCheckBox) e.getSource()).isSelected());
		} else if (cmd.equals("layout")) {
			String layoutName = ((JComboBox) e.getSource()).getSelectedItem()
					.toString();
			if (layoutName.startsWith("KK"))
				vv.setGraphLayout(new KKLayout(graph));
			else if (layoutName.startsWith("Spring"))
				vv.setGraphLayout(new SpringLayout(graph));
			else if (layoutName.startsWith("Circle"))
				vv.setGraphLayout(new SizeAwareCircleLayout(graph));
			else if (layoutName.startsWith("FR"))
				vv.setGraphLayout(new FRLayout(graph));
			else if (layoutName.startsWith("ISOM"))
				vv.setGraphLayout(new ISOMLayout(graph));
			else if (layoutName.startsWith("DAG"))
				vv.setGraphLayout(new DAGLayout(graph));
			else
				throw new RuntimeException("Unknown layout");
		}

		vv.repaint();
	}

	public void setColorMode(int mode) {
		if (mode == OntologyWithClassHierarchyGraph.PARTITION_FOCUS_COLOR) {
			myColorScheme = new PartitionFocusColorScheme();
			//pr.setVertexPaintFunction( myPartitionFocusColor );
		} else if (mode == OntologyWithClassHierarchyGraph.OVERLAY_GRAPH_COLOR) {
			myColorScheme = new OverlayGraphScheme();
			currentView = RELATION_VIEW;
		} else {
			myColorScheme = new DefaultColorScheme();
			currentView = NORMAL_VIEW;
			//this.myOverlayGraph.clear(); // reset edges/graphs
			//pr.setVertexPaintFunction( myBasicColor );
		}
		vv.repaint();
	}

	public void resetPartitionFocus() {
		Set vertexSet = this.graph.getVertices();
		for (Iterator it = vertexSet.iterator(); it.hasNext();) {
			SwoopOntologyVertex v = (SwoopOntologyVertex) it.next();
			v.setPartitionState(SwoopOntologyVertex.NONE);
			v.setPartitionDirtyBit(false);
		}
	}

	public void setPartitionFocus(OWLOntology ont) {
		SwoopOntologyVertex v = (SwoopOntologyVertex) this.graph
				.getUserDatum(ont);
		v.setPartitionState(SwoopOntologyVertex.FOCUS);
		v.setPartitionDirtyBit(true);
		LinkedList upList = new LinkedList();
		upList.addAll(v.getPredecessors());
		LinkedList downList = new LinkedList();
		downList.addAll(v.getSuccessors());
		markUpStream(upList);
		markDownStream(downList);
	}

	protected void markUpStream(LinkedList list) {
		if (list.isEmpty())
			return;
		SwoopOntologyVertex head = (SwoopOntologyVertex) list.removeFirst();
		head.setPartitionState(SwoopOntologyVertex.UPSTREAM);
		head.setPartitionDirtyBit(true);
		Set incomingNeighbors = head.getPredecessors();
		for (Iterator it = incomingNeighbors.iterator(); it.hasNext();) {
			SwoopOntologyVertex n = (SwoopOntologyVertex) it.next();
			if (n.isPartitionStateSet) // if the list already contains it...
									   // (cycles)
				continue;
			else
				list.add(n);
		}
		markUpStream(list);
	}

	protected void markDownStream(LinkedList list) {
		if (list.isEmpty())
			return;
		SwoopOntologyVertex head = (SwoopOntologyVertex) list.removeFirst();
		head.setPartitionState(SwoopOntologyVertex.DOWNSTREAM);
		head.setPartitionDirtyBit(true);
		Set outgoingNeighbors = head.getSuccessors();
		for (Iterator it = outgoingNeighbors.iterator(); it.hasNext();) {
			SwoopOntologyVertex n = (SwoopOntologyVertex) it.next();
			if (n.isPartitionStateSet) // if the list already contains it...
									   // (cycles)
				continue;
			else
				list.add(n);
		}
		markDownStream(list);
	}

	protected SwoopOntologyVertex addToGraph(DirectedSparseGraph graph, OntologyGraphNode node) 
	{
		SwoopOntologyVertex vertex = (SwoopOntologyVertex) graph.getUserDatum(node.getOntology());

		if (vertex == null) 
		{
			vertex = new SwoopOntologyVertex(this);
			vertex.setUserDatum(DATA, node, SHARE); // vertex.DATA =OntologyGraphNode
			graph.setUserDatum(node, vertex, SHARE); // graph.OntologyNode = SwoopOntologyVertex
			graph.setUserDatum(node.getOntology(), vertex, SHARE); // graph.onto = vertex (unique identifier)
			graph.addVertex(vertex); // add vertex to graph

			OWLOntology ont = node.getOntology();
			Collection linkedOnts = myProps.getLinkedElements(ont);
			for (Iterator i = linkedOnts.iterator(); i.hasNext();) 
			{
				Object linkedObj = i.next();
				OWLOntology linkedOnt = (OWLOntology) linkedObj;
				SwoopOntologyVertex linkedVertex = addToGraph(graph, buildOntologyNode(linkedOnt));
				DirectedSparseEdge edge = new DirectedSparseEdge(vertex, linkedVertex);
				graph.addEdge(edge);
			}
		}
		return vertex;
	}

	public DirectedSparseGraph getVisualGraph() 
	{ return graph; }

	public SwoopModel getModel() {
		return myModel;
	}

	public GraphColorScheme getColorScheme() {
		return myColorScheme;
	}

	public OverlayGraph getOverlayGraph() {
		return myOverlayGraph;
	}

	public HashMap getMyReasonerMap()
	{ return myReasonerMap; }
	
	private final class VertexSize implements VertexSizeFunction {
		boolean scale = true;

		int maxSize = Integer.MIN_VALUE;

		int minSize = Integer.MAX_VALUE;

		double factor = 1.0;

		public VertexSize() {
		}

		/*
		 * public void setGraph( Graph g ) { for(Iterator i =
		 * g.getVertices().iterator(); i.hasNext();) { Vertex vertex = (Vertex)
		 * i.next(); int size = ((TestGraphNode)vertex.getUserDatum( DATA
		 * )).getSize() ; maxSize = Math.max( maxSize, size ); minSize =
		 * Math.min( minSize, size ); }
		 * 
		 * if( maxSize == minSize ) factor = 0.0; else factor = (double) (MAX -
		 * MIN) / (maxSize - minSize); }
		 */

		public void setScaling(boolean scale) {
			this.scale = scale;
		}

		public boolean getScaling() {
			return scale;
		}

		public int getSize(Vertex vertex) {
			if (scale) {
				//int size = ((TestGraphNode)vertex.getUserDatum( DATA
				// )).getSize() ;
				//return ((int) ((size - minSize) * factor)) + MIN;
				return ((OntologyGraphNode) vertex.getUserDatum(DATA))
						.getDiameter();
			} else
				return MIN;
		}
	}

	private class VertexLabel implements VertexStringer {
		private boolean shortLabel;

		public VertexLabel(boolean qname) {
			this.shortLabel = qname;
		}

		public String getLabel(Vertex vertex) {
			OntologyGraphNode node = (OntologyGraphNode) vertex
					.getUserDatum(DATA);

			if (shortLabel)
				return myProps.getShortName(node.getOntology());
			else
				return myProps.getLongName(node.getOntology());
		}
	}

	public class VertexTips implements VisualizationViewer.ToolTipListener {
		public VertexTips() {
		}

		// shows name of the ontology in graph vertex
		//   or if mouse is over a class inside the ontology, show the short form
		// name
		//	 of the class
		public String getToolTipText(MouseEvent e) {
			PickSupport pickSupport = vv.getPickSupport();
			Point2D p = vv.transform(e.getPoint());

			Vertex v = pickSupport.getVertex(p.getX(), p.getY());
			if (v != null) {
				OntologyWithClassHierarchyRenderer rend = (OntologyWithClassHierarchyRenderer) vv
						.getRenderer();
				if (rend.getIsDrawContent()) {
					ClassTreeNode topNode = ((OntologyGraphNode) v
							.getUserDatum(OntologyWithClassHierarchyGraph.DATA))
							.getTreeNode();
					ClassTreeNode selectedNode = topNode.getSelectedChild(p);
					if (selectedNode != null) {
						//ConciseFormatEntityRenderer cfer = new
						// ConciseFormatEntityRenderer();
						//ConciseFormatVisitor vis = new ConciseFormatVisitor(
						// cfer, myModel);
						//vis.reset();
						//selectedNode.get
						return "<html> <b> &nbsp;" + selectedNode.toString() + "</b> <br>" +
						       "&nbsp; &nbsp; depth: " + selectedNode.getDepth() + "</b> <br>" +
							   "&nbsp; &nbsp; size: "  + (selectedNode.getSubTreeSize() ) + "</b> <br>" +
							   " </html>";
					}
				}

				return LONG_LABEL.getLabel(v);
			} else {
				Edge edge = pickSupport.getEdge(p.getX(), p.getY());
				if (edge != null) {
					return edge.toString();
				}

				return "<html><center>Use the mouse wheel to zoom<p>Click and Drag the mouse to pan</center></html>";
			}
		}
	}

	private final class BasicVertexColor implements VertexPaintFunction {
		protected PickedInfo pi;

		public BasicVertexColor(VisualizationViewer vv) {
			this.pi = vv.getPickedState();
		}

		public Paint getDrawPaint(Vertex v) {
			return pi.isPicked(v) ? myColorScheme
					.getOntologyNodeSelectOutlineColor((SwoopOntologyVertex) v)
					: myColorScheme
							.getOntologyNodeOutlineColor((SwoopOntologyVertex) v);
		}

		public Paint getFillPaint(Vertex v) {
			return myColorScheme
					.getOntologyNodeFillColor((SwoopOntologyVertex) v);
		}
	}

	private final static class VertexFont implements VertexFontFunction {
		protected boolean bold = false;

		Font f = new Font("Helvetica", Font.PLAIN, 12);

		Font b = new Font("Helvetica", Font.BOLD, 12);

		public void setBold(boolean bold) {
			this.bold = bold;
		}

		public Font getFont(Vertex v) {
			return bold ? b : f;
		}
	}

	private class EdgeShapeFn implements EdgeShapeFunction {
		public Shape getShape(Edge edge) {
			Pair pair = edge.getEndpoints();
			Vertex from = (Vertex) pair.getFirst();
			Vertex to = (Vertex) pair.getSecond();
			if (to.findEdge(from) == null)
				return LINE.getShape(edge);
			else
				return CURVE.getShape(edge);
		}

		public void setControlOffsetIncrement(float inc) {
			LINE.setControlOffsetIncrement(inc);
			CURVE.setControlOffsetIncrement(inc);
		}

	}

	public void modelChanged(ModelChangeEvent event) {

		if (event.getType() == ModelChangeEvent.MOTHERSHIP_DISPLAY) 
		{
			this.displayMotherShip();
		}
	}

	public void keyTyped(KeyEvent e) 
	{
		if (e.getKeyCode() == KeyEvent.VK_A )
			System.out.println("a");
	}

	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}

⌨️ 快捷键说明

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