1 7 package com.sun.java.swing.plaf.nimbus; 8 9 import java.util.HashMap ; 10 import java.util.Map ; 11 import javax.swing.JComponent ; 12 import javax.swing.plaf.synth.SynthConstants ; 13 14 55 public abstract class State<T extends JComponent >{ 56 static final Map <String , StandardState> standardStates = new HashMap <String , StandardState>(7); 57 static final State Enabled = new StandardState(SynthConstants.ENABLED); 58 static final State MouseOver = new StandardState(SynthConstants.MOUSE_OVER); 59 static final State Pressed = new StandardState(SynthConstants.PRESSED); 60 static final State Disabled = new StandardState(SynthConstants.DISABLED); 61 static final State Focused = new StandardState(SynthConstants.FOCUSED); 62 static final State Selected = new StandardState(SynthConstants.SELECTED); 63 static final State Default = new StandardState(SynthConstants.DEFAULT); 64 65 private String name; 66 67 82 protected State(String name) { 83 this.name = name; 84 } 85 86 @Override public String toString() { return name; } 87 88 108 boolean isInState(T c, int s) { 109 return isInState(c); 110 } 111 112 128 protected abstract boolean isInState(T c); 129 130 String getName() { return name; } 131 132 static boolean isStandardStateName(String name) { 133 return standardStates.containsKey(name); 134 } 135 136 static StandardState getStandardState(String name) { 137 return standardStates.get(name); 138 } 139 140 static final class StandardState extends State<JComponent > { 141 private int state; 142 143 private StandardState(int state) { 144 super(toString(state)); 145 this.state = state; 146 standardStates.put(getName(), this); 147 } 148 149 public int getState() { 150 return state; 151 } 152 153 @Override 154 boolean isInState(JComponent c, int s) { 155 return (s & state) == state; 156 } 157 158 @Override 159 protected boolean isInState(JComponent c) { 160 throw new AssertionError ("This method should never be called"); 161 } 162 163 private static String toString(int state) { 164 StringBuffer buffer = new StringBuffer (); 165 if ((state & SynthConstants.DEFAULT) == SynthConstants.DEFAULT) { 166 buffer.append("Default"); 167 } 168 if ((state & SynthConstants.DISABLED) == SynthConstants.DISABLED) { 169 if (buffer.length() > 0) buffer.append("+"); 170 buffer.append("Disabled"); 171 } 172 if ((state & SynthConstants.ENABLED) == SynthConstants.ENABLED) { 173 if (buffer.length() > 0) buffer.append("+"); 174 buffer.append("Enabled"); 175 } 176 if ((state & SynthConstants.FOCUSED) == SynthConstants.FOCUSED) { 177 if (buffer.length() > 0) buffer.append("+"); 178 buffer.append("Focused"); 179 } 180 if ((state & SynthConstants.MOUSE_OVER) == SynthConstants.MOUSE_OVER) { 181 if (buffer.length() > 0) buffer.append("+"); 182 buffer.append("MouseOver"); 183 } 184 if ((state & SynthConstants.PRESSED) == SynthConstants.PRESSED) { 185 if (buffer.length() > 0) buffer.append("+"); 186 buffer.append("Pressed"); 187 } 188 if ((state & SynthConstants.SELECTED) == SynthConstants.SELECTED) { 189 if (buffer.length() > 0) buffer.append("+"); 190 buffer.append("Selected"); 191 } 192 return buffer.toString(); 193 } 194 } 195 } 196 | Popular Tags |