summaryrefslogtreecommitdiff
path: root/platform/structure-view-api/src/com/intellij/ide/projectView/PresentationData.java
blob: 435c5ef9279d4926ecc0cc854713c810a09bea95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.ide.projectView;

import com.intellij.ide.util.treeView.PresentableNodeDescriptor;
import com.intellij.navigation.ColoredItemPresentation;
import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.ItemPresentationWithSeparator;
import com.intellij.navigation.LocationPresentation;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.update.ComparableObject;
import com.intellij.util.ui.update.ComparableObjectCheck;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * Default implementation of the {@link com.intellij.navigation.ItemPresentation} interface.
 */

public class PresentationData implements ColoredItemPresentation, ComparableObject, LocationPresentation {
  protected final List<PresentableNodeDescriptor.ColoredFragment> myColoredText = ContainerUtil.createLockFreeCopyOnWriteList();

  private Icon myIcon;

  private String myLocationString;
  private String myPresentableText;

  private String myTooltip;
  private TextAttributesKey myAttributesKey;

  private Color myForcedTextForeground;

  private Font myFont;

  private boolean mySeparatorAbove = false;

  private boolean myChanged;
  private String myLocationPrefix;
  private String myLocationSuffix;

  /**
   * Creates an instance with the specified parameters.
   *
   * @param presentableText the name of the object to be presented in most renderers across the program.
   * @param locationString  the location of the object (for example, the package of a class). The location
   *                        string is used by some renderers and usually displayed as grayed text next to
   *                        the item name.
   * @param icon            the icon shown for the node when it is collapsed in a tree, or when it is displayed
   *                        in a non-tree view.
   * @param attributesKey   the attributes for rendering the item text.
   */
  public PresentationData(String presentableText, String locationString, Icon icon,
                          @Nullable TextAttributesKey attributesKey) {
    myIcon = icon;
    myLocationString = locationString;
    myPresentableText = presentableText;
    myAttributesKey = attributesKey;
  }

  /**
   * @deprecated Use constructor with single icon instead.
   */
  public PresentationData(String presentableText, String locationString, Icon openIcon, Icon closedIcon,
                          @Nullable TextAttributesKey attributesKey) {
    this(presentableText, locationString, closedIcon, attributesKey);
  }


  /**
   * Creates an instance with no parameters specified.
   */
  public PresentationData() {
  }

  @Override
  public Icon getIcon(boolean open) {
    return myIcon;
  }

  @Nullable
  public Color getForcedTextForeground() {
    return myForcedTextForeground;
  }

  public void setForcedTextForeground(@Nullable Color forcedTextForeground) {
    myForcedTextForeground = forcedTextForeground;
  }

  @Override
  public String getLocationString() {
    return myLocationString;
  }

  @Override
  public String getPresentableText() {
    return myPresentableText;
  }

  public void setIcon(Icon icon) {
    myIcon = icon;
  }

  /**
   * Sets the location of the object (for example, the package of a class). The location
   * string is used by some renderers and usually displayed as grayed text next to the item name.
   *
   * @param locationString the location of the object.
   */

  public void setLocationString(String locationString) {
    myLocationString = locationString;
  }

  /**
   * Sets the name of the object to be presented in most renderers across the program.
   *
   * @param presentableText the name of the object.
   */
  public void setPresentableText(String presentableText) {
    myPresentableText = presentableText;
  }

  /**
   * @param closedIcon the closed icon for the node.
   * @see #setIcons(javax.swing.Icon)
   * @deprecated Different icons for open/closed no longer supported. Use setIcon instead
   *             Sets the icon shown for the node when it is collapsed in a tree, or when it is displayed
   *             in a non-tree view.
   */
  public void setClosedIcon(Icon closedIcon) {
    setIcon(closedIcon);
  }


  /**
   * @param openIcon the open icon for the node.
   * @see #setIcons(javax.swing.Icon)
   * @deprecated Different icons for open/closed no longer supported. This function is no op.
   *             Sets the icon shown for the node when it is expanded in the tree.
   */
  @Deprecated
  public void setOpenIcon(Icon openIcon) {
  }

  /**
   * @param icon the icon for the node.
   * @see #setOpenIcon(javax.swing.Icon)
   * @see #setClosedIcon(javax.swing.Icon)
   * @deprecated Different icons for open/closed no longer supported. Use setIcon instead.
   *             Sets both the open and closed icons of the node to the specified icon.
   */

  public void setIcons(Icon icon) {
    setIcon(icon);
  }

  /**
   * Copies the presentation parameters from the specified presentation instance.
   *
   * @param presentation the instance to copy the parameters from.
   */
  public void updateFrom(ItemPresentation presentation) {
    setIcon(presentation.getIcon(false));
    setPresentableText(presentation.getPresentableText());
    setLocationString(presentation.getLocationString());
    if (presentation instanceof ColoredItemPresentation) {
      setAttributesKey(((ColoredItemPresentation)presentation).getTextAttributesKey());
    }
    setSeparatorAbove(presentation instanceof ItemPresentationWithSeparator);
    if (presentation instanceof LocationPresentation) {
      myLocationPrefix = ((LocationPresentation)presentation).getLocationPrefix();
      myLocationSuffix = ((LocationPresentation)presentation).getLocationSuffix();
    }
  }

  public boolean hasSeparatorAbove() {
    return mySeparatorAbove;
  }

  public void setSeparatorAbove(final boolean b) {
    mySeparatorAbove = b;
  }

  @Override
  public TextAttributesKey getTextAttributesKey() {
    return myAttributesKey;
  }

  /**
   * Sets the attributes for rendering the item text.
   *
   * @param attributesKey the attributes for rendering the item text.
   */
  public void setAttributesKey(final TextAttributesKey attributesKey) {
    myAttributesKey = attributesKey;
  }

  public String getTooltip() {
    return myTooltip;
  }

  public void setTooltip(@Nullable final String tooltip) {
    myTooltip = tooltip;
  }

  public boolean isChanged() {
    return myChanged;
  }

  public void setChanged(boolean changed) {
    myChanged = changed;
  }

  @NotNull
  public List<PresentableNodeDescriptor.ColoredFragment> getColoredText() {
    return myColoredText;
  }

  public void addText(PresentableNodeDescriptor.ColoredFragment coloredFragment) {
    myColoredText.add(coloredFragment);
  }

  public void addText(String text, SimpleTextAttributes attributes) {
    myColoredText.add(new PresentableNodeDescriptor.ColoredFragment(text, attributes));
  }

  public void clearText() {
    myColoredText.clear();
  }

  public void clear() {
    myIcon = null;
    clearText();
    myAttributesKey = null;
    myFont = null;
    myForcedTextForeground = null;
    myLocationString = null;
    myPresentableText = null;
    myTooltip = null;
    myChanged = false;
    mySeparatorAbove = false;
    myLocationSuffix = null;
    myLocationPrefix = null;
  }

  @Override
  @NotNull
  public Object[] getEqualityObjects() {
    return new Object[]{myIcon, myColoredText, myAttributesKey, myFont, myForcedTextForeground, myPresentableText,
      myLocationString, mySeparatorAbove, myLocationPrefix, myLocationSuffix};
  }

  @Override
  public int hashCode() {
    return ComparableObjectCheck.hashCode(this, super.hashCode());
  }

  @Override
  public boolean equals(Object obj) {
    return ComparableObjectCheck.equals(this, obj);
  }

  public void copyFrom(PresentationData from) {
    if (from == this) {
      return;
    }
    myAttributesKey = from.myAttributesKey;
    myIcon = from.myIcon;
    clearText();
    myColoredText.addAll(from.myColoredText);
    myFont = from.myFont;
    myForcedTextForeground = from.myForcedTextForeground;
    myLocationString = from.myLocationString;
    myPresentableText = from.myPresentableText;
    myTooltip = from.myTooltip;
    mySeparatorAbove = from.mySeparatorAbove;
    myLocationPrefix = from.myLocationPrefix;
    myLocationSuffix = from.myLocationSuffix;
  }

  @Override
  public PresentationData clone() {
    PresentationData clone = new PresentationData();
    clone.copyFrom(this);
    return clone;
  }

  public void applyFrom(PresentationData from) {
    myAttributesKey = getValue(myAttributesKey, from.myAttributesKey);
    myIcon = getValue(myIcon, from.myIcon);

    if (myColoredText.isEmpty()) {
      myColoredText.addAll(from.myColoredText);
    }

    myFont = getValue(myFont, from.myFont);
    myForcedTextForeground = getValue(myForcedTextForeground, from.myForcedTextForeground);
    myLocationString = getValue(myLocationString, from.myLocationString);
    myPresentableText = getValue(myPresentableText, from.myPresentableText);
    myTooltip = getValue(myTooltip, from.myTooltip);
    mySeparatorAbove = mySeparatorAbove || from.mySeparatorAbove;
    myLocationPrefix = getValue(myLocationPrefix, from.myLocationPrefix);
    myLocationSuffix = getValue(myLocationSuffix, from.myLocationSuffix);
  }

  private static <T> T getValue(T ownValue, T fromValue) {
    return ownValue != null ? ownValue : fromValue;
  }

  @Override
  public String getLocationPrefix() {
    return myLocationPrefix == null ? DEFAULT_LOCATION_PREFIX : myLocationPrefix;
  }

  @Override
  public String getLocationSuffix() {
    return myLocationSuffix == null ? DEFAULT_LOCATION_SUFFIX : myLocationSuffix;
  }
}