001package votorola.g.web.gwt; // Copyright 2012, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. 002 003import com.google.gwt.dom.client.Element; 004 005 006/** {@linkplain Element Element} utilities. 007 */ 008public final class ElementX 009{ 010 011 012 private ElementX() {} 013 014 015 016 /** Returns true if the value of the 'class' attribute (allNames) contains the 017 * specified class name. 018 * 019 * @param allNames the value of the 'class' attribute, a space-separated list of 020 * class names. 021 */ 022 public static boolean hasClassName( final String allNames, final String className ) 023 { 024 // copied verbatim from removeClassName(), for consistency 025 026 final String oldStyle = allNames; 027 int idx = oldStyle.indexOf(className); 028 029 // Calculate matching index. 030 while (idx != -1) { 031 if (idx == 0 || oldStyle.charAt(idx - 1) == ' ') { 032 int last = idx + className.length(); 033 int lastPos = oldStyle.length(); 034 if ((last == lastPos) 035 || ((last < lastPos) && (oldStyle.charAt(last) == ' '))) { 036 break; 037 } 038 } 039 idx = oldStyle.indexOf(className, idx + 1); 040 } 041 042 return idx != -1; 043 } 044 045 046 047 /** Does nothing if oldN and newN are equal; otherwise removes oldN if not null, and 048 * adds newN if not null. This is similar to e.{@linkplain 049 * Element#replaceClassName(String,String) replaceClassName}(oldN,newN) except that 050 * it allows for null values. 051 * 052 * @return true if the class name was replaced as a result, false otherwise. 053 */ 054 public static boolean replaceNullClassName( final String oldN, final String newN, 055 final Element e ) 056 { 057 final boolean isChanged; 058 if( oldN == null ) 059 { 060 if( newN == null ) isChanged = false; 061 else 062 { 063 e.addClassName( newN ); 064 isChanged = true; 065 } 066 } 067 else if( newN == null ) 068 { 069 e.removeClassName( oldN ); 070 isChanged = true; 071 } 072 else if( !oldN.equals( newN )) 073 { 074 e.removeClassName( oldN ); 075 e.addClassName( newN ); 076 isChanged = true; 077 } 078 else isChanged = false; 079 return isChanged; 080 } 081 082 083}