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

📄 xmformlayout.java

📁 Libgist is an implementation of the Generalized Search Tree, a template index structure that makes i
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *  XmFormLayout.java $Revision: 1.1 $ (a Java layout manager  *  for AWT 1.0 which was inspired by the Motif XmForm widget) *  Copyright (C) 1997 Softbear Inc.  (info@softbear.com) *  Latest version at http://www.softbear.com/java/xmformlm * *  This program is free software; you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *  the Free Software Foundation; either version 2 of the License, or *  (at your option) any later version. * *  This program is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU General Public License for more details. * *  You should have received a copy of the GNU General Public License *  along with this program; if not, write to the Free Software *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. **//* *  This layout manager WILL work with AWT 1.1 because AWT 1.1 is backward *  compatible with AWT 1.0.  However, if you compile with the -deprecated *  switch (or its equivalent) you will see that this layout manager does *  use some deprecated AWT 1.0 APIs. * *  Ben Hill (ben@opnsys.com) notes that you can port to pure AWT 1.1 *  (which is incompatible with AWT 1.0) by performing the following *  substitutions (see lines marked with "AWT 1.1" in the code below): * *    preferredSize changed to getPreferredSize *    insets changed to getInsets *    size changed to getSize *    reshape changed to setBounds *    *//* *	The XmFormLayout lays out components according to *	a list of constraints. * *	Example 1: Fixed coordinates (form-relative).  *	============================================= *	The following constraints will cause the component named "foo" *	to be positioned at the point (x=10, y=10). * *		String constraints[] = { *			"foo.top=form.top+10", *			"foo.left=form.left+10" *		}; * *	Example 2: Relative coordinates *	=============================== *	The following constraints will cause the component named *	"bar" to be positioned to the right of "foo" and made wide *	enough to reach the right side of the form. * *		String constraints[] = { *			"foo.top=form.top+10", *			"foo.left=form.left+10", *			"bar.top=foo.top", *			"bar.left=foo.right+10", *			"bar.right=form.right-10" *		}; * *	Note: in order to understand the runtime operation (or errors) *	in the code below, you may need to uncomment-out debug statements.*/public class XmFormLayout implements java.awt.LayoutManager {	/*	 *	Component tuple constants, these must range from 0...9.	 *	Attribute numbers must range from BOTTOM...TOP.	*/	private final static int	COMPONENT = 0;	private final static int	BOTTOM = 1;	private final static int	LEFT = 2;	private final static int	HCENTER = 3;	private final static int	VCENTER = 4;	private final static int	RIGHT = 5;	private final static int	TOP = 6;	private final static int	BITSET = 7;	private final static int	DIMENSION = 8;	private final static int	NAME = 9;	/*	 *	Other constants.	*/	private final static int	FORM = -32765;	private final static int	INVALID = -32766;	private final static int	UNBOUND = -32767;	private final static boolean	m_debug = false;	private final static boolean	m_bigDebug = false;	/*	 *	The constraints and the results of the	 *	last reshape() computations.	*/	private String			m_myName;	private java.awt.Dimension	m_formDimensions; // (as of last reconstrain)	private java.awt.Point		m_formLocation;	// (as of last reconstrain)	private java.util.Vector	m_tuples;	// (4 attrs, component, bitset)	private java.util.Hashtable	m_nameToIndex;	private String			m_constraints[];	private boolean			m_changedFlag;	/*	 *	Add a component.	*/	public void addLayoutComponent(String name, java.awt.Component component) {		if (m_debug) {			System.out.println(m_myName + "'addLayoutComponent('" + name + "')...\n");		}		Object tuple[] = new Object[10];		tuple[XmFormLayout.COMPONENT] = component;		tuple[XmFormLayout.BITSET] = new java.util.BitSet();		tuple[XmFormLayout.NAME] = name;		int componentNr = m_tuples.size();		m_tuples.addElement(tuple);		m_nameToIndex.put(name, new Integer(componentNr));		m_changedFlag = true;	}	/*	 *	Parses an expression 'a.b+c' (or 'a.b-c' or 'a.b' or	 *	'+c' or '-c') into an rvalue 'a.b' and a signed	 *	number 'c'.  If the rvalue is unbound then return	 *	XmFormLayout.UNBOUND, otherwise return the value of	 *	the expression.	*/	private final int evaluate(String expression) throws Exception {		int i, s = 1, v = 0;		if (m_debug) {			System.out.println(m_myName + "'evaluate() expr: " +				expression + "\n");		}		i = expression.indexOf('+');		if (i == -1) {			i = expression.indexOf('-');			s = -1;		}		if (i == -1) {			v = this.getAttr(expression);		} else if (i > 1) {			String rvalue = expression.substring(0, i);			v = this.getAttr(rvalue);		}		if (i != -1 && v != XmFormLayout.UNBOUND) {			String unicodeNumber = expression.substring(i+1);			int number = Integer.parseInt(unicodeNumber, 10);			v += (s*number);		}		return v;	}	/*	 *	Enforce explicit constraints by binding lvalues and	 *	also marking those lvalues which can't be bound.	 *	Parses an explicit constraint 'a.b=c.d+e' into	 *	an lvalue 'a.b' and an expression 'c.d+e'.	 *	Returns true if all possible explicit lvalues are bound.	*/	private final boolean explicit(java.util.BitSet constraintBitset) throws Exception {		if (m_debug) {			System.out.println(m_myName + "'explicit()...\n");		}		boolean deferFlag = false;		if (m_constraints == null || m_constraints.length == 0) {			throw new Exception("no constraints (explicit)");		}		for (int i = 0; i < m_constraints.length; ++i) {			if (constraintBitset.get(i)) {				continue;			}			String constraint = m_constraints[i];			if (m_debug) {				System.out.println(m_myName + "'explicit() constraint: " + constraint + "\n"); 			}			int j = constraint.indexOf('=');			if (j < 3) {				throw new Exception("invalid constraint, expected '=': " + constraint + "(explicit)");			}			String lvalue =  constraint.substring(0, j);			String expression =  constraint.substring(j+1);			if (this.setAttr(lvalue, this.evaluate(expression))) {				constraintBitset.set(i);			} else {				deferFlag = true;			}		}		return !deferFlag;	}	/*	 *	If the attribute is bound then return its'	 *	value, otherwise return XmFormLayout.UNBOUND.	*/	private final int getAttr(int componentNr, int attrNr) {/*		if (m_bigDebug) {			System.out.println(m_myName + "'getAttr(): c=" +				componentNr + ", a=" + attrNr + "\n");		}*/		int v = XmFormLayout.UNBOUND;		if (componentNr == XmFormLayout.FORM) {			if (attrNr == XmFormLayout.BOTTOM) {				v = m_formLocation.y +					m_formDimensions.height;			} else if (attrNr == XmFormLayout.LEFT) {				v = m_formLocation.x;			} else if (attrNr == XmFormLayout.HCENTER) {				v = m_formLocation.x + m_formDimensions.width/2;			} else if (attrNr == XmFormLayout.VCENTER) {				v = m_formLocation.y + m_formDimensions.height/2;			} else if (attrNr == XmFormLayout.RIGHT) {				v = m_formLocation.x +					m_formDimensions.width;			} else if (attrNr == XmFormLayout.TOP) {				v = m_formLocation.y;			}		} else if (isBound(componentNr, attrNr)) {			Object tuple[] = (Object[]) m_tuples.elementAt(componentNr);			v = ((Integer) tuple[attrNr]).intValue();		}		return v;	}	/*	 *	If the attribute is bound then return its'	 *	value, otherwise return XmFormLayout.UNBOUND.	*/	private final int getAttr(String fqAttrName) throws Exception {		int tuple[] = this.parseAttr(fqAttrName);		int value = this.getAttr(tuple[0], tuple[1]);		if (m_debug) {			System.out.println(m_myName + "'getAttr(): fqAttr=" +				fqAttrName + ", value=" + value + "\n");		}		return value;	}	private final int getAttrNr(String attrName) { /*		if (m_bigDebug) {			System.out.println(m_myName + "'getAttrNr() attr: " +				attrName + "\n");		}*/		int attrNr = XmFormLayout.INVALID; // invalid attr # (never happens)		if (attrName.equals("bottom")) {			attrNr = XmFormLayout.BOTTOM;		} else if (attrName.equals("left") || attrName.equals("x")) {			attrNr = XmFormLayout.LEFT;		} else if (attrName.equals("hcenter") || attrName.equals("c")) {			attrNr = XmFormLayout.HCENTER;		} else if (attrName.equals("vcenter")) {			attrNr = XmFormLayout.VCENTER;		} else if (attrName.equals("right")) {			attrNr = XmFormLayout.RIGHT;		} else if (attrName.equals("top") || attrName.equals("y")) {			attrNr = XmFormLayout.TOP;		}		return attrNr;	}	private final java.util.BitSet getBitset(int componentNr) {		Object tuple[] = (Object[]) m_tuples.elementAt(componentNr);		return (java.util.BitSet) tuple[XmFormLayout.BITSET];	}	private final java.awt.Component getComponent(int componentNr) {		Object tuple[] = (Object[]) m_tuples.elementAt(componentNr);		return (java.awt.Component) tuple[XmFormLayout.COMPONENT];	}	private final String getComponentName(int componentNr) {		Object tuple[] = (Object[]) m_tuples.elementAt(componentNr);		return (String) tuple[XmFormLayout.NAME];	}	private final int getComponentNr(String componentName) throws Exception {/*		if (m_bigDebug) {			System.out.println(m_myName +				"'getComponentNr() attr: " +				componentName + "\n");		}*/		Object n = m_nameToIndex.get(componentName);		if (n == null) {			/*			 *	This exception occurs if there is a component			 *	mentioned in the constraints that isn't			 *	also in the form.  There are two common			 *	reasons for this:			 *			 *	 1. The "add(name, component)" method was			 *	 never called, or was called with the			 *	 wrong name; or			 *			 *	 2. The same component was added twice but			 *	 with a different name (the second name			 *	 will automatically remove the first name).			 *			*/			throw new Exception("component not found: '" +				componentName + "' (getComponentNr)");		}		return ((Integer)n).intValue();	}	/*	 *	Returns the preferred dimension of the specified	 *	component.  Results are cached for greater efficiency.	*/	private final java.awt.Dimension getDimension(int componentNr) {		Object tuple[] = (Object[]) m_tuples.elementAt(componentNr);		java.awt.Dimension dimension;		if (tuple[XmFormLayout.DIMENSION] == null) {			java.awt.Component component = (java.awt.Component) tuple[XmFormLayout.COMPONENT];			dimension = component.getPreferredSize(); // For AWT 1.1, use "component.getPreferredSize()"			tuple[XmFormLayout.DIMENSION] = (Object) dimension; 		} else {			dimension = (java.awt.Dimension) tuple[XmFormLayout.DIMENSION];		}		return dimension;	}	/*	 *	Returns the preferred offset (width or height)	 *	between the specified attribute and its' opposite.	*/	private final int getOppsOffset(int componentNr, int attrNr) {		int offset = XmFormLayout.INVALID; // invalid offset (never happens)		java.awt.Dimension componentDimensions = this.getDimension(componentNr);		if (attrNr == XmFormLayout.BOTTOM) {			offset = -componentDimensions.height;		} else if (attrNr == XmFormLayout.LEFT) {			offset = componentDimensions.width;		} else if (attrNr == XmFormLayout.RIGHT) {			offset = -componentDimensions.width;		} else if (attrNr == XmFormLayout.TOP) {			offset = componentDimensions.height;		}		return offset;	}	/*	 *	Returns the preferred offset (width or height)	 *	between the specified attribute and its' relative.	*/	private final int getRelsOffset(int componentNr, int attrNr) {		int offset = XmFormLayout.INVALID; // invalid offset (never happens)		java.awt.Dimension componentDimensions = this.getDimension(componentNr);		if (attrNr == XmFormLayout.HCENTER) {

⌨️ 快捷键说明

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