Coverage Report - net.miginfocom.swt.SwtComponentWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
SwtComponentWrapper
N/A
N/A
2.459
 
 1  
 package net.miginfocom.swt;
 2  
 /*
 3  
  * License (BSD):
 4  
  * ==============
 5  
  *
 6  
  * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (miglayout (at) miginfocom (dot) com)
 7  
  * All rights reserved.
 8  
  *
 9  
  * Redistribution and use in source and binary forms, with or without modification,
 10  
  * are permitted provided that the following conditions are met:
 11  
  * Redistributions of source code must retain the above copyright notice, this list
 12  
  * of conditions and the following disclaimer.
 13  
  * Redistributions in binary form must reproduce the above copyright notice, this
 14  
  * list of conditions and the following disclaimer in the documentation and/or other
 15  
  * materials provided with the distribution.
 16  
  * Neither the name of the MiG InfoCom AB nor the names of its contributors may be
 17  
  * used to endorse or promote products derived from this software without specific
 18  
  * prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 21  
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 22  
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 23  
  * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 24  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 25  
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 26  
  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 27  
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 28  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 29  
  * OF SUCH DAMAGE.
 30  
  *
 31  
  * @version 1.0
 32  
  * @author Mikael Grev, MiG InfoCom AB
 33  
  *         Date: 2006-sep-08
 34  
  */
 35  
 
 36  
 import net.miginfocom.layout.ComponentWrapper;
 37  
 import net.miginfocom.layout.ContainerWrapper;
 38  
 import net.miginfocom.layout.PlatformDefaults;
 39  
 import org.eclipse.swt.SWT;
 40  
 import org.eclipse.swt.custom.StyledText;
 41  
 import org.eclipse.swt.graphics.*;
 42  
 import org.eclipse.swt.widgets.*;
 43  
 
 44  
 import java.util.Random;
 45  
 
 46  
 /**
 47  
  */
 48  
 public class SwtComponentWrapper implements ComponentWrapper
 49  
 {
 50  
         /** Debug color for component bounds outline.
 51  
          */
 52  
         private static Color DB_COMP_OUTLINE = new Color(Display.getCurrent(), 0, 0, 200);
 53  
 
 54  
         private static boolean vp = false;
 55  
         private static boolean mz = false;
 56  
 
 57  
         private final Control c;
 58  
         private int compType = TYPE_UNSET;
 59  
 
 60  
         public SwtComponentWrapper(Control c)
 61  
         {
 62  
                 this.c = c;
 63  
         }
 64  
 
 65  
         public final int getBaseline(int width, int height)
 66  
         {
 67  
                 return -1;
 68  
         }
 69  
 
 70  
         public final Object getComponent()
 71  
         {
 72  
                 return c;
 73  
         }
 74  
 
 75  
         public final float getPixelUnitFactor(boolean isHor)
 76  
         {
 77  
                 switch (PlatformDefaults.getLogicalPixelBase()) {
 78  
                         case PlatformDefaults.BASE_FONT_SIZE:
 79  
                                 GC gc = new GC(c);
 80  
                                 FontMetrics fm = gc.getFontMetrics();
 81  
                                 float f = isHor ? fm.getAverageCharWidth() / 5f : fm.getHeight() / 13f;
 82  
                                 gc.dispose();
 83  
                                 return f;
 84  
 
 85  
                         case PlatformDefaults.BASE_SCALE_FACTOR:
 86  
                                 Float s = isHor ? PlatformDefaults.getHorizontalScaleFactor() : PlatformDefaults.getVerticalScaleFactor();
 87  
                                 if (s != null)
 88  
                                         return s.floatValue();
 89  
                                 return (isHor ? getHorizontalScreenDPI() : getVerticalScreenDPI()) / (float) PlatformDefaults.getDefaultDPI();
 90  
 
 91  
                         default:
 92  
                                 return 1f;
 93  
                 }
 94  
         }
 95  
 
 96  
 //        /** Cache.
 97  
 //         */
 98  
 //        private final static IdentityHashMap<FontMetrics, Point.Float> FM_MAP2 = new IdentityHashMap<FontMetrics, Point.Float>(4);
 99  
 //        private final static Font SUBST_FONT2 = new Font("sansserif", Font.PLAIN, 11);
 100  
 //
 101  
 //        public float getDialogUnit(boolean isHor)
 102  
 //        {
 103  
 //                Font font = c.getFont();
 104  
 //                FontMetrics fm = c.getFontMetrics(font != null ? font : SUBST_FONT2);
 105  
 //                Point.Float dluP = FM_MAP2.get(fm);
 106  
 //                if (dluP == null) {
 107  
 //                        float w = fm.charWidth('X') / 4f;
 108  
 //                        int ascent = fm.getAscent();
 109  
 //                        float h = (ascent > 14 ? ascent : ascent + (15 - ascent) / 3) / 8f;
 110  
 //
 111  
 //                        dluP = new Point.Float(w, h);
 112  
 //                        FM_MAP2.put(fm, dluP);
 113  
 //                }
 114  
 //                return isHor ? dluP.x : dluP.y;
 115  
 //        }
 116  
 
 117  
         public final int getX()
 118  
         {
 119  
                 return c.getLocation().x;
 120  
         }
 121  
 
 122  
         public final int getY()
 123  
         {
 124  
                 return c.getLocation().y;
 125  
         }
 126  
 
 127  
         public final int getWidth()
 128  
         {
 129  
                 return c.getSize().x;
 130  
         }
 131  
 
 132  
         public final int getHeight()
 133  
         {
 134  
                 return c.getSize().y;
 135  
         }
 136  
 
 137  
         public final int getScreenLocationX()
 138  
         {
 139  
                 return c.toDisplay(0, 0).x;
 140  
         }
 141  
 
 142  
         public final int getScreenLocationY()
 143  
         {
 144  
                 return c.toDisplay(0, 0).y;
 145  
         }
 146  
 
 147  
         public final int getMinimumHeight(int sz)
 148  
         {
 149  
                 return mz ? 0 : computeSize(false, sz).y;
 150  
         }
 151  
 
 152  
         public final int getMinimumWidth(int sz)
 153  
         {
 154  
                 return mz ? 0 : computeSize(true, sz).x;
 155  
         }
 156  
 
 157  
         public final int getPreferredHeight(int sz)
 158  
         {
 159  
                 return computeSize(false, sz).y;
 160  
         }
 161  
 
 162  
         public final int getPreferredWidth(int sz)
 163  
         {
 164  
                 return computeSize(true, sz).x;
 165  
         }
 166  
 
 167  
         public final int getMaximumHeight(int sz)
 168  
         {
 169  
                 return Short.MAX_VALUE;
 170  
         }
 171  
 
 172  
         public final int getMaximumWidth(int sz)
 173  
         {
 174  
                 return Short.MAX_VALUE;
 175  
         }
 176  
 
 177  
         private Point computeSize(boolean hor, int sz)
 178  
         {
 179  
                 //Point p = c.getSize();
 180  
                 int wHint = hor ? SWT.DEFAULT : sz;     //(hor || p.x <= 0) ? sz : p.x;
 181  
                 int hHint = !hor ? SWT.DEFAULT : sz;    //(!hor || p.y <= 0) ? sz : p.y;
 182  
 
 183  
                 if (wHint != SWT.DEFAULT || hHint != SWT.DEFAULT) {
 184  
                         int trim = 0;
 185  
                         if (c instanceof Scrollable) {
 186  
                                 Rectangle rect = ((Scrollable) c).computeTrim(0, 0, 0, 0);
 187  
                                 trim = hor ? rect.width : rect.height;
 188  
                         } else {
 189  
                                 trim = (c.getBorderWidth() << 1);
 190  
                         }
 191  
                         if (wHint == SWT.DEFAULT) {
 192  
                                 hHint = Math.max(0, hHint - trim);
 193  
                         } else {
 194  
                                 wHint = Math.max(0, wHint - trim);
 195  
                         }
 196  
                 }
 197  
 
 198  
                 return c.computeSize(wHint, hHint);
 199  
         }
 200  
 
 201  
         public final ContainerWrapper getParent()
 202  
         {
 203  
                 return new SwtContainerWrapper(c.getParent());
 204  
         }
 205  
 
 206  
         public int getHorizontalScreenDPI()
 207  
         {
 208  
                 return c.getDisplay().getDPI().x;
 209  
         }
 210  
 
 211  
         public int getVerticalScreenDPI()
 212  
         {
 213  
                 return c.getDisplay().getDPI().y;
 214  
         }
 215  
 
 216  
         public final int getScreenWidth()
 217  
         {
 218  
                 return c.getDisplay().getBounds().width;
 219  
         }
 220  
 
 221  
         public final int getScreenHeight()
 222  
         {
 223  
                 return c.getDisplay().getBounds().height;
 224  
         }
 225  
 
 226  
         public final boolean hasBaseline()
 227  
         {
 228  
                 return false;
 229  
         }
 230  
 
 231  
         public final String getLinkId()
 232  
         {
 233  
                 return null;
 234  
         }
 235  
 
 236  
         public final void setBounds(int x, int y, int width, int height)
 237  
         {
 238  
                 c.setBounds(x, y, width, height);
 239  
         }
 240  
 
 241  
         public boolean isVisible()
 242  
         {
 243  
                 return c.getVisible();
 244  
         }
 245  
 
 246  
         public final int[] getVisualPadding()
 247  
         {
 248  
                 return null;
 249  
         }
 250  
 
 251  
         public static boolean isUseVisualPadding()
 252  
         {
 253  
                 return vp;
 254  
         }
 255  
 
 256  
         public static void setUseVisualPadding(boolean b)
 257  
         {
 258  
                 vp = b;
 259  
         }
 260  
 
 261  
         /** Sets if minimum size for SWT components should be preferred size (default, false) or 0.
 262  
          * @return <code>true</code> means minimum size is 0.
 263  
          */
 264  
         public static boolean isMinimumSizeZero()
 265  
         {
 266  
                 return mz;
 267  
         }
 268  
 
 269  
         /** Sets if minimum size for SWT components should be preferred size (default, false) or 0.
 270  
          * @param b <code>true</code> means minimum size is 0.
 271  
          */
 272  
         public static void setMinimumSizeZero(boolean b)
 273  
         {
 274  
                 mz = b;
 275  
         }
 276  
 
 277  
         public int getLayoutHashCode()
 278  
         {
 279  
                 if (c.isDisposed())
 280  
                         return -1;
 281  
 
 282  
                 Point sz = c.getSize();
 283  
                 Point p = c.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
 284  
                 int h = p.x + (p.y << 12) + (sz.x << 22) + (sz.y << 16);
 285  
 
 286  
                 if (c.isVisible())
 287  
                         h += 1324511;
 288  
 
 289  
                 String id = getLinkId();
 290  
                 if (id != null)
 291  
                         h += id.hashCode();
 292  
                 return h;
 293  
         }
 294  
 
 295  
         public final void paintDebugOutline()
 296  
         {
 297  
                 if (c.isDisposed())
 298  
                         return;
 299  
 
 300  
                 GC gc = new GC(c);
 301  
 
 302  
                 gc.setLineJoin(SWT.JOIN_MITER);
 303  
                 gc.setLineCap(SWT.CAP_SQUARE);
 304  
                 gc.setLineStyle(SWT.LINE_DOT);
 305  
 
 306  
                 gc.setForeground(DB_COMP_OUTLINE);
 307  
                 gc.drawRectangle(0, 0, getWidth() - 1, getHeight() - 1);
 308  
 
 309  
                 gc.dispose();
 310  
         }
 311  
 
 312  
         public int getComponetType(boolean disregardScrollPane)
 313  
         {
 314  
                 if (compType == TYPE_UNSET)
 315  
                         compType = checkType();
 316  
 
 317  
                 return compType;
 318  
         }
 319  
 
 320  
         private int checkType()
 321  
         {
 322  
                 int s = c.getStyle();
 323  
 
 324  
                 if (c instanceof Text || c instanceof StyledText) {
 325  
                         return (s & SWT.MULTI) > 0 ? TYPE_TEXT_AREA : TYPE_TEXT_FIELD;
 326  
                 } else if (c instanceof Label) {
 327  
                         return (s & SWT.SEPARATOR) > 0 ? TYPE_SEPARATOR : TYPE_LABEL;
 328  
                 } else if (c instanceof Button) {
 329  
                         if ((s & SWT.CHECK) > 0 || (s & SWT.RADIO) > 0)
 330  
                                 return TYPE_CHECK_BOX;
 331  
                         return TYPE_BUTTON;
 332  
                 } else if (c instanceof Canvas) {
 333  
                         return TYPE_PANEL;
 334  
                 } else if (c instanceof List) {
 335  
                         return TYPE_LIST;
 336  
                 } else if (c instanceof Table) {
 337  
                         return TYPE_TABLE;
 338  
                 } else if (c instanceof Spinner) {
 339  
                         return TYPE_SPINNER;
 340  
                 } else if (c instanceof ProgressBar) {
 341  
                         return TYPE_PROGRESS_BAR;
 342  
                 } else if (c instanceof Slider) {
 343  
                         return TYPE_SLIDER;
 344  
                 } else if (c instanceof Composite) {    // only AWT components is not containers.
 345  
                         return TYPE_CONTAINER;
 346  
                 }
 347  
                 return TYPE_UNKNOWN;
 348  
         }
 349  
 
 350  
         public final int hashCode()
 351  
         {
 352  
                 return c.hashCode();
 353  
         }
 354  
 
 355  
         public final boolean equals(Object o)
 356  
         {
 357  
                 if (o == null || o instanceof ComponentWrapper == false)
 358  
                         return false;
 359  
 
 360  
                 return getComponent().equals(((ComponentWrapper) o).getComponent());
 361  
         }
 362  
 }