📄 graphconstants.java
字号:
Object[] keys,
Object[] values) {
if (keys != null && values != null && keys.length != values.length)
throw new IllegalArgumentException("Keys and values must have same length");
Map nested = new Hashtable();
for (int i = 0; i < cells.length; i++) {
if (cells[i] != null) {
Map attributes = createMap();
for (int j = 0; j < keys.length; j++)
if (keys[j] != null && values[j] != null)
attributes.put(keys[j], values[j]);
nested.put(cells[i], attributes);
}
}
return nested;
}
/**
* Returns a new map, from cells to property maps. The <code>elements</code>
* may be instances of <code>CellView</code>, in which case the cell view's
* corresponding cell is used as a key, and its attributes are used as
* a property map. In any other case, the <code>element</code> is considered
* as a cell and looked-up in the cell mapper to find the corresponding
* view. If a view is found, its attributes are cloned and used as a
* property map, along with the cell as a key.<p>
* <strong>Note:</strong> This method returns a map of maps! This is
* different from the createMap method, which creates a map, from keys
* to values. This method returns a map, from cells to maps, which in turn
* map from keys to values.
*/
public static Map createAttributes(Object[] elements, CellMapper cm) {
Map attributes = new Hashtable();
for (int i = 0; i < elements.length; i++) {
CellView view = null;
Object key = elements[i];
if (key instanceof CellView) {
view = (CellView) key;
key = view.getCell();
} else if (cm != null) // else is assumed by clients!
view = cm.getMapping(key, false);
if (view != null) {
attributes.put(
key,
GraphConstants.cloneMap(view.getAllAttributes()));
}
}
return attributes;
}
// Returns a nested map of cell, Map pairs where the map reflects
// the attributes returned by the model for this cell.
public static Map createAttributesFromModel(
Object[] elements,
GraphModel model) {
Map attributes = new Hashtable();
for (int i = 0; i < elements.length; i++) {
Map attr = model.getAttributes(elements[i]);
if (attr != null && attr.size() > 0)
attributes.put(elements[i], GraphConstants.cloneMap(attr));
}
return attributes;
}
//
// Common Methods
//
/**
* Creates an empty map. This method returns a new instance of
* <code>Hashtable</code>.
*/
public static Map createMap() {
return new java.util.Hashtable();
}
/**
* Replace the keys in <code>map</code> using <code>keyMap</code, which
* maps from old to new keys. The value in <code>map</code> must itself
* be a map, and is cloned using <code>cloneMap</code>.
*/
public static Map replaceKeys(Map keyMap, Map map) {
Map newMap = new Hashtable();
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
if (entry.getValue() instanceof Map) {
Object newKey = keyMap.get(entry.getKey());
if (newKey != null) {
Map val = GraphConstants.cloneMap((Map) entry.getValue());
newMap.put(newKey, val);
}
}
}
return newMap;
}
/**
* Returns a clone of <code>map</code>, from keys to values. If the map
* contains bounds or points, these are cloned as well. References to
* <code>PortViews</code> are replaces by points.
*/
public static Map cloneMap(Map map) {
Map newMap = new Hashtable(map);
// Clone Bounds
Rectangle bounds = getBounds(newMap);
if (bounds != null)
setBounds(newMap, new Rectangle(bounds));
// Clone List Of Points
java.util.List points = getPoints(newMap);
if (points != null)
setPoints(newMap, clonePoints(points));
// Clone Edge Label
Point label = GraphConstants.getLabelPosition(newMap);
if (label != null)
setLabelPosition(newMap, new Point(label));
return newMap;
}
/**
* Returns a list where all instances of PortView are replaced
* by their correspnding Point instance.
*/
public static java.util.List clonePoints(java.util.List points) {
ArrayList newList = new ArrayList();
Iterator it = points.iterator();
while (it.hasNext()) {
// Clone Point
Object point = it.next();
if (point instanceof PortView)
point = ((PortView) point).getLocation(null);
else if (point instanceof Point)
point = new Point((Point) point);
newList.add(point);
}
return newList;
}
/**
* Translates the maps in <code>c</code> using
* <code>translate(Map, int, int)</code>.
*/
public static void translate(Collection c, int dx, int dy) {
Iterator it = c.iterator();
while (it.hasNext()) {
Object map = it.next();
if (map instanceof Map)
GraphConstants.translate((Map) map, dx, dy);
}
}
/**
* Translates <code>map</code> by the given amount.
*/
public static void translate(Map map, int dx, int dy) {
// Translate Bounds
if (GraphConstants.isMoveable(map)) {
Rectangle bounds = getBounds(map);
if (bounds != null) {
bounds.translate(dx, dy);
// Locations must be > 0
if (!NEGATIVE_ALLOWED) {
bounds.x = Math.max(0, bounds.x);
bounds.y = Math.max(0, bounds.y);
}
setBounds(map, bounds);
}
// Translate Points
java.util.List points = getPoints(map);
if (points != null) {
for (int i = 0; i < points.size(); i++) {
Object obj = points.get(i);
if (obj instanceof Point) {
Point pt = (Point) obj;
pt.translate(dx, dy);
// Locations must be > 0
if (!NEGATIVE_ALLOWED) {
pt.x = Math.max(0, pt.x);
pt.y = Math.max(0, pt.y);
}
}
}
}
}
}
/**
* Scales <code>map</code> by the given amount.
*/
public static void scale(Map map, double sx, double sy, Point origin) {
// Scale Bounds
Rectangle bounds = getBounds(map);
if (bounds != null) {
Point p = new Point(bounds.getLocation());
p.x = origin.x + (int) Math.round((double) (p.x - origin.x) * sx);
p.y = origin.y + (int) Math.round((double) (p.y - origin.y) * sy);
Point loc = bounds.getLocation();
if (!p.equals(loc)) // Scale Location
translate(map, p.x - loc.x, p.y - loc.y);
Dimension d = bounds.getSize();
Dimension n =
new Dimension(
Math.max(1, (int) Math.round(d.width * sx)),
Math.max(1, (int) Math.round(d.height * sy)));
if (!d.equals(n)) // Scale Size
bounds.setSize(n);
}
// Scale Points
java.util.List points = getPoints(map);
if (points != null) {
Iterator it = points.iterator();
while (it.hasNext()) {
Object obj = it.next();
if (obj instanceof Point) {
// Scale Point
Point loc = (Point) obj;
Point p = new Point(loc);
p.x =
origin.x
+ (int) Math.round((double) (p.x - origin.x) * sx);
p.y =
origin.y
+ (int) Math.round((double) (p.y - origin.y) * sy);
// Move Point
if (!p.equals(loc))
loc.translate(p.x - loc.x, p.y - loc.y);
}
}
}
}
/**
* Sets the value attribute in the specified map to the
specified
* font value.
*
* @param map The map to store the font attribute in.
* @param font The value to set the font attribute to.
*/
public static void setFont(Map map, Font font) {
map.put(FONT, font);
}
/**
* Returns the font for the specified attribute map.
Uses default
* font if no font is specified in the attribute map.
*/
public static Font getFont(Map map) {
Font font = (Font) map.get(FONT);
if (font == null)
font = defaultFont;
return font;
}
/**
* Sets the value attribute in the specified map to the specified value.
*/
public static final void setRemoveAttributes(Map map, Object[] value) {
map.put(REMOVEATTRIBUTES, value);
}
/**
* Returns the value attribute from the specified map.
*/
public static final Object[] getRemoveAttributes(Map map) {
return (Object[]) map.get(REMOVEATTRIBUTES);
}
/**
* Apply the <code>change</code> to the <code>target</code>.
* <code>change</code> must be a <code>Map</code> previously
* obtained from this object.
* Returns a map that may be used to undo the change to target.
*/
public static Map applyMap(Map change, Map target) {
Map undo = new Hashtable();
if (change != null) {
// Handle Remove All
if (isRemoveAll(change)) {
undo.putAll(target);
target.clear();
}
// Handle Remove Individual
Object[] remove = GraphConstants.getRemoveAttributes(change);
if (remove != null) {
// don't store command
for (int i = 0; i < remove.length; i++) {
Object oldValue = target.remove(remove[i]);
if (oldValue != null)
undo.put(remove[i], oldValue);
}
}
// Attributes that were empty are added
// to removeattibutes
Set removeAttributes = new HashSet();
Iterator it = change.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
if (!key.equals(REMOVEALL) && !key.equals(REMOVEATTRIBUTES)) {
Object oldValue = target.put(key, entry.getValue());
if (oldValue == null)
removeAttributes.add(key);
else
undo.put(key, oldValue);
}
}
if (!removeAttributes.isEmpty())
setRemoveAttributes(undo, removeAttributes.toArray());
}
return undo;
}
/**
* Sets the value attribute in the specified map to the specified value.
*/
public static final void setIcon(Map map, Icon value) {
map.put(ICON, value);
}
/**
* Returns the value attribute from the specified map.
*/
public static final Icon getIcon(Map map) {
return (Icon) map.get(ICON);
}
/**
* Sets the opaque attribute in the specified map to the specified value.
*/
public static final void setOpaque(Map map, boolean flag) {
map.put(OPAQUE, new Boolean(flag));
}
/**
* Returns the opaque attribute from the specified map.
*/
public static final boolean isOpaque(Map map) {
Boolean bool = (Boolean) map.get(OPAQUE);
if (bool != null)
return bool.booleanValue();
return false;
}
/**
* Sets the value attribute in the specified map to the specified value.
*/
public static final void setBorder(Map map, Border value) {
map.put(BORDER, value);
}
/**
* Returns the value attribute from the specified map.
*/
public static final Border getBorder(Map map) {
return (Border) map.get(BORDER);
}
/**
* Sets the value attribute in the specified map to the specified value.
*/
public static final void setLineColor(Map map, Color value) {
map.put(LINECOLOR, value);
}
/**
* Returns the value attribute from the specified map.
*/
public static final Color getLineColor(Map map) {
return (Color) map.get(LINECOLOR);
}
/**
* Sets the value attribute in the specified map to the specified value.
*/
public static final void setBorderColor(Map map, Color value) {
map.put(BORDERCOLOR, value);
}
/**
* Returns the value attribute from the specified map.
*/
public static final Color getBorderColor(Map map) {
return (Color) map.get(BORDERCOLOR);
}
/**
* Sets the opaque attribute in the specified map to the specified value.
*/
public static final void setLineWidth(Map map, float width) {
map.put(LINEWIDTH, new Float(width));
}
/**
* Returns the opaque attribute from the specified map.
*/
public static final float getLineWidth(Map map) {
Float floatObj = (Float) map.get(LINEWIDTH);
if (floatObj != null)
return floatObj.floatValue();
return 1;
}
/**
* Sets the value attribute in the specified map to the specified value.
*/
public static final void setForeground(Map map, Color value) {
map.put(FOREGROUND, value);
}
/**
* Returns the value attribute from the specified map.
*/
public static final Color getForeground(Map map) {
return (Color) map.get(FOREGROUND);
}
/**
* Sets the value attribute in the specified map to the specified value.
*/
public static final void setBackground(Map map, Color value) {
map.put(BACKGROUND, value);
}
/**
* Returns the value attribute from the specified map.
*/
public static final Color getBackground(Map map) {
return (Color) map.get(BACKGROUND);
}
/**
* Sets the opaque attribute in the specified map to the specified value.
*/
public static final void setVerticalAlignment(Map map, int width) {
map.put(VERTICAL_ALIGNMENT, new Integer(width));
}
/**
* Returns the opaque attribute from the specified map.
*/
public static final int getVerticalAlignment(Map map) {
Integer intObj = (Integer) map.get(VERTICAL_ALIGNMENT);
if (intObj != null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -