/** * Add a new rule. * * <p>Several cases can occur : * * <ul> * <li>The rule to add has an ID or class and the rule does not yet exists and is not an event * rule : add it directly. * <li>If the rule has an ID or class but the rule already exists, augment to already existing * rule. * <li>If the rule has no ID or class and is not an event, augment the default style. * <li>If the rule is an event, the corresponding normal rule is searched, if it does not * exists, it is created then or else, the event is added to the found rule. * </ul> * * @param newRule The rule to add or copy. * @return It the rule added augments an existing rule, this existing rule is returned, else * null is returned. */ protected Rule addRule(Rule newRule) { Rule oldRule = null; if (newRule.selector.getPseudoClass() != null) { oldRule = addEventRule(newRule); } else if (newRule.selector.getId() != null) { oldRule = byId.get(newRule.selector.getId()); if (oldRule != null) { oldRule.getStyle().augment(newRule.getStyle()); } else { byId.put(newRule.selector.getId(), newRule); newRule.getStyle().reparent(defaultRule); } } else if (newRule.selector.getClazz() != null) { oldRule = byClass.get(newRule.selector.getClazz()); if (oldRule != null) { oldRule.getStyle().augment(newRule.getStyle()); } else { byClass.put(newRule.selector.getClazz(), newRule); newRule.getStyle().reparent(defaultRule); } } else { oldRule = defaultRule; defaultRule.getStyle().augment(newRule.getStyle()); newRule = defaultRule; } // That's it. return oldRule; }
protected Rule addEventRule(Rule newRule) { Rule parentRule = null; if (newRule.selector.getId() != null) { parentRule = byId.get(newRule.selector.getId()); if (parentRule == null) { parentRule = addRule( new Rule( new Selector( newRule.selector.getType(), newRule.selector.getId(), newRule.selector.getClazz()))); } } else if (newRule.selector.getClazz() != null) { parentRule = byClass.get(newRule.selector.getClazz()); if (parentRule == null) { parentRule = addRule( new Rule( new Selector( newRule.selector.getType(), newRule.selector.getId(), newRule.selector.getClazz()))); } } else { parentRule = defaultRule; } newRule.getStyle().reparent(parentRule); parentRule.getStyle().addAlternateStyle(newRule.selector.getPseudoClass(), newRule); return parentRule; }
/** * Create the default rules. This method is the place to set defaults for specific element types. * This is here that the edge width is reset to one, since the default width is larger. The * default z index that is different for every class of element is also set here. */ protected void initRules() { defaultRule = new Rule(new Selector(Selector.Type.ANY), null); defaultRule.getStyle().setDefaults(); graphRules.defaultRule = new Rule(new Selector(Selector.Type.GRAPH), defaultRule); nodeRules.defaultRule = new Rule(new Selector(Selector.Type.NODE), defaultRule); edgeRules.defaultRule = new Rule(new Selector(Selector.Type.EDGE), defaultRule); spriteRules.defaultRule = new Rule(new Selector(Selector.Type.SPRITE), defaultRule); graphRules.defaultRule.getStyle().setValue("padding", new Values(Style.Units.PX, 30)); edgeRules.defaultRule.getStyle().setValue("shape", StyleConstants.Shape.LINE); edgeRules.defaultRule.getStyle().setValue("size", new Values(Style.Units.PX, 1)); edgeRules.defaultRule.getStyle().setValue("z-index", new Integer(1)); nodeRules.defaultRule.getStyle().setValue("z-index", new Integer(2)); spriteRules.defaultRule.getStyle().setValue("z-index", new Integer(3)); Colors colors = new Colors(); colors.add(Color.WHITE); graphRules.defaultRule.getStyle().setValue("fill-color", colors); graphRules.defaultRule.getStyle().setValue("stroke-mode", StrokeMode.NONE); for (StyleSheetListener listener : listeners) { listener.styleAdded(defaultRule, defaultRule); listener.styleAdded(graphRules.defaultRule, graphRules.defaultRule); listener.styleAdded(nodeRules.defaultRule, nodeRules.defaultRule); listener.styleAdded(edgeRules.defaultRule, edgeRules.defaultRule); listener.styleAdded(spriteRules.defaultRule, spriteRules.defaultRule); } // for( StyleSheetListener listener: listeners ) // listener.styleAdded( defaultRule, defaultRule ); // for( StyleSheetListener listener: listeners ) // listener.styleAdded( graphRules.defaultRule, graphRules.defaultRule // ); // for( StyleSheetListener listener: listeners ) // listener.styleAdded( nodeRules.defaultRule, nodeRules.defaultRule ); // for( StyleSheetListener listener: listeners ) // listener.styleAdded( edgeRules.defaultRule, edgeRules.defaultRule ); // for( StyleSheetListener listener: listeners ) // listener.styleAdded( spriteRules.defaultRule, spriteRules.defaultRule // ); }