001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Alexandre Russel
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.gwt.client.util;
021
022import com.google.gwt.dom.client.Element;
023
024/**
025 * @author Alexandre Russel
026 */
027public class CSSClassManager {
028    private Element element;
029
030    public CSSClassManager(Element element) {
031        this.element = element;
032    }
033
034    /**
035     * remove the class from the specified element.
036     *
037     * @return true if the class was removed, false if the class was not found.
038     */
039    public boolean removeClass(String name) {
040        String className = element.getClassName();
041        if (isClassPresent(name)) {
042            element.setClassName(className.replaceAll("\\b" + name + "\\b\\s*", ""));
043            return true;
044        }
045        return false;
046    }
047
048    public boolean isClassPresent(String name) {
049        String cssClass = element.getClassName();
050        if (cssClass == null) {
051            return false;
052        }
053        return cssClass.matches(".*\\b" + name + "\\b.*");
054    }
055
056    /**
057     * Add the class to the element.
058     *
059     * @return true if the class was add, false if the class was already present.
060     */
061    public boolean addClass(String name) {
062        String className = element.getClassName();
063        if (!isClassPresent(name)) {
064            element.setClassName(className + " " + name);
065            return true;
066        }
067        return false;
068    }
069}