faces highlighting
[fedora-idea.git] / treeStructure / src / jetbrains / fabrique / ui / treeStructure / SimpleNode.java
blob06c72bcb3fe16a0eda4c419c5bbb84e67c8fb94f
1 /*
2 * Copyright (c) 2000-2004 by JetBrains s.r.o. All Rights Reserved.
3 * Use is subject to license terms.
4 */
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;
18 import javax.swing.*;
19 import java.awt.*;
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;
30 private Font myFont;
32 protected SimpleNode(Project project) {
33 this(project, null);
36 protected SimpleNode(Project project, NodeDescriptor parentDescriptor) {
37 super(project, parentDescriptor);
38 myName = "";
41 protected SimpleNode(SimpleNode parent) {
42 this(parent == null ? null : parent.myProject, parent);
45 protected SimpleNode() {
46 super(null, null);
49 public String toString() {
50 return getName();
53 public int getWeight() {
54 return myWeight;
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) {
66 myWeight = 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();
84 myName = getName();
86 if (SimpleNode.this instanceof DeletableNode) {
87 DeletableNode deletableNode = (DeletableNode) SimpleNode.this;
88 if (deletableNode.isReadOnly()) {
89 makeIconsReadOnly();
93 return Boolean.valueOf(result);
95 }).booleanValue();
98 private void makeIconsReadOnly() {
99 myOpenIcon = makeIconReadOnly(myOpenIcon);
100 myClosedIcon = makeIconReadOnly(myClosedIcon);
103 private Icon makeIconReadOnly(Icon icon) {
104 if (icon != null) {
105 LayeredIcon layeredIcon = new LayeredIcon(2);
106 layeredIcon.setIcon(icon, 0);
107 layeredIcon.setIcon(Icons.LOCKED_ICON, 1);
108 return layeredIcon;
110 return icon;
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){
123 clearColoredText();
124 SimpleTextAttributes attributes = hasError ? getErrorAttributes() : getPlainAttributes();
125 myColoredText.add(new ColoredFragment(text, tooltip, attributes));
128 public final void setPlainText(String aText) {
129 clearColoredText();
130 addPlainText(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() {
158 return false;
161 public final Object getElement() {
162 return this;
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() {
182 return false;
185 public boolean isAutoExpandNode() {
186 return false;
189 public boolean shouldHaveSeparator() {
190 return false;
193 public void setUniformIcon(Icon aIcon) {
194 setIcons(aIcon, aIcon);
197 public final void setIcons(Icon aClosed, Icon aOpen) {
198 myOpenIcon = 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) {
216 myText = aText;
217 myAttributes = aAttributes;
218 myToolTip = toolTip;
221 public String getToolTip() {
222 return myToolTip;
225 public String getText() {
226 return myText;
229 public SimpleTextAttributes getAttributes() {
230 return myAttributes;
234 public boolean isAncestorOrSelf(SimpleNode selectedNode) {
235 SimpleNode node = selectedNode;
236 while (node != null) {
237 if (equals(node)) return true;
238 node = node.getParent();
240 return false;
243 public Font getFont() {
244 return myFont;
247 public void setFont(Font font) {
248 myFont = 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};