001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.swing.plaf.synth;
019:
020: import java.util.ArrayList;
021: import java.util.regex.Pattern;
022:
023: import javax.swing.JComponent;
024:
025: /**
026: * The defaultStyleFactory returns styles fills up by putStyle(String, String,
027: * SynthStyle) method
028: */
029: class DefaultStyleFactory extends SynthStyleFactory {
030:
031: /**
032: * List that containing styles
033: */
034: private final ArrayList<StyleInfo> styles = new ArrayList<StyleInfo>();
035:
036: @Override
037: public SynthStyle getStyle(JComponent comp, Region reg) {
038:
039: SynthStyle result = findStyle(comp, reg);
040:
041: if (result == null) {
042: return SynthStyle.NULL_STYLE;
043: }
044:
045: return result;
046: }
047:
048: public void putStyle(String bindType, String bindKey,
049: SynthStyle style) {
050:
051: styles.add(new StyleInfo(bindType, bindKey, style));
052: }
053:
054: private SynthStyle findStyle(JComponent comp, Region reg) {
055:
056: String name = comp.getName();
057: SynthStyle foundByName = null;
058: SynthStyle foundByRegion = null;
059:
060: for (StyleInfo candidate : styles) {
061:
062: if ((name != null)
063: && ("name".equals(candidate.getBindType())) //$NON-NLS-1$
064: && Pattern.matches(name, candidate.getBindKey())) {
065: foundByName = candidate.getStyle();
066: }
067:
068: if (("region".equals(candidate.getBindType())) //$NON-NLS-1$
069: && reg.getName().equals(candidate.getBindKey())) {
070: foundByRegion = candidate.getStyle();
071: }
072: }
073:
074: // foundByName has a priority
075: if (foundByName == null) {
076:
077: return foundByRegion;
078: }
079:
080: return foundByName;
081: }
082:
083: /**
084: * StyleInfo can be used for representing the style and for finding a style
085: * from list
086: */
087: private static class StyleInfo {
088:
089: private final String bindType;
090:
091: private final String bindKey;
092:
093: private final SynthStyle currentStyle;
094:
095: /**
096: * Note that the constructor lowercases type and key for the StyleKey
097: */
098: public StyleInfo(String bindType, String bindKey,
099: SynthStyle currentStyle) {
100: this .bindKey = bindKey.toLowerCase();
101: this .bindType = bindType.toLowerCase();
102: this .currentStyle = currentStyle;
103: }
104:
105: public String getBindType() {
106: return bindType;
107: }
108:
109: public String getBindKey() {
110: return bindKey;
111: }
112:
113: public SynthStyle getStyle() {
114: return currentStyle;
115: }
116:
117: @Override
118: public String toString() {
119: return "KEY[" + bindType + " " + bindKey + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
120: }
121:
122: }
123:
124: }
|