📄 mosaiclayout.java
字号:
m_comp2Node.remove( comp );
m_initialized = m_finalized = false;
}
}
//
// s e t C o n s t r a i n t s
//
// This method defines the default constraints. They
// are stored under the name "Default" in the constraints
// repository m_name2Constraints.
public void setConstraints( MosaicConstraints constraints )
{
setConstraints( "Default", constraints );
}
//
// s e t C o n s t r a i n t s
//
// Inserts constraints into the repository of constraints,
// under the given name. These constraints can then later
// be reused and assigned to components. Existing
// constraints with the same name are overwritten (only
// in the repository!).
public void setConstraints( String name,
MosaicConstraints constraints )
{
m_name2Constraints.put( name, constraints.clone() );
}
//
// s e t C o n s t r a i n t s
//
// Defines the constraints for component comp. If no
// leaf node for component comp exists yet, a new one
// is created, and its position in the tree (its coord
// stack) is guessed from the last position encountered.
public void setConstraints( Component comp,
MosaicConstraints constraints )
{
if( ! m_comp2Node.containsKey( comp ) ) {
m_comp2Node.put( comp, new MosaicLeafNode( this, comp,
(MosaicConstraints) constraints.clone(), newCoordStack() ) );
}
else {
MosaicLeafNode N = lookupNode( comp );
N.m_constraints = (MosaicConstraints) constraints.clone();
N.m_initialized = false;
}
m_initialized = m_finalized = false;
}
//
// g e t C o n s t r a i n t s
//
// Retrieves a copy of the default constraints.
public MosaicConstraints getConstraints()
{
return getConstraints( "Default" );
}
//
// g e t C o n s t r a i n t s
//
// Retrieves a copy of the constraints with the specified
// name. If no constraints with that name exist,
// null is returned.
public MosaicConstraints getConstraints( String name )
{
MosaicConstraints c = lookupConstraints( name );
if( c != null )
return (MosaicConstraints) c.clone();
return null;
}
//
// g e t C o n s t r a i n t s
//
// Retrieves a copy of the constraints of the component
// comp. The component comp has to be known to the
// layout manager.
public MosaicConstraints getConstraints( Component comp )
throws NoSuchElementException
{
MosaicLeafNode N = lookupNode( comp );
if( N == null )
throw new NoSuchElementException();
return (MosaicConstraints) N.m_constraints.clone();
}
//
// s e t C o o r d S t a c k
//
// Sets the position (coord stack) of the next component
// for which a leaf node is generated (that is the next
// component that is registered with addLayoutComponent(),
// or that is not known yet to the layout manager and for
// which a leaf node is generated when its constraints
// are submitted). One of the ways to build a hierarchy
// is to have pairs like
//
// m_layout.setCoordStack( ... );
// add( "", comp );
//
// in the code as shown in the example above. Note that
// the version of add() that takes a name as its first
// argument calls addLayoutComponent(), whereas the version
// that doesn't take a name does not.
public void setCoordStack( MosaicCoordStack coordStack )
{
m_nextCoordStack = (MosaicCoordStack) coordStack.clone();
}
//
// s e t C o o r d S t a c k
//
// An alternative way to specify the position of each component
// is to define it directly. This works even if the component
// has not been added to the parent container. The example
// in the introduction does not demonstrates this method, however,
// but it is straightforward to apply.
public void setCoordStack( Component comp, MosaicCoordStack coordStack )
{
m_lastCoordStack = (MosaicCoordStack) coordStack.clone();
m_nextCoordStack = null;
if( ! m_comp2Node.containsKey( comp ) ) {
m_comp2Node.put( comp, new MosaicLeafNode( this, comp,
newConstraints( "Default" ), m_lastCoordStack ) );
}
else {
lookupNode( comp ).m_coordStack = m_lastCoordStack;
}
m_initialized = m_finalized = false;
}
//
// g e t C o o r d S t a c k
//
// Retrieves a copy of the position or coord stack of the
// given component.
public MosaicCoordStack getCoordStack( Component comp )
throws NoSuchElementException
{
MosaicLeafNode N = lookupNode( comp );
if( N == null )
throw new NoSuchElementException();
return (MosaicCoordStack) N.m_coordStack.clone();
}
//
// s e t P o s
//
// The layout manager maintains a default coord stack that
// is used whenever a new leaf node is generated and no
// other coord stack is available. (The coord stack of
// components that have been positioned in this default manner
// can be overwritten at any time, of course.) This default
// coord stack can be manipulated with the following functions.
// The first function sets the position to the specified
// row and column at the top level of the hierarchy.
public void setPos( int col, int row )
{
m_nextCoordStack = new MosaicCoordStack();
m_nextCoordStack.setPos( col, row );
}
//
// s e t P o s
//
// This function creates a default position at the second
// level; col1/row1 and col2/row2 form the path that has
// to be traversed while visiting the top and second level
// interior grid nodes in order to arrive at the destination.
public void setPos( int col1, int row1, int col2, int row2 )
{
m_nextCoordStack = new MosaicCoordStack();
m_nextCoordStack.setPos( col1, row1, col2, row2 );
}
//
// s e t P o s
//
// This function creates a default position at the third
// level; col1/row1, col2/row2 and col3/row3 form the path
// that has to be traversed while visiting the top, second
// and third level interior grid nodes in order to arrive
// at the destination.
public void setPos( int col1, int row1, int col2, int row2,
int col3, int row3 )
{
m_nextCoordStack = new MosaicCoordStack();
m_nextCoordStack.setPos( col1, row1, col2, row2 );
m_nextCoordStack.pushPos( col3, row3 );
}
//
// s e t P o s
//
// This function creates a default position at the forth
// level, analogous to the preceding functions.
public void setPos( int col1, int row1, int col2, int row2,
int col3, int row3, int col4, int row4 )
{
m_nextCoordStack = new MosaicCoordStack();
m_nextCoordStack.setPos( col1, row1, col2, row2 );
m_nextCoordStack.pushPos( col3, row3 );
m_nextCoordStack.pushPos( col4, row4 );
}
//
// g e t S a s h S i z e
//
protected int getSashSize( boolean topLevel )
{
if( m_panel == null )
return 0;
return topLevel ? m_topLevelSashSize : m_medLevelSashSize;
}
//
// c h e c k P a n e l
//
protected void checkPanel( Container parent )
{
if( m_panel != null )
return;
if( ! ( parent instanceof MosaicPanel ) ) {
m_panel = null;
m_frame = null;
return;
}
m_panel = (MosaicPanel) parent;
while( parent != null && ! ( parent instanceof Frame ) )
parent = parent.getParent();
m_frame = (Frame) parent;
if( m_frame != null ) {
// !!!!!!
// These cursors seem to work fine everywhere I tried - AMB 6/5/98.
// Changed "Frame." to "Cursor.". -sigue Jul 13, 1999
colSashCursor = Cursor.E_RESIZE_CURSOR;
rowSashCursor = Cursor.N_RESIZE_CURSOR;
crossSashCursor = Cursor.MOVE_CURSOR;
/*
colSashCursor = Frame.MOVE_CURSOR;
rowSashCursor = Frame.MOVE_CURSOR;
crossSashCursor = Frame.MOVE_CURSOR;
*/
}
}
//
// h a n d l e E v e n t
//
// The events are received from the MosaicPanel parent of
// the layout manager.
protected boolean handleEvent( Event evt )
{
// !!!!!!
// Bug fix - cursor would get stuck when moving over sash. AMB 6/5/98.
if( ! m_finalized || m_root == null /*|| evt.target != m_panel*/ )
return false;
switch( evt.id ) {
case Event.MOUSE_EXIT:
if( m_activeNode == null )
defaultCursor();
break;
case Event.MOUSE_ENTER:
case Event.MOUSE_MOVE:
case Event.MOUSE_DOWN:
if( m_activeNode == null )
m_root.handleEvent( evt );
break;
case Event.MOUSE_UP:
if( m_activeNode != null ) {
m_activeNode.drag( evt.x, evt.y );
m_activeNode.releaseSash();
// take care of cursor
m_root.handleEvent( evt );
m_panel.validate();
}
break;
case Event.MOUSE_DRAG:
if( m_activeNode != null )
m_activeNode.drag( evt.x, evt.y );
break;
}
return true;
}
//
// p a i n t
//
// The paint request comes from the MosaicPanel parent of
// the layout manager.
protected void paint( Graphics g )
{
if( m_finalized && m_root != null )
m_root.paint( g );
}
//
// l o o k u p C o n s t r a i n t s
//
protected MosaicConstraints lookupConstraints( String name )
{
return (MosaicConstraints) m_name2Constraints.get( name );
}
//
// l o o k u p N o d e
//
protected MosaicLeafNode lookupNode( Component comp )
{
return (MosaicLeafNode) m_comp2Node.get( comp );
}
//
// n e w C o n s t r a i n t s
//
protected MosaicConstraints newConstraints( String name )
{
MosaicConstraints constraints = getConstraints( name );
if( constraints == null ) {
constraints = getConstraints( "Default" );
if( constraints == null )
constraints = new MosaicConstraints();
}
return constraints;
}
//
// n e w C o o r d S t a c k
//
protected MosaicCoordStack newCoordStack()
{
if( m_nextCoordStack != null ) {
m_lastCoordStack = m_nextCoordStack;
m_nextCoordStack = null;
}
else
if( m_lastCoordStack != null ) {
m_lastCoordStack = (MosaicCoordStack) m_lastCoordStack.clone();
m_lastCoordStack.nextCol();
}
else {
m_lastCoordStack = new MosaicCoordStack();
}
return m_lastCoordStack;
}
//
// i n i t i a l i z e G e o m e t r y
//
// This function generates the hierarchy tree. There are
// two cases where the tree has to be generated:
// a) m_initialized == false. This is the case if new
// components, constraints or coord stacks have been
// registered. b) A new component that is not yet known
// to the layout manager has been added to the parent panel,
// by using the add( comp ) function. Because *all*
// components that are children of the parent panel are
// returned by getComponents(), the second case can be
// discovered as well.
protected void initializeGeometry( Container parent )
{
int i;
Component comp[] = parent.getComponents();
for( i = 0; i < comp.length; ++i ) {
if( ! m_comp2Node.containsKey( comp[i] ) ) {
addLayoutComponent( "", comp[i] );
m_initialized = false;
}
}
if( m_initialized )
return;
m_root = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -