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 import java.util.ArrayList;
  31 import java.util.List;
  32 
  33 class UIStyle {
  34 
  35     public enum CacheMode {
  36         NO_CACHING, FIXED_SIZES, NINE_SQUARE_SCALE
  37     }
  38 
  39     private UIColor textForeground = null;
  40     private boolean textForegroundInherited = true;
  41 
  42     private UIColor textBackground = null;
  43     private boolean textBackgroundInherited = true;
  44 
  45     private UIColor background = null;
  46     private boolean backgroundInherited = true;
  47 
  48     private boolean cacheSettingsInherited = true;
  49     CacheMode cacheMode = CacheMode.FIXED_SIZES;
  50     String maxHozCachedImgScaling = "1.0";
  51     String maxVertCachedImgScaling = "1.0";
  52 
  53     private List<UIProperty> uiProperties = new ArrayList<>();
  54 
  55     UIStyle() {
  56     }
  57 
  58     UIStyle(XMLStreamReader reader) throws XMLStreamException {
  59         while (reader.hasNext()) {
  60             int eventType = reader.next();
  61             switch (eventType) {
  62                 case XMLStreamReader.START_ELEMENT:
  63                     switch (reader.getLocalName()) {
  64                         case "textForeground":
  65                             textForeground = new UIColor(reader);
  66                             break;
  67                         case "textBackground":
  68                             textBackground = new UIColor(reader);
  69                             break;
  70                         case "background":
  71                             background = new UIColor(reader);
  72                             break;
  73                         case "uiProperty":
  74                             uiProperties.add(new UIProperty(reader));
  75                             break;
  76                         case "inherit-textForeground":
  77                             textForegroundInherited = Boolean.parseBoolean(reader.getElementText());
  78                             break;
  79                         case "inherit-textBackground":
  80                             textBackgroundInherited = Boolean.parseBoolean(reader.getElementText());
  81                             break;
  82                         case "cacheSettingsInherited":
  83                             cacheSettingsInherited = Boolean.parseBoolean(reader.getElementText());
  84                             break;
  85                         case "inherit-background":
  86                             backgroundInherited = Boolean.parseBoolean(reader.getElementText());
  87                             break;
  88                         case "cacheMode":
  89                             cacheMode = CacheMode.valueOf(reader.getElementText());
  90                             break;
  91                         case "maxHozCachedImgScaling":
  92                             maxHozCachedImgScaling = reader.getElementText();
  93                             break;
  94                         case "maxVertCachedImgScaling":
  95                             maxVertCachedImgScaling = reader.getElementText();
  96                             break;
  97                     }
  98                     break;
  99                 case XMLStreamReader.END_ELEMENT:
 100                     switch (reader.getLocalName()) {
 101                         case "style":
 102                             return;
 103                     }
 104                     break;
 105             }
 106         }
 107     }
 108 
 109     private UIStyle parentStyle = null;
 110     public void setParentStyle(UIStyle parentStyle) {
 111         this.parentStyle = parentStyle;
 112     }
 113 
 114     public CacheMode getCacheMode() {
 115         if (cacheSettingsInherited) {
 116             return (parentStyle == null ?
 117                 CacheMode.FIXED_SIZES : parentStyle.getCacheMode());
 118         } else {
 119             return cacheMode;
 120         }
 121     }
 122 
 123     public String getMaxHozCachedImgScaling() {
 124         if (cacheSettingsInherited) {
 125             return (parentStyle == null ?
 126                 "1.0" : parentStyle.getMaxHozCachedImgScaling());
 127         } else {
 128             return maxHozCachedImgScaling;
 129         }
 130     }
 131 
 132     public String getMaxVertCachedImgScaling() {
 133         if (cacheSettingsInherited) {
 134             return (parentStyle == null ?
 135                 "1.0" : parentStyle.getMaxVertCachedImgScaling());
 136         } else {
 137             return maxVertCachedImgScaling;
 138         }
 139     }
 140 
 141     public String write(String prefix) {
 142         StringBuilder sb = new StringBuilder();
 143         if (! textForegroundInherited) {
 144             sb.append(String.format("        addColor(d, \"%s%s\", %s);\n",
 145                     prefix, "textForeground", textForeground.getValue().write()));
 146         }
 147         if (! textBackgroundInherited) {
 148             sb.append(String.format("        addColor(d, \"%s%s\", %s);\n",
 149                     prefix, "textBackground", textBackground.getValue().write()));
 150         }
 151         if (! backgroundInherited) {
 152             sb.append(String.format("        addColor(d, \"%s%s\", %s);\n",
 153                     prefix, "background", background.getValue().write()));
 154         }
 155         for (UIProperty property : uiProperties) {
 156             sb.append(property.write(prefix));
 157         }
 158         return sb.toString();
 159     }
 160 }