Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2011 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.refactoring.java.spi.ui; |
43 |
|
44 |
import java.awt.Dialog; |
45 |
import java.awt.event.ActionEvent; |
46 |
import java.awt.event.ActionListener; |
47 |
import java.util.concurrent.atomic.AtomicBoolean; |
48 |
import javax.swing.JLabel; |
49 |
import javax.swing.SwingConstants; |
50 |
import javax.swing.SwingUtilities; |
51 |
import javax.swing.border.EmptyBorder; |
52 |
import org.netbeans.api.java.source.SourceUtils; |
53 |
import org.netbeans.modules.refactoring.java.RefactoringUtils; |
54 |
import org.openide.DialogDescriptor; |
55 |
import org.openide.DialogDisplayer; |
56 |
import org.openide.util.Exceptions; |
57 |
import org.openide.util.NbBundle; |
58 |
import org.openide.util.RequestProcessor; |
59 |
|
60 |
/** |
61 |
* UI helper class. |
62 |
* @author Jan Becicka |
63 |
* @since 1.33 |
64 |
*/ |
65 |
public class UI { |
66 |
|
67 |
/** |
68 |
* This is a helper method to provide support for delaying invocations of |
69 |
* actions depending on java model. |
70 |
* <br> |
71 |
* If classpath scanning is not in progress, runnable's run() is called. <br> |
72 |
* If classpath scanning is in progress, modal cancellable notification |
73 |
* dialog with specified title is opened. </ul> As soon as classpath scanning |
74 |
* finishes, this dialog is closed and runnable's run() is called. This |
75 |
* method must be called in AWT EventQueue. Runnable is performed in AWT |
76 |
* thread. |
77 |
* |
78 |
* @param runnable Runnable instance which will be called. |
79 |
* @param actionName Title of wait dialog. |
80 |
* @return true action was cancelled <br> false action was performed |
81 |
*/ |
82 |
public static boolean invokeAfterScanFinished(final Runnable runnable, final String actionName) { |
83 |
assert SwingUtilities.isEventDispatchThread(); |
84 |
if (SourceUtils.isScanInProgress()) { |
85 |
final ActionPerformer ap = new ActionPerformer(runnable); |
86 |
ActionListener listener = new ActionListener() { |
87 |
|
88 |
@Override |
89 |
public void actionPerformed(ActionEvent e) { |
90 |
ap.cancel(); |
91 |
waitTask.cancel(); |
92 |
} |
93 |
}; |
94 |
JLabel label = new JLabel(NbBundle.getMessage(RefactoringUtils.class, "MSG_WaitScan"), javax.swing.UIManager.getIcon("OptionPane.informationIcon"), SwingConstants.LEFT); |
95 |
label.setBorder(new EmptyBorder(12, 12, 11, 11)); |
96 |
DialogDescriptor dd = new DialogDescriptor(label, actionName, true, new Object[]{NbBundle.getMessage(RefactoringUtils.class, "LBL_CancelAction",actionName)}, null, 0, null, listener); |
97 |
waitDialog = DialogDisplayer.getDefault().createDialog(dd); |
98 |
waitDialog.pack(); |
99 |
//100ms is workaround for 127536 |
100 |
waitTask = RequestProcessor.getDefault().post(ap, 100); |
101 |
waitDialog.setVisible(true); |
102 |
waitTask = null; |
103 |
waitDialog = null; |
104 |
return ap.hasBeenCancelled(); |
105 |
} else { |
106 |
runnable.run(); |
107 |
return false; |
108 |
} |
109 |
} |
110 |
private static Dialog waitDialog = null; |
111 |
private static RequestProcessor.Task waitTask = null; |
112 |
|
113 |
private static class ActionPerformer implements Runnable { |
114 |
|
115 |
private Runnable action; |
116 |
private AtomicBoolean cancel = new AtomicBoolean(); |
117 |
|
118 |
ActionPerformer(Runnable a) { |
119 |
this.action = a; |
120 |
} |
121 |
|
122 |
public boolean hasBeenCancelled() { |
123 |
return cancel.get(); |
124 |
} |
125 |
|
126 |
@Override |
127 |
public void run() { |
128 |
try { |
129 |
SourceUtils.waitScanFinished(); |
130 |
} catch (InterruptedException ie) { |
131 |
Exceptions.printStackTrace(ie); |
132 |
} |
133 |
SwingUtilities.invokeLater(new Runnable() { |
134 |
|
135 |
@Override |
136 |
public void run() { |
137 |
if (!cancel.get()) { |
138 |
if (waitDialog != null) { |
139 |
waitDialog.setVisible(false); |
140 |
waitDialog.dispose(); |
141 |
} |
142 |
action.run(); |
143 |
} |
144 |
} |
145 |
}); |
146 |
} |
147 |
|
148 |
public void cancel() { |
149 |
assert SwingUtilities.isEventDispatchThread(); |
150 |
// check if the scanning did not finish during cancel |
151 |
// invocation - in such case do not set cancel to true |
152 |
// and do not try to hide waitDialog window |
153 |
if (waitDialog != null) { |
154 |
cancel.set(true); |
155 |
waitDialog.setVisible(false); |
156 |
waitDialog.dispose(); |
157 |
} |
158 |
} |
159 |
} |
160 |
|
161 |
} |