📄 jawegraphmodel.java
字号:
* @param connectionSet * @param parentMap * @param name */ public GraphModelEdit(Object[] inserted, Object[] removed, Map attributes, ConnectionSet connectionSet, ParentMap parentMap, String name) { super(); this.insert = inserted; this.remove = removed; this.connectionSet = connectionSet; this.attributes = attributes; this.parentMap = parentMap; this.name = name; previousAttributes = null; previousConnectionSet = connectionSet; previousParentMap = parentMap; // Remove Empty Parents if (parentMap != null) { // Compute Empty Group /*Map childCount = new Hashtable(); Iterator it = parentMap.entries(); while (it.hasNext()) { ParentMap.Entry entry = (ParentMap.Entry) it.next(); Object child = entry.getChild(); if (!isPort(child)) { Object oldParent = getParent(child); Object newParent = entry.getParent(); if (oldParent != newParent) { changeChildCount(childCount, oldParent, -1); changeChildCount(childCount, newParent, 1); } } } handleEmptyGroups(filterParents(childCount, 0));*/ } } public Object[] filterParents(Map childCount, int children) { ArrayList list = new ArrayList(); Iterator it = childCount.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); if (entry.getValue() instanceof Integer) { if (((Integer) entry.getValue()).intValue() == children) list.add(entry.getKey()); } } return list.toArray(); } protected void changeChildCount(Map childCount, Object parent, int change) { if (parent != null) { Integer count = (Integer) childCount.get(parent); if (count == null) { count = new Integer(getChildCount(parent)); } int newValue = count.intValue() + change; childCount.put(parent, new Integer(newValue)); } } /** * Adds the groups that become empty to the cells that * will be removed. (Auto remove empty cells.) Removed * cells will be re-inserted on undo, and the parent- * child relations will be restored. */ protected void handleEmptyGroups(Object[] groups) { if (groups != null && groups.length > 0) { if (remove == null) remove = new Object[] {}; Object[] tmp = new Object[remove.length + groups.length]; System.arraycopy(remove, 0, tmp, 0, remove.length); System.arraycopy(groups, 0, tmp, remove.length, groups.length); remove = tmp; } } public boolean isSignificant() { return true; } /** * Returns <code>getPresentationName</code> from the * last <code>UndoableEdit</code> added to * <code>edits</code>. If <code>edits</code> is empty, * calls super. */ public String getPresentationName() { return name; } /** * Returns the source of this change. This can either be a * view or a model, if this change is a GraphModelChange. */ public Object getSource() { return JaWEGraphModel.this; } /** * Returns the cells that have changed. This includes the cells * that have been changed through a call to getAttributes and the * edges that have been changed with the ConnectionSet. */ public Object[] getChanged() { return changed; } /** * Returns the objects that have not changed explicitly, but * implicitly because one of their dependent cells has changed. */ public Object[] getContext() { return context; } /** * Returns the cells that were inserted. */ public Object[] getInserted() { return inserted; } /** * Returns the cells that were inserted. */ public Object[] getRemoved() { return removed; } /** * Returns a map that contains (object, map) pairs * of the attributes that have been stored in the model. */ public Map getPreviousAttributes() { return previousAttributes; } /** * Returns a map of (object, view attributes). The objects * are model objects which need to be mapped to views. */ public Map getAttributes() { return attributes; } /** * Returns the connectionSet. * @return ConnectionSet */ public ConnectionSet getConnectionSet() { return connectionSet; } public ConnectionSet getPreviousConnectionSet() { return previousConnectionSet; } /** * Returns the parentMap. * @return ParentMap */ public ParentMap getParentMap() { return parentMap; } public ParentMap getPreviousParentMap() { return previousParentMap; } /** * Redoes a change. * * @exception CannotRedoException if the change cannot be redone */ public void redo() throws CannotRedoException { super.redo(); execute(); } /** * Undoes a change. * * @exception CannotUndoException if the change cannot be undone */ public void undo() throws CannotUndoException { super.undo(); execute(); } /** * Execute this edit such that the next invocation to this * method will invert the last execution. */ public void execute() { // Compute Changed Cells Set tmp = new HashSet(); if (attributes != null) tmp.addAll(attributes.keySet()); if (parentMap != null) tmp.addAll(parentMap.getChangedNodes()); // Note: One must also include the previous parents! if (connectionSet != null) tmp.addAll(connectionSet.getChangedEdges()); if (remove != null) { for (int i = 0; i < remove.length; i++) tmp.remove(remove[i]); } changed = tmp.toArray(); // Context cells List ctx = getEdges(JaWEGraphModel.this, changed); context = ctx.toArray(); // Do Execute inserted = insert; removed = remove; remove = handleInsert(inserted); previousParentMap = parentMap; parentMap = handleParentMap(parentMap); if (parentMap != null) tmp.addAll(parentMap.getChangedNodes()); previousConnectionSet = connectionSet; connectionSet = handleConnectionSet(connectionSet); insert = handleRemove(removed); previousAttributes = attributes; attributes = handleAttributes(attributes); changed = tmp.toArray(); // Fire Event fireGraphChanged(JaWEGraphModel.this, this); } public void putViews(GraphLayoutCache view, CellView[] views) { if (view != null && views != null) cellViews.put(view, views); } public CellView[] getViews(GraphLayoutCache view) { return (CellView[]) cellViews.get(view); } public String toString() { String s = new String(); if (inserted != null) { s += "Inserted:\n"; for (int i = 0; i < inserted.length; i++) s += " " + inserted[i] + "\n"; } else s += "None inserted\n"; if (removed != null) { s += "Removed:\n"; for (int i = 0; i < removed.length; i++) s += " " + removed[i] + "\n"; } else s += "None removed\n"; if (changed != null && changed.length > 0) { s += "Changed:\n"; for (int i = 0; i < changed.length; i++) s += " " + changed[i] + "\n"; } else s += "None changed\n"; if (parentMap != null) s += parentMap.toString(); else s += "No parent map\n"; return s; } } // ended class GraphModelEdit /** * An implementation of GraphViewChange. */ public class GraphModelLayerEdit extends AbstractUndoableEdit implements GraphModelEvent.GraphModelChange { public static final int FRONT = -1, BACK = -2; protected Object changeSource; protected transient Object[] cells; protected transient int[] next, prev; protected int layer; // The cell that change are the parents, because they need to // reload their childs for reordering! protected Object[] changed; /** * Constructs a GraphModelEdit. This modifies the order of the cells * in the model. */ public GraphModelLayerEdit(Object[] cells, int layer) { this.cells = cells; this.layer = layer; next = new int[cells.length]; prev = new int[cells.length]; updateNext(); // Compute array of changed cells (roots or parents of cells) Set par = new HashSet(); for (int i = 0; i < cells.length; i++) { Object cell = JaWEGraphModel.this.getParent(cells[i]); if (cell == null) cell = cells[i]; par.add(cell); } changed = par.toArray(); } protected void updateNext() { for (int i = 0; i < next.length; i++) next[i] = layer; } /** * Returns the source of this change. This can either be a * view or a model, if this change is a GraphModelChange. */ public Object getSource() { return JaWEGraphModel.this; } /** * Returns the cells that have changed. */ public Object[] getChanged() { return changed; } /** * Returns the cells that have changed. */ public Object[] getInserted() { return null; } /** * Returns the cells that have changed. */ public Object[] getRemoved() { return null; } /** * Returns null. */ public Object[] getContext() { return null; } /** * Returns null. */ public Map getAttributes() { return null; } /** * Returns null. */ public Map getPreviousAttributes() { return null; } public ConnectionSet getConnectionSet() { return null; } public ConnectionSet getPreviousConnectionSet() { return null; } /** * Returns null. */ public ParentMap getParentMap() { return null; } public ParentMap getPreviousParentMap() { return null; } /** * Allows a <code>GraphLayoutCache</code> to add and execute and * UndoableEdit in this change. This does also work if the * parent edit has already been executed, in which case the * to be added edit will be executed immediately, after * addition. * This is used to handle changes to the view that are * triggered by certain changes of the model. Such implicit * edits may be associated with the view so that they may be * undone and redone correctly, and are stored in the model's * global history together with the parent event as one unit. */ public void addImplicitEdit(UndoableEdit edit) { // ignore } /** * Returns the views that have not changed explicitly, but * implicitly because one of their dependent cells has changed. */ public CellView[] getViews(GraphLayoutCache view) { return null; } /** * Returns the views that have not changed explicitly, but * implicitly because one of their dependent cells has changed. */ public void putViews(GraphLayoutCache view, CellView[] cellViews) { // ignore } /** * Redoes a change. * * @exception CannotRedoException * if the change cannot be redone */ public void redo() throws CannotRedoException { super.redo(); updateNext(); execute(); } /** * Undoes a change. * * @exception CannotUndoException * if the change cannot be undone */ public void undo() throws CannotUndoException { super.undo(); execute(); } /** * Execute this edit such that the next invocation to this method will * invert the last execution. */ public void execute() { for (int i = 0; i < cells.length; i++) { List list = getParentList(cells[i]); if (list != null) { prev[i] = list.indexOf(cells[i]); if (prev[i] >= 0) { list.remove(prev[i]); int n = next[i]; if (n == FRONT) n = list.size(); else if (n == BACK) n = 0; list.add(n, cells[i]); next[i] = prev[i]; } } } updateListeners(); } protected void updateListeners() { fireGraphChanged(JaWEGraphModel.this, this); } /** * Returns the list that exclusively contains <code>view</code>. */ protected List getParentList(Object cell) { List list = null; if (cell instanceof DefaultMutableTreeNode) { Object parent = ((DefaultMutableTreeNode) cell).getParent(); if (parent instanceof DefaultGraphCell) list = ((DefaultGraphCell) parent).getChildren(); else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -