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

📄 gpgraph.java

📁 用JGraph编的软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		Set edges =
			DefaultGraphModel.getEdges(graphModel, new Object[] { vertex1 });
		Iterator it = edges.iterator();
		while (it.hasNext()) {
			Object edge = it.next();
			Object source = getSourceVertex(edge);
			Object target = getTargetVertex(edge);
			if ((source == vertex1 && target == vertex2)
				|| (source == vertex2 && target == vertex1))
				result.add(edge);
		}
		return result.toArray();
	}

	/**
	 * Overrides <code>JComponent</code>'buttonSelect <code>getToolTipText</code>
	 * method in order to allow the graph controller to create a tooltip
	 * for the topmost cell under the mousepointer. This differs from JTree
	 * where the renderers tooltip is used.
	 * <p>
	 * NOTE: For <code>JGraph</code> to properly display tooltips of its
	 * renderers, <code>JGraph</code> must be a registered component with the
	 * <code>ToolTipManager</code>.  This can be done by invoking
	 * <code>ToolTipManager.sharedInstance().registerComponent(graph)</code>.
	 * This is not done automatically!
	 * @param event the <code>MouseEvent</code> that initiated the
	 * <code>ToolTip</code> display
	 * @return a string containing the  tooltip or <code>null</code>
	 * if <code>event</code> is null
	 */
	public String getToolTipText(MouseEvent event) {
		if (event != null) {
			Object cell = getFirstCellForLocation(event.getX(), event.getY());
			if (cell != null) {
				String tmp = convertValueToString(cell);
				String s = "<html>";
				if (tmp != null && tmp.length() > 0)
					s = s + "<strong>" + tmp + "</strong><br>";
				return s + getToolTipForCell(cell) + "</html>";
			}
		}
		return null;
	}

	protected String getToolTipForCell(Object cell) {
		CellView view = getGraphLayoutCache().getMapping(cell, false);
		String s = "";
		Rectangle bounds = view.getBounds();
		if (bounds != null) {
			s = s + "Location: " + bounds.x + ", " + bounds.y + "<br>";
			s = s + "Size: " + bounds.width + ", " + bounds.height + "<br>";
		}
		java.util.List points = GraphConstants.getPoints(view.getAttributes());
		if (points != null)
			s = s + "Points: " + points.size() + "<br>";
		if (!(cell instanceof Edge) && !(cell instanceof Port)) {
			s = s + "Children: " + graphModel.getChildCount(cell) + "<br>";
			int n =
				DefaultGraphModel
					.getEdges(getModel(), new Object[] { cell })
					.size();
			s = s + "Edges: " + n;
		} else if (cell instanceof Edge) {
			Edge edge = (Edge) cell;
			Object source = graphModel.getSource(edge);
			if (source != null) {
				String host =
					convertValueToString(graphModel.getParent(source));
				String port = convertValueToString(source);
				s = s + "Source: " + host + ":" + port + "<br>";
			}
			Object target = graphModel.getTarget(edge);
			if (target != null) {
				String host =
					convertValueToString(graphModel.getParent(target));
				String port = convertValueToString(target);
				s = s + "Target: " + host + "/" + port + "<br>";
			}
		}
		return s;
	}

	/**
	 * Notification from the <code>UIManager</code> that the L&F has changed.
	 * Replaces the current UI object with the latest version from the
	 * <code>UIManager</code>. Subclassers can override this to support
	 * different GraphUIs.
	 * @see JComponent#updateUI
	 *
	 */
	public void updateUI() {
		setUI(new GPGraphUI());
		invalidate();
	}


	/** Returns true if the given vertices are conntected by a single edge
	 * in this document.
	 */
	public boolean isNeighbour(Object v1, Object v2) {
		Object[] edges = getEdgesBetween(v1, v2);
		return (edges != null && edges.length > 0);
	}

	// Serialization support
	private void readObject(ObjectInputStream s)
		throws IOException, ClassNotFoundException {
		s.defaultReadObject();
		pageFormat = new PageFormat();
	}

	//
	// Multiline View
	//

	class MultiLinedView extends VertexView {
		// static
		final MultiLinedRenderer renderer = new MultiLinedRenderer();
		// static
		final MultiLinedEditor editor = new MultiLinedEditor();

		public MultiLinedView(Object cell, JGraph graph, CellMapper cm) {
			super(cell, graph, cm);
		}

		public CellViewRenderer getRenderer() {
			return renderer;
		}

		public GraphCellEditor getEditor() {
			return editor;
		}

		class MultiLinedEditor extends DefaultGraphCellEditor {
			class RealCellEditor
				extends AbstractCellEditor
				implements GraphCellEditor {
				JTextArea editorComponent = new JTextArea();
				public RealCellEditor() {
					editorComponent.setBorder(
						UIManager.getBorder("Tree.editorBorder"));
					editorComponent.setLineWrap(true);
					editorComponent.setWrapStyleWord(true);

					//substitute a JTextArea's VK_ENTER action with our own that will stop an edit.
					editorComponent.getInputMap(JComponent.WHEN_FOCUSED).put(
						KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
						"enter");
					editorComponent
						.getActionMap()
						.put("enter", new AbstractAction() {
						public void actionPerformed(ActionEvent e) {
							stopCellEditing();
						}
					});
				}

				public Component getGraphCellEditorComponent(
					JGraph graph,
					Object value,
					boolean isSelected) {
					editorComponent.setText(value.toString());
					editorComponent.selectAll();
					return editorComponent;
				}

				public Object getCellEditorValue() {
					return editorComponent.getText();
				}

				public boolean stopCellEditing() {
					//set the size of a vertex to that of an editor.
					CellView view =
						graph.getGraphLayoutCache().getMapping(
							graph.getEditingCell(),
							false);
					Map map = view.getAllAttributes();
					Rectangle cellBounds = GraphConstants.getBounds(map);
					Rectangle editingBounds = editorComponent.getBounds();
					cellBounds.setBounds(cellBounds.x,
							cellBounds.y,
							editingBounds.width,
							editingBounds.height);
					// Call view.setBounds() to make sure the view stores
					// the new bounds
					return super.stopCellEditing();
				}

				public boolean shouldSelectCell(EventObject event) {
					editorComponent.requestFocus();
					return super.shouldSelectCell(event);
				}
			}

			public MultiLinedEditor() {
				super();
			}
			/**
			 * Overriding this in order to set the size of an editor to that of an edited view.
			 */
			public Component getGraphCellEditorComponent(
				JGraph graph,
				Object cell,
				boolean isSelected) {

				Component component =
					super.getGraphCellEditorComponent(graph, cell, isSelected);

				//set the size of an editor to that of a view
				CellView view =
					graph.getGraphLayoutCache().getMapping(cell, false);
				editingComponent.setBounds(view.getBounds());

				//I have to set a font here instead of in the RealCellEditor.getGraphCellEditorComponent() because
				//I don't know what cell is being edited when in the RealCellEditor.getGraphCellEditorComponent().
				Font font = GraphConstants.getFont(view.getAllAttributes());
				editingComponent.setFont(
					(font != null) ? font : graph.getFont());

				return component;
			}

			protected GraphCellEditor createGraphCellEditor() {
				return new MultiLinedEditor.RealCellEditor();
			}

			/**
			 * Overriting this so that I could modify an eiditor container.
			 * see http://sourceforge.net/forum/forum.php?thread_id=781479&forum_id=140880
			 */
			protected Container createContainer() {
				return new MultiLinedEditor.ModifiedEditorContainer();
			}

			class ModifiedEditorContainer extends EditorContainer {
				public void doLayout() {
					super.doLayout();
					//substract 2 pixels that were added to the preferred size of the container for the border.
					Dimension cSize = getSize();
					Dimension dim = editingComponent.getSize();
					editingComponent.setSize(dim.width - 2, dim.height);

					//reset container's size based on a potentially new preferred size of a real editor.
					setSize(cSize.width, getPreferredSize().height);
				}
			}
		}

		class MultiLinedRenderer extends JTextArea
			implements CellViewRenderer {
			{
				setLineWrap(true);
				setWrapStyleWord(true);
			}

			public Component getRendererComponent(
				JGraph graph,
				CellView view,
				boolean sel,
				boolean focus,
				boolean preview) {
				setText(view.getCell().toString());

				Map attributes = view.getAllAttributes();
				installAttributes(graph, attributes);
				return this;
			}

			protected void installAttributes(JGraph graph, Map attributes) {
				setOpaque(GraphConstants.isOpaque(attributes));
				Color foreground = GraphConstants.getForeground(attributes);
				setForeground(
					(foreground != null) ? foreground : graph.getForeground());
				Color background = GraphConstants.getBackground(attributes);
				setBackground(
					(background != null) ? background : graph.getBackground());
				Font font = GraphConstants.getFont(attributes);
				setFont((font != null) ? font : graph.getFont());
				Border border = GraphConstants.getBorder(attributes);
				Color bordercolor = GraphConstants.getBorderColor(attributes);
				if (border != null)
					setBorder(border);
				else if (bordercolor != null) {
					int borderWidth =
						Math.max(
							1,
							Math.round(
								GraphConstants.getLineWidth(attributes)));
					setBorder(
						BorderFactory.createLineBorder(
							bordercolor,
							borderWidth));
				}
			}
		}
	}


	/**
	 * Returns the writeProperties.
	 * @return Hashtable
	 */
	public Hashtable getWriteProperties() {
		return writeProperties;
	}

	/**
	 * Sets the writeProperties.
	 * @param writeProperties The writeProperties to set
	 */
	public void setWriteProperties(Hashtable writeProperties) {
		this.writeProperties = writeProperties;
	}

}

⌨️ 快捷键说明

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