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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package com.vci.client.common;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
 
import com.vci.client.LogonApplication;
import com.vci.client.logon.client.StatusBar;
import com.vci.client.ui.swing.VCISwingUtil;
import com.vci.client.ui.swing.VCITitleBar;
import com.vci.client.ui.swing.components.VCIJButton;
import com.vci.client.ui.swing.components.VCIJFrame;
import com.vci.client.ui.swing.components.VCIJLabel;
import com.vci.client.ui.swing.components.VCIJPanel;
import com.vci.client.ui.swing.components.VCIJTextArea;
 
public class PositionMessager {
    
    private int x = 0;
    private int y = 0;
    private int width = 320;
    private int height = 200;
 
    private int scrollPosition = 3;
    private int scrollLazy = 10;
    private int showTimes = 3000;
    private boolean pause = false;
    private boolean showing = false;
    private boolean closeing = false;
 
    private Frame frame = null;
    private String title = "";
    private String content = "";
    
    public PositionMessager(){
        setScrollPosition(scrollPosition);
        setScrollLazy(scrollLazy);
        setShowTimes(showTimes);
        setPause(false);
    }
    
    public void show(String content){
        show("信息提示", content);
    }
    
    public void show(final String title, final String content){
        setTitle(title);
        setContent(content);
        new Thread(new Runnable() {
            @Override
            public void run() {
                showThreadRun(title, content);
            }
        }).start();
    }
    
    private void showThreadRun(String title, String content){
        frame = getShowFrame(title, content);
        
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();   
        Insets screenInsets  = Toolkit.getDefaultToolkit().getScreenInsets(LogonApplication.frame.getGraphicsConfiguration()); 
        x = (int) (dim.getWidth() - width - 3);
        y = (int) (dim.getHeight() - screenInsets.bottom - 3);
        StatusBar.getInstance().setStatusMessage(content);
        frame.setSize(new Dimension(width, height));
        frame.setVisible(true);
        show(frame);
    }
    
    private void show(Frame frame){
        setShowing(true);
        btnClose.setEnabled(false);
        for (int i = 0; i <= height; ) {
            try {
                frame.setLocation(x, y - i);
                Thread.sleep(getScrollLazy());
                
                if(isPause()) continue;
                i += getScrollPosition();
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
        btnClose.setEnabled(true);
        setShowing(false);
        
        try {
            Thread.sleep(getShowTimes());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        close(frame);
    }
    
    private void close(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                close(frame);
            }
        }).start();
    }
    
    private void close(Frame frame){
        if(isShowing()) return;
        if(isCloseing()) return;
        
        x = frame.getX();
        y = frame.getY();
        setCloseing(true);
        for (int i = 0; i <= height; ) {
            try {
                frame.setLocation(x, y + i);
                Thread.sleep(getScrollLazy());
                
                if(isPause()) continue;
                i += getScrollPosition();
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
        setCloseing(false);
        frame.dispose();
        frame.setVisible(false);
    }
    
    private Frame getShowFrame(String title, String content){
        VCIJFrame frame = new VCIJFrame(title);
        frame.setUndecorated(true);
        // 禁用默认的关闭处理程序
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        // 不显示最大、最小化按钮图标
        frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
        // 加入自定义的关闭处理程序
        frame.addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent e) {
                 close();
             }
        });
        // 加入鼠标事件,控制滚动是否是否需要暂停
        frame.addMouseListener(getMouseAdapter());
        frame.setResizable(false);
        frame.setAlwaysOnTop(true);
        frame.setLayout(new BorderLayout());
        frame.add(getContentPanel(), BorderLayout.CENTER);
        return frame;
    }
    
    private VCIJPanel getContentPanel(){
        VCIJPanel pal = new VCIJPanel(new BorderLayout());
        pal.setBackground(Color.black);
        if(isNeedAddTitleBar()){
            pal.add(getTopButtonPanel(), BorderLayout.NORTH);
        }
        pal.add(getCenterContentPanel(), BorderLayout.CENTER);
        pal.add(getSouthButtonPanel(), BorderLayout.SOUTH);
        pal.addMouseListener(getMouseAdapter());
        return pal;
    }
    private boolean isNeedAddTitleBar(){
        boolean res = true;
        LookAndFeel laf = UIManager.getLookAndFeel();
        if(laf.getClass().getSimpleName().contains("PlasticLookAndFeel")){
            res = false;
        }
        return res;
    }
    private VCIJPanel getTopButtonPanel(){
        VCIJPanel pal = new VCIJPanel(new BorderLayout());
        VCITitleBar tb = new VCITitleBar(getTitle(), "information.png");
        tb.setPreferredSize(new Dimension(100, 25));
        tb.addMouseListener(getMouseAdapter());
        pal.addMouseListener(getMouseAdapter());
        pal.add(tb, BorderLayout.NORTH);
        return pal;
    }
    
    private VCIJPanel getCenterContentPanel(){
        VCIJPanel pal = new VCIJPanel(new BorderLayout(20, 0));
        VCIJTextArea txt = new VCIJTextArea(getContent());
        txt.setWrapStyleWord(true);
        txt.setLineWrap(true);
        txt.setEditable(false);
        txt.setBorder(BorderFactory.createEmptyBorder());
        txt.addMouseListener(getMouseAdapter());
        pal.addMouseListener(getMouseAdapter());
        pal.add(new VCIJLabel("  "), BorderLayout.NORTH);
        pal.add(new VCIJLabel(""), BorderLayout.WEST);
        pal.add(txt, BorderLayout.CENTER);
        pal.add(new VCIJLabel(""), BorderLayout.EAST);
        pal.add(new VCIJLabel(""), BorderLayout.SOUTH);
        pal.setBorder(BorderFactory.createEmptyBorder());
        return pal;
    }
    private VCIJPanel getSouthButtonPanel(){
        VCIJPanel pal = new VCIJPanel(new FlowLayout(FlowLayout.CENTER));
        pal.add(getCloseButton());
        pal.addMouseListener(getMouseAdapter());
        return pal;
    }
 
    private VCIJButton btnClose = VCISwingUtil.createVCIJButton("close", "关闭", "关闭", "close.png");
    private VCIJButton getCloseButton(){
        btnClose.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btnClose.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                close();
            }
        });
        btnClose.addMouseListener(getMouseAdapter());
        return btnClose;
    }
 
    private MouseAdapter getMouseAdapter(){
        return new MouseAdapter() {
            public void mouseMoved(MouseEvent e) {
                setPauseAndCurosor(e, true);
            }
 
            public void mouseClicked(MouseEvent e) {
                setPauseAndCurosor(e, true);
            }
 
            public void mouseEntered(MouseEvent e) {
                setPauseAndCurosor(e, true);
            }
 
            public void mouseExited(MouseEvent e) {
                setPauseAndCurosor(e, false);
            }
        };
    }
    
    private boolean mouseLoctionIsCloseButtonRegion(MouseEvent e){
        boolean res = false;
        int mex = e.getX();
        int mey = e.getY();
        int imgRegWidth = 32;
        int imgRegHeight = 16;
        if(frame.getWidth() - imgRegWidth <= mex && mex <= frame.getWidth() &&
                0 <= mey && mey <= imgRegHeight){
            res = true;
        }
        return res;
    }
    
    private void setPauseAndCurosor(MouseEvent e, boolean pause){
        Object source = e.getSource();
        if(mouseLoctionIsCloseButtonRegion(e)){
            pause = true;
        }
        setPause(pause);
        boolean sourceIsComponent = Component.class.isAssignableFrom(source.getClass());
        if(sourceIsComponent){
            Component compt = (Component)source;
            if(pause){
                compt.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            } else {
                compt.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
        }
    }
    
    public int getScrollPosition() {
        return scrollPosition;
    }
    public void setScrollPosition(int scrollPosition) {
        this.scrollPosition = scrollPosition;
    }
    public int getScrollLazy() {
        return scrollLazy;
    }
    public void setScrollLazy(int scrollLazy) {
        this.scrollLazy = scrollLazy;
    }
    public int getShowTimes() {
        return showTimes;
    }
 
    public void setShowTimes(int showTimes) {
        this.showTimes = showTimes;
    }
 
    public boolean isPause() {
        return pause;
    }
 
    public void setPause(boolean pause) {
        this.pause = pause;
    }
    public boolean isShowing() {
        return showing;
    }
 
    public void setShowing(boolean showing) {
        this.showing = showing;
    }
 
    public boolean isCloseing() {
        return closeing;
    }
    public void setCloseing(boolean closeing) {
        this.closeing = closeing;
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
 
    public String getContent() {
        return content;
    }
 
    public void setContent(String content) {
        this.content = content;
    }
    
}