001/* 002 * Copyright 2006 - 2013 003 * Stefan Balev <stefan.balev@graphstream-project.org> 004 * Julien Baudry <julien.baudry@graphstream-project.org> 005 * Antoine Dutot <antoine.dutot@graphstream-project.org> 006 * Yoann Pigné <yoann.pigne@graphstream-project.org> 007 * Guilhelm Savin <guilhelm.savin@graphstream-project.org> 008 * 009 * This file is part of GraphStream <http://graphstream-project.org>. 010 * 011 * GraphStream is a library whose purpose is to handle static or dynamic 012 * graph, create them from scratch, file or any source and display them. 013 * 014 * This program is free software distributed under the terms of two licenses, the 015 * CeCILL-C license that fits European law, and the GNU Lesser General Public 016 * License. You can use, modify and/ or redistribute the software under the terms 017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following 018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by 019 * the Free Software Foundation, either version 3 of the License, or (at your 020 * option) any later version. 021 * 022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY 023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 024 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 025 * 026 * You should have received a copy of the GNU Lesser General Public License 027 * along with this program. If not, see <http://www.gnu.org/licenses/>. 028 * 029 * The fact that you are presently reading this means that you have had 030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms. 031 */ 032package org.graphstream.ui.graphicGraph.stylesheet; 033 034import java.util.HashSet; 035 036/** 037 * Style application rule. 038 * 039 * <p> 040 * A rule is made of a selector and values. The selector identifies the 041 * element(s) this rule applies to, and the values are styles to apply to the 042 * matched elements. 043 * </p> 044 */ 045public class Rule { 046 // Attributes 047 048 /** 049 * The match. 050 */ 051 public Selector selector; 052 053 /** 054 * The style. 055 */ 056 public Style style; 057 058 /** 059 * Optionally, the rule can store all the style groups it participates in. 060 */ 061 public HashSet<String> groups; 062 063 // Constructors 064 065 protected Rule() { 066 } 067 068 /** 069 * New rule with a matcher. 070 * 071 * @param selector 072 * The rule selector. 073 */ 074 public Rule(Selector selector) { 075 this.selector = selector; 076 } 077 078 public Rule(Selector selector, Rule parent) { 079 this.selector = selector; 080 this.style = new Style(parent); 081 } 082 083 /** 084 * This rule style. 085 * 086 * @return The rule style. 087 */ 088 public Style getStyle() { 089 return style; 090 } 091 092 /** 093 * The group this rule participate in, maybe null if the rule does not 094 * participate in any group. 095 * 096 * @return The group set or null. 097 */ 098 public HashSet<String> getGroups() { 099 return groups; 100 } 101 102 /** 103 * True if this rule selector match the given identifier. 104 * 105 * @param identifier 106 * The identifier to test for the match. 107 * @return True if matching. 108 */ 109 public boolean matchId(String identifier) { 110 String ident = selector.getId(); 111 112 if (ident != null) 113 return ident.equals(identifier); 114 115 return false; 116 } 117 118 /** 119 * Change the style. 120 * 121 * @param style 122 * A style specification. 123 */ 124 public void setStyle(Style style) { 125 this.style = style; 126 } 127 128 /** 129 * Specify that this rule participates in the given style group. 130 * 131 * @param groupId 132 * The group unique identifier. 133 */ 134 public void addGroup(String groupId) { 135 if (groups == null) 136 groups = new HashSet<String>(); 137 groups.add(groupId); 138 } 139 140 /** 141 * Remove this rule from the style group. 142 * 143 * @param groupId 144 * The group unique identifier. 145 */ 146 public void removeGroup(String groupId) { 147 if (groups != null) 148 groups.remove(groupId); 149 } 150 151 @Override 152 public String toString() { 153 return toString(-1); 154 } 155 156 public String toString(int level) { 157 StringBuilder builder = new StringBuilder(); 158 String prefix = ""; 159 160 if (level > 0) { 161 for (int i = 0; i < level; i++) 162 prefix += " "; 163 } 164 165 builder.append(prefix); 166 builder.append(selector.toString()); 167 builder.append(style.toString(level + 1)); 168 169 return builder.toString(); 170 } 171}