Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/generatenimbus/UIRegion.java
32287 views
/*1* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package build.tools.generatenimbus;2627import java.util.ArrayList;28import java.util.List;29import java.util.Map;3031import javax.xml.bind.annotation.XmlAttribute;32import javax.xml.bind.annotation.XmlElement;33import javax.xml.bind.annotation.XmlElementWrapper;34import javax.xml.bind.annotation.XmlElements;3536class UIRegion {37@XmlAttribute protected String name;38@XmlAttribute protected String key;39@XmlAttribute private boolean opaque = false;4041@XmlElement private Insets contentMargins = new Insets(0, 0, 0, 0);4243@XmlElement(name="state")44@XmlElementWrapper(name="backgroundStates")45protected List<UIState> backgroundStates = new ArrayList<UIState>();46public List<UIState> getBackgroundStates() { return backgroundStates; }4748@XmlElement(name="state")49@XmlElementWrapper(name="foregroundStates")50protected List<UIState> foregroundStates = new ArrayList<UIState>();51public List<UIState> getForegroundStates() { return foregroundStates; }5253@XmlElement(name="state")54@XmlElementWrapper(name="borderStates")55protected List<UIState> borderStates = new ArrayList<UIState>();56public List<UIState> getBorderStates() { return borderStates; }5758@XmlElement private UIStyle style = new UIStyle();5960@XmlElements({61@XmlElement(name = "region", type = UIRegion.class),62@XmlElement(name = "uiComponent", type = UIComponent.class),63@XmlElement(name = "uiIconRegion", type = UIIconRegion.class)64})65@XmlElementWrapper(name="regions")66private List<UIRegion> subRegions = new ArrayList<UIRegion>();67public List<UIRegion> getSubRegions() { return subRegions; }6869protected void initStyles(UIStyle parentStyle) {70style.setParentStyle(parentStyle);71for (UIState state: backgroundStates) {72state.getStyle().setParentStyle(this.style);73}74for (UIState state: foregroundStates) {75state.getStyle().setParentStyle(this.style);76}77for (UIState state: borderStates) {78state.getStyle().setParentStyle(this.style);79}80for (UIRegion region: subRegions) {81region.initStyles(this.style);82}83}8485public String getKey() {86return key == null || "".equals(key) ? name : key;87}8889private boolean hasCanvas() {90for (UIState s : backgroundStates) {91if (s.hasCanvas()) return true;92}93for (UIState s : borderStates) {94if (s.hasCanvas()) return true;95}96for (UIState s : foregroundStates) {97if (s.hasCanvas()) return true;98}99for (UIRegion r: subRegions) {100if (r.hasCanvas()) return true;101}102return false;103}104105public void write(StringBuilder sb, StringBuilder styleBuffer,106UIComponent comp, String prefix, String pkg) {107// write content margins108sb.append(String.format(" d.put(\"%s.contentMargins\", %s);\n",109prefix, contentMargins.write(true)));110// write opaque if true111if (opaque) {112sb.append(String.format(" d.put(\"%s.opaque\", Boolean.TRUE);\n", prefix));113}114115// register component with LAF116String regionCode = "Region." + Utils.regionNameToCaps(name);117styleBuffer.append(String.format(" register(%s, \"%s\");\n",118regionCode, prefix));119120//write the State, if necessary121StringBuffer regString = new StringBuffer();122List<UIStateType> types = comp.getStateTypes();123if (types != null && types.size() > 0) {124for (UIStateType type : types) {125regString.append(type.getKey());126regString.append(",");127}128//remove the last ","129regString.deleteCharAt(regString.length() - 1);130}131132if (! regString.equals("Enabled,MouseOver,Pressed,Disabled,Focused,Selected,Default") && types.size() > 0) {133//there were either custom states, or the normal states were in a custom order134//so go ahead and write out prefix.State135sb.append(String.format(" d.put(\"%s.States\", \"%s\");\n",136prefix, regString));137}138139// write out any custom states, if necessary140for (UIStateType type : types) {141String synthState = type.getKey();142if (! "Enabled".equals(synthState) &&143! "MouseOver".equals(synthState) &&144! "Pressed".equals(synthState) &&145! "Disabled".equals(synthState) &&146! "Focused".equals(synthState) &&147! "Selected".equals(synthState) &&148! "Default".equals(synthState)) {149150//what we have here, gentlemen, is a bona-fide custom state.151//if the type is not one of the standard types, then construct a name for152//the new type, and write out a new subclass of State.153String className = Utils.normalize(prefix) + synthState + "State";154sb.append(String.format(" d.put(\"%s.%s\", new %s());\n",155prefix, synthState, className));156157String body = type.getCodeSnippet();158Map<String, String> variables = Generator.getVariables();159variables.put("STATE_NAME", className);160variables.put("STATE_KEY", synthState);161variables.put("BODY", body);162163Generator.writeSrcFile("StateImpl", variables, className);164}165}166167// write style168sb.append(style.write(prefix + '.'));169170String fileName = Utils.normalize(prefix) + "Painter";171boolean hasCanvas = hasCanvas();172if (hasCanvas) {173PainterGenerator.writePainter(this, fileName);174}175// write states ui defaults176for (UIState state : backgroundStates) {177state.write(sb, prefix, pkg, fileName, "background");178}179for (UIState state : foregroundStates) {180state.write(sb, prefix, pkg, fileName, "foreground");181}182for (UIState state : borderStates) {183state.write(sb, prefix, pkg, fileName, "border");184}185186// handle sub regions187for (UIRegion subreg : subRegions) {188String p = prefix;189if (! (subreg instanceof UIIconRegion)) {190p = prefix + ":" + Utils.escape(subreg.getKey());191}192UIComponent c = comp;193if (subreg instanceof UIComponent) {194c = (UIComponent) subreg;195}196subreg.write(sb, styleBuffer, c, p, pkg);197}198}199}200201202