PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/bundles/plugins-trunk/XML/sidekick/css/parser/CSSNode.java

#
Java | 117 lines | 94 code | 23 blank | 0 comment | 14 complexity | ed045ce733b912c5fe05f119cf8e45d0 MD5 | raw file
  1package sidekick.css.parser;
  2
  3import java.util.*;
  4import javax.swing.text.Position;
  5import sidekick.util.*;
  6import org.gjt.sp.jedit.jEdit;
  7
  8public class CSSNode implements SideKickElement, Comparable<CSSNode> {
  9
 10    private String name = "";
 11    private List<CSSNode> children = new ArrayList<CSSNode>();
 12    private Location start = null;
 13    private Location end = null;
 14    private Position startPosition = null;
 15    private Position endPosition = null;
 16
 17    public CSSNode() {}
 18
 19    public CSSNode( String name ) {
 20        this.name = name;
 21    }
 22
 23    public void setName( String name ) {
 24        if ( name != null ) {
 25            this.name = name;
 26        }
 27    }
 28
 29    public String getName() {
 30        return name;
 31    }
 32
 33    public String toString() {
 34        String name = getName();
 35        if (jEdit.getBooleanProperty( "sidekick.css.showLineNums", false )) {
 36            StringBuilder sb = new StringBuilder();
 37            sb.append(getStartLocation().line).append(": ").append(name);
 38            name = sb.toString();
 39        }
 40        return name;
 41    }
 42
 43    public void setStartLocation( Location loc ) {
 44        start = loc;
 45    }
 46
 47    public Location getStartLocation() {
 48        if ( start == null ) {
 49            start = new Location();
 50        }
 51        return start;
 52    }
 53
 54    public void setEndLocation( Location loc ) {
 55        end = loc;
 56    }
 57
 58    public Location getEndLocation() {
 59        if ( end == null ) {
 60            end = new Location();
 61        }
 62        return end;
 63    }
 64
 65    public void setStartPosition( Position s ) {
 66        startPosition = s;
 67    }
 68
 69    public Position getStartPosition() {
 70        return startPosition;
 71    }
 72
 73    public void setEndPosition( Position s ) {
 74        endPosition = s;
 75    }
 76
 77    public Position getEndPosition() {
 78        return endPosition;
 79    }
 80
 81    public void addChild( CSSNode child ) {
 82        children.add( child );
 83    }
 84
 85    public void addChildren( List<CSSNode> kids ) {
 86        children.addAll( kids );
 87    }
 88
 89    public List<CSSNode> getChildren() {
 90        Collections.sort(children);
 91        return children;
 92    }
 93
 94    public boolean hasChildren() {
 95        return children != null && children.size() > 0;
 96    }
 97
 98    public boolean equals( Object o ) {
 99        return getName().equals( o ) && getChildren().equals(((CSSNode)o).getChildren());
100    }
101
102    public int hashCode() {
103        return getName().hashCode();
104    }
105
106    public int compareTo( CSSNode node ) {
107        String my_name = getName().toLowerCase();
108        if ( my_name.startsWith( "." ) || my_name.startsWith( "#" ) ) {
109            my_name = my_name.substring( 1 );
110        }
111        String your_name = node.getName().toLowerCase();
112        if ( your_name.startsWith( "." ) || your_name.startsWith( "#" ) ) {
113            your_name = your_name.substring( 1 );
114        }
115        return my_name.compareTo( your_name );
116    }
117}