gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / java / org / gvsig / scripting / app / extension / messagewait / MessageWait.java @ 955
History | View | Annotate | Download (3.08 KB)
1 | 955 | jjdelcerro | package org.gvsig.scripting.app.extension.messagewait; |
---|---|---|---|
2 | |||
3 | import java.awt.Dimension; |
||
4 | import java.awt.event.ActionEvent; |
||
5 | import java.awt.event.ActionListener; |
||
6 | import java.net.URL; |
||
7 | import javax.swing.ImageIcon; |
||
8 | import javax.swing.SwingUtilities; |
||
9 | import org.gvsig.tools.ToolsLocator; |
||
10 | import org.gvsig.tools.i18n.I18nManager; |
||
11 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
12 | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
||
13 | |||
14 | |||
15 | public class MessageWait extends MessageWaitView { |
||
16 | |||
17 | private static final long serialVersionUID = 6668980523307140920L; |
||
18 | private WaitingCondition condition;
|
||
19 | private int seconds = 3; |
||
20 | private boolean cancelled = false; |
||
21 | private Runnable action; |
||
22 | |||
23 | public interface WaitingCondition { |
||
24 | |||
25 | public boolean stopWaiting(); |
||
26 | } |
||
27 | |||
28 | public MessageWait() {
|
||
29 | this.btnCancel.addActionListener(new ActionListener() { |
||
30 | @Override
|
||
31 | public void actionPerformed(ActionEvent e) { |
||
32 | cancelled = true;
|
||
33 | setVisible(false);
|
||
34 | } |
||
35 | }); |
||
36 | URL url = this.getClass().getResource("/images/spinner.gif"); |
||
37 | ImageIcon icon = new ImageIcon(url); |
||
38 | this.lblIcon.setIcon(icon);
|
||
39 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
40 | this.btnCancel.setText(i18n.getTranslation("Cancel")); |
||
41 | } |
||
42 | |||
43 | public void showMessage(String title, String msg, WaitingCondition condition, Runnable action) { |
||
44 | this.showMessage(title, msg, condition, action, 300, -1, 3); |
||
45 | } |
||
46 | |||
47 | public void showMessage(String title, String msg, WaitingCondition condition, Runnable action, int width, int height, int seconds) { |
||
48 | if( condition.stopWaiting() ) {
|
||
49 | SwingUtilities.invokeLater(action);
|
||
50 | return;
|
||
51 | } |
||
52 | this.setVisible(true); |
||
53 | this.condition = condition;
|
||
54 | this.seconds = seconds;
|
||
55 | this.action = action;
|
||
56 | this.cancelled = false; |
||
57 | this.lblMsg.setText("<html>"+msg.replace("\n", "<br>\n")+"</html>"); |
||
58 | Dimension d = this.getPreferredSize(); |
||
59 | if( height<d.height ) {
|
||
60 | height = d.height; |
||
61 | } |
||
62 | if( width<d.width ) {
|
||
63 | width = d.width; |
||
64 | } |
||
65 | this.setPreferredSize(new Dimension(width, height)); |
||
66 | WindowManager winmgr = ToolsSwingLocator.getWindowManager(); |
||
67 | winmgr.showWindow(this, title, WindowManager.MODE.TOOL);
|
||
68 | Thread th = new Thread(new Runnable() { |
||
69 | @Override
|
||
70 | public void run() { |
||
71 | doWait(); |
||
72 | } |
||
73 | }, "MessageWait");
|
||
74 | th.start(); |
||
75 | } |
||
76 | |||
77 | private void doWait() { |
||
78 | while( true ) { |
||
79 | if( this.condition.stopWaiting() ) { |
||
80 | break;
|
||
81 | } |
||
82 | try {
|
||
83 | Thread.sleep(this.seconds*1000); |
||
84 | } catch (InterruptedException ex) { |
||
85 | |||
86 | } |
||
87 | } |
||
88 | if( this.cancelled ) { |
||
89 | return;
|
||
90 | } |
||
91 | SwingUtilities.invokeLater(new Runnable() { |
||
92 | @Override
|
||
93 | public void run() { |
||
94 | setVisible(false);
|
||
95 | action.run(); |
||
96 | } |
||
97 | }); |
||
98 | } |
||
99 | } |