summaryrefslogtreecommitdiff
path: root/platform/platform-api/src/com/intellij/openapi/ui/DetailsComponent.java
blob: 34aaa93d274170a34d1e5d7c87cbcb4434c8b23b (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
/*
 * 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.openapi.ui;

import com.intellij.openapi.util.registry.Registry;
import com.intellij.ui.components.panels.NonOpaquePanel;
import com.intellij.ui.components.panels.Wrapper;
import com.intellij.util.ArrayUtil;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.UIUtil;
import com.intellij.xml.util.XmlStringUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.geom.GeneralPath;
import java.util.ArrayList;

public class DetailsComponent {

  private final JPanel myComponent;

  private JComponent myContent;


  private final Banner myBannerLabel;

  private final JLabel myEmptyContentLabel;
  private final NonOpaquePanel myBanner;

  private String[] myBannerText;
  private boolean myDetailsEnabled = !Registry.is("ide.new.project.settings");
  private String[] myPrefix;
  private String[] myText;

  private final Wrapper myContentGutter = new Wrapper();

  private boolean myPaintBorder = !Registry.is("ide.new.project.settings");

  public DetailsComponent() {
    myComponent = new JPanel(new BorderLayout()) {
      @Override
      protected void paintComponent(final Graphics g) {
        if (NullableComponent.Check.isNull(myContent) || !myDetailsEnabled) return;

        GraphicsConfig c = new GraphicsConfig(g);
        c.setAntialiasing(true);

        Insets insets = getInsets();
        if (insets == null) {
          insets = new Insets(0, 0, 0, 0);
        }

        g.setColor(UIUtil.getFocusedFillColor());

        final Rectangle banner = myBanner.getBounds();
        final GeneralPath header = new GeneralPath();

        final int leftX = insets.left;
        final int leftY = insets.top;
        final int rightX = insets.left + getWidth() - 1 - insets.right;
        final int rightY = banner.y + banner.height;

        header.moveTo(leftX, rightY);
        int arc = 8;
        header.lineTo(leftX, leftY + arc);
        header.quadTo(leftX, leftY, leftX + arc, leftY);
        header.lineTo(rightX - arc, leftY);
        header.quadTo(rightX, leftY, rightX, leftY + arc);
        header.lineTo(rightX, rightY);
        header.closePath();

        c.getG().fill(header);

        g.setColor(UIUtil.getFocusedBoundsColor());

        c.getG().draw(header);

        if (myPaintBorder) {
          final int down = getHeight() - insets.top - insets.bottom - 1;
          g.drawLine(leftX, rightY, leftX, down);
          g.drawLine(rightX, rightY, rightX, down);
          g.drawLine(leftX, down, rightX, down);
        }

        c.restore();
      }
    };

    myComponent.setOpaque(false);
    myContentGutter.setOpaque(false);
    myContentGutter.setBorder(null);

    myBanner = new NonOpaquePanel(new BorderLayout());
    myBannerLabel = new Banner();

    if (!Registry.is("ide.new.project.settings")) {
      myBanner.add(myBannerLabel, BorderLayout.CENTER);
    }

    myEmptyContentLabel = new JLabel("", SwingConstants.CENTER);

    revalidateDetailsMode();
  }

  private void revalidateDetailsMode() {
    myComponent.removeAll();
    myComponent.add(myContentGutter, BorderLayout.CENTER);

    if (myDetailsEnabled) {
      myComponent.add(myBanner, BorderLayout.NORTH);
    }

    if (myContent != null) {
      myContentGutter.add(myContent, BorderLayout.CENTER);
      invalidateContentBorder();
    }

    myComponent.revalidate();
    myComponent.repaint();
  }

  public void setBannerActions(Action[] actions) {
    myBannerLabel.clearActions();
    for (Action each : actions) {
      myBannerLabel.addAction(each);
    }

    myComponent.revalidate();
    myComponent.repaint();
  }

  public void setContent(@Nullable JComponent c) {
    if (myContent != null) {
      myContentGutter.remove(myContent);
    }

    myContent = new MyWrapper(c);

    myContent.setOpaque(false);

    invalidateContentBorder();

    myContentGutter.add(myContent, BorderLayout.CENTER);

    updateBanner();

    myComponent.revalidate();
    myComponent.repaint();
  }

  private void invalidateContentBorder() {
    if (myDetailsEnabled) {
      myContent.setBorder(new EmptyBorder(UIUtil.PANEL_REGULAR_INSETS));
    }
    else {
      myContent.setBorder(null);
    }
  }


  public void setPrefix(@Nullable String... prefix) {
    myPrefix = prefix;
    if (myText != null) {
      setText(myText);
    }
  }

  public void setText(@Nullable String... text) {
    myText = text;
    update();
  }

  public void update() {
    ArrayList<String> strings = new ArrayList<String>();
    if (myPrefix != null) {
      ContainerUtil.addAll(strings, myPrefix);
    }

    if (myText != null) {
      ContainerUtil.addAll(strings, myText);
    }

    myBannerText = ArrayUtil.toStringArray(strings);

    updateBanner();
  }

  private void updateBanner() {
    myBannerLabel.setText(NullableComponent.Check.isNull(myContent) || myBannerText == null ? ArrayUtil.EMPTY_STRING_ARRAY : myBannerText);

    myBannerLabel.revalidate();
    myBannerLabel.repaint();
  }

  public void setPaintBorder(final boolean paintBorder) {
    myPaintBorder = paintBorder;
  }

  public DetailsComponent setEmptyContentText(@Nullable final String emptyContentText) {
    @NonNls final String s = XmlStringUtil.wrapInHtml("<center>" + (emptyContentText != null ? emptyContentText : "") + "</center>");
    myEmptyContentLabel.setText(s);
    return this;
  }

  public JComponent getComponent() {
    return myComponent;
  }

  public JComponent getContentGutter() {
    return myContentGutter;
  }

  public void setBannerMinHeight(final int height) {
    myBannerLabel.setMinHeight(height);
  }

  public void disposeUIResources() {
    setContent(null);
  }

  public void updateBannerActions() {
    myBannerLabel.updateActions();
  }

  public void setDetailsModeEnabled(final boolean enabled) {
    if (myDetailsEnabled == enabled) return;

    myDetailsEnabled = enabled;

    revalidateDetailsMode();
  }


  public interface Facade {
    DetailsComponent getDetailsComponent();
  }

  private class MyWrapper extends Wrapper implements NullableComponent {
    public MyWrapper(final JComponent c) {
      super(c == null || NullableComponent.Check.isNull(c) ? myEmptyContentLabel : c);
    }

    @Override
    public boolean isNull() {
      return getTargetComponent() == myEmptyContentLabel;
    }
  }


}