📄 xmformlayout.java
字号:
offset = -componentDimensions.width/2; } else if (attrNr == XmFormLayout.LEFT) { offset = componentDimensions.width/2; } else if (attrNr == XmFormLayout.TOP) { offset = componentDimensions.height/2; } else if (attrNr == XmFormLayout.VCENTER) { offset = -componentDimensions.height/2; } return offset; } /* * Enforces implicit constraints by binding component * attributes which haven't an lvalue in ANY explicit * constraint and whose opposite attribute is already * bound, e.g. if foo.top isn't an lvalue in any explicit * constraint, and foo.bottom is bound, and foo has a * preferred height of 123, then enforce this implicit * constraint: * "foo.top=foo.bottom-123" * Returns true if all possible implicit constraints are bound. */ private final boolean implicit() throws Exception { if (m_debug) { System.out.println(m_myName + "'implicit()...\n"); } boolean deferFlag = false; for (int c = 0; c < m_tuples.size(); ++c) { for (int a = XmFormLayout.BOTTOM; a <= XmFormLayout.TOP; ++a) { if (this.isMarked(c, a) == false) { if (m_debug) { System.out.println(m_myName + "'implicit(): c=" + c + ", a=" + a + "\n"); } int r = this.relative(a); if (r != XmFormLayout.INVALID && this.isBound(c, r) == true) { // this also marks the attr this.setAttr(c, a, this.getAttr(c, r) + this.getRelsOffset(c, r)); } else { int o = this.opposite(a); if (o != XmFormLayout.INVALID && this.isBound(c, o) == true) { // this also marks the attr this.setAttr(c, a, this.getAttr(c, o) + this.getOppsOffset(c, o)); } else { deferFlag = true; } } } } } return !deferFlag; } /* * Returns true if this component's attribute was * bound; that is, if this.setAttr() was called. */ private final boolean isBound(int componentNr, int attributeNr) { java.util.BitSet bitset = this.getBitset(componentNr); return bitset.get(attributeNr); } /* * Returns true if this component's attribute is * an lvalue in any explicit constraint; that is, * if this.setMarked() was called. */ private final boolean isMarked(int componentNr, int attributeNr) { java.util.BitSet bitset = this.getBitset(componentNr); return bitset.get(attributeNr + (XmFormLayout.TOP+1)); } /* * Reshape all components in the container. Typically, * this causes a call to this.reconstrain() only the first time. */ public void layoutContainer(java.awt.Container parent) { java.awt.Dimension dummy = this.preferredLayoutSize(parent); // reconstrain if necessary if (m_debug) { System.out.println(m_myName + "'layoutContainer(): w=" + dummy.width + ", h=" + dummy.height + ", changedFlag=" + m_changedFlag + " (should be false)\n"); } if (!m_changedFlag) { for (int c = 0; c < m_tuples.size(); ++c) { Object tuple[] = (Object[]) m_tuples.elementAt(c); int x = ((Integer)tuple[XmFormLayout.LEFT]).intValue(); int y = ((Integer)tuple[XmFormLayout.TOP]).intValue(); int w = ((Integer)tuple[XmFormLayout.RIGHT]).intValue() - x; int h = ((Integer)tuple[XmFormLayout.BOTTOM]).intValue() - y; if (m_debug) { System.out.println(m_myName + "'layoutContainer(): c=" + (String)tuple[XmFormLayout.NAME] + ", x=" + x + ", y=" + y + ", w=" + w + ", h=" + h + "\n"); } java.awt.Component component = (java.awt.Component) tuple[XmFormLayout.COMPONENT]; component.setBounds(x, y, w, h); // For AWT 1.1, use "component.setBounds(...)" } } } /* * XmFormLayout knowns only one size (the preferred size). */ public java.awt.Dimension minimumLayoutSize(java.awt.Container parent) { return preferredLayoutSize(parent); } private final int opposite(int attrNr) { int oppositeAttrNr = XmFormLayout.INVALID; // attr doesn't have an opposite if (attrNr == XmFormLayout.BOTTOM) { oppositeAttrNr = XmFormLayout.TOP; } else if (attrNr == XmFormLayout.LEFT) { oppositeAttrNr = XmFormLayout.RIGHT; } else if (attrNr == XmFormLayout.RIGHT) { oppositeAttrNr = XmFormLayout.LEFT; } else if (attrNr == XmFormLayout.TOP) { oppositeAttrNr = XmFormLayout.BOTTOM; } return oppositeAttrNr; } /* * Parse a fully-qualified attribute name 'a.b' into * a component 'a' and an attribute 'b', and return * the component number and attribute number. */ private final int[] parseAttr(String fqAttrName) throws Exception { int i = fqAttrName.indexOf('.'); if (i < 1) { throw new Exception(m_myName + "invalid attribute, expected '.': '" + fqAttrName + "' (parseAttr)"); } String componentName = fqAttrName.substring(0, i); String attrName = fqAttrName.substring(i+1); int tuple[] = new int[2]; if (componentName.equals("form")) { tuple[0] = XmFormLayout.FORM; } else { tuple[0] = this.getComponentNr(componentName); } tuple[1] = this.getAttrNr(attrName); return tuple; } public java.awt.Dimension preferredLayoutSize(java.awt.Container parent) { java.awt.Insets insets = parent.getInsets(); // For AWT 1.1, use "parent.getInsets()" int dx = insets.left + insets.right; int dy = insets.top + insets.bottom; if (m_debug) { System.out.println(m_myName + "'preferredLayoutSize(): dx=" + dx + ", dy=" + dy + "\n"); } int x = insets.left; int y = insets.top; int w = parent.getSize().width - dx; // For AWT 1.1, use "parent.getSize()" int h = parent.getSize().height - dy; // For AWT 1.1, use "parent.getSize()" this.setBounds(x, y, w, h); return new java.awt.Dimension(m_formDimensions.width + dx, m_formDimensions.height + dy); } private final int relative(int attrNr) { int relativeAttrNr = XmFormLayout.INVALID; // attr doesn't have a relative if (attrNr == XmFormLayout.HCENTER) { relativeAttrNr = XmFormLayout.LEFT; } else if (attrNr == XmFormLayout.LEFT) { relativeAttrNr = XmFormLayout.HCENTER; } else if (attrNr == XmFormLayout.TOP) { relativeAttrNr = XmFormLayout.VCENTER; } else if (attrNr == XmFormLayout.VCENTER) { relativeAttrNr = XmFormLayout.TOP; } return relativeAttrNr; } /* * Removes a component. This isn't particularly fast, * but it is rarely called, if ever. */ public void removeLayoutComponent(java.awt.Component oldComponent) { int nrComponents = m_tuples.size(); for (int c = 0; c < nrComponents; ++c) { if (this.getComponent(c) == oldComponent) { String componentName = this.getComponentName(c); if (m_debug) { System.out.println(m_myName + "'removeLayoutComponent('" + componentName + "')...\n"); } m_nameToIndex.remove(componentName); m_changedFlag = true; } } } /* * Layout the container according to constraints. * This may be time-consuming, so it should only be * done if changedFlag is true. */ private final void reconstrain() throws Exception { java.util.BitSet constraintBitset = new java.util.BitSet(); for (int c = 0; c < m_tuples.size(); ++c) { Object tuple[] = (Object[]) m_tuples.elementAt(c); tuple[XmFormLayout.DIMENSION] = null; // component's preferred size may have changed java.util.BitSet componentBitset = (java.util.BitSet) tuple[XmFormLayout.BITSET]; /* * The componentBitset has no clearAllBits() * method so AND with any blank bitset to clear it. */ componentBitset.and(constraintBitset); } boolean deferFlag; int nrTries = 0; do { if (m_debug) { System.out.println(m_myName + "'reconstrain(): iteration=" + (nrTries+1) + "*****\n"); } deferFlag = false; if (! this.explicit(constraintBitset)) { deferFlag = true; } if (! this.implicit()) { deferFlag = true; } } while (deferFlag && ++nrTries < 30); if (deferFlag) { throw new Exception("possible cycle in constraints, failed after " + nrTries + " iterations (reconstrain)"); } m_changedFlag = false; } /* * If the container's location or shape has * changed, or if any of the component's have * changed, then re-compute the constraints; * otherwise use what was already computed. */ private final void setBounds(int x, int y, int w, int h) { if (m_formDimensions.width != w || m_formDimensions.height != h) { if (m_debug) { System.out.println(m_myName + "'setBounds(): ***SIZE CHANGED*** w=" + w + ", h=" + h + " *******\n"); } m_formDimensions.width = w; m_formDimensions.height = h; m_changedFlag = true; } if (m_formLocation.x != x || m_formLocation.y != y) { m_formLocation.x = x; m_formLocation.y = y; m_changedFlag = true; } if (m_changedFlag) { try { this.reconstrain(); } catch (Exception exception) { System.out.println(m_myName + " error: " + exception.getMessage() + "\n"); } } } /* * Try to set a attribute; returns true if the was set * or false if it was deferred (the latter will happen * if the value is XmFormLayout.UNBOUND). */ private final boolean setAttr(int componentNr, int attrNr, int value) throws Exception { if (m_debug) { System.out.println(m_myName + "'setAttr(): c=" + componentNr + ", a=" + attrNr + ", v=" + value + "\n"); } if (componentNr == XmFormLayout.FORM) { throw new Exception("invalid constraint, can't set attributes of form (setAtttr)"); } this.setMarked(componentNr, attrNr); boolean boundFlag = false; if (value != XmFormLayout.UNBOUND) { Object tuple[] = (Object[]) m_tuples.elementAt(componentNr); tuple[attrNr] = new Integer(value); this.setBound(componentNr, attrNr); boundFlag = true; } return boundFlag; } /* * Try to set a attribute 'c.a'; returns true if the * was set or false if it was deferred (the latter * will happen if the value is XmFormLayout.UNBOUND). */ private final boolean setAttr(String fqAttrName, int value) throws Exception { if (m_debug) { System.out.println(m_myName + "'setAttr() fqAttr=" + fqAttrName + ", value=" + value + "\n"); } int tuple[] = this.parseAttr(fqAttrName); return this.setAttr(tuple[0], tuple[1], value); } private final void setBound(int componentNr, int attrNr) {/* if (m_bigDebug) { System.out.println(m_myName + "'setBound(): c=" + componentNr + ", a=" + attrNr + "\n"); }*/ java.util.BitSet bitset = this.getBitset(componentNr); bitset.set(attrNr); } private final void setMarked(int componentNr, int attrNr) { java.util.BitSet bitset = this.getBitset(componentNr); bitset.set(attrNr + (XmFormLayout.TOP+1)); } /* * The constructor. */ public XmFormLayout(String newConstraints[]) { m_myName = (((Object) this).getClass()).getName(); if (m_debug && newConstraints == null) { System.out.println(m_myName + "'XmFormLayout() newConstraints is null!\n"); } m_constraints = newConstraints; m_formDimensions = new java.awt.Dimension(0,0); m_formLocation = new java.awt.Point(0,0); m_nameToIndex = new java.util.Hashtable(); m_tuples = new java.util.Vector(); m_changedFlag = true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -