1 36 37 40 41 42 import javax.swing.plaf.*; 43 import javax.swing.plaf.metal.*; 44 import javax.swing.*; 45 import javax.swing.border.*; 46 import java.awt.*; 47 import java.io.*; 48 import java.util.*; 49 50 68 public class PropertiesMetalTheme extends DefaultMetalTheme { 69 70 private String name = "Custom Theme"; 71 72 private ColorUIResource primary1; 73 private ColorUIResource primary2; 74 private ColorUIResource primary3; 75 76 private ColorUIResource secondary1; 77 private ColorUIResource secondary2; 78 private ColorUIResource secondary3; 79 80 private ColorUIResource black; 81 private ColorUIResource white; 82 83 84 89 public PropertiesMetalTheme( InputStream stream ) { 90 initColors(); 91 loadProperties(stream); 92 } 93 94 97 private void initColors() { 98 primary1 = super.getPrimary1(); 99 primary2 = super.getPrimary2(); 100 primary3 = super.getPrimary3(); 101 102 secondary1 = super.getSecondary1(); 103 secondary2 = super.getSecondary2(); 104 secondary3 = super.getSecondary3(); 105 106 black = super.getBlack(); 107 white = super.getWhite(); 108 } 109 110 114 private void loadProperties(InputStream stream) { 115 Properties prop = new Properties(); 116 try { 117 prop.load(stream); 118 } catch (IOException e) { 119 System.out.println(e); 120 } 121 122 Object tempName = prop.get("name"); 123 if (tempName != null) { 124 name = tempName.toString(); 125 } 126 127 Object colorString = null; 128 129 colorString = prop.get("primary1"); 130 if (colorString != null){ 131 primary1 = parseColor(colorString.toString()); 132 } 133 134 colorString = prop.get("primary2"); 135 if (colorString != null) { 136 primary2 = parseColor(colorString.toString()); 137 } 138 139 colorString = prop.get("primary3"); 140 if (colorString != null) { 141 primary3 = parseColor(colorString.toString()); 142 } 143 144 colorString = prop.get("secondary1"); 145 if (colorString != null) { 146 secondary1 = parseColor(colorString.toString()); 147 } 148 149 colorString = prop.get("secondary2"); 150 if (colorString != null) { 151 secondary2 = parseColor(colorString.toString()); 152 } 153 154 colorString = prop.get("secondary3"); 155 if (colorString != null) { 156 secondary3 = parseColor(colorString.toString()); 157 } 158 159 colorString = prop.get("black"); 160 if (colorString != null) { 161 black = parseColor(colorString.toString()); 162 } 163 164 colorString = prop.get("white"); 165 if (colorString != null) { 166 white = parseColor(colorString.toString()); 167 } 168 169 } 170 171 public String getName() { return name; } 172 173 protected ColorUIResource getPrimary1() { return primary1; } 174 protected ColorUIResource getPrimary2() { return primary2; } 175 protected ColorUIResource getPrimary3() { return primary3; } 176 177 protected ColorUIResource getSecondary1() { return secondary1; } 178 protected ColorUIResource getSecondary2() { return secondary2; } 179 protected ColorUIResource getSecondary3() { return secondary3; } 180 181 protected ColorUIResource getBlack() { return black; } 182 protected ColorUIResource getWhite() { return white; } 183 184 187 private ColorUIResource parseColor(String s) { 188 int red = 0; 189 int green = 0; 190 int blue = 0; 191 try { 192 StringTokenizer st = new StringTokenizer(s, ","); 193 194 red = Integer.parseInt(st.nextToken()); 195 green = Integer.parseInt(st.nextToken()); 196 blue = Integer.parseInt(st.nextToken()); 197 198 } catch (Exception e) { 199 System.out.println(e); 200 System.out.println("Couldn't parse color :" + s); 201 } 202 203 return new ColorUIResource(red, green, blue); 204 } 205 } 206 | Popular Tags |