20 package net.sf.gridarta.gui.utils;
22 import java.awt.Component;
23 import java.awt.Container;
24 import java.awt.Dimension;
25 import java.awt.Insets;
26 import java.awt.LayoutManager2;
27 import java.io.Serializable;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
76 private static final Dimension
EMPTY =
new Dimension(0, 0);
83 DAIMONIN_TO_DIRECTION_MAP[direction.ordinal()] = direction;
110 private Component
cN;
124 private Component
cE;
138 private Component
cS;
152 private Component
cW;
159 private Component
cC;
166 private Component
cU;
173 private Component
cD;
192 @SuppressWarnings(
"ChainOfInstanceofChecks")
195 if (constraints == null) {
200 if (constraints instanceof Integer) {
202 }
else if (!(constraints instanceof
Direction)) {
203 throw new IllegalArgumentException(
"DirectionLayout does not support " + constraints.getClass().getName() +
" constraints.");
205 direction = (Direction) constraints;
207 synchronized (comp.getTreeLock()) {
248 throw new IllegalArgumentException(
"DirectionLayout does not support String constraints.");
252 @SuppressWarnings(
"ObjectEquality")
254 synchronized (comp.getTreeLock()) {
257 }
else if (cN == comp) {
259 }
else if (cE == comp) {
261 }
else if (cS == comp) {
263 }
else if (cW == comp) {
265 }
else if (cNE == comp) {
267 }
else if (cSE == comp) {
269 }
else if (cSW == comp) {
271 }
else if (cNW == comp) {
273 }
else if (cU == comp) {
275 }
else if (cD == comp) {
284 synchronized (parent.getTreeLock()) {
285 final Dimension dim =
new Dimension(0, 0);
297 dim.height =
vGap * 4 + dNW.height +
max(dW.height, dN.height) +
max(dSW.height, dC.height, dNE.height) +
max(dS.height, dE.height) + dSE.height +
max(dU.height, dD.height);
298 dim.width =
max(
hGap * 2 + dSW.width +
max(dNW.width, dC.width, dSE.width) + dNE.width,
hGap +
max(dW.width, dS.width) +
max(dN.width, dE.width),
max(dNW.width, dSE.width));
299 final Insets insets = parent.getInsets();
300 dim.width += insets.left + insets.right;
301 dim.height += insets.top + insets.bottom;
314 return component == null ?
EMPTY : component.getMinimumSize();
320 synchronized (parent.getTreeLock()) {
321 final Dimension dim =
new Dimension(0, 0);
333 dim.height =
vGap * 4 + dNW.height +
max(dW.height, dN.height) +
max(dSW.height, dC.height, dNE.height) +
max(dS.height, dE.height) + dSE.height +
max(dU.height, dD.height);
334 dim.width =
hGap * 2 + dSW.width +
max(dNW.width, dC.width, dSE.width) + dNE.width;
335 final Insets insets = parent.getInsets();
336 dim.width += insets.left + insets.right;
337 dim.height += insets.top + insets.bottom;
350 return component == null ?
EMPTY : component.getMinimumSize();
356 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
376 synchronized (parent.getTreeLock()) {
377 final Dimension size = parent.getSize();
378 final Insets padding = parent.getInsets();
379 final int innerWidth = size.width - padding.left - padding.right;
380 final int innerHeight = size.height - padding.top - padding.bottom;
386 if (
cW != null ||
cN != null) {
389 if (
cSW != null ||
cC != null ||
cNE != null) {
392 if (
cS != null ||
cE != null) {
399 if (
cU != null ||
cD != null) {
405 final int colWidth = (innerWidth -
hGap * 2) / 3;
406 final int rowHeight = (innerHeight -
vGap * (rows - 1)) / rows;
407 int top = padding.top;
408 final int left = padding.left;
410 cNW.setBounds(left + colWidth +
hGap, top, colWidth, rowHeight);
411 top += rowHeight +
vGap;
413 if (
cW != null ||
cN != null) {
415 cW.setBounds(left + ((colWidth +
hGap) / 2), top, colWidth, rowHeight);
418 cN.setBounds(left + ((colWidth +
hGap) * 3 / 2), top, colWidth, rowHeight);
420 top += rowHeight +
vGap;
422 if (
cSW != null ||
cC != null ||
cNE != null) {
424 cSW.setBounds(left, top, colWidth, rowHeight);
427 cC.setBounds(left + colWidth +
hGap, top, colWidth, rowHeight);
430 cNE.setBounds(left + ((colWidth +
hGap) * 2), top, colWidth, rowHeight);
432 top += rowHeight +
vGap;
434 if (
cS != null ||
cE != null) {
436 cS.setBounds(left + ((colWidth +
hGap) / 2), top, colWidth, rowHeight);
439 cE.setBounds(left + ((colWidth +
hGap) * 3 / 2), top, colWidth, rowHeight);
441 top += rowHeight +
vGap;
444 cSE.setBounds(left + colWidth +
hGap, top, colWidth, rowHeight);
445 top += rowHeight +
vGap;
447 if (
cU != null ||
cD != null) {
449 cU.setBounds(left + ((colWidth +
hGap) / 2), top, colWidth, rowHeight);
452 cD.setBounds(left + ((colWidth +
hGap) * 3 / 2), top, colWidth, rowHeight);
454 top += rowHeight +
vGap;
462 return getClass().getName() +
"[hGap=" +
hGap +
",vGap=" +
vGap +
']';
473 private static int max(@NotNull
final int... numbers) {
474 int ret = Integer.MIN_VALUE;
475 for (
final int number : numbers) {
DirectionLayout(final int hGap, final int vGap)
Creates a DirectionLayout with specified gaps.
void removeLayoutComponent(@NotNull final Component comp)
Component cD
Down Component.
Component cC
Center Component.
static final long serialVersionUID
Serial Version.
static Dimension getPreferredSize(@Nullable final Component component)
Returns the preferred size of a component or EMPTY for.
Component cU
Up Component.
Component cSW
SouthWest Component.
Dimension minimumLayoutSize(@NotNull final Container parent)
static final Direction [] DAIMONIN_TO_DIRECTION_MAP
The array to quickly find the direction enum constants for a daimonin direction.
static Direction getDirectionFromDaimonin(final int daimoninDirection)
Returns the Direction for a Daimonin direction.
Component cW
West Component.
void invalidateLayout(@NotNull final Container target)
Component cNE
NorthEast Component.
final int hGap
Horizontal Gap.
float getLayoutAlignmentX(@NotNull final Container target)
Base package of all Gridarta classes.
float getLayoutAlignmentY(@NotNull final Container target)
Component cNW
NorthWest Component.
Dimension preferredLayoutSize(final Container parent)
void addLayoutComponent(@NotNull final String name, @NotNull final Component comp)
static final Dimension EMPTY
Empty dimension as fallback for.
DirectionLayout()
Creates a DirectionLayout with zero gaps.
void addLayoutComponent(@NotNull final Component comp, @Nullable final Object constraints)
Component cSE
SouthEast Component.
Component cE
East Component.
void layoutContainer(@NotNull final Container parent)
static void addDirection(@NotNull final Direction direction)
Adds an entry to DAIMONIN_TO_DIRECTION_MAP.
Dimension maximumLayoutSize(@NotNull final Container target)
Component cS
South Component.
static Dimension getMinimumSize(@Nullable final Component component)
Returns the minimum size of a component or EMPTY for.
Component cN
North Component.
static int max(@NotNull final int... numbers)
Helper method for returning the maximum of an unspecified number of arguments.
This class implements a layout that is similar to java.awt.BorderLayout but implements those directio...
final int vGap
Vertical Gap.