1 package net.miginfocom.layout;
3 import java.util.HashMap;
4 import java.util.WeakHashMap;
9 * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (miglayout (at) miginfocom (dot) com)
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without modification,
13 * are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice, this list
15 * of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice, this
17 * list of conditions and the following disclaimer in the documentation and/or other
18 * materials provided with the distribution.
19 * Neither the name of the MiG InfoCom AB nor the names of its contributors may be
20 * used to endorse or promote products derived from this software without specific
21 * prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
29 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 * @author Mikael Grev, MiG InfoCom AB
41 public final class LinkHandler
43 public static final int X = 0;
44 public static final int Y = 1;
45 public static final int WIDTH = 2;
46 public static final int HEIGHT = 3;
47 public static final int X2 = 4;
48 public static final int Y2 = 5;
50 // indices for values of LAYOUTS
51 private static final int VALUES = 0;
52 private static final int VALUES_TEMP = 1;
54 private static final WeakHashMap<Object, HashMap<String, int[]>[]> LAYOUTS = new WeakHashMap<Object, HashMap<String, int[]>[]>();
60 public synchronized static Integer getValue(Object layout, String key, int type)
64 HashMap<String, int[]>[] layoutValues = LAYOUTS.get(layout);
65 if (layoutValues != null) {
66 int[] rect = layoutValues[VALUES_TEMP].get(key);
67 if (rect != null && rect[type] != LayoutUtil.NOT_SET) {
70 rect = layoutValues[VALUES].get(key);
71 ret = (rect != null && rect[type] != LayoutUtil.NOT_SET) ? rect[type] : null;
77 /** Sets a key that can be linked to from any component.
78 * @param layout The MigLayout instance
79 * @param key The key to link to. This is the same as the ID in a component constraint.
83 * @param height Height
84 * @return If the value was changed
86 public synchronized static boolean setBounds(Object layout, String key, int x, int y, int width, int height)
88 return setBounds(layout, key, x, y, width, height, false, false);
91 synchronized static boolean setBounds(Object layout, String key, int x, int y, int width, int height, boolean temporary, boolean incCur)
93 HashMap<String, int[]>[] layoutValues = LAYOUTS.get(layout);
94 if (layoutValues != null) {
95 HashMap<String, int[]> map = layoutValues[temporary ? VALUES_TEMP : VALUES];
96 int[] old = map.get(key);
98 if (old == null || old[X] != x || old[Y] != y || old[WIDTH] != width || old[HEIGHT] != height) {
99 if (old == null || incCur == false) {
100 map.put(key, new int[] {x, y, width, height, x + width, y + height});
103 boolean changed = false;
105 if (x != LayoutUtil.NOT_SET) {
106 if (old[X] == LayoutUtil.NOT_SET || x < old[X]) {
108 old[WIDTH] = old[X2] - x;
112 if (width != LayoutUtil.NOT_SET) {
114 if (old[X2] == LayoutUtil.NOT_SET || x2 > old[X2]) {
116 old[WIDTH] = x2 - old[X];
122 if (y != LayoutUtil.NOT_SET) {
123 if (old[Y] == LayoutUtil.NOT_SET || y < old[Y]) {
125 old[HEIGHT] = old[Y2] - y;
129 if (height != LayoutUtil.NOT_SET) {
131 if (old[Y2] == LayoutUtil.NOT_SET || y2 > old[Y2]) {
133 old[HEIGHT] = y2 - old[Y];
144 int[] bounds = new int[] {x, y, width, height, x + width, y + height};
146 HashMap<String, int[]> values_temp = new HashMap<String, int[]>(4);
148 values_temp.put(key, bounds);
150 HashMap<String, int[]> values = new HashMap<String, int[]>(4);
151 if (temporary == false)
152 values.put(key, bounds);
154 LAYOUTS.put(layout, new HashMap[] {values, values_temp});
159 /** This method clear any weak references right away instead of waiting for the GC. This might be advantageous
160 * if lots of layout are created and disposed of quickly to keep memory consumption down.
163 public synchronized static void clearWeakReferencesNow()
168 public synchronized static boolean clearBounds(Object layout, String key)
170 HashMap<String, int[]>[] layoutValues = LAYOUTS.get(layout);
171 if (layoutValues != null)
172 return layoutValues[VALUES].remove(key) != null;
176 synchronized static void clearTemporaryBounds(Object layout)
178 HashMap<String, int[]>[] layoutValues = LAYOUTS.get(layout);
179 if (layoutValues != null)
180 layoutValues[VALUES_TEMP].clear();