Changeset 909


Ignore:
Timestamp:
Oct 27, 2010, 6:47:29 PM (7 years ago)
Author:
stefan
Message:

merged code for loading styles from xml files and fixed the bug that the global.properties cannot be viewed correctly

Location:
trunk/src/jlatexeditor
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/jlatexeditor/gproperties/GPropertiesStyles.java

    r744 r909  
    3333  public static final byte U_MISSPELLED = 101;
    3434
    35   private static Map<TextAttribute, Object>[] stylesMap = new Map[Byte.MAX_VALUE + 1];
     35  private static Map<TextAttribute, Object>[] stylesMap;
    3636
    3737  private static Map<String, Byte> name2Id = new HashMap<String, Byte>();
     
    4040
    4141  static {
    42     load();
     42    init();
     43    load();
    4344  }
    4445
     
    5051    name2Id.put("number", NUMBER);
    5152    name2Id.put("error", ERROR);
    52 
    53     for (int i = 0; i < stylesMap.length; i++) stylesMap[i] = null;
     53    name2Id.put("user", USER);
    5454  }
    5555
    56   public static void addStyles(SCEDocument document) {
    57     Map<TextAttribute, Object> styleText = stylesMap[TEXT];
    58     for (int id = 0; id < stylesMap.length; id++) {
    59       if (stylesMap[id] == null) continue;
    60       Map<TextAttribute, Object> style = document.addStyle((byte) id, styleText);
    61       style.putAll(stylesMap[id]);
    62     }
     56  public static void load() {
     57    stylesMap = StyleLoader.load(styleFile, name2Id);
     58  }
    6359
    64     //// underline styles ////
     60  public static void save(String fileName) {
     61    // TODO
     62  }
    6563
    66     // normal
    67     Map<TextAttribute, Object> underlineNormal = document.addStyle(U_NORMAL, null);
     64  public static void addStyles(SCEDocument document) {
     65    Map<TextAttribute, Object> styleText = stylesMap[TEXT];
     66    for (int id = 0; id < stylesMap.length; id++) {
     67      if (stylesMap[id] == null) continue;
     68      Map<TextAttribute, Object> style = document.addStyle((byte) id, styleText);
     69      style.putAll(stylesMap[id]);
     70    }
    6871
    69     // misspelling
    70     Map<TextAttribute, Object> underlineMisspelling = document.addStyle(U_MISSPELLED, null);
    71     underlineMisspelling.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_GRAY);
    72   }
     72    //// underline styles ////
    7373
    74   public static void load() {
    75     load(styleFile);
    76   }
     74    // normal
     75    Map<TextAttribute, Object> underlineNormal = document.addStyle(U_NORMAL, null);
    7776
    78   public static void load(String filename) {
    79     styleFile = filename;
    80     init();
    81 
    82     XMLParser xmlParser = new XMLParser();
    83     XMLDocument stylesDocument;
    84     try {
    85       stylesDocument = xmlParser.parse(StreamUtils.readFile(filename));
    86     } catch (Exception e) {
    87       e.printStackTrace();
    88       return;
    89     }
    90 
    91     XMLElement stylesXML = stylesDocument.getRootElement();
    92     if (!stylesXML.getName().equalsIgnoreCase("styles")) {
    93       System.out.println("Error in [styles].xml: root element must be 'styles'");
    94       return;
    95     }
    96 
    97     int userStyle = USER;
    98     for (XMLElement styleElement : stylesXML.getChildElements()) {
    99       Byte id = name2Id.get(styleElement.getAttribute("name"));
    100       if (id == null) {
    101         id = (byte) userStyle++;
    102         name2Id.put(styleElement.getAttribute("name"), id);
    103       }
    104 
    105       stylesMap[id] = getStyle(styleElement);
    106     }
    107   }
    108 
    109   public static void save(String fileName) {
    110     // TODO
    111   }
    112 
    113   private static Map<TextAttribute, Object> getStyle(XMLElement styleElement) {
    114     Map<TextAttribute, Object> style = new HashMap<TextAttribute, Object>();
    115 
    116     Font font = GProperties.getEditorFont();
    117     Font fontBold = GProperties.getEditorFont().deriveFont(Font.BOLD);
    118     Font fontItalic = GProperties.getEditorFont().deriveFont(Font.ITALIC);
    119 
    120     style.put(TextAttribute.FONT, font);
    121     if (styleElement.getAttribute("style").equals("bold")) style.put(TextAttribute.FONT, fontBold);
    122     if (styleElement.getAttribute("style").equals("italic")) style.put(TextAttribute.FONT, fontItalic);
    123 
    124     XMLElement foreground = styleElement.getChildElement("foreground");
    125     if (foreground != null) {
    126       style.put(TextAttribute.FOREGROUND, getColor(foreground.getChildElement("color")));
    127     }
    128 
    129     XMLElement background = styleElement.getChildElement("background");
    130     if (background != null) {
    131       style.put(TextAttribute.BACKGROUND, getColor(background.getChildElement("color")));
    132     }
    133 
    134     return style;
    135   }
    136 
    137   private static Color getColor(XMLElement color) {
    138     if (color.getAttribute("rgb") != null) {
    139       return new Color(Integer.parseInt(color.getAttribute("rgb"), 16));
    140     } else {
    141       int r = Integer.parseInt(color.getAttribute("r"));
    142       int g = Integer.parseInt(color.getAttribute("g"));
    143       int b = Integer.parseInt(color.getAttribute("b"));
    144       return new Color(r, g, b);
    145     }
    146   }
     77    // misspelling
     78    Map<TextAttribute, Object> underlineMisspelling = document.addStyle(U_MISSPELLED, null);
     79    underlineMisspelling.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_GRAY);
     80  }
    14781}
  • trunk/src/jlatexeditor/syntaxhighlighting/LatexStyles.java

    r869 r909  
    55package jlatexeditor.syntaxhighlighting;
    66
    7 import jlatexeditor.gproperties.GProperties;
    8 import my.XML.XMLDocument;
    9 import my.XML.XMLElement;
    10 import my.XML.XMLParser;
     7import jlatexeditor.gproperties.StyleLoader;
    118import sce.component.SCEDocument;
    12 import util.StreamUtils;
    139
    14 import java.awt.*;
    1510import java.awt.font.TextAttribute;
    1611import java.util.HashMap;
     
    3833  public static final byte U_MISSPELLED = 101;
    3934
    40   private static Map<TextAttribute, Object>[] stylesMap = new Map[Byte.MAX_VALUE + 1];
    41   private static Map<String, CommandStyle> commandsMap = new HashMap<String, CommandStyle>();
     35  private static Map<TextAttribute, Object>[] stylesMap;
    4236
    4337  private static Map<String, Byte> name2Id = new HashMap<String, Byte>();
    4438
    4539  private static String styleFile = "data/styles/user.xml";
    46   private static final CommandStyle NORMAL_COMMAND_STYLE = new CommandStyle(COMMAND, (byte) -1);
    4740
    4841  static {
     42    init();
    4943    load();
    5044  }
     
    6054    name2Id.put("todo", TODO);
    6155    name2Id.put("error", ERROR);
     56    name2Id.put("user", USER);
     57  }
    6258
    63     for (int i = 0; i < stylesMap.length; i++) stylesMap[i] = null;
    64   }
     59  public static void load() {
     60    stylesMap = StyleLoader.load(styleFile, name2Id);
     61  }
    6562
    6663  public static void addStyles(SCEDocument document) {
     
    8279  }
    8380
    84   public static CommandStyle getCommandStyle(String command) {
    85     CommandStyle style = commandsMap.get(command);
    86     return style != null ? style : NORMAL_COMMAND_STYLE;
    87   }
    88 
    8981  public static Byte getStyle(String style) {
    9082    return name2Id.get(style);
    9183  }
    9284
    93   public static void load() {
    94     load(styleFile);
    95   }
    96 
    97   public static void load(String filename) {
    98     styleFile = filename;
    99     init();
    100 
    101     XMLParser xmlParser = new XMLParser();
    102     XMLDocument stylesDocument;
    103     try {
    104       stylesDocument = xmlParser.parse(StreamUtils.readFile(filename));
    105     } catch (Exception e) {
    106       e.printStackTrace();
    107       return;
    108     }
    109 
    110     XMLElement stylesXML = stylesDocument.getRootElement();
    111     if (!stylesXML.getName().equalsIgnoreCase("styles")) {
    112       System.out.println("Error in [styles].xml: root element must be 'styles'");
    113       return;
    114     }
    115 
    116     int userStyle = USER;
    117     for (XMLElement styleElement : stylesXML.getChildElements()) {
    118       if (styleElement.getName().equals("special")) {
    119         for (XMLElement element : styleElement.getChildElements()) {
    120           if (element.getName().equals("command")) {
    121             commandsMap.put(element.getAttribute("name"), getCommandStyle(element));
    122           }
    123         }
    124         continue;
    125       }
    126 
    127       Byte id = name2Id.get(styleElement.getAttribute("name"));
    128       if (id == null) {
    129         id = (byte) userStyle++;
    130         name2Id.put(styleElement.getAttribute("name"), id);
    131       }
    132 
    133       stylesMap[id] = getStyle(styleElement);
    134     }
    135   }
    136 
    137   private static CommandStyle getCommandStyle(XMLElement element) {
    138     byte commandStyle = name2Id.get(element.getAttribute("style"));
    139     byte paramStyle = -1;
    140     if (element.getAttribute("paramStyle") != null) {
    141       paramStyle = name2Id.get(element.getAttribute("paramStyle"));
    142     }
    143     return new CommandStyle(commandStyle, paramStyle);
    144   }
    145 
    14685  public static void save(String fileName) {
    14786    // TODO
    14887  }
    149 
    150   private static Map<TextAttribute, Object> getStyle(XMLElement styleElement) {
    151     Map<TextAttribute, Object> style = new HashMap<TextAttribute, Object>();
    152 
    153     Font font = GProperties.getEditorFont();
    154     Font fontBold = GProperties.getEditorFont().deriveFont(Font.BOLD);
    155     Font fontItalic = GProperties.getEditorFont().deriveFont(Font.ITALIC);
    156     Font fontBoldItalic = GProperties.getEditorFont().deriveFont(Font.BOLD | Font.ITALIC);
    157 
    158     style.put(TextAttribute.FONT, font);
    159     if (styleElement.getAttribute("style").equals("bold")) style.put(TextAttribute.FONT, fontBold);
    160     if (styleElement.getAttribute("style").equals("italic")) style.put(TextAttribute.FONT, fontItalic);
    161     if (styleElement.getAttribute("style").equals("bold,italic") ||
    162             styleElement.getAttribute("style").equals("italic,bold")) style.put(TextAttribute.FONT, fontBoldItalic);
    163 
    164     String foreground = styleElement.getAttribute("foreground");
    165     if (foreground != null) {
    166       style.put(TextAttribute.FOREGROUND, hex2color(foreground));
    167     }
    168 
    169     String background = styleElement.getAttribute("background");
    170     if (background != null) {
    171       style.put(TextAttribute.BACKGROUND, hex2color(background));
    172     }
    173 
    174     return style;
    175   }
    176 
    177   private static Color hex2color(String hexColor) {
    178     return new Color(Integer.parseInt(hexColor, 16));
    179   }
    180 
    181   // (command style, parameter style) pair
    182   public static class CommandStyle {
    183     public byte commandStyle;
    184     public byte parameterStyle;
    185 
    186     public CommandStyle(byte commandStyle, byte parameterStyle) {
    187       this.commandStyle = commandStyle;
    188       this.parameterStyle = parameterStyle;
    189     }
    190   }
    19188}
Note: See TracChangeset for help on using the changeset viewer.