UIRegion.java revision 8845:4be14673b9bf
1/*
2 * Copyright (c) 2002, 2012, 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
26package build.tools.generatenimbus;
27
28import java.util.ArrayList;
29import java.util.List;
30import java.util.Map;
31
32import javax.xml.bind.annotation.XmlAttribute;
33import javax.xml.bind.annotation.XmlElement;
34import javax.xml.bind.annotation.XmlElementWrapper;
35import javax.xml.bind.annotation.XmlElements;
36
37class UIRegion {
38    @XmlAttribute protected String name;
39    @XmlAttribute protected String key;
40    @XmlAttribute private boolean opaque = false;
41
42    @XmlElement private Insets contentMargins = new Insets(0, 0, 0, 0);
43
44    @XmlElement(name="state")
45    @XmlElementWrapper(name="backgroundStates")
46    protected List<UIState> backgroundStates = new ArrayList<UIState>();
47    public List<UIState> getBackgroundStates() { return backgroundStates; }
48
49    @XmlElement(name="state")
50    @XmlElementWrapper(name="foregroundStates")
51    protected List<UIState> foregroundStates = new ArrayList<UIState>();
52    public List<UIState> getForegroundStates() { return foregroundStates; }
53
54    @XmlElement(name="state")
55    @XmlElementWrapper(name="borderStates")
56    protected List<UIState> borderStates = new ArrayList<UIState>();
57    public List<UIState> getBorderStates() { return borderStates; }
58
59    @XmlElement private UIStyle style = new UIStyle();
60
61    @XmlElements({
62        @XmlElement(name = "region", type = UIRegion.class),
63        @XmlElement(name = "uiComponent", type = UIComponent.class),
64        @XmlElement(name = "uiIconRegion", type = UIIconRegion.class)
65    })
66    @XmlElementWrapper(name="regions")
67    private List<UIRegion> subRegions = new ArrayList<UIRegion>();
68    public List<UIRegion> getSubRegions() { return subRegions; }
69
70    protected void initStyles(UIStyle parentStyle) {
71        style.setParentStyle(parentStyle);
72        for (UIState state: backgroundStates) {
73            state.getStyle().setParentStyle(this.style);
74        }
75        for (UIState state: foregroundStates) {
76            state.getStyle().setParentStyle(this.style);
77        }
78        for (UIState state: borderStates) {
79            state.getStyle().setParentStyle(this.style);
80        }
81        for (UIRegion region: subRegions) {
82            region.initStyles(this.style);
83        }
84    }
85
86    public String getKey() {
87        return key == null || "".equals(key) ? name : key;
88    }
89
90    private boolean hasCanvas() {
91        for (UIState s : backgroundStates) {
92            if (s.hasCanvas()) return true;
93        }
94        for (UIState s : borderStates) {
95            if (s.hasCanvas()) return true;
96        }
97        for (UIState s : foregroundStates) {
98            if (s.hasCanvas()) return true;
99        }
100        for (UIRegion r: subRegions) {
101            if (r.hasCanvas()) return true;
102        }
103        return false;
104    }
105
106    public void write(StringBuilder sb, StringBuilder styleBuffer,
107                      UIComponent comp, String prefix, String pkg) {
108        // write content margins
109        sb.append(String.format("        d.put(\"%s.contentMargins\", %s);\n",
110                                prefix, contentMargins.write(true)));
111        // write opaque if true
112        if (opaque) {
113            sb.append(String.format("        d.put(\"%s.opaque\", Boolean.TRUE);\n", prefix));
114        }
115
116        // register component with LAF
117        String regionCode = "Region." + Utils.regionNameToCaps(name);
118        styleBuffer.append(String.format("        register(%s, \"%s\");\n",
119                                         regionCode, prefix));
120
121        //write the State, if necessary
122        StringBuffer regString = new StringBuffer();
123        List<UIStateType> types = comp.getStateTypes();
124        if (types != null && types.size() > 0) {
125            for (UIStateType type : types) {
126                regString.append(type.getKey());
127                regString.append(",");
128            }
129            //remove the last ","
130            regString.deleteCharAt(regString.length() - 1);
131        }
132
133        if (! regString.equals("Enabled,MouseOver,Pressed,Disabled,Focused,Selected,Default") && types.size() > 0) {
134            //there were either custom states, or the normal states were in a custom order
135            //so go ahead and write out prefix.State
136            sb.append(String.format("        d.put(\"%s.States\", \"%s\");\n",
137                                    prefix, regString));
138        }
139
140        // write out any custom states, if necessary
141        for (UIStateType type : types) {
142            String synthState = type.getKey();
143            if (! "Enabled".equals(synthState) &&
144                ! "MouseOver".equals(synthState) &&
145                ! "Pressed".equals(synthState) &&
146                ! "Disabled".equals(synthState) &&
147                ! "Focused".equals(synthState) &&
148                ! "Selected".equals(synthState) &&
149                ! "Default".equals(synthState)) {
150
151                //what we have here, gentlemen, is a bona-fide custom state.
152                //if the type is not one of the standard types, then construct a name for
153                //the new type, and write out a new subclass of State.
154                String className = Utils.normalize(prefix) + synthState + "State";
155                sb.append(String.format("        d.put(\"%s.%s\", new %s());\n",
156                                        prefix, synthState, className));
157
158                String body = type.getCodeSnippet();
159                Map<String, String> variables = Generator.getVariables();
160                variables.put("STATE_NAME", className);
161                variables.put("STATE_KEY", synthState);
162                variables.put("BODY", body);
163
164                Generator.writeSrcFile("StateImpl", variables, className);
165            }
166        }
167
168        // write style
169        sb.append(style.write(prefix + '.'));
170
171        String fileName = Utils.normalize(prefix) + "Painter";
172        boolean hasCanvas = hasCanvas();
173        if (hasCanvas) {
174            PainterGenerator.writePainter(this, fileName);
175        }
176        // write states ui defaults
177        for (UIState state : backgroundStates) {
178            state.write(sb, prefix, pkg, fileName, "background");
179        }
180        for (UIState state : foregroundStates) {
181            state.write(sb, prefix, pkg, fileName, "foreground");
182        }
183        for (UIState state : borderStates) {
184            state.write(sb, prefix, pkg, fileName, "border");
185        }
186
187        // handle sub regions
188        for (UIRegion subreg : subRegions) {
189            String p = prefix;
190            if (! (subreg instanceof UIIconRegion)) {
191                p = prefix + ":" + Utils.escape(subreg.getKey());
192            }
193            UIComponent c = comp;
194            if (subreg instanceof UIComponent) {
195                c = (UIComponent) subreg;
196            }
197            subreg.write(sb, styleBuffer, c, p, pkg);
198        }
199    }
200}
201