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