PageRenderTime 59ms CodeModel.GetById 14ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. */
  20. package org.jedit.ruby.ri;
  21. import org.gjt.sp.jedit.jEdit;
  22. import org.gjt.sp.jedit.View;
  23. import org.gjt.sp.jedit.syntax.Token;
  24. import javax.swing.text.html.StyleSheet;
  25. import java.awt.Color;
  26. import java.util.Map;
  27. import java.util.HashMap;
  28. import java.util.Set;
  29. /**
  30. * @author robmckinnon at users.sourceforge.net
  31. */
  32. final class RDocStyleSheet extends StyleSheet {
  33. private final View view;
  34. public RDocStyleSheet(View view) {
  35. this.view = view;
  36. addRule(body());
  37. addRule(pre());
  38. addRule(preparam());
  39. addRule(hr());
  40. addRule(tt());
  41. addRule(em());
  42. }
  43. private Rule preparam() {
  44. Rule r = new Rule("pre.param");
  45. r.add("color", getColor("view.fgColor"));
  46. return r;
  47. }
  48. private void addRule(Rule rule) {
  49. super.addRule(rule.toString());
  50. }
  51. private Rule hr() {
  52. Rule r = new Rule("hr");
  53. r.add("border-width", "0px");
  54. r.add("height", "1px");
  55. r.add("background-color", getColor(Token.KEYWORD2));
  56. return r;
  57. }
  58. private Rule body() {
  59. Rule r = new Rule("body");
  60. r.add("font-size", getViewFontSize());
  61. r.add("background-color", getColor("view.bgColor"));
  62. r.add("color", getColor("view.fgColor"));
  63. return r;
  64. }
  65. private Rule pre() {
  66. Rule r = new Rule("pre");
  67. r.add("font-family", "monospace");
  68. r.add("font-size", getViewFontSize() - 1);
  69. r.add("color", getColor(Token.KEYWORD2));
  70. return r;
  71. }
  72. private Rule tt() {
  73. return new Rule("tt", pre().attributes);
  74. }
  75. private Rule em() {
  76. Rule r = new Rule("em");
  77. // r.addMembers("font-style", "italic");
  78. r.add("color", getColor(Token.KEYWORD1));
  79. return r;
  80. }
  81. private static int getViewFontSize() {
  82. return jEdit.getFontProperty("view.font").getSize();
  83. }
  84. private String getColor(byte styleId) {
  85. Color color = view.getTextArea().getPainter().getStyles()[styleId].getForegroundColor();
  86. return getHexColor(color);
  87. }
  88. private static String getColor(String property) {
  89. Color color = jEdit.getColorProperty(property);
  90. return getHexColor(color);
  91. }
  92. private static String getHexColor(Color color) {
  93. StringBuffer buffer = new StringBuffer("#");
  94. buffer.append(getHex(color.getRed()));
  95. buffer.append(getHex(color.getGreen()));
  96. buffer.append(getHex(color.getBlue()));
  97. return buffer.toString();
  98. }
  99. private static String getHex(int part) {
  100. String hex = Integer.toHexString(part);
  101. int length = hex.length();
  102. if (length == 0) {
  103. return "00";
  104. } else if(length == 1) {
  105. return "0" + hex;
  106. } else {
  107. return hex;
  108. }
  109. }
  110. private static final class Rule {
  111. private final String name;
  112. private final Map<String, String> attributes;
  113. public Rule(String name, Map<String, String> attributes) {
  114. this.name = name;
  115. this.attributes = attributes;
  116. }
  117. public Rule(String name) {
  118. this(name, new HashMap<String, String>());
  119. }
  120. public final void add(String attribute, int value) {
  121. attributes.put(attribute, "" + value);
  122. }
  123. public final void add(String attribute, String value) {
  124. attributes.put(attribute, value);
  125. }
  126. public final String toString() {
  127. StringBuffer buffer = new StringBuffer(name + " {\n");
  128. Set<String> keys = attributes.keySet();
  129. for (String attribute : keys) {
  130. String value = attributes.get(attribute);
  131. buffer.append(" ").append(attribute).append(": ").append(value).append(";\n");
  132. }
  133. buffer.append("}");
  134. return buffer.toString();
  135. }
  136. }
  137. }