1 /*
   2  * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package org.openjdk.buildtools.generatenimbus;
  27 
  28 import javax.xml.stream.XMLStreamException;
  29 import javax.xml.stream.XMLStreamReader;
  30 
  31 class UIProperty extends UIDefault<String> {
  32     public enum PropertyType {
  33         BOOLEAN, INT, FLOAT, DOUBLE, STRING, FONT, COLOR, INSETS, DIMENSION, BORDER
  34     }
  35     private PropertyType type;
  36 
  37     private Border border;
  38     private Dimension dimension;
  39     private Insets insets;
  40     private Matte matte;
  41     private Typeface typeface;
  42 
  43     UIProperty(XMLStreamReader reader) throws XMLStreamException {
  44         name = reader.getAttributeValue(null, "name");
  45         setValue(reader.getAttributeValue(null, "value"));
  46         try {
  47             type = PropertyType.valueOf(reader.getAttributeValue(null, "type"));
  48         } catch (Exception e) {}
  49 
  50         while (reader.hasNext()) {
  51             int eventType = reader.next();
  52             switch (eventType) {
  53                 case XMLStreamReader.START_ELEMENT:
  54                     switch (reader.getLocalName()) {
  55                         case "border":
  56                             border = new Border(reader);
  57                             break;
  58                         case "dimension":
  59                             dimension = new Dimension(reader);
  60                             break;
  61                         case "insets":
  62                             insets = new Insets(reader);
  63                             break;
  64                         case "matte":
  65                             matte = new Matte(reader);
  66                             break;
  67                         case "typeface":
  68                             typeface = new Typeface(reader);
  69                             break;
  70                     }
  71                     break;
  72                 case XMLStreamReader.END_ELEMENT:
  73                     switch (reader.getLocalName()) {
  74                         case "uiProperty":
  75                             return;
  76                     }
  77                     break;
  78             }
  79         }
  80     }
  81 
  82     @Override public void setValue(String value) {
  83         super.setValue(value);
  84     }
  85 
  86     public String write(String prefix) {
  87         switch (type) {
  88             case BOOLEAN:
  89                 return String.format("        d.put(\"%s%s\", Boolean.%s);\n",
  90                                      prefix, getName(), getValue().toUpperCase());  ///autobox
  91             case STRING:
  92                 return String.format("        d.put(\"%s%s\", \"%s\");\n",
  93                                      prefix, getName(), getValue());
  94             case INT:
  95                 return String.format("        d.put(\"%s%s\", Integer.valueOf(%s));\n",
  96                                      prefix, getName(), getValue());
  97             case FLOAT:
  98                 return String.format("        d.put(\"%s%s\", Float.valueOf(%sf));\n",
  99                                      prefix, getName(), getValue());
 100             case DOUBLE:
 101                 return String.format("        d.put(\"%s%s\", Double.valueOf(%s));\n",
 102                                      prefix, getName(), getValue());
 103             case COLOR:
 104                 return String.format("        addColor(d, \"%s%s\", %s);\n",
 105                                      prefix, getName(), matte.write());
 106             case FONT:
 107                 return String.format("        d.put(\"%s%s\", %s);\n",
 108                                      prefix, getName(), typeface.write());
 109             case INSETS:
 110                 return String.format("        d.put(\"%s%s\", %s);\n",
 111                                      prefix, getName(), insets.write(true));
 112             case DIMENSION:
 113                 return String.format("        d.put(\"%s%s\", new DimensionUIResource(%d, %d));\n",
 114                                      prefix, getName(), dimension.width, dimension.height);
 115             case BORDER:
 116                 return String.format("        d.put(\"%s%s\", new BorderUIResource(%s));\n",
 117                                      prefix, getName(), border.write());
 118             default:
 119                 return "###  Look, something's wrong with UIProperty.write()  $$$";
 120         }
 121     }
 122 }