summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/ui/BalloonLayoutImpl.java
blob: 1417d0e75ac56c57f176020f72dd1d6450c5d1f6 (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
/*
 * 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.ui;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.util.Disposer;
import com.intellij.util.Alarm;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ArrayList;
import java.util.List;

public class BalloonLayoutImpl implements BalloonLayout {

  private final JLayeredPane myParent;
  private final Insets myInsets;

  private final List<Balloon> myBalloons = new ArrayList<Balloon>();

  private final Alarm myRelayoutAlarm = new Alarm();
  private final Runnable myRelayoutRunnable = new Runnable() {
    public void run() {
      relayout();
    }
  };

  public BalloonLayoutImpl(@NotNull JLayeredPane parent, @NotNull Insets insets) {
    myParent = parent;
    myInsets = insets;
    myParent.addComponentListener(new ComponentAdapter() {
      @Override
      public void componentResized(ComponentEvent e) {
        queueRelayout();
      }
    });
  }

  @Override
  public void add(final Balloon balloon) {
    myBalloons.add(balloon);
    Disposer.register(balloon, new Disposable() {
      public void dispose() {
        myBalloons.remove(balloon);
        queueRelayout();
      }
    });

    relayout();
    balloon.show(myParent);
  }


  private void queueRelayout() {
    myRelayoutAlarm.cancelAllRequests();
    myRelayoutAlarm.addRequest(myRelayoutRunnable, 200);
  }

  private void relayout() {
    final Dimension size = myParent.getSize();

    size.width -= myInsets.left + myInsets.right;
    size.height -= myInsets.top + myInsets.bottom;

    final Rectangle layoutRec = new Rectangle(new Point(myInsets.left, myInsets.top), size);

    List<ArrayList<Balloon>> columns = createColumns(layoutRec);
    while (columns.size() > 1) {
      myBalloons.remove(0);
      columns = createColumns(layoutRec);
    }
    List<Integer> columnWidths = computeWidths(columns);

    int eachCoumnX = (int)layoutRec.getMaxX(); 
    for (int i = 0; i < columns.size(); i++) {
      final ArrayList<Balloon> eachColumn = columns.get(i);
      final Integer eachWidth = columnWidths.get(i);
      eachCoumnX -= eachWidth.intValue();
      int eachY = layoutRec.y + 47; //todo[kb] calculate Y offset so balloon places exactly editor's top-right corner
      for (Balloon eachBalloon : eachColumn) {
        final Rectangle eachRec = new Rectangle();
        final Dimension eachPrefSize = eachBalloon.getPreferredSize();
        eachRec.setSize(eachPrefSize);
        if (((BalloonImpl)eachBalloon).hasShadow()) {
          final int shadowSize = ((BalloonImpl)eachBalloon).getShadowBorderSize();
          eachRec.width += 2 * shadowSize;
          eachRec.height += 2 * shadowSize;
        }
        eachY+=2; //space between two notifications
        eachRec.setLocation(eachCoumnX + eachWidth.intValue() - eachRec.width - 14, eachY);
        eachBalloon.setBounds(eachRec);
        eachY += eachRec.height;
      }
    }
  }



  private static List<Integer> computeWidths(List<ArrayList<Balloon>> columns) {
    List<Integer> columnWidths = new ArrayList<Integer>();
    for (ArrayList<Balloon> eachColumn : columns) {
      int maxWidth = 0;
      for (Balloon each : eachColumn) {
        maxWidth = Math.max(each.getPreferredSize().width, maxWidth);
      }
      columnWidths.add(maxWidth);
    }
    return columnWidths;
  }

  private List<ArrayList<Balloon>> createColumns(Rectangle layoutRec) {
    List<ArrayList<Balloon>> columns = new ArrayList<ArrayList<Balloon>>();

    ArrayList<Balloon> eachColumn = new ArrayList<Balloon>();
    columns.add(eachColumn);

    int eachColumnHeight = 0;
    for (Balloon each : myBalloons) {
      final Dimension eachSize = each.getPreferredSize();
      if (eachColumnHeight + eachSize.height > layoutRec.getHeight()) {
        eachColumn = new ArrayList<Balloon>();
        columns.add(eachColumn);
        eachColumnHeight = 0;
      }
      eachColumn.add(each);
      eachColumnHeight += eachSize.height;
    }
    return columns;
  }

}