Coverage Report - net.miginfocom.layout.LinkHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
LinkHandler
N/A
N/A
6.857
 
 1  
 package net.miginfocom.layout;
 2  
 
 3  
 import java.lang.ref.WeakReference;
 4  
 import java.util.ArrayList;
 5  
 import java.util.HashMap;
 6  
 /*
 7  
  * License (BSD):
 8  
  * ==============
 9  
  *
 10  
  * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (miglayout (at) miginfocom (dot) com)
 11  
  * All rights reserved.
 12  
  *
 13  
  * Redistribution and use in source and binary forms, with or without modification,
 14  
  * are permitted provided that the following conditions are met:
 15  
  * Redistributions of source code must retain the above copyright notice, this list
 16  
  * of conditions and the following disclaimer.
 17  
  * Redistributions in binary form must reproduce the above copyright notice, this
 18  
  * list of conditions and the following disclaimer in the documentation and/or other
 19  
  * materials provided with the distribution.
 20  
  * Neither the name of the MiG InfoCom AB nor the names of its contributors may be
 21  
  * used to endorse or promote products derived from this software without specific
 22  
  * prior written permission.
 23  
  *
 24  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 25  
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 26  
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 27  
  * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 28  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 29  
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 30  
  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 31  
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 32  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 33  
  * OF SUCH DAMAGE.
 34  
  *
 35  
  * @version 1.0
 36  
  * @author Mikael Grev, MiG InfoCom AB
 37  
  *         Date: 2006-sep-08
 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  
         /** Sets a key that can be linked to from any component.
 87  
          * @param layout The MigLayout instance
 88  
          * @param key The key to link to. This is the same as the ID in a component constraint.
 89  
          * @param x x
 90  
          * @param y y
 91  
          * @param width Width
 92  
          * @param height Height
 93  
          * @return If the value was changed
 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  
         /** This method clear any weak references right away instead of waiting for the GC. This might be advantageous
 172  
          * if lots of layout are created and disposed of quickly to keep memory consumption down.
 173  
          * @since 3.7.4
 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  
 }