Rycochet

New widget code...

Jan 23rd, 2012
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.89 KB | None | 0 0
  1.     /**
  2.      * Set a single attribute.
  3.      * @param key the attribute to set
  4.      * @param value the value to set it to
  5.      * @return if successful
  6.      */
  7.     boolean setAttr(Attr key, Object value);
  8.  
  9.     /**
  10.      * Get an attribute but return our own default if it doesn't exist.
  11.      * @param key the attribute to get
  12.      * @param def the default if it doesn't exist
  13.      * @return the value
  14.      */
  15.     Object getAttr(Attr key, Object def);
  16.  
  17.     /**
  18.      * Get an attribute, return null if not found.
  19.      * @param key the attribute to get
  20.      * @return the value
  21.      */
  22.     Object getAttr(Attr key);
  23.  
  24.     /**
  25.      * Check if an attribute has been set.
  26.      * @param key the attribute to check
  27.      * @return if it exists
  28.      */
  29.     boolean hasAttr(Attr key);
  30.  
  31.     /** All attribute enums need to implement this. */
  32.     interface Attr {
  33.  
  34.         /**
  35.          * Get the allowed types for an attribute.
  36.          * By default attributes should accept Integer, however String and Enum
  37.          * are both common types.
  38.          * @return the allowed class
  39.          */
  40.         Class getType();
  41.  
  42.         /**
  43.          * Get the default value for an attribute (if set).
  44.          * @return the default value
  45.          */
  46.         Object getDefault();
  47.     }
  48.  
  49.     /**
  50.      * Used for set/get etc.
  51.      */
  52.     enum WidgetAttr implements Attr {
  53.  
  54.         /** Widget top offset. */
  55.         TOP,
  56.         /** Widget right offset. */
  57.         RIGHT,
  58.         /** Widget bottom offset. */
  59.         BOTTOM,
  60.         /** Widget left offset. */
  61.         LEFT,
  62.         /** Widget width. */
  63.         WIDTH,
  64.         /** Widget minimum width. */
  65.         MIN_WIDTH,
  66.         /** Widget maximum width. */
  67.         MAX_WIDTH,
  68.         /** Widget height. */
  69.         HEIGHT,
  70.         /** Widget minimum height. */
  71.         MIN_HEIGHT,
  72.         /** Widget maximum height. */
  73.         MAX_HEIGHT,
  74.         /** Tooltip on mouse hover. */
  75.         TOOLTIP(String.class),
  76.         /** How to display the widget "box". */
  77.         DISPLAY(Display.class),
  78.         /** What to do when the content overflows the box. */
  79.         OVERFLOW(Overflow.class),
  80.         /** Where to layout. */
  81.         POSITION(Position.class),
  82.         /** Padding top size. */
  83.         PADDING_TOP,
  84.         /** Padding right size. */
  85.         PADDING_RIGHT,
  86.         /** Padding bottom size. */
  87.         PADDING_BOTTOM,
  88.         /** Padding left size. */
  89.         PADDING_LEFT,
  90.         /** Border top size. */
  91.         BORDER_TOP,
  92.         /** Border top color. */
  93.         BORDER_TOP_COLOR(Color.class),
  94.         /** Border top style. */
  95.         BORDER_TOP_STYLE(Border.class),
  96.         /** Border right size. */
  97.         BORDER_RIGHT,
  98.         /** Border right color. */
  99.         BORDER_RIGHT_COLOR(Color.class),
  100.         /** Border right style. */
  101.         BORDER_RIGHT_STYLE(Border.class),
  102.         /** Border bottom size. */
  103.         BORDER_BOTTOM,
  104.         /** Border bottom color. */
  105.         BORDER_BOTTOM_COLOR(Color.class),
  106.         /** Border bottom style. */
  107.         BORDER_BOTTOM_STYLE(Border.class),
  108.         /** Border left size. */
  109.         BORDER_LEFT,
  110.         /** Border left color. */
  111.         BORDER_LEFT_COLOR(Color.class),
  112.         /** Border left style. */
  113.         BORDER_LEFT_STYLE(Border.class),
  114.         /** Margin top size. */
  115.         MARGIN_TOP,
  116.         /** Margin right size. */
  117.         MARGIN_RIGHT,
  118.         /** Margin bottom size. */
  119.         MARGIN_BOTTOM,
  120.         /** Margin left size. */
  121.         MARGIN_LEFT,;
  122.         /** The allowed type of data - Integer.class as default. */
  123.         private final Class type;
  124.         /** The default value - null by default. */
  125.         private final Object def;
  126.  
  127.         /**
  128.          * Default type is Integer.
  129.          */
  130.         private WidgetAttr() {
  131.             this(Integer.class, null);
  132.         }
  133.  
  134.         /**
  135.          * Set the type explicitely.
  136.          * @param allow this class and subclasses only
  137.          */
  138.         private WidgetAttr(final Class allow) {
  139.             this(allow, null);
  140.         }
  141.  
  142.         /**
  143.          * Set the default value for an int attribute.
  144.          * @param defValue default value
  145.          */
  146.         private WidgetAttr(final int defValue) {
  147.             this(Integer.class, defValue);
  148.         }
  149.  
  150.         /**
  151.          * Set the type and default.
  152.          * @param allow class
  153.          * @param defValue default value
  154.          */
  155.         private WidgetAttr(final Class allow, final Object defValue) {
  156.             type = allow;
  157.             def = defValue;
  158.         }
  159.  
  160.         @Override
  161.         public final Class getType() {
  162.             return type;
  163.         }
  164.  
  165.         @Override
  166.         public final Object getDefault() {
  167.             return def;
  168.         }
  169.     }
Advertisement
Add Comment
Please, Sign In to add comment