summaryrefslogtreecommitdiff
path: root/src/main/org/apache/tools/ant/taskdefs/optional/splash/SplashScreen.java
blob: e7d1c31c370a9737931140157b67081ad4494414 (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
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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 org.apache.tools.ant.taskdefs.optional.splash;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;

import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.BuildListener;
import org.apache.tools.ant.Project;

class SplashScreen extends JWindow implements ActionListener, BuildListener {
    private static final long serialVersionUID = 1L;
    private static final int FONT_SIZE = 12;
    private JLabel text;
    private JProgressBar pb;
    private int total;
    private static final int MIN = 0;
    private static final int MAX = 200;
    private Pattern progressRegExpPattern;

    public SplashScreen(String msg) {
        this(msg, null, null);
    }

    public SplashScreen(ImageIcon img) {
        this(img, null, null);
    }

    public SplashScreen(String msg, String progressRegExp, String displayText) {
        init(null, progressRegExp, displayText);
        setText(msg);
    }

    public SplashScreen(ImageIcon img, String progressRegExp,
                        String displayText) {
        init(img, progressRegExp, displayText);
    }

    protected void init(ImageIcon img) {
        init(img, null, null);
    }

    protected void init(ImageIcon img, String progressRegExp,
                        String displayText) {
        if (progressRegExp != null) {
            progressRegExpPattern = Pattern.compile(progressRegExp);
        }

        JPanel pan = (JPanel) getContentPane();
        JLabel piccy;
        if (img == null) {
            piccy = new JLabel();
        } else {
            piccy = new JLabel(img);
        }

        piccy.setBorder(BorderFactory.createLineBorder(Color.black, 1));
        if (displayText == null) {
            displayText = "Building....";
        }
        text = new JLabel(displayText, JLabel.CENTER);
        text.setFont(new Font("Sans-Serif", Font.BOLD, FONT_SIZE));
        text.setBorder(BorderFactory.createEtchedBorder());

        pb = new JProgressBar(MIN, MAX);
        pb.setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        JPanel pan2 = new JPanel();
        pan2.setLayout(new BorderLayout());

        pan2.add(text, BorderLayout.NORTH);
        pan2.add(pb, BorderLayout.SOUTH);

        pan.setLayout(new BorderLayout());
        pan.add(piccy, BorderLayout.CENTER);
        pan.add(pan2, BorderLayout.SOUTH);

        pan.setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        pack();

        Dimension size = getSize();
        Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (scr.width - size.width) / 2;
        int y = (scr.height - size.height) / 2;
        setBounds(x, y, size.width, size.height);
    }

    public void setText(String txt) {
        text.setText(txt);
    }

    @Override
    public void actionPerformed(ActionEvent a) {
        if (!hasProgressPattern()) {
            if (total < MAX) {
                total++;
            } else {
                total = MIN;
            }
            pb.setValue(total);
        }
    }

    @Override
    public void buildStarted(BuildEvent event) {
        actionPerformed(null);
    }

    @Override
    public void buildFinished(BuildEvent event) {
        pb.setValue(MAX);
        setVisible(false);
        dispose();
    }
    @Override
    public void targetStarted(BuildEvent event) {
        actionPerformed(null);
    }

    @Override
    public void targetFinished(BuildEvent event) {
        actionPerformed(null);
    }

    @Override
    public void taskStarted(BuildEvent event) {
        actionPerformed(null);
    }

    @Override
    public void taskFinished(BuildEvent event) {
        actionPerformed(null);
    }

    @Override
    public void messageLogged(BuildEvent event) {
        actionPerformed(null);
        if (hasProgressPattern()) {
            String message = event.getMessage();
            Matcher matcher = progressRegExpPattern.matcher(message);
            if (matcher != null && matcher.matches()) {
                String gr = matcher.group(1);
                try {
                    pb.setValue(Math.min(Integer.parseInt(gr) * 2, MAX));
                } catch (NumberFormatException e) {
                    event.getProject().log(
                        "Number parsing error in progressRegExp",
                        Project.MSG_VERBOSE);
                }
            }
        }
    }

    protected boolean hasProgressPattern() {
        return progressRegExpPattern != null;
    }

}