summaryrefslogtreecommitdiff
path: root/java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/SidePanel.java
blob: 355888b02a42cbcc839757f824d6894ebe053948 (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
/*
 * 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.roots.ui.configuration;

import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.ui.GraphicsConfig;
import com.intellij.openapi.ui.popup.ListItemDescriptor;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.Gray;
import com.intellij.ui.JBColor;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.SeparatorWithText;
import com.intellij.ui.components.JBList;
import com.intellij.ui.components.panels.NonOpaquePanel;
import com.intellij.ui.navigation.History;
import com.intellij.ui.navigation.Place;
import com.intellij.ui.popup.list.GroupedItemsListRenderer;
import com.intellij.util.ui.EmptyIcon;
import com.intellij.util.ui.GraphicsUtil;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import static javax.swing.SwingConstants.CENTER;
import static javax.swing.SwingConstants.LEFT;

public class SidePanel extends JPanel {

  private final JList myList;
  private final DefaultListModel myModel;
  private final Place.Navigator myNavigator;
  private final ArrayList<Place> myPlaces = new ArrayList<Place>();

  private final Map<Integer, String> myIndex2Separator = new HashMap<Integer, String>();
  private final Map<Place, Presentation> myPlace2Presentation = new HashMap<Place, Presentation>();
  private final History myHistory;

  public SidePanel(Place.Navigator navigator, History history) {
    myHistory = history;
    myNavigator = navigator;

    setLayout(new BorderLayout());

    myModel = new DefaultListModel();
    myList = new JBList(myModel);
    if (Registry.is("ide.new.project.settings")) {
      myList.setBackground(UIUtil.getSidePanelColor());
      myList.setBorder(new EmptyBorder(5, 0, 0, 0));
    }
    final ListItemDescriptor descriptor = new ListItemDescriptor() {
      @Override
      public String getTextFor(final Object value) {
        return myPlace2Presentation.get(value).getText();
      }

      @Override
      public String getTooltipFor(final Object value) {
        return getTextFor(value);
      }

      @Override
      public Icon getIconFor(final Object value) {
        return Registry.is("ide.new.project.settings") ? EmptyIcon.create(16, 20) : null;
        //return myPlace2Presentation.get(value).getIcon();
      }

      @Override
      public boolean hasSeparatorAboveOf(final Object value) {
        final int index = myPlaces.indexOf(value);
        return myIndex2Separator.get(index) != null;
      }

      @Override
      public String getCaptionAboveOf(final Object value) {
        return myIndex2Separator.get(myPlaces.indexOf(value));
      }
    };

    myList.setCellRenderer(new GroupedItemsListRenderer(descriptor) {
      JPanel myExtraPanel;
      CountLabel myCountLabel;
      {
        mySeparatorComponent.setCaptionCentered(false);
      }

      @Override
      protected Color getForeground() {
        return Registry.is("ide.new.project.settings") ? new JBColor(Gray._60, Gray._140) : super.getForeground();
      }

      @Override
      protected SeparatorWithText createSeparator() {
        return new SeparatorWithText() {
          @Override
          protected void paintComponent(Graphics g) {
            if (Registry.is("ide.new.project.settings")) {
              g.setColor(new JBColor(POPUP_SEPARATOR_FOREGROUND, Gray._80));
              if ("--".equals(getCaption())) {
                g.drawLine(0, getHeight()/ 2, getWidth(), getHeight() /2);
                return;
              }
              Rectangle viewR = new Rectangle(0, getVgap(), getWidth() - 1, getHeight() - getVgap() - 1);
              Rectangle iconR = new Rectangle();
              Rectangle textR = new Rectangle();
              String s = SwingUtilities
                .layoutCompoundLabel(g.getFontMetrics(), getCaption(), null, CENTER,
                                     LEFT,
                                     CENTER,
                                     LEFT,
                                     viewR, iconR, textR, 0);
              GraphicsUtil.setupAAPainting(g);
              g.setColor(new JBColor(Gray._255.withAlpha(80), Gray._0.withAlpha(80)));
              g.drawString(s, textR.x + 10, textR.y + 1 + g.getFontMetrics().getAscent());
              g.setColor(new JBColor(new Color(0x5F6D7B), Gray._120));
              g.drawString(s, textR.x + 10, textR.y + g.getFontMetrics().getAscent());
            }
            else {
              super.paintComponent(g);
            }
          }
        };
      }

      @Override
      protected void layout() {
        if (Registry.is("ide.new.project.settings")) {
          myRendererComponent.add(mySeparatorComponent, BorderLayout.NORTH);
          myExtraPanel.add(myComponent, BorderLayout.CENTER);
          myExtraPanel.add(myCountLabel, BorderLayout.EAST);
          myRendererComponent.add(myExtraPanel, BorderLayout.CENTER);
        } else {
          super.layout();
        }
      }

      @Override
      public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        myCountLabel.setText("");
        final Component component = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        if ("Problems".equals(descriptor.getTextFor(value))) {
          final ErrorPaneConfigurable errorPane = (ErrorPaneConfigurable)((Place)value).getPath("category");
          if (errorPane != null && errorPane.getErrorsCount() > 0) {
            myCountLabel.setSelected(isSelected);
            myCountLabel.setText(String.valueOf(errorPane.getErrorsCount()));
          }
        }
        return component;
      }

      @Override
      protected JComponent createItemComponent() {
        myExtraPanel = new NonOpaquePanel(new BorderLayout());
        myCountLabel = new CountLabel();
        final JComponent component = super.createItemComponent();

        if (Registry.is("ide.new.project.settings")) {
          myTextLabel.setForeground(Gray._240);
          myTextLabel.setOpaque(true);
        }

        return component;
      }

      @Override
      protected Color getBackground() {
        return Registry.is("ide.new.project.settings") ? UIUtil.getSidePanelColor() : super.getBackground();
      }
    });

    add(ScrollPaneFactory.createScrollPane(myList, Registry.is("ide.new.project.settings")), BorderLayout.CENTER);
    myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    myList.addListSelectionListener(new ListSelectionListener() {
      @Override
      public void valueChanged(final ListSelectionEvent e) {
        if (e.getValueIsAdjusting()) return;
        final Object value = myList.getSelectedValue();
        if (value != null) {
          myNavigator.navigateTo(((Place)value), false);
        }
      }
    });
  }

  public JList getList() {
    return myList;
  }

  public void addPlace(Place place, @NotNull Presentation presentation) {
    myModel.addElement(place);
    myPlaces.add(place);
    myPlace2Presentation.put(place, presentation);
    revalidate();
    repaint();
  }

  public void addSeparator(String text) {
    myIndex2Separator.put(myPlaces.size(), text);
  }

  public Collection<Place> getPlaces() {
    return myPlaces;
  }

  public void select(final Place place) {
    myList.setSelectedValue(place, true);
  }

  private static class CountLabel extends JLabel {
    private boolean mySelected;

    public CountLabel() {
      super();
      setBorder(new Border() {
        @Override
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        }

        @Override
        public Insets getBorderInsets(Component c) {
          return StringUtil.isEmpty(getText()) ? new Insets(0,0,0,0) : new Insets(2, 6, 2, 6 + 6);
        }

        @Override
        public boolean isBorderOpaque() {
          return false;
        }
      });
      setFont(UIUtil.getListFont().deriveFont(Font.BOLD));
    }

    public boolean isSelected() {
      return mySelected;
    }

    public void setSelected(boolean selected) {
      mySelected = selected;
    }

    @Override
    protected void paintComponent(Graphics g) {
      g.setColor(isSelected() ? UIUtil.getListSelectionBackground() : UIUtil.getSidePanelColor());
      g.fillRect(0, 0, getWidth(), getHeight());
      if (StringUtil.isEmpty(getText())) return;
      final JBColor deepBlue = new JBColor(new Color(0x97A4B2), new Color(92, 98, 113));
      g.setColor(isSelected() ? Gray._255.withAlpha(UIUtil.isUnderDarcula() ? 100 : 220) : deepBlue);
      final GraphicsConfig config = GraphicsUtil.setupAAPainting(g);
      g.fillRoundRect(0, 3, getWidth() - 6 -1, getHeight()-6 , (getHeight() - 6), (getHeight() - 6));
      config.restore();
      setForeground(isSelected() ? deepBlue.darker() : UIUtil.getListForeground(true));

      super.paintComponent(g);
    }
  }
}