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 build.tools.generatenimbus;
27 
28 import javax.xml.stream.XMLStreamException;
29 import javax.xml.stream.XMLStreamReader;
30 import java.util.ArrayList;
31 
32 public class SynthModel {
33     private UIStyle style;
34 
35     private ArrayList<UIColor> colors;
36 
37     private ArrayList<UIFont> fonts;
38 
39     private ArrayList<UIComponent> components;
40 
SynthModel(XMLStreamReader reader)41     SynthModel(XMLStreamReader reader) throws XMLStreamException {
42         while (reader.hasNext()) {
43             int eventType = reader.next();
44             switch (eventType) {
45                 case XMLStreamReader.START_ELEMENT:
46                     switch (reader.getLocalName()) {
47                         case "style":
48                             style = new UIStyle(reader);
49                             break;
50                         case "colors":
51                             colors = new ArrayList<>();
52                             break;
53                         case "fonts":
54                             fonts = new ArrayList<>();
55                             break;
56                         case "components":
57                             components = new ArrayList<>();
58                             break;
59                         case "uiColor":
60                             colors.add(new UIColor(reader));
61                             break;
62                         case "uiFont":
63                             fonts.add(new UIFont(reader));
64                             break;
65                         case "uiComponent":
66                             components.add(new UIComponent(reader));
67                             break;
68                     }
69                     break;
70                 case XMLStreamReader.END_ELEMENT:
71                     break;
72             }
73         }
74     }
75 
initStyles()76     public void initStyles() {
77         for (UIComponent c: components) {
78             c.initStyles(this.style);
79         }
80     }
81 
write(StringBuilder defBuffer, StringBuilder styleBuffer, String packageName)82     public void write(StringBuilder defBuffer, StringBuilder styleBuffer, String packageName) {
83         defBuffer.append("        //Color palette\n");
84         for (UIColor c: colors) defBuffer.append(c.write());
85         defBuffer.append('\n');
86 
87         defBuffer.append("        //Font palette\n");
88         defBuffer.append("        d.put(\"defaultFont\", new FontUIResource(defaultFont));\n");
89         for (UIFont f: fonts) defBuffer.append(f.write());
90         defBuffer.append('\n');
91 
92         defBuffer.append("        //Border palette\n");
93         defBuffer.append('\n');
94 
95         defBuffer.append("        //The global style definition\n");
96         defBuffer.append(style.write(""));
97         defBuffer.append('\n');
98 
99         for (UIComponent c: components) {
100             String prefix = Utils.escape(c.getKey());
101             defBuffer.append("        //Initialize ").append(prefix).append("\n");
102             c.write(defBuffer, styleBuffer, c, prefix, packageName);
103             defBuffer.append('\n');
104         }
105     }
106 }
107