PageRenderTime 125ms CodeModel.GetById 116ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/ri/RDocStyleSheet.java

#
Java | 162 lines | 114 code | 25 blank | 23 comment | 7 complexity | 5151a7f39fa156e8ea2fc9624e1d92eb MD5 | raw file
  1/*
  2 * RDocStyleSheet.java - 
  3 *
  4 * Copyright 2005 Robert McKinnon
  5 *
  6 * This program is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU General Public License
  8 * as published by the Free Software Foundation; either version 2
  9 * of the License, or any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 * GNU General Public License for more details.
 15 *
 16 * You should have received a copy of the GNU General Public License
 17 * along with this program; if not, write to the Free Software
 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 19 */
 20package org.jedit.ruby.ri;
 21
 22import org.gjt.sp.jedit.jEdit;
 23import org.gjt.sp.jedit.View;
 24import org.gjt.sp.jedit.syntax.Token;
 25
 26import javax.swing.text.html.StyleSheet;
 27import java.awt.Color;
 28import java.util.Map;
 29import java.util.HashMap;
 30import java.util.Set;
 31
 32/**
 33 * @author robmckinnon at users.sourceforge.net
 34 */
 35final class RDocStyleSheet extends StyleSheet {
 36
 37    private final View view;
 38
 39    public RDocStyleSheet(View view) {
 40        this.view = view;
 41        addRule(body());
 42        addRule(pre());
 43        addRule(preparam());
 44        addRule(hr());
 45        addRule(tt());
 46        addRule(em());
 47    }
 48
 49    private Rule preparam() {
 50        Rule r = new Rule("pre.param");
 51        r.add("color", getColor("view.fgColor"));
 52        return r;
 53    }
 54
 55    private void addRule(Rule rule) {
 56        super.addRule(rule.toString());
 57    }
 58
 59    private Rule hr() {
 60        Rule r = new Rule("hr");
 61        r.add("border-width", "0px");
 62        r.add("height", "1px");
 63        r.add("background-color", getColor(Token.KEYWORD2));
 64        return r;
 65    }
 66
 67    private Rule body() {
 68        Rule r = new Rule("body");
 69        r.add("font-size", getViewFontSize());
 70        r.add("background-color", getColor("view.bgColor"));
 71        r.add("color", getColor("view.fgColor"));
 72        return r;
 73    }
 74
 75    private Rule pre() {
 76        Rule r = new Rule("pre");
 77        r.add("font-family", "monospace");
 78        r.add("font-size", getViewFontSize() - 1);
 79        r.add("color", getColor(Token.KEYWORD2));
 80        return r;
 81    }
 82
 83    private Rule tt() {
 84        return new Rule("tt", pre().attributes);
 85    }
 86
 87    private Rule em() {
 88        Rule r = new Rule("em");
 89//        r.addMembers("font-style", "italic");
 90        r.add("color", getColor(Token.KEYWORD1));
 91        return r;
 92    }
 93
 94    private static int getViewFontSize() {
 95        return jEdit.getFontProperty("view.font").getSize();
 96    }
 97
 98    private String getColor(byte styleId) {
 99        Color color = view.getTextArea().getPainter().getStyles()[styleId].getForegroundColor();
100        return getHexColor(color);
101    }
102
103    private static String getColor(String property) {
104        Color color = jEdit.getColorProperty(property);
105        return getHexColor(color);
106    }
107
108    private static String getHexColor(Color color) {
109        StringBuffer buffer = new StringBuffer("#");
110        buffer.append(getHex(color.getRed()));
111        buffer.append(getHex(color.getGreen()));
112        buffer.append(getHex(color.getBlue()));
113        return buffer.toString();
114    }
115
116    private static String getHex(int part) {
117        String hex = Integer.toHexString(part);
118        int length = hex.length();
119        if (length == 0) {
120            return "00";
121        } else if(length == 1) {
122            return "0" + hex;
123        } else {
124            return hex;
125        }
126    }
127
128    private static final class Rule {
129
130        private final String name;
131        private final Map<String, String> attributes;
132
133        public Rule(String name, Map<String, String> attributes) {
134            this.name = name;
135            this.attributes = attributes;
136        }
137
138        public Rule(String name) {
139            this(name, new HashMap<String, String>());
140        }
141
142        public final void add(String attribute, int value) {
143            attributes.put(attribute, "" + value);
144        }
145
146        public final void add(String attribute, String value) {
147            attributes.put(attribute, value);
148        }
149
150        public final String toString() {
151            StringBuffer buffer = new StringBuffer(name + " {\n");
152            Set<String> keys = attributes.keySet();
153            for (String attribute : keys) {
154                String value = attributes.get(attribute);
155                buffer.append("    ").append(attribute).append(": ").append(value).append(";\n");
156            }
157            buffer.append("}");
158            return buffer.toString();
159        }
160    }
161
162}