| 1 | |
package net.miginfocom.layout; |
| 2 | |
|
| 3 | |
import java.lang.ref.WeakReference; |
| 4 | |
import java.util.ArrayList; |
| 5 | |
import java.util.HashMap; |
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
public final class LinkHandler |
| 43 | |
{ |
| 44 | |
public static final int X = 0; |
| 45 | |
public static final int Y = 1; |
| 46 | |
public static final int WIDTH = 2; |
| 47 | |
public static final int HEIGHT = 3; |
| 48 | |
public static final int X2 = 4; |
| 49 | |
public static final int Y2 = 5; |
| 50 | |
|
| 51 | |
private static final ArrayList<WeakReference<Object>> LAYOUTS = new ArrayList<WeakReference<Object>>(4); |
| 52 | |
private static final ArrayList<HashMap<String, int[]>> VALUES = new ArrayList<HashMap<String, int[]>>(4); |
| 53 | |
private static final ArrayList<HashMap<String, int[]>> VALUES_TEMP = new ArrayList<HashMap<String, int[]>>(4); |
| 54 | |
|
| 55 | |
private LinkHandler() |
| 56 | |
{ |
| 57 | |
} |
| 58 | |
|
| 59 | |
public synchronized static Integer getValue(Object layout, String key, int type) |
| 60 | |
{ |
| 61 | |
Integer ret = null; |
| 62 | |
boolean cont = true; |
| 63 | |
|
| 64 | |
for (int i = LAYOUTS.size() - 1; i >= 0; i--) { |
| 65 | |
Object l = LAYOUTS.get(i).get(); |
| 66 | |
if (ret == null && l == layout) { |
| 67 | |
int[] rect = VALUES_TEMP.get(i).get(key); |
| 68 | |
if (cont && rect != null && rect[type] != LayoutUtil.NOT_SET) { |
| 69 | |
ret = rect[type]; |
| 70 | |
} else { |
| 71 | |
rect = VALUES.get(i).get(key); |
| 72 | |
ret = (rect != null && rect[type] != LayoutUtil.NOT_SET) ? rect[type] : null; |
| 73 | |
} |
| 74 | |
cont = false; |
| 75 | |
} |
| 76 | |
|
| 77 | |
if (l == null) { |
| 78 | |
LAYOUTS.remove(i); |
| 79 | |
VALUES.remove(i); |
| 80 | |
VALUES_TEMP.remove(i); |
| 81 | |
} |
| 82 | |
} |
| 83 | |
return ret; |
| 84 | |
} |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public synchronized static boolean setBounds(Object layout, String key, int x, int y, int width, int height) |
| 96 | |
{ |
| 97 | |
return setBounds(layout, key, x, y, width, height, false, false); |
| 98 | |
} |
| 99 | |
|
| 100 | |
synchronized static boolean setBounds(Object layout, String key, int x, int y, int width, int height, boolean temporary, boolean incCur) |
| 101 | |
{ |
| 102 | |
for (int i = LAYOUTS.size() - 1; i >= 0; i--) { |
| 103 | |
Object l = LAYOUTS.get(i).get(); |
| 104 | |
if (l == layout) { |
| 105 | |
HashMap<String, int[]> map = (temporary ? VALUES_TEMP : VALUES).get(i); |
| 106 | |
int[] old = map.get(key); |
| 107 | |
|
| 108 | |
if (old == null || old[X] != x || old[Y] != y || old[WIDTH] != width || old[HEIGHT] != height) { |
| 109 | |
if (old == null || incCur == false) { |
| 110 | |
map.put(key, new int[] {x, y, width, height, x + width, y + height}); |
| 111 | |
return true; |
| 112 | |
} else { |
| 113 | |
boolean changed = false; |
| 114 | |
|
| 115 | |
if (x != LayoutUtil.NOT_SET) { |
| 116 | |
if (old[X] == LayoutUtil.NOT_SET || x < old[X]) { |
| 117 | |
old[X] = x; |
| 118 | |
old[WIDTH] = old[X2] - x; |
| 119 | |
changed = true; |
| 120 | |
} |
| 121 | |
|
| 122 | |
if (width != LayoutUtil.NOT_SET) { |
| 123 | |
int x2 = x + width; |
| 124 | |
if (old[X2] == LayoutUtil.NOT_SET || x2 > old[X2]) { |
| 125 | |
old[X2] = x2; |
| 126 | |
old[WIDTH] = x2 - old[X]; |
| 127 | |
changed = true; |
| 128 | |
} |
| 129 | |
} |
| 130 | |
} |
| 131 | |
|
| 132 | |
if (y != LayoutUtil.NOT_SET) { |
| 133 | |
if (old[Y] == LayoutUtil.NOT_SET || y < old[Y]) { |
| 134 | |
old[Y] = y; |
| 135 | |
old[HEIGHT] = old[Y2] - y; |
| 136 | |
changed = true; |
| 137 | |
} |
| 138 | |
|
| 139 | |
if (height != LayoutUtil.NOT_SET) { |
| 140 | |
int y2 = y + height; |
| 141 | |
if (old[Y2] == LayoutUtil.NOT_SET || y2 > old[Y2]) { |
| 142 | |
old[Y2] = y2; |
| 143 | |
old[HEIGHT] = y2 - old[Y]; |
| 144 | |
changed = true; |
| 145 | |
} |
| 146 | |
} |
| 147 | |
} |
| 148 | |
return changed; |
| 149 | |
} |
| 150 | |
} |
| 151 | |
return false; |
| 152 | |
} |
| 153 | |
} |
| 154 | |
|
| 155 | |
LAYOUTS.add(new WeakReference<Object>(layout)); |
| 156 | |
int[] bounds = new int[] {x, y, width, height, x + width, y + height}; |
| 157 | |
|
| 158 | |
HashMap<String, int[]> values = new HashMap<String, int[]>(4); |
| 159 | |
if (temporary) |
| 160 | |
values.put(key, bounds); |
| 161 | |
VALUES_TEMP.add(values); |
| 162 | |
|
| 163 | |
values = new HashMap<String, int[]>(4); |
| 164 | |
if (temporary == false) |
| 165 | |
values.put(key, bounds); |
| 166 | |
VALUES.add(values); |
| 167 | |
|
| 168 | |
return true; |
| 169 | |
} |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
public synchronized static void clearWeakReferencesNow() |
| 176 | |
{ |
| 177 | |
LAYOUTS.clear(); |
| 178 | |
} |
| 179 | |
|
| 180 | |
public synchronized static boolean clearBounds(Object layout, String key) |
| 181 | |
{ |
| 182 | |
for (int i = LAYOUTS.size() - 1; i >= 0; i--) { |
| 183 | |
Object l = LAYOUTS.get(i).get(); |
| 184 | |
if (l == layout) |
| 185 | |
return VALUES.get(i).remove(key) != null; |
| 186 | |
} |
| 187 | |
return false; |
| 188 | |
} |
| 189 | |
|
| 190 | |
synchronized static void clearTemporaryBounds(Object layout) |
| 191 | |
{ |
| 192 | |
for (int i = LAYOUTS.size() - 1; i >= 0; i--) { |
| 193 | |
Object l = LAYOUTS.get(i).get(); |
| 194 | |
if (l == layout) { |
| 195 | |
VALUES_TEMP.get(i).clear(); |
| 196 | |
return; |
| 197 | |
} |
| 198 | |
} |
| 199 | |
} |
| 200 | |
} |