001 package nl.cwi.sen1.gui.plugin.editor; 002 003 import javax.swing.text.Style; 004 import javax.swing.text.StyleConstants; 005 006 import nl.cwi.sen1.configapi.types.Color; 007 import nl.cwi.sen1.configapi.types.Property; 008 import nl.cwi.sen1.configapi.types.PropertyList; 009 import nl.cwi.sen1.configapi.types.TextAttribute; 010 import nl.cwi.sen1.configapi.types.TextAttributeMap; 011 import nl.cwi.sen1.configapi.types.TextCategoryName; 012 import nl.cwi.sen1.configapi.types.TextStyle; 013 import nl.cwi.sen1.configapi.types.property.TextCategory; 014 015 public class StyleRegistrar { 016 private static final String FOCUS_STYLE_NAME = "***focus***"; 017 018 private static final String SELECTION_STYLE_NAME = "***selection***"; 019 020 static public void registerTextCategories(EditorPane editor, 021 PropertyList categories) { 022 editor.unsetStyles(); 023 024 for (; !categories.isEmpty(); categories = categories.getTail()) { 025 Property prop = categories.getHead(); 026 027 if (prop.isTextCategory()) { 028 registerTextCategory(editor, (TextCategory) prop); 029 } 030 } 031 } 032 033 static private void registerTextCategory(EditorPane editor, 034 TextCategory category) { 035 TextCategoryName type = category.getCategory(); 036 TextAttributeMap attrs = category.getMap(); 037 038 if (type.isExtern()) { 039 String name = category.getCategory().getName(); 040 registerAttributes(editor, name, attrs); 041 } else if (type.isNormal()) { 042 setStyleAttributes(editor, attrs, editor.getDefaultStyle()); 043 } else if (type.isSelection()) { 044 Style style = registerAttributes(editor, SELECTION_STYLE_NAME, 045 attrs); 046 editor.setSelectedTextColor((java.awt.Color) style 047 .getAttribute(StyleConstants.Foreground)); 048 editor.setSelectionColor((java.awt.Color) style 049 .getAttribute(StyleConstants.Background)); 050 } else if (type.isFocus()) { 051 Style style = registerAttributes(editor, FOCUS_STYLE_NAME, attrs); 052 editor.setFocusColor((java.awt.Color) style 053 .getAttribute(StyleConstants.Background)); 054 } 055 } 056 057 static private Style registerAttributes(EditorPane editor, String name, 058 TextAttributeMap attrs) { 059 Style style = editor.addStyle(name, editor.getDefaultStyle()); 060 setStyleAttributes(editor, attrs, style); 061 return style; 062 } 063 064 private static void setStyleAttributes(EditorPane editor, TextAttributeMap attrs, Style style) { 065 for (; !attrs.isEmpty(); attrs = attrs.getTail()) { 066 TextAttribute attr = attrs.getHead(); 067 068 if (attr.isForegroundColor()) { 069 StyleConstants.setForeground(style, convertColor(attr 070 .getColor())); 071 } else if (attr.isBackgroundColor()) { 072 if (convertColor(attr.getColor()).equals( 073 editor.getBackgroundColor())) { 074 // the linehighlight is drawed in a lower layer than the 075 // text, 076 // so if the textbackground is the same as the background of 077 // the 078 // editor we need to remove the background attribute to 079 // prevent 080 // gaps in the linehighlight 081 style.removeAttribute(StyleConstants.Background); 082 } else { 083 StyleConstants.setBackground(style, convertColor(attr 084 .getColor())); 085 } 086 } else if (attr.isStyle()) { 087 TextStyle textStyle = attr.getStyle(); 088 089 if (textStyle.isBold()) { 090 StyleConstants.setBold(style, true); 091 } else if (textStyle.isItalics()) { 092 StyleConstants.setItalic(style, true); 093 } else if (textStyle.isUnderlined()) { 094 StyleConstants.setUnderline(style, true); 095 } 096 } else if (attr.isFont()) { 097 StyleConstants.setFontFamily(style, attr.getName()); 098 } else if (attr.isSize()) { 099 StyleConstants.setFontSize(style, attr.getPoint()); 100 } 101 } 102 } static private java.awt.Color convertColor(Color color) { 103 return new java.awt.Color(color.getRed(), color.getGreen(), color 104 .getBlue()); 105 } 106 }