001/* 002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * bstefanescu 016 */ 017package org.nuxeo.shell.swing; 018 019import java.awt.Color; 020import java.awt.Font; 021import java.util.HashMap; 022import java.util.Map; 023 024import org.nuxeo.shell.ShellException; 025import org.nuxeo.shell.utils.StringUtils; 026 027/** 028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 029 */ 030public class Theme { 031 032 protected static Map<String, Theme> themes = new HashMap<String, Theme>(); 033 034 protected static String defTheme = "Default"; 035 036 protected static Font defFont = new Font(Font.MONOSPACED, Font.PLAIN, 14); 037 038 static { 039 themes.put("Green", new Theme("Default", defFont, Color.GREEN, Color.BLACK)); 040 themes.put("Linux", new Theme("Linux", defFont, Color.WHITE, Color.BLACK)); 041 themes.put("Default", new Theme("White", defFont, Color.BLACK, Color.WHITE)); 042 } 043 044 public static void addTheme(Theme theme) { 045 themes.put(theme.getName(), theme); 046 } 047 048 public static Theme getTheme(String name) { 049 return themes.get(name); 050 } 051 052 public static Theme[] getThemes() { 053 return themes.values().toArray(new Theme[themes.size()]); 054 } 055 056 public static Font getFont(String desc) { 057 return Font.decode(desc); 058 } 059 060 public static int getFontStyle(String weight) { 061 if ("bold".equals(weight)) { 062 return Font.BOLD; 063 } else if ("italic".equals(weight)) { 064 return Font.ITALIC; 065 } else { 066 return Font.PLAIN; 067 } 068 } 069 070 public static String getFontStyleName(int code) { 071 switch (code) { 072 case Font.BOLD: 073 return "bold"; 074 case Font.ITALIC: 075 return "italic"; 076 case Font.PLAIN: 077 return "plain"; 078 default: 079 if (code == (Font.BOLD | Font.ITALIC)) { 080 return "bolditalic"; 081 } 082 return "plain"; 083 } 084 } 085 086 public static String getFontString(Font font) { 087 return font.getName().concat("-").concat(getFontStyleName(font.getStyle())).concat("-").concat( 088 String.valueOf(font.getSize())); 089 } 090 091 public static String getColorName(Color color) { 092 String r = Integer.toHexString(color.getRed()); 093 if (r.length() == 1) { 094 r = "0" + r; 095 } 096 String g = Integer.toHexString(color.getGreen()); 097 if (g.length() == 1) { 098 g = "0" + g; 099 } 100 String b = Integer.toHexString(color.getBlue()); 101 if (b.length() == 1) { 102 b = "0" + b; 103 } 104 return r + g + b; 105 } 106 107 public static Color getColor(String rgb) { 108 if (rgb.startsWith("#")) { 109 rgb = rgb.substring(1); 110 } 111 if (rgb.length() != 6) { 112 throw new ShellException("Invalid color: " + rgb 113 + ". Should be #RRGGBB in hexa. The # character may be omited."); 114 } 115 String r = rgb.substring(0, 2); 116 String g = rgb.substring(2, 4); 117 String b = rgb.substring(4); 118 return new Color(Integer.parseInt(r, 16), Integer.parseInt(g, 16), Integer.parseInt(b, 16)); 119 } 120 121 protected String name; 122 123 protected Color bg; 124 125 protected Color fg; 126 127 protected Font font; 128 129 public Theme(String name, Font font, Color fg, Color bg) { 130 this.name = name; 131 this.font = font; 132 this.bg = bg; 133 this.fg = fg; 134 } 135 136 public void setName(String name) { 137 this.name = name; 138 } 139 140 public String getName() { 141 return name; 142 } 143 144 public Font getFont() { 145 return font; 146 } 147 148 public Color getBgColor() { 149 return bg; 150 } 151 152 public Color getFgColor() { 153 return fg; 154 } 155 156 public void setFgColor(Color fg) { 157 this.fg = fg; 158 } 159 160 public void setFont(Font font) { 161 this.font = font; 162 } 163 164 public void setBgColor(Color bg) { 165 this.bg = bg; 166 } 167 168 public static Theme fromString(String name, String expr) { 169 String[] ar = StringUtils.split(expr, ';', true); 170 if (ar.length != 3) { 171 throw new ShellException("Bad theme expression: " + expr); 172 } 173 Font font = Theme.getFont(ar[0]); 174 Color color = Theme.getColor(ar[1]); 175 Color bgcolor = Theme.getColor(ar[2]); 176 return new Theme(name, font, color, bgcolor); 177 } 178 179 public String toString() { 180 return getFontString(font).concat("; ").concat(getColorName(fg)).concat("; ").concat(getColorName(bg)); 181 } 182 183}