2 * Copyright (c) 2000-2004 by JetBrains s.r.o. All Rights Reserved.
3 * Use is subject to license terms.
5 package jetbrains
.fabrique
.ui
.treeStructure
;
7 import com
.intellij
.ide
.util
.treeView
.NodeDescriptor
;
8 import com
.intellij
.openapi
.application
.ApplicationManager
;
9 import com
.intellij
.openapi
.project
.Project
;
10 import com
.intellij
.openapi
.util
.Computable
;
11 import com
.intellij
.openapi
.vcs
.FileStatus
;
12 import com
.intellij
.ui
.LayeredIcon
;
13 import com
.intellij
.ui
.SimpleTextAttributes
;
14 import com
.intellij
.util
.Icons
;
15 import com
.intellij
.util
.ui
.update
.ComparableObject
;
16 import com
.intellij
.util
.ui
.update
.ComparableObjectCheck
;
20 import java
.awt
.event
.InputEvent
;
21 import java
.util
.ArrayList
;
22 import java
.util
.List
;
24 public abstract class SimpleNode
extends NodeDescriptor
implements ComparableObject
{
26 protected static final SimpleNode
[] NO_CHILDREN
= new SimpleNode
[0];
28 protected List
<ColoredFragment
> myColoredText
= new ArrayList
<ColoredFragment
>();
29 private int myWeight
= 10;
32 protected SimpleNode(Project project
) {
36 protected SimpleNode(Project project
, NodeDescriptor parentDescriptor
) {
37 super(project
, parentDescriptor
);
41 protected SimpleNode(SimpleNode parent
) {
42 this(parent
== null ?
null : parent
.myProject
, parent
);
45 protected SimpleNode() {
49 public String
toString() {
53 public int getWeight() {
57 protected SimpleTextAttributes
getErrorAttributes() {
58 return new SimpleTextAttributes(SimpleTextAttributes
.STYLE_WAVED
, getColor(), Color
.red
);
61 protected SimpleTextAttributes
getPlainAttributes() {
62 return new SimpleTextAttributes(Font
.PLAIN
, getColor());
65 public final void setWeight(int weight
) {
69 protected FileStatus
getFileStatus() {
70 return FileStatus
.NOT_CHANGED
;
73 public final boolean update() {
74 return ApplicationManager
.getApplication().runReadAction(new Computable
<Boolean
>() {
75 public Boolean
compute() {
76 myColor
= Color
.black
;
77 assert getFileStatus() != null: getClass().getName() + ' ' + toString();
78 Color fileStatusColor
= getFileStatus().getColor();
79 if (fileStatusColor
!= null) {
80 myColor
= fileStatusColor
;
83 final boolean result
= doUpdate();
86 if (SimpleNode
.this instanceof DeletableNode
) {
87 DeletableNode deletableNode
= (DeletableNode
) SimpleNode
.this;
88 if (deletableNode
.isReadOnly()) {
93 return Boolean
.valueOf(result
);
98 private void makeIconsReadOnly() {
99 myOpenIcon
= makeIconReadOnly(myOpenIcon
);
100 myClosedIcon
= makeIconReadOnly(myClosedIcon
);
103 private Icon
makeIconReadOnly(Icon icon
) {
105 LayeredIcon layeredIcon
= new LayeredIcon(2);
106 layeredIcon
.setIcon(icon
, 0);
107 layeredIcon
.setIcon(Icons
.LOCKED_ICON
, 1);
113 public final String
getName() {
114 StringBuffer result
= new StringBuffer("");
115 for (int i
= 0; i
< myColoredText
.size(); i
++) {
116 ColoredFragment each
= myColoredText
.get(i
);
117 result
.append(each
.getText());
119 return result
.toString();
122 public final void setNodeText(String text
, String tooltip
, boolean hasError
){
124 SimpleTextAttributes attributes
= hasError ?
getErrorAttributes() : getPlainAttributes();
125 myColoredText
.add(new ColoredFragment(text
, tooltip
, attributes
));
128 public final void setPlainText(String aText
) {
133 public final void addPlainText(String aText
) {
134 myColoredText
.add(new ColoredFragment(aText
, getPlainAttributes()));
137 public final void addErrorText(String aText
, String errorTooltipText
) {
138 myColoredText
.add(new ColoredFragment(aText
, errorTooltipText
, getErrorAttributes()));
141 public final void clearColoredText() {
142 myColoredText
.clear();
145 public final void addColoredFragment(String aText
, SimpleTextAttributes aAttributes
) {
146 addColoredFragment(aText
, null, aAttributes
);
149 public final void addColoredFragment(String aText
, String toolTip
, SimpleTextAttributes aAttributes
) {
150 myColoredText
.add(new ColoredFragment(aText
, toolTip
, aAttributes
));
153 public final void addColoredFragment(ColoredFragment fragment
) {
154 myColoredText
.add(new ColoredFragment(fragment
.getText(), fragment
.getAttributes()));
157 protected boolean doUpdate() {
161 public final Object
getElement() {
165 public final SimpleNode
getParent() {
166 return (SimpleNode
) getParentDescriptor();
169 public abstract SimpleNode
[] getChildren();
171 public void accept(SimpleNodeVisitor visitor
) {
172 visitor
.accept(this);
175 public void handleSelection(SimpleTree tree
) {
178 public void handleDoubleClickOrEnter(SimpleTree tree
, InputEvent inputEvent
) {
181 public boolean isAlwaysShowPlus() {
185 public boolean isAutoExpandNode() {
189 public boolean shouldHaveSeparator() {
193 public void setUniformIcon(Icon aIcon
) {
194 setIcons(aIcon
, aIcon
);
197 public final void setIcons(Icon aClosed
, Icon aOpen
) {
199 myClosedIcon
= aClosed
;
202 public final ColoredFragment
[] getColoredText() {
203 return myColoredText
.toArray(new ColoredFragment
[myColoredText
.size()]);
206 public static class ColoredFragment
{
207 private String myText
;
208 private String myToolTip
;
209 private SimpleTextAttributes myAttributes
;
211 public ColoredFragment(String aText
, SimpleTextAttributes aAttributes
) {
212 this(aText
, null, aAttributes
);
215 public ColoredFragment(String aText
, String toolTip
, SimpleTextAttributes aAttributes
) {
217 myAttributes
= aAttributes
;
221 public String
getToolTip() {
225 public String
getText() {
229 public SimpleTextAttributes
getAttributes() {
234 public boolean isAncestorOrSelf(SimpleNode selectedNode
) {
235 SimpleNode node
= selectedNode
;
236 while (node
!= null) {
237 if (equals(node
)) return true;
238 node
= node
.getParent();
243 public Font
getFont() {
247 public void setFont(Font font
) {
252 public final boolean equals(Object o
) {
253 return ComparableObjectCheck
.equals(this, o
);
256 public final int hashCode() {
257 return ComparableObjectCheck
.hashCode(this, super.hashCode());
260 public Object
[] getSelectionEqualityObjects() {
261 return new Object
[] {this};