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

📄 graphlayoutcache.java

📁 用JGraph编的软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			if (changed != null) {				for (int i = 0; i < changed.length; i++) {					if (!isVisible(changed[i])) {						Object source = graphModel.getSource(changed[i]);						Object target = graphModel.getTarget(changed[i]);						if ((source != null || target != null)							&& (isVisible(source) && isVisible(target)))							setVisible(changed[i], true);					}				}			}		}	}	/**	 * Adds the specified model root cells to the view.	 */	public void insertRoots(CellView[] views) {		if (views != null) {			refresh(views, true);			for (int i = 0; i < views.length; i++) {				if (views[i] != null					&& getMapping(views[i].getCell(), false) != null) {					CellView parentView = views[i].getParentView();					Object parent =						(parentView != null) ? parentView.getCell() : null;					if (!(views[i] instanceof PortView)						&& !roots.contains(views[i])						&& parent == null) {						roots.add(views[i]);						// Remove children for non-partial views?					}				}			}		}	}	/**	 * Removes the specified model root cells from the view by removing	 * the mapping between the cell and its view and makes the cells	 * invisible.	 */	public CellView[] removeRoots(Object[] cells) {		if (cells != null) {			CellView[] views = new CellView[cells.length];			for (int i = 0; i < cells.length; i++) {				views[i] = removeMapping(cells[i]);				if (views[i] != null) {					views[i].removeFromParent();					roots.remove(views[i]);				}			}			setVisibleImpl(cells, false);			return views;		}		return null;	}	//	// Cell Mapping	//	/**	 * Takes an array of views and returns the array of the corresponding	 * cells by using <code>getCell</code> for each view.	 */	public Object[] getCells(CellView[] views) {		if (views != null) {			Object[] cells = new Object[views.length];			for (int i = 0; i < views.length; i++)				if (views[i] != null)					cells[i] = views[i].getCell();			return cells;		}		return null;	}	/**	 * Returns the view for the specified cell. If create is true	 * and no view is found then a view is created using	 * createView(Object).	 */	public CellView getMapping(Object cell, boolean create) {		if (cell == null)			return null;		CellView view = (CellView) mapping.get(cell);		if (view == null && create) {			view = (CellView) hiddenSet.get(cell);			if (view != null && isVisible(cell)) {				putMapping(cell, view);				hiddenSet.remove(cell);			} else				view = factory.createView(cell, this);		}		return view;	}	/**	 * Returns the views for the specified array of cells without	 * creating these views on the fly.	 */	public CellView[] getMapping(Object[] cells) {		return getMapping(cells, false);	}	/**	 * Returns the views for the specified array of cells. Returned	 * array may contain null pointers if the respective cell is not	 * mapped in this view and <code>create</code> is <code>false</code>.	 */	public CellView[] getMapping(Object[] cells, boolean create) {		if (cells != null) {			CellView[] result = new CellView[cells.length];			for (int i = 0; i < cells.length; i++)				result[i] = getMapping(cells[i], create);			return result;		}		return null;	}	/**	 * Associates the specified model cell with the specified view.	 * Updates the portlist if necessary.	 */	public void putMapping(Object cell, CellView view) {		// Remove isVisible-condition?		if (cell != null && view != null && isVisible(cell))			mapping.put(cell, view);	}	/**	 * Removes the associaten for the specified model cell and	 * returns the view that was previously associated with the cell.	 * Updates the portlist if necessary.	 */	public CellView removeMapping(Object cell) {		if (cell != null) {			CellView view = (CellView) mapping.remove(cell);			return view;		}		return null;	}	//	// Partial View	//	// Null is always visible!	public boolean isVisible(Object cell) {		return !isPartial() || visibleSet.contains(cell) || cell == null;	}	public Set getVisibleSet() {		return new HashSet(visibleSet);	}	public void setVisibleSet(Set visible) {		visibleSet = visible;	}	public void setVisible(Object cell, boolean visible) {		setVisible(new Object[] { cell }, visible);	}	public void setVisible(Object[] cells, boolean visible) {		if (visible)			setVisible(cells, null);		else			setVisible(null, cells);	}	public void setVisible(Object[] visible, Object[] invisible) {		visible = addVisibleDependencies(visible, true);		invisible = addVisibleDependencies(invisible, false);		GraphViewEdit edit = new GraphViewEdit(null, visible, invisible);		edit.end();		graphModel.edit(null, null, null, new UndoableEdit[] { edit });	}	// This is used to augment the array passed to the setVisible method.	public Object[] addVisibleDependencies(Object[] cells, boolean visible) {		if (cells != null) {			if (visible) {				// Make Direct Child Ports and source and target vertex visible				Set all = new HashSet();				for (int i = 0; i < cells.length; i++) {					all.add(cells[i]);					// Add ports					all.addAll(getPorts(cells[i]));					// Add source vertex and ports					all.addAll(getParentPorts(graphModel.getSource(cells[i])));					// Add target vertex and ports					all.addAll(getParentPorts(graphModel.getTarget(cells[i])));				}				// Show attached edge				if (showEdgesOnShow) {					Set tmp = DefaultGraphModel.getEdges(getModel(), cells);					Iterator it = tmp.iterator();					while (it.hasNext()) {						Object obj = it.next();						Object source = graphModel.getSource(obj);						Object target = graphModel.getTarget(obj);						if ((isVisible(source) || all.contains(source))							&& (isVisible(target) || all.contains(target)))							all.add(obj);					}				}				all.removeAll(visibleSet);				return all.toArray();			} else {				// Hide attached edges				if (hideEdgesOnHide) {					Set all = new HashSet();					for (int i = 0; i < cells.length; i++) {						all.addAll(getPorts(cells[i]));						all.add(cells[i]);					}					all.addAll(DefaultGraphModel.getEdges(graphModel, cells));					all.retainAll(visibleSet);					return all.toArray();				}			}		}		return null;	}	// You must call update ports if this method returns true.	public boolean setVisibleImpl(Object[] cells, boolean visible) {		if (cells != null && isPartial()) {			boolean updatePorts = false;			// Update Visible Set			for (int i = 0; i < cells.length; i++) {				if (cells[i] != null) {					if (visible)						visibleSet.add(cells[i]);					else						visibleSet.remove(cells[i]);				}			}			// Insert Root Views (if not already in place)			for (int i = 0; i < cells.length; i++) {				if (cells[i] != null) {					if (!visible) {						CellView view = getMapping(cells[i], false);						if (view != null) {							view.removeFromParent();							view.refresh(false);							removeMapping(cells[i]);							roots.remove(view);							if (graphModel.contains(cells[i])								&& rememberCellViews)								hiddenSet.put(view.getCell(), view);							updatePorts = true;						}					} else { // don't check if in model, see DefaultGraphModel.insert						CellView view = getMapping(cells[i], true);						// Remove all children from roots						CellView[] children =							AbstractCellView.getDescendantViews(								new CellView[] { view });						for (int j = 0; j < children.length; j++)							roots.remove(children[j]);						// Refresh View						view.refresh(false);						// Link cellView into graphLayoutCache						factory.updateAutoSize(view);						CellView parentView = view.getParentView();						if (parentView != null)							parentView.refresh(true);						else							insertRoots(new CellView[] { view });						updatePorts = true;					}				}			}			return updatePorts;		}		return false;	}	protected Collection getParentPorts(Object cell) {		Object parent = graphModel.getParent(cell);		Collection collection = getPorts(parent);		collection.add(parent);		return collection;	}	protected Collection getPorts(Object cell) {		LinkedList list = new LinkedList();		for (int i = 0; i < graphModel.getChildCount(cell); i++) {			Object child = graphModel.getChild(cell, i);			if (graphModel.isPort(child))				list.add(child);		}		return list;	}	//	// Change Support	//	public boolean isOrdered() {		return ordered;	}	public boolean isPartial() {		return partial;	}	/**	 * Inserts the <code>cells</code> and connections into the model,	 * and absorbs the local attributes. This implementation sets the	 * inserted cells visible. (Note: No undo required for this visibility	 * change.)	 */	public void insert(		Object[] roots,		Map attributes,		ConnectionSet cs,		ParentMap pm,		UndoableEdit[] e) {		// setVisibleImpl(all, true);		Object[] visible = null;		if (isPartial()) {			Set tmp =				new HashSet(					DefaultGraphModel.getDescendants(graphModel, roots));			tmp.removeAll(visibleSet);			if (!tmp.isEmpty())				visible = tmp.toArray();		}		GraphViewEdit edit = createLocalEdit(attributes, visible, null);		// enable select on insert		// see DefaultGraphModel.insert		setVisibleImpl(visible, true);		if (edit != null)			e = augment(e, edit);		// Absorb local attributes		graphModel.insert(roots, attributes, cs, pm, e);	}	/**	 * Removes <code>cells</code> from the model. If <code>removeChildren</code>	 * is <code>true</code>, the children are also removed.	 * Notifies the model- and undo listeners of the change.	 */	public void remove(Object[] roots) {		graphModel.remove(roots);	}	/**	 * Applies the <code>propertyMap</code> and the connection changes to	 * the model. The initial <code>edits</code> that triggered the call	 * are considered to be part of this transaction.	 * Notifies the model- and undo listeners of the change.	 * Note: The passed in attributes may contain PortViews.	 */	public void edit(		Map attributes,		ConnectionSet cs,		ParentMap pm,		UndoableEdit[] e) {		//System.out.println("GraphLayoutCache_edit_attributes="+attributes);		Object[] visible = null;		if (isPartial()) {			Set tmp = new HashSet(attributes.keySet());			tmp.removeAll(visibleSet);			if (!tmp.isEmpty())				visible = tmp.toArray();		}		GraphViewEdit edit = createLocalEdit(attributes, visible, null);		if (edit != null)			e = augment(e, edit);		// Pass to model		graphModel.edit(attributes, cs, pm, e);	}	protected UndoableEdit[] augment(UndoableEdit[] e, UndoableEdit edit) {		if (edit != null) {			int size = (e != null) ? e.length + 1 : 1;			UndoableEdit[] result = new UndoableEdit[size];			if (e != null)				System.arraycopy(e, 0, result, 0, size - 2);			result[size - 1] = edit;			return result;		}		return e;	}	/**	 * Sends <code>cells</code> to back. Note: This expects an array of cells!	 */	public void toBack(Object[] cells) {		if (cells != null && cells.length > 0) {			if (isOrdered()) {				CellView[] views = getMapping(cells, false);				GraphViewLayerEdit edit =					new GraphViewLayerEdit(						this,						views,						GraphViewLayerEdit.BACK);				graphModel.edit(null, null, null, new UndoableEdit[] { edit });			} else				graphModel.toBack(cells);		}	}	/**	 * Brings <code>cells</code> to front. Note: This expects an array of cells!	 */	public void toFront(Object[] cells) {		if (cells != null && cells.length > 0) {			if (isOrdered()) {				CellView[] views = getMapping(cells, false);				GraphViewLayerEdit edit =					new GraphViewLayerEdit(						this,						views,						GraphViewLayerEdit.FRONT);				graphModel.edit(null, null, null, new UndoableEdit[] { edit });			} else				graphModel.toFront(cells);		}	}	protected GraphViewEdit createLocalEdit(		Map nested,		Object[] visible,		Object[] invisible) {		if (visible != null || invisible != null) {			GraphViewEdit edit = new GraphViewEdit(null, visible, invisible);			edit.end();			return edit;		}		return null;	}	//	// Cell Order	//	/**	 * Returns the specified cells in view-order if the model is not	 * ordered.	 */	public Object[] order(Object[] cells) {		if (cells != null) {			if (graphModel != null && isOrdered()) {				// Create Set for Lookup				Set cellSet = new HashSet();				for (int i = 0; i < cells.length; i++)					cellSet.add(cells[i]);				// Get Roots in View Order				CellView[] views = getRoots();				// Add Roots to Stack				Stack s = new Stack();				for (int i = 0; i < views.length; i++)					s.add(views[i]);				List result = new ArrayList();				// Traverse All Children In View Order				while (!s.isEmpty()) {					CellView cell = (CellView) s.pop();					// Add To List if Selected or Port					if (cellSet.contains(cell.getCell()))						result.add(cell.getCell());					// Add Children to Stack					CellView[] children = cell.getChildViews();					for (int i = 0; i < children.length; i++)						s.add(children[i]);					// Add PortViews to Stack					for (int i = graphModel.getChildCount(cell.getCell())-1; i >=0 ; i--) {						Object port = graphModel.getChild(cell.getCell(), i);						if (graphModel.isPort(port)) {							CellView portView = getMapping(port, false);							if (portView != null)								s.add(portView);						}					}				}				// Invert Result				int i = result.size();				Object[] tmp = new Object[i];				Iterator it = result.iterator();				while (it.hasNext())					tmp[--i] = it.next();

⌨️ 快捷键说明

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