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
034/**
035 * A selector is the part of a CSS rule that defines to which element a style
036 * applies in the graph.
037 * 
038 * @author Antoine Dutot
039 * @author Yoann Pign�
040 */
041public class Selector {
042        /**
043         * Types of elements.
044         */
045        public static enum Type {
046                ANY, GRAPH, NODE, EDGE, SPRITE
047        };
048
049        /**
050         * The kind of element this matcher applies to.
051         */
052        public Type type;
053
054        /**
055         * If the selector specify an identifier.
056         */
057        public String id;
058
059        /**
060         * If the selector specify a class.
061         */
062        public String clazz;
063
064        /**
065         * If the selector also specify a pseudo class.
066         */
067        public String pseudoClass;
068
069        /**
070         * New selector for a given type of element.
071         * 
072         * @param type
073         *            The element type of this selector.
074         */
075        public Selector(Type type) {
076                this.type = type;
077        }
078
079        /**
080         * New selector for a given type of element. This constructor allows to
081         * specify either an identifier or a class to restrict this selector. If the
082         * identifier is given, the class will never be used (as identifiers are
083         * finer than classes). If the identifier is null the class will be used.
084         * The identifier allow to select only one element by its name. The class
085         * allows to select several elements.
086         * 
087         * @param type
088         *            The element type of this selector.
089         * @param identifier
090         *            The element name.
091         * @param clazz
092         *            The element class.
093         */
094        public Selector(Type type, String identifier, String clazz) {
095                this.type = type;
096                setId(identifier);
097                setClass(clazz);
098        }
099
100        /**
101         * Utility constructor that assign the correct type to the selector from a
102         * string. The type must be "node", "edge", "graph", or "sprite".
103         * 
104         * @param type
105         *            Either "node", "edge", "graph" or "sprite".
106         */
107        public Selector(String type) {
108                if (type.equals("node"))
109                        this.type = Type.NODE;
110                else if (type.equals("edge"))
111                        this.type = Type.EDGE;
112                else if (type.equals("graph"))
113                        this.type = Type.GRAPH;
114                else if (type.equals("sprite"))
115                        this.type = Type.SPRITE;
116                else
117                        throw new RuntimeException("invalid matcher type '" + type + "'");
118        }
119
120        /**
121         * New selector, copy of another.
122         * 
123         * @param other
124         *            The other selector.
125         */
126        public Selector(Selector other) {
127                this.type = other.type;
128                setId(other.id);
129                setClass(other.clazz);
130        }
131
132        /**
133         * Specify the identifier of the unique element this selector applies to.
134         * 
135         * @param id
136         *            A string that identifies an element of the graph.
137         */
138        public void setId(String id) {
139                this.id = id;
140        }
141
142        /**
143         * Specify the class of the elements this selector applies to.
144         * 
145         * @param clazz
146         *            A string that matches all elements of a given class.
147         */
148        public void setClass(String clazz) {
149                this.clazz = clazz;
150        }
151
152        /**
153         * Specify the pseudo-class of the elements this selector applies to.
154         * 
155         * @param pseudoClass
156         *            A string that matches all elements of a given pseudo-class.
157         */
158        public void setPseudoClass(String pseudoClass) {
159                this.pseudoClass = pseudoClass;
160        }
161
162        /**
163         * The kind of elements this selector applies to.
164         * 
165         * @return An element type.
166         */
167        public Type getType() {
168                return type;
169        }
170
171        /**
172         * The identifier of the element this selector uniquely applies to. This can
173         * be null if this selector is general.
174         * 
175         * @return The identifier or null if the selector is general.
176         */
177        public String getId() {
178                return id;
179        }
180
181        /**
182         * The class of elements this selector applies to. This can be null if this
183         * selector is general.
184         * 
185         * @return A class name or null if the selector is general.
186         */
187        public String getClazz() {
188                return clazz;
189        }
190
191        /**
192         * The pseudo-class of elements this selector applies to. This can be null.
193         * 
194         * @return A pseudo-class name or null.
195         */
196        public String getPseudoClass() {
197                return pseudoClass;
198        }
199
200        @Override
201        public String toString() {
202                return String.format("%s%s%s%s", type.toString(),
203                                id != null ? String.format("#%s", id) : "",
204                                clazz != null ? String.format(".%s", clazz) : "",
205                                pseudoClass != null ? String.format(":%s", pseudoClass) : "");
206        }
207}