unused - could be trashed.
[jalview.git] / unused / net / miginfocom / layout / LinkHandler.java
1 package net.miginfocom.layout;
2
3 import java.util.HashMap;
4 import java.util.WeakHashMap;
5 /*
6  * License (BSD):
7  * ==============
8  *
9  * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (miglayout (at) miginfocom (dot) com)
10  * All rights reserved.
11  *
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.
22  *
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
32  * OF SUCH DAMAGE.
33  *
34  * @version 1.0
35  * @author Mikael Grev, MiG InfoCom AB
36  *         Date: 2006-sep-08
37  */
38
39 /**
40  */
41 public final class LinkHandler
42 {
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;
49
50         // indices for values of LAYOUTS
51         private static final int VALUES = 0;
52         private static final int VALUES_TEMP = 1;
53
54         private static final WeakHashMap<Object, HashMap<String, int[]>[]> LAYOUTS = new WeakHashMap<Object, HashMap<String, int[]>[]>();
55
56         private LinkHandler()
57         {
58         }
59
60         public synchronized static Integer getValue(Object layout, String key, int type)
61         {
62                 Integer ret = null;
63
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) {
68                                 ret = rect[type];
69                         } else {
70                                 rect = layoutValues[VALUES].get(key);
71                                 ret = (rect != null && rect[type] != LayoutUtil.NOT_SET) ? rect[type] : null;
72                         }
73                 }
74                 return ret;
75         }
76
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.
80          * @param x x
81          * @param y y
82          * @param width Width
83          * @param height Height
84          * @return If the value was changed
85          */
86         public synchronized static boolean setBounds(Object layout, String key, int x, int y, int width, int height)
87         {
88                 return setBounds(layout, key, x, y, width, height, false, false);
89         }
90
91         synchronized static boolean setBounds(Object layout, String key, int x, int y, int width, int height, boolean temporary, boolean incCur)
92         {
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);
97
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});
101                                         return true;
102                                 } else {
103                                         boolean changed = false;
104
105                                         if (x != LayoutUtil.NOT_SET) {
106                                                 if (old[X] == LayoutUtil.NOT_SET || x < old[X]) {
107                                                         old[X] = x;
108                                                         old[WIDTH] = old[X2] - x;
109                                                         changed = true;
110                                                 }
111
112                                                 if (width != LayoutUtil.NOT_SET) {
113                                                         int x2 = x + width;
114                                                         if (old[X2] == LayoutUtil.NOT_SET || x2 > old[X2]) {
115                                                                 old[X2] = x2;
116                                                                 old[WIDTH] = x2 - old[X];
117                                                                 changed = true;
118                                                         }
119                                                 }
120                                         }
121
122                                         if (y != LayoutUtil.NOT_SET) {
123                                                 if (old[Y] == LayoutUtil.NOT_SET || y < old[Y]) {
124                                                         old[Y] = y;
125                                                         old[HEIGHT] = old[Y2] - y;
126                                                         changed = true;
127                                                 }
128
129                                                 if (height != LayoutUtil.NOT_SET) {
130                                                         int y2 = y + height;
131                                                         if (old[Y2] == LayoutUtil.NOT_SET || y2 > old[Y2]) {
132                                                                 old[Y2] = y2;
133                                                                 old[HEIGHT] = y2 - old[Y];
134                                                                 changed = true;
135                                                         }
136                                                 }
137                                         }
138                                         return changed;
139                                 }
140                         }
141                         return false;
142                 }
143
144                 int[] bounds = new int[] {x, y, width, height, x + width, y + height};
145
146                 HashMap<String, int[]> values_temp = new HashMap<String, int[]>(4);
147                 if (temporary)
148                         values_temp.put(key, bounds);
149
150                 HashMap<String, int[]> values = new HashMap<String, int[]>(4);
151                 if (temporary == false)
152                         values.put(key, bounds);
153
154                 LAYOUTS.put(layout, new HashMap[] {values, values_temp});
155
156                 return true;
157         }
158
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.
161          * @since 3.7.4
162          */
163         public synchronized static void clearWeakReferencesNow()
164         {
165                 LAYOUTS.clear();
166         }
167
168         public synchronized static boolean clearBounds(Object layout, String key)
169         {
170                 HashMap<String, int[]>[] layoutValues = LAYOUTS.get(layout);
171                 if (layoutValues != null)
172                         return layoutValues[VALUES].remove(key) != null;
173                 return false;
174         }
175
176         synchronized static void clearTemporaryBounds(Object layout)
177         {
178                 HashMap<String, int[]>[] layoutValues = LAYOUTS.get(layout);
179                 if (layoutValues != null)
180                         layoutValues[VALUES_TEMP].clear();
181         }
182 }