Yay for type safety!

git-svn-id: https://samskivert.googlecode.com/svn/trunk@1096 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2003-04-10 21:43:13 +00:00
parent c925bd6304
commit d4082ae0b7
3 changed files with 102 additions and 127 deletions
@@ -1,5 +1,5 @@
// //
// $Id: GroupLayout.java,v 1.6 2002/09/25 07:26:19 mdb Exp $ // $Id: GroupLayout.java,v 1.7 2003/04/10 21:43:13 mdb Exp $
// //
// samskivert library - useful routines for java programs // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -31,7 +31,7 @@ import java.util.HashMap;
* Group layout managers lay out widgets in horizontal or vertical groups. * Group layout managers lay out widgets in horizontal or vertical groups.
*/ */
public abstract class GroupLayout public abstract class GroupLayout
implements LayoutManager2, SwingConstants implements LayoutManager2
{ {
/** /**
* The group layout managers supports two constraints: fixedness * The group layout managers supports two constraints: fixedness
@@ -74,6 +74,28 @@ public abstract class GroupLayout
} }
} }
/** A class used to make our policy constants type-safe. */
public static class Policy
{
int code;
public Policy (int code)
{
this.code = code;
}
}
/** A class used to make our policy constants type-safe. */
public static class Justification
{
int code;
public Justification (int code)
{
this.code = code;
}
}
/** /**
* A constraints object that indicates that the component should be * A constraints object that indicates that the component should be
* fixed and have the default weight of one. This is so commonly used * fixed and have the default weight of one. This is so commonly used
@@ -84,43 +106,58 @@ public abstract class GroupLayout
/** /**
* Do not adjust the widgets on this axis. * Do not adjust the widgets on this axis.
*/ */
public final static int NONE = 0; public final static Policy NONE = new Policy(0);
/** /**
* Stretch all the widgets to their maximum possible size on this * Stretch all the widgets to their maximum possible size on this
* axis. * axis.
*/ */
public final static int STRETCH = 1; public final static Policy STRETCH = new Policy(1);
/** /**
* Stretch all the widgets to be equal to the size of the largest * Stretch all the widgets to be equal to the size of the largest
* widget on this axis. * widget on this axis.
*/ */
public final static int EQUALIZE = 2; public final static Policy EQUALIZE = new Policy(2);
/** /**
* Only valid for off-axis policy, this leaves widgets alone unless * Only valid for off-axis policy, this leaves widgets alone unless
* they are larger in the off-axis direction than their container, in * they are larger in the off-axis direction than their container, in
* which case it constrains them to fit on the off-axis. * which case it constrains them to fit on the off-axis.
*/ */
public final static int CONSTRAIN = 3; public final static Policy CONSTRAIN = new Policy(3);
public void setPolicy (int policy) /** A justification constant. */
public final static Justification CENTER = new Justification(0);
/** A justification constant. */
public final static Justification LEFT = new Justification(1);
/** A justification constant. */
public final static Justification RIGHT = new Justification(2);
/** A justification constant. */
public final static Justification TOP = new Justification(3);
/** A justification constant. */
public final static Justification BOTTOM = new Justification(4);
public void setPolicy (Policy policy)
{ {
_policy = policy; _policy = policy;
} }
public int getPolicy () public Policy getPolicy ()
{ {
return _policy; return _policy;
} }
public void setOffAxisPolicy (int offpolicy) public void setOffAxisPolicy (Policy offpolicy)
{ {
_offpolicy = offpolicy; _offpolicy = offpolicy;
} }
public int getOffAxisPolicy () public Policy getOffAxisPolicy ()
{ {
return _offpolicy; return _offpolicy;
} }
@@ -135,22 +172,22 @@ public abstract class GroupLayout
return _gap; return _gap;
} }
public void setJustification (int justification) public void setJustification (Justification justification)
{ {
_justification = justification; _justification = justification;
} }
public int getJustification () public Justification getJustification ()
{ {
return _justification; return _justification;
} }
public void setOffAxisJustification (int justification) public void setOffAxisJustification (Justification justification)
{ {
_offjust = justification; _offjust = justification;
} }
public int getOffAxisJustification () public Justification getOffAxisJustification ()
{ {
return _offjust; return _offjust;
} }
@@ -310,7 +347,7 @@ public abstract class GroupLayout
* HGroupLayout} with a configuration conducive to containing a row of * HGroupLayout} with a configuration conducive to containing a row of
* buttons. * buttons.
*/ */
public static JPanel makeButtonBox (int justification) public static JPanel makeButtonBox (Justification justification)
{ {
return new JPanel(new HGroupLayout(NONE, justification)); return new JPanel(new HGroupLayout(NONE, justification));
} }
@@ -339,7 +376,7 @@ public abstract class GroupLayout
* HGroupLayout} with the specified on-axis policy (default * HGroupLayout} with the specified on-axis policy (default
* configuration otherwise). * configuration otherwise).
*/ */
public static JPanel makeHBox (int policy) public static JPanel makeHBox (Policy policy)
{ {
return new JPanel(new HGroupLayout(policy)); return new JPanel(new HGroupLayout(policy));
} }
@@ -349,7 +386,7 @@ public abstract class GroupLayout
* HGroupLayout} with the specified on-axis policy and justification * HGroupLayout} with the specified on-axis policy and justification
* (default configuration otherwise). * (default configuration otherwise).
*/ */
public static JPanel makeHBox (int policy, int justification) public static JPanel makeHBox (Policy policy, Justification justification)
{ {
return new JPanel(new HGroupLayout(policy, justification)); return new JPanel(new HGroupLayout(policy, justification));
} }
@@ -359,8 +396,8 @@ public abstract class GroupLayout
* HGroupLayout} with the specified on-axis policy, justification and * HGroupLayout} with the specified on-axis policy, justification and
* off-axis policy (default configuration otherwise). * off-axis policy (default configuration otherwise).
*/ */
public static JPanel makeHBox (int policy, int justification, public static JPanel makeHBox (Policy policy, Justification justification,
int offAxisPolicy) Policy offAxisPolicy)
{ {
return new JPanel(new HGroupLayout(policy, offAxisPolicy, return new JPanel(new HGroupLayout(policy, offAxisPolicy,
DEFAULT_GAP, justification)); DEFAULT_GAP, justification));
@@ -380,7 +417,7 @@ public abstract class GroupLayout
* VGroupLayout} with the specified on-axis policy (default * VGroupLayout} with the specified on-axis policy (default
* configuration otherwise). * configuration otherwise).
*/ */
public static JPanel makeVBox (int policy) public static JPanel makeVBox (Policy policy)
{ {
return new JPanel(new VGroupLayout(policy)); return new JPanel(new VGroupLayout(policy));
} }
@@ -390,7 +427,7 @@ public abstract class GroupLayout
* VGroupLayout} with the specified on-axis policy and justification * VGroupLayout} with the specified on-axis policy and justification
* (default configuration otherwise). * (default configuration otherwise).
*/ */
public static JPanel makeVBox (int policy, int justification) public static JPanel makeVBox (Policy policy, Justification justification)
{ {
return new JPanel(new VGroupLayout(policy, justification)); return new JPanel(new VGroupLayout(policy, justification));
} }
@@ -400,8 +437,8 @@ public abstract class GroupLayout
* VGroupLayout} with the specified on-axis policy, justification and * VGroupLayout} with the specified on-axis policy, justification and
* off-axis policy (default configuration otherwise). * off-axis policy (default configuration otherwise).
*/ */
public static JPanel makeVBox (int policy, int justification, public static JPanel makeVBox (Policy policy, Justification justification,
int offAxisPolicy) Policy offAxisPolicy)
{ {
return new JPanel(new VGroupLayout(policy, offAxisPolicy, return new JPanel(new VGroupLayout(policy, offAxisPolicy,
DEFAULT_GAP, justification)); DEFAULT_GAP, justification));
@@ -417,11 +454,11 @@ public abstract class GroupLayout
return new JPanel(new VGroupLayout(STRETCH, STRETCH, gap, CENTER)); return new JPanel(new VGroupLayout(STRETCH, STRETCH, gap, CENTER));
} }
protected int _policy = NONE; protected Policy _policy = NONE;
protected int _offpolicy = CONSTRAIN; protected Policy _offpolicy = CONSTRAIN;
protected int _gap = DEFAULT_GAP; protected int _gap = DEFAULT_GAP;
protected int _justification = CENTER; protected Justification _justification = CENTER;
protected int _offjust = CENTER; protected Justification _offjust = CENTER;
protected HashMap _constraints; protected HashMap _constraints;
@@ -1,5 +1,5 @@
// //
// $Id: HGroupLayout.java,v 1.9 2002/05/16 02:02:38 mdb Exp $ // $Id: HGroupLayout.java,v 1.10 2003/04/10 21:43:13 mdb Exp $
// //
// samskivert library - useful routines for java programs // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -24,8 +24,8 @@ import java.awt.*;
public class HGroupLayout extends GroupLayout public class HGroupLayout extends GroupLayout
{ {
public HGroupLayout (int policy, int offpolicy, int gap, public HGroupLayout (Policy policy, Policy offpolicy, int gap,
int justification) Justification justification)
{ {
_policy = policy; _policy = policy;
_offpolicy = offpolicy; _offpolicy = offpolicy;
@@ -33,20 +33,20 @@ public class HGroupLayout extends GroupLayout
_justification = justification; _justification = justification;
} }
public HGroupLayout (int policy, int gap, int justification) public HGroupLayout (Policy policy, int gap, Justification justification)
{ {
_policy = policy; _policy = policy;
_gap = gap; _gap = gap;
_justification = justification; _justification = justification;
} }
public HGroupLayout (int policy, int justification) public HGroupLayout (Policy policy, Justification justification)
{ {
_policy = policy; _policy = policy;
_justification = justification; _justification = justification;
} }
public HGroupLayout (int policy) public HGroupLayout (Policy policy)
{ {
_policy = policy; _policy = policy;
} }
@@ -60,17 +60,11 @@ public class HGroupLayout extends GroupLayout
DimenInfo info = computeDimens(parent, type); DimenInfo info = computeDimens(parent, type);
Dimension dims = new Dimension(); Dimension dims = new Dimension();
switch (_policy) { if (_policy == STRETCH || _policy == EQUALIZE) {
case STRETCH:
case EQUALIZE:
dims.width = info.maxwid * (info.count - info.numfix) + dims.width = info.maxwid * (info.count - info.numfix) +
info.fixwid + _gap * info.count; info.fixwid + _gap * info.count;
break; } else { // NONE or CONSTRAIN
case NONE:
default:
dims.width = info.totwid + _gap * info.count; dims.width = info.totwid + _gap * info.count;
break;
} }
dims.width -= _gap; dims.width -= _gap;
@@ -106,8 +100,7 @@ public class HGroupLayout extends GroupLayout
// do the on-axis policy calculations // do the on-axis policy calculations
int defwid = 0; int defwid = 0;
switch (_policy) { if (_policy == STRETCH) {
case STRETCH:
if (freecount > 0) { if (freecount > 0) {
int freewid = b.width - info.fixwid - totgap; int freewid = b.width - info.fixwid - totgap;
defwid = freewid / freecount; defwid = freewid / freecount;
@@ -116,47 +109,30 @@ public class HGroupLayout extends GroupLayout
} else { } else {
totwid = info.fixwid + totgap; totwid = info.fixwid + totgap;
} }
break;
case EQUALIZE: } else if (_policy == EQUALIZE) {
defwid = info.maxwid; defwid = info.maxwid;
totwid = info.fixwid + defwid * freecount + totgap; totwid = info.fixwid + defwid * freecount + totgap;
break;
default: } else { // NONE or CONSTRAIN
case NONE:
totwid = info.totwid + totgap; totwid = info.totwid + totgap;
break;
} }
// do the off-axis policy calculations // do the off-axis policy calculations
int defhei = 0; int defhei = 0;
switch (_offpolicy) { if (_offpolicy == STRETCH) {
case STRETCH:
defhei = b.height; defhei = b.height;
break; } else if (_offpolicy == EQUALIZE) {
case EQUALIZE:
defhei = info.maxhei; defhei = info.maxhei;
break;
default:
case NONE:
break;
} }
// do the justification-related calculations // do the justification-related calculations
switch (_justification) { if (_justification == LEFT || _justification == TOP) {
default:
case LEFT:
case TOP:
sx = insets.left; sx = insets.left;
break; } else if (_justification == CENTER) {
case CENTER:
sx = insets.left + (b.width - totwid)/2; sx = insets.left + (b.width - totwid)/2;
break; } else { // RIGHT or BOTTOM
case RIGHT:
case BOTTOM:
sx = insets.left + b.width - totwid; sx = insets.left + b.width - totwid;
break;
} }
// do the layout // do the layout
@@ -186,19 +162,12 @@ public class HGroupLayout extends GroupLayout
} }
// determine our off-axis position // determine our off-axis position
switch (_offjust) { if (_offjust == LEFT || _offjust == TOP) {
case LEFT:
case TOP:
sy = insets.top; sy = insets.top;
break; } else if (_offjust == RIGHT || _offjust == BOTTOM) {
case RIGHT:
case BOTTOM:
sy = insets.top + b.height - newhei; sy = insets.top + b.height - newhei;
break; } else { // CENTER
default:
case CENTER:
sy = insets.top + (b.height - newhei)/2; sy = insets.top + (b.height - newhei)/2;
break;
} }
child.setBounds(sx, sy, newwid, newhei); child.setBounds(sx, sy, newwid, newhei);
@@ -1,5 +1,5 @@
// //
// $Id: VGroupLayout.java,v 1.10 2002/05/16 02:02:38 mdb Exp $ // $Id: VGroupLayout.java,v 1.11 2003/04/10 21:43:13 mdb Exp $
// //
// samskivert library - useful routines for java programs // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -24,8 +24,8 @@ import java.awt.*;
public class VGroupLayout extends GroupLayout public class VGroupLayout extends GroupLayout
{ {
public VGroupLayout (int policy, int offpolicy, int gap, public VGroupLayout (Policy policy, Policy offpolicy, int gap,
int justification) Justification justification)
{ {
_policy = policy; _policy = policy;
_offpolicy = offpolicy; _offpolicy = offpolicy;
@@ -33,20 +33,20 @@ public class VGroupLayout extends GroupLayout
_justification = justification; _justification = justification;
} }
public VGroupLayout (int policy, int gap, int justification) public VGroupLayout (Policy policy, int gap, Justification justification)
{ {
_policy = policy; _policy = policy;
_gap = gap; _gap = gap;
_justification = justification; _justification = justification;
} }
public VGroupLayout (int policy, int justification) public VGroupLayout (Policy policy, Justification justification)
{ {
_policy = policy; _policy = policy;
_justification = justification; _justification = justification;
} }
public VGroupLayout (int policy) public VGroupLayout (Policy policy)
{ {
_policy = policy; _policy = policy;
} }
@@ -60,17 +60,11 @@ public class VGroupLayout extends GroupLayout
DimenInfo info = computeDimens(parent, type); DimenInfo info = computeDimens(parent, type);
Dimension dims = new Dimension(); Dimension dims = new Dimension();
switch (_policy) { if (_policy == STRETCH || _policy == EQUALIZE) {
case STRETCH:
case EQUALIZE:
dims.height = info.maxhei * (info.count - info.numfix) + dims.height = info.maxhei * (info.count - info.numfix) +
info.fixhei + _gap * info.count; info.fixhei + _gap * info.count;
break; } else {
case NONE:
default:
dims.height = info.tothei + _gap * info.count; dims.height = info.tothei + _gap * info.count;
break;
} }
dims.height -= _gap; dims.height -= _gap;
@@ -106,8 +100,7 @@ public class VGroupLayout extends GroupLayout
// do the on-axis policy calculations // do the on-axis policy calculations
int defhei = 0; int defhei = 0;
switch (_policy) { if (_policy == STRETCH) {
case STRETCH:
if (freecount > 0) { if (freecount > 0) {
int freehei = b.height - info.fixhei - totgap; int freehei = b.height - info.fixhei - totgap;
defhei = freehei / freecount; defhei = freehei / freecount;
@@ -116,47 +109,30 @@ public class VGroupLayout extends GroupLayout
} else { } else {
tothei = info.fixhei + totgap; tothei = info.fixhei + totgap;
} }
break;
case EQUALIZE: } else if (_policy == EQUALIZE) {
defhei = info.maxhei; defhei = info.maxhei;
tothei = info.fixhei + defhei * freecount + totgap; tothei = info.fixhei + defhei * freecount + totgap;
break;
default: } else {
case NONE:
tothei = info.tothei + totgap; tothei = info.tothei + totgap;
break;
} }
// do the off-axis policy calculations // do the off-axis policy calculations
int defwid = 0; int defwid = 0;
switch (_offpolicy) { if (_offpolicy == STRETCH) {
case STRETCH:
defwid = b.width; defwid = b.width;
break; } else if (_offpolicy == EQUALIZE) {
case EQUALIZE:
defwid = info.maxwid; defwid = info.maxwid;
break;
default:
case NONE:
break;
} }
// do the justification-related calculations // do the justification-related calculations
switch (_justification) { if (_justification == LEFT || _justification == TOP) {
default:
case LEFT:
case TOP:
sy = insets.top; sy = insets.top;
break; } else if (_justification == CENTER) {
case CENTER:
sy = insets.top + (b.height - tothei)/2; sy = insets.top + (b.height - tothei)/2;
break; } else { // RIGHT or BOTTOM
case RIGHT:
case BOTTOM:
sy = insets.top + b.height - tothei; sy = insets.top + b.height - tothei;
break;
} }
// do the layout // do the layout
@@ -186,19 +162,12 @@ public class VGroupLayout extends GroupLayout
} }
// determine our off-axis position // determine our off-axis position
switch (_offjust) { if (_offjust == LEFT || _offjust == TOP) {
case LEFT:
case TOP:
sx = insets.left; sx = insets.left;
break; } else if (_offjust == RIGHT || _offjust == BOTTOM) {
case RIGHT:
case BOTTOM:
sx = insets.left + b.width - newwid; sx = insets.left + b.width - newwid;
break; } else { // CENTER
default:
case CENTER:
sx = insets.left + (b.width - newwid)/2; sx = insets.left + (b.width - newwid)/2;
break;
} }
child.setBounds(sx, sy, newwid, newhei); child.setBounds(sx, sy, newwid, newhei);