Added
Link Here
|
1 |
/* |
2 |
* The contents of this file are subject to the terms of the Common Development |
3 |
* and Distribution License (the License). You may not use this file except in |
4 |
* compliance with the License. |
5 |
* |
6 |
* You can obtain a copy of the License at http://www.netbeans.org/cddl.html |
7 |
* or http://www.netbeans.org/cddl.txt. |
8 |
* |
9 |
* When distributing Covered Code, include this CDDL Header Notice in each file |
10 |
* and include the License file at http://www.netbeans.org/cddl.txt. |
11 |
* If applicable, add the following below the CDDL Header, with the fields |
12 |
* enclosed by brackets [] replaced by your own identifying information: |
13 |
* "Portions Copyrighted [year] [name of copyright owner]" |
14 |
* |
15 |
* Portions Copyrighted 2007 Sun Microsystems, Inc. |
16 |
*/ |
17 |
package org.netbeans.api.java.source.ui; |
18 |
|
19 |
import java.awt.Dialog; |
20 |
import java.awt.event.ActionEvent; |
21 |
import java.awt.event.ActionListener; |
22 |
import java.io.IOException; |
23 |
import java.net.URL; |
24 |
import java.util.concurrent.Future; |
25 |
import javax.swing.JLabel; |
26 |
import javax.swing.SwingConstants; |
27 |
import javax.swing.SwingUtilities; |
28 |
import javax.swing.border.EmptyBorder; |
29 |
import org.netbeans.api.java.platform.JavaPlatform; |
30 |
import org.netbeans.api.java.source.ClasspathInfo; |
31 |
import org.netbeans.api.java.source.CompilationController; |
32 |
import org.netbeans.api.java.source.JavaSource; |
33 |
import org.netbeans.api.java.source.SourceUtils; |
34 |
import org.netbeans.api.java.source.Task; |
35 |
import org.netbeans.spi.java.classpath.support.ClassPathSupport; |
36 |
import org.openide.DialogDescriptor; |
37 |
import org.openide.DialogDisplayer; |
38 |
import org.openide.util.Exceptions; |
39 |
import org.openide.util.NbBundle; |
40 |
|
41 |
/** |
42 |
* Support for notifying user about the background scan. |
43 |
* @since 1.2 |
44 |
* @author Tomas Zezula |
45 |
*/ |
46 |
public class ScanDialog { |
47 |
|
48 |
/** |
49 |
* This is a helper method to provide support for delaying invocations of actions |
50 |
* depending on java model. |
51 |
* <br>Behavior of this method is following:<br> |
52 |
* If classpath scanning is not in progress, runnable's run() is called. <br> |
53 |
* If classpath scanning is in progress, modal cancellable notification dialog with specified |
54 |
* tile is opened. |
55 |
* </ul> |
56 |
* As soon as classpath scanning finishes, this dialog is closed and runnable's run() is called. |
57 |
* This method must be called in AWT EventQueue. Runnable is performed in AWT thread. |
58 |
* |
59 |
* @param runnable Runnable instance which will be called. |
60 |
* @param actionName Title of wait dialog. |
61 |
* @return true action was cancelled <br> |
62 |
* false action was performed |
63 |
*/ |
64 |
public static boolean runWhenScanFinished (final Runnable runnable, final String actionName) { |
65 |
assert runnable != null; |
66 |
assert actionName != null; |
67 |
assert SwingUtilities.isEventDispatchThread(); |
68 |
if (SourceUtils.isScanInProgress()) { |
69 |
|
70 |
class AL implements ActionListener { |
71 |
|
72 |
private Dialog dialog; |
73 |
private Future<Void> monitor; |
74 |
|
75 |
public synchronized void start (final Future<Void> monitor) { |
76 |
assert monitor != null; |
77 |
this.monitor = monitor; |
78 |
if (dialog != null) { |
79 |
dialog.setVisible(true); |
80 |
} |
81 |
} |
82 |
|
83 |
public void actionPerformed(ActionEvent e) { |
84 |
monitor.cancel(false); |
85 |
close (); |
86 |
} |
87 |
|
88 |
synchronized void close () { |
89 |
if (dialog != null) { |
90 |
dialog.setVisible(false); |
91 |
dialog.dispose(); |
92 |
dialog = null; |
93 |
} |
94 |
} |
95 |
}; |
96 |
final AL listener = new AL (); |
97 |
JLabel label = new JLabel(NbBundle.getMessage(ScanDialog.class,"MSG_WaitScan"), |
98 |
javax.swing.UIManager.getIcon("OptionPane.informationIcon"), SwingConstants.LEFT); |
99 |
label.setBorder(new EmptyBorder(12,12,11,11)); |
100 |
DialogDescriptor dd = new DialogDescriptor(label, actionName, true, new Object[]{NbBundle.getMessage(ScanDialog.class,"LBL_CancelAction",actionName)}, null, 0, null, listener); |
101 |
listener.dialog = DialogDisplayer.getDefault().createDialog(dd); |
102 |
listener.dialog.pack(); |
103 |
final ClasspathInfo info = ClasspathInfo.create(JavaPlatform.getDefault().getBootstrapLibraries(), |
104 |
ClassPathSupport.createClassPath(new URL[0]), |
105 |
ClassPathSupport.createClassPath(new URL[0])); |
106 |
final JavaSource js = JavaSource.create(info); |
107 |
try { |
108 |
Future<Void> monitor = js.runWhenScanFinished(new Task<CompilationController>() { |
109 |
public void run(CompilationController parameter) throws Exception { |
110 |
SwingUtilities.invokeLater(new Runnable () { |
111 |
public void run () { |
112 |
listener.close(); |
113 |
runnable.run(); |
114 |
} |
115 |
}); |
116 |
} |
117 |
}, true); |
118 |
if (!monitor.isDone()) { |
119 |
listener.start(monitor); |
120 |
} |
121 |
return monitor.isCancelled(); |
122 |
}catch (IOException e) { |
123 |
Exceptions.printStackTrace(e); |
124 |
return true; |
125 |
} |
126 |
} else { |
127 |
runnable.run(); |
128 |
return false; |
129 |
} |
130 |
} |
131 |
|
132 |
} |