summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/ui/Splash.java
blob: 0f4147f61753f1e51fc838d4cd4b67a191c6788d (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
/*
 * 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.ide.StartupProgress;
import com.intellij.openapi.application.ex.ApplicationInfoEx;
import com.intellij.openapi.application.impl.ApplicationInfoImpl;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.Nullable;

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


/**
 * To customize your IDE splash go to YourIdeNameApplicationInfo.xml and find
 * section corresponding to IDE logo. It should look like:
 * <p>
 *   &lt;logo url=&quot;/idea_logo.png&quot; textcolor=&quot;919191&quot; progressColor=&quot;264db5&quot; progressY=&quot;235&quot;/&gt;
 * </p>
 * <p>where <code>url</code> is path to your splash image
 * <p><code>textColor</code> is HEX representation of text color for user name
 * <p><code>progressColor</code> is progress bar color
 * <p><code>progressY</code> is Y coordinate of the progress bar
 * <p><code>progressTailIcon</code> is a path to flame effect icon
 *
 * @author Konstantin Bulenkov
 */
public class Splash extends JDialog implements StartupProgress {
  @Nullable public static Rectangle BOUNDS;
  private final Icon myImage;
  private int myProgressHeight = 2;
  private Color myProgressColor = null;
  private int myProgressY;
  private float myProgress;
  private boolean mySplashIsVisible;
  private int myProgressLastPosition = 0;
  private final JLabel myLabel;
  private Icon myProgressTail;

  public Splash(String imageName, final Color textColor) {
    super((Frame)null, false);

    setUndecorated(true);
    if (!(SystemInfo.isLinux && SystemInfo.isJavaVersionAtLeast("1.7"))) {
      setResizable(false);
    }
    setFocusableWindowState(false);

    Icon originalImage = IconLoader.getIcon(imageName);
    myImage = new SplashImage(originalImage, textColor);
    myLabel = new JLabel(myImage) {
      @Override
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        mySplashIsVisible = true;
        myProgressLastPosition = 0;
        paintProgress(g);
      }
    };
    Container contentPane = getContentPane();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(myLabel, BorderLayout.CENTER);
    Dimension size = getPreferredSize();
    setSize(size);
    pack();
    setLocationInTheCenterOfScreen();
  }

  private void setLocationInTheCenterOfScreen() {
    Rectangle deviceBounds = getGraphicsConfiguration().getBounds();
    setLocation((deviceBounds.width - getWidth()) / 2, (deviceBounds.height - getHeight()) / 2);
  }

  public Splash(ApplicationInfoEx info) {
    this(info.getSplashImageUrl(), info.getSplashTextColor());
    if (info instanceof ApplicationInfoImpl) {
      final ApplicationInfoImpl appInfo = (ApplicationInfoImpl)info;
      myProgressHeight = 2;
      myProgressColor = appInfo.getProgressColor();
      myProgressY = appInfo.getProgressY();
      myProgressTail = appInfo.getProgressTailIcon();
    }
  }

  @SuppressWarnings("deprecation")
  public void show() {
    super.show();
    toFront();
    //noinspection AssignmentToStaticFieldFromInstanceMethod
    BOUNDS = getBounds();
    //Sometimes this causes deadlock in EDT
    // http://bugs.sun.com/view_bug.do?bug_id=6724890
    //
    //myLabel.paintImmediately(0, 0, myImage.getIconWidth(), myImage.getIconHeight());
  }

  @Override
  public void showProgress(String message, float progress) {
    if (getProgressColor() == null) return;
    //myMessage = message;
    myProgress = progress;
    myLabel.paintImmediately(0, 0, myImage.getIconWidth(), myImage.getIconHeight());
  }

  private void paintProgress(Graphics g) {
    final Color color = getProgressColor();
    if (color == null) return;

    if (!mySplashIsVisible) {
      myImage.paintIcon(this, g, 0, 0);
      mySplashIsVisible = true;
    }

    final int progressWidth = (int)((myImage.getIconWidth() - 2) * myProgress);
    final int width = progressWidth - myProgressLastPosition;
    g.setColor(color);
    g.fillRect(1, getProgressY(), width, getProgressHeight());
    if (myProgressTail != null) {
      myProgressTail.paintIcon(this, g, width - (myProgressTail.getIconWidth()/2), getProgressY() - (myProgressTail.getIconHeight() - getProgressHeight())/2);
    }
    myProgressLastPosition = progressWidth;
  }

  private int getProgressHeight() {
    return myProgressHeight;
  }

  private Color getProgressColor() {
    return myProgressColor;
  }

  private int getProgressY() {
    return myProgressY;
  }

  public static boolean showLicenseeInfo(Graphics g, int x, int y, final int height, final Color textColor) {
    if (ApplicationInfoImpl.getShadowInstance().showLicenseeInfo()) {
      final LicensingFacade provider = LicensingFacade.getInstance();
      if (provider != null) {
        UIUtil.applyRenderingHints(g);
        g.setFont(new Font(UIUtil.ARIAL_FONT_NAME, Font.BOLD, SystemInfo.isUnix ? 10 : 11));

        g.setColor(textColor);
        final String licensedToMessage = provider.getLicensedToMessage();
        final List<String> licenseRestrictionsMessages = provider.getLicenseRestrictionsMessages();
        g.drawString(licensedToMessage, x + 15, y + height - 30);
        if (licenseRestrictionsMessages.size() > 0) {
          g.drawString(licenseRestrictionsMessages.get(0), x + 15, y + height - 14);
        }
      }
      return true;
    }
    return false;
  }

  private static final class SplashImage implements Icon {
    private final Icon myIcon;
    private final Color myTextColor;
    private boolean myRedrawing;

    public SplashImage(Icon originalIcon, Color textColor) {
      myIcon = originalIcon;
      myTextColor = textColor;
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
      if (!myRedrawing) {
        try {
          Thread.sleep(10);
        } catch (InterruptedException ignore) {}
        myRedrawing = true;
      }

      myIcon.paintIcon(c, g, x, y);

      showLicenseeInfo(g, x, y, getIconHeight(), myTextColor);
    }

    public int getIconWidth() {
      return myIcon.getIconWidth();
    }

    public int getIconHeight() {
      return myIcon.getIconHeight();
    }
  }

  //public static void main(String[] args) {
  //  final ImageIcon icon = new ImageIcon("c:\\IDEA\\ultimate\\ultimate-resources\\src\\progress_tail.png");
  //
  //  final int w = icon.getIconWidth();
  //  final int h = icon.getIconHeight();
  //  final BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment()
  //    .getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(w, h, Color.TRANSLUCENT);
  //  final Graphics2D g = image.createGraphics();
  //  icon.paintIcon(null, g, 0, 0);
  //  g.dispose();
  //
  //  for (int y = 0; y < image.getHeight(); y++) {
  //    for (int x = 0; x < image.getWidth(); x++) {
  //      final Color c = new Color(image.getRGB(x, y), true);
  //      System.out.print(String.format("[%3d,%3d,%3d,%3d]  ", c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()));
  //    }
  //    System.out.println("");
  //  }
  //}
}