Lines 42-83
Link Here
|
42 |
|
42 |
|
43 |
package org.netbeans.modules.junit; |
43 |
package org.netbeans.modules.junit; |
44 |
|
44 |
|
45 |
import java.awt.event.ActionEvent; |
|
|
46 |
import org.netbeans.api.autoupdate.OperationSupport; |
47 |
import org.netbeans.api.autoupdate.OperationSupport.Restarter; |
48 |
import org.netbeans.api.options.OptionsDisplayer; |
49 |
import javax.swing.JButton; |
45 |
import javax.swing.JButton; |
50 |
import javax.swing.JTextArea; |
|
|
51 |
import javax.swing.JPanel; |
52 |
import java.util.logging.Level; |
53 |
import java.util.logging.Logger; |
46 |
import java.util.logging.Logger; |
54 |
import java.awt.Dialog; |
|
|
55 |
import java.awt.event.ActionListener; |
56 |
import javax.swing.JComponent; |
57 |
import javax.swing.JLabel; |
58 |
import org.openide.DialogDisplayer; |
59 |
import java.io.IOException; |
60 |
import java.util.List; |
61 |
import java.util.concurrent.Callable; |
47 |
import java.util.concurrent.Callable; |
62 |
import javax.swing.JScrollPane; |
|
|
63 |
import org.netbeans.api.autoupdate.InstallSupport; |
64 |
import org.netbeans.api.autoupdate.OperationContainer; |
65 |
import org.netbeans.api.autoupdate.UpdateElement; |
66 |
import org.netbeans.api.autoupdate.UpdateManager; |
67 |
import org.netbeans.api.autoupdate.UpdateUnit; |
68 |
import org.netbeans.api.autoupdate.UpdateUnitProvider; |
69 |
import org.netbeans.api.autoupdate.UpdateUnitProviderFactory; |
70 |
import org.netbeans.api.progress.ProgressHandle; |
71 |
import org.netbeans.api.progress.ProgressHandleFactory; |
72 |
import org.netbeans.api.project.libraries.Library; |
48 |
import org.netbeans.api.project.libraries.Library; |
73 |
import org.netbeans.api.project.libraries.LibraryManager; |
49 |
import org.netbeans.api.project.libraries.LibraryManager; |
74 |
import org.netbeans.modules.autoupdate.ui.api.PluginManager; |
50 |
import org.netbeans.modules.autoupdate.ui.api.PluginManager; |
75 |
import org.netbeans.spi.java.project.support.ui.BrokenReferencesSupport.LibraryDefiner; |
51 |
import org.netbeans.spi.java.project.support.ui.BrokenReferencesSupport.LibraryDefiner; |
76 |
import org.openide.DialogDescriptor; |
|
|
77 |
import org.openide.NotifyDescriptor; |
78 |
import org.openide.awt.Mnemonics; |
52 |
import org.openide.awt.Mnemonics; |
79 |
import org.openide.util.NbBundle.Messages; |
53 |
import org.openide.util.NbBundle.Messages; |
80 |
import org.openide.util.RequestProcessor; |
|
|
81 |
import org.openide.util.lookup.ServiceProvider; |
54 |
import org.openide.util.lookup.ServiceProvider; |
82 |
import static org.netbeans.modules.junit.Bundle.*; |
55 |
import static org.netbeans.modules.junit.Bundle.*; |
83 |
|
56 |
|
Lines 85-111
Link Here
|
85 |
* Defines JUnit 3.x/4.x libraries by downloading their defining modules. |
58 |
* Defines JUnit 3.x/4.x libraries by downloading their defining modules. |
86 |
*/ |
59 |
*/ |
87 |
@ServiceProvider(service=LibraryDefiner.class) |
60 |
@ServiceProvider(service=LibraryDefiner.class) |
88 |
public class JUnitLibraryDownloader implements LibraryDefiner { |
61 |
public class JUnitLibraryDefiner implements LibraryDefiner { |
89 |
private static RequestProcessor RP = new RequestProcessor(JUnitLibraryDownloader.class.getName(), 1); |
|
|
90 |
private static final Logger LOG = Logger.getLogger(JUnitLibraryDownloader.class.getName()); |
91 |
|
62 |
|
92 |
@Messages({ |
63 |
@Messages({ |
93 |
"searching_handle=Searching for \"junit\" library on NetBeans plugin portal...", |
|
|
94 |
"resolve_title=Resolve \"junit\" Reference Problem", |
95 |
"networkproblem_header=Unable to connect to the NetBeans plugin portal", |
96 |
"networkproblem_message=Check your proxy settings or try again later. " |
97 |
+ "The server may be unavailable at the moment. " |
98 |
+ "You may also want to make sure that your firewall is not blocking network traffic. \n\n" |
99 |
+ "If you have the missing \"junit\" library, you can resolve the reference " |
100 |
+ "problem manually using Library Manager.", |
101 |
"proxy_button=&Proxy Settings...", |
102 |
"library_button=&Library Manager...", |
64 |
"library_button=&Library Manager...", |
103 |
"tryagain_button=Try &Again", |
65 |
"library_name=junit" |
104 |
"nodownload_header=\"junit\" library has not been downloaded", |
|
|
105 |
"nodownload_message=You can try to download \"junit\" library again, or \n\n" |
106 |
+ "if you have the missing \"junit\" library, you can resolve the reference " |
107 |
+ "problem manually using Library Manager.", |
108 |
"active_handle=Activating JUnit" |
109 |
}) |
66 |
}) |
110 |
|
67 |
|
111 |
public @Override Callable<Library> missingLibrary(final String name) { |
68 |
public @Override Callable<Library> missingLibrary(final String name) { |
Lines 113-384
Link Here
|
113 |
return null; |
70 |
return null; |
114 |
} |
71 |
} |
115 |
return new Callable<Library>() { |
72 |
return new Callable<Library>() { |
|
|
73 |
|
74 |
@SuppressWarnings("SleepWhileInLoop") |
116 |
public @Override Library call() throws Exception { |
75 |
public @Override Library call() throws Exception { |
117 |
return download(name); |
76 |
JButton libraryManager = new JButton(); |
118 |
} |
77 |
Mnemonics.setLocalizedText(libraryManager, library_button()); |
119 |
}; |
78 |
Object ret = PluginManager.installPlugin("org.netbeans.modules.junitlib", library_name(), libraryManager); |
120 |
} |
79 |
if (libraryManager.equals(ret)) { |
121 |
|
80 |
throw new Exception("junitlib failed/canceled to install properly, open library manager instaed"); |
122 |
private JButton libraryManager; |
|
|
123 |
private JButton tryAgain; |
124 |
private JButton proxySettings; |
125 |
|
126 |
@SuppressWarnings("SleepWhileInLoop") |
127 |
private Library download(String name) throws Exception { |
128 |
UpdateUnit unit = findJUnitLib(); |
129 |
if (unit == null) { |
130 |
final ProgressHandle handle = ProgressHandleFactory.createHandle(searching_handle()); |
131 |
initButtons(); |
132 |
final DialogDescriptor searching = new DialogDescriptor(searchingPanel(new JLabel(searching_handle()), |
133 |
ProgressHandleFactory.createProgressComponent(handle)), resolve_title(), true, null); |
134 |
handle.setInitialDelay (0); |
135 |
handle.start (); |
136 |
searching.setOptions(new Object[] {NotifyDescriptor.CANCEL_OPTION}); |
137 |
searching.setMessageType(NotifyDescriptor.PLAIN_MESSAGE); |
138 |
final Dialog dlg = DialogDisplayer.getDefault().createDialog(searching); |
139 |
RP.post(new Runnable() { |
140 |
|
141 |
@Override |
142 |
public void run() { |
143 |
// May be first start, when no update lists have yet been downloaded. |
144 |
try { |
145 |
for (UpdateUnitProvider p : UpdateUnitProviderFactory.getDefault().getUpdateUnitProviders(true)) { |
146 |
p.refresh(handle, true); |
147 |
} |
148 |
// close searching |
149 |
dlg.dispose(); |
150 |
} catch (IOException ex) { |
151 |
Logger.getLogger(JUnitLibraryDownloader.class.getName()).log(Level.FINE, ex.getMessage(), ex); |
152 |
if (! dlg.isVisible()) { |
153 |
LOG.fine("dialog not visible => do nothing"); |
154 |
return ; |
155 |
} |
156 |
DialogDescriptor networkProblem = new DialogDescriptor( |
157 |
problemPanel(resolve_title(), networkproblem_message()), // message |
158 |
networkproblem_header(), // title |
159 |
true, // modal |
160 |
null); |
161 |
networkProblem.setOptions(new Object[] {tryAgain, proxySettings, NotifyDescriptor.CANCEL_OPTION}); |
162 |
networkProblem.setAdditionalOptions(new Object[] {libraryManager}); |
163 |
networkProblem.setClosingOptions(new Object[] {libraryManager, tryAgain, NotifyDescriptor.CANCEL_OPTION}); |
164 |
networkProblem.setMessageType(NotifyDescriptor.WARNING_MESSAGE); |
165 |
Dialog networkProblemDialog = DialogDisplayer.getDefault().createDialog(networkProblem); |
166 |
networkProblemDialog.setVisible(true); |
167 |
Object answer = networkProblem.getValue(); |
168 |
if (NotifyDescriptor.CANCEL_OPTION.equals(answer) || answer.equals(-1) /* escape */ ) { |
169 |
LOG.fine("cancel network problem dialog"); |
170 |
searching.setValue(answer); |
171 |
dlg.dispose(); |
172 |
} else if (tryAgain.equals(answer)) { |
173 |
LOG.fine("try again searching"); |
174 |
RP.post(this); |
175 |
} else if (libraryManager.equals(answer)) { |
176 |
LOG.fine("open library manager"); |
177 |
searching.setValue(answer); |
178 |
dlg.dispose(); |
179 |
} else { |
180 |
assert false : "Unknown " + answer; |
181 |
} |
182 |
} |
183 |
} |
81 |
} |
184 |
}); |
|
|
185 |
dlg.setVisible(true); |
186 |
handle.finish(); |
187 |
if (NotifyDescriptor.CANCEL_OPTION.equals(searching.getValue()) || searching.getValue().equals(-1) /* escape */) { |
188 |
LOG.fine("user canceled searching JUnit"); |
189 |
return showNoDownloadDialog(name); |
190 |
} else if (libraryManager.equals(searching.getValue())) { |
191 |
throw new Exception("user canceled searching"); |
192 |
} |
193 |
unit = findJUnitLib(); |
194 |
if (unit == null) { |
195 |
LOG.fine("could not find junitlib on any update site"); |
196 |
return showNoDownloadDialog(name); |
197 |
} |
198 |
} |
199 |
// check if JUnit installed |
200 |
if (unit.getInstalled() != null) { |
201 |
LOG.fine(unit.getInstalled() + " already installed. Is active? " + unit.getInstalled().isEnabled()); |
202 |
if (unit.getInstalled().isEnabled()) { |
203 |
throw new Exception(unit.getInstalled() + " already installed and active"); |
204 |
} else { |
205 |
// activate it |
206 |
OperationContainer<OperationSupport> oc = OperationContainer.createForEnable(); |
207 |
if (!oc.canBeAdded(unit, unit.getInstalled())) { |
208 |
throw new Exception("could not add " + unit.getInstalled() + " for activation"); |
209 |
} |
210 |
for (UpdateElement req : oc.add(unit.getInstalled()).getRequiredElements()) { |
211 |
oc.add(req); |
212 |
} |
213 |
ProgressHandle activeHandle = ProgressHandleFactory.createHandle (active_handle()); |
214 |
Restarter restarter = oc.getSupport().doOperation(activeHandle); |
215 |
assert restarter == null : "No Restater need to make " + unit.getInstalled() + " active"; |
216 |
// XXX new library & build.properties apparently do not show up immediately... how to listen properly? |
82 |
// XXX new library & build.properties apparently do not show up immediately... how to listen properly? |
217 |
for (int i = 0; i < 10; i++) { |
83 |
for (int i = 0; i < 20; i++) { |
218 |
Library lib = LibraryManager.getDefault().getLibrary(name); |
84 |
Library lib = LibraryManager.getDefault().getLibrary(name); |
219 |
if (lib != null) { |
85 |
if (lib != null) { |
220 |
return lib; |
86 |
return lib; |
221 |
} |
87 |
} |
222 |
Thread.sleep(1000); |
88 |
Thread.sleep(1000); |
223 |
} |
89 |
} |
224 |
LOG.info("junitlib failed to make active properly"); |
90 |
return null; |
225 |
return showNoDownloadDialog(name); |
|
|
226 |
} |
91 |
} |
227 |
} |
92 |
}; |
228 |
List<UpdateElement> updates = unit.getAvailableUpdates(); |
|
|
229 |
if (updates.isEmpty()) { |
230 |
throw new Exception("no updates for " + unit); |
231 |
} |
232 |
OperationContainer<InstallSupport> oc = OperationContainer.createForInstall(); |
233 |
UpdateElement element = updates.get(0); |
234 |
if (!oc.canBeAdded(unit, element)) { |
235 |
throw new Exception("could not add " + element + " to updates"); |
236 |
} |
237 |
for (UpdateElement req : oc.add(element).getRequiredElements()) { |
238 |
oc.add(req); |
239 |
} |
240 |
if (!PluginManager.openInstallWizard(oc)) { |
241 |
LOG.fine("user canceled PM"); |
242 |
return showNoDownloadDialog(name); |
243 |
} |
244 |
// XXX new library & build.properties apparently do not show up immediately... how to listen properly? |
245 |
for (int i = 0; i < 20; i++) { |
246 |
Library lib = LibraryManager.getDefault().getLibrary(name); |
247 |
if (lib != null) { |
248 |
return lib; |
249 |
} |
250 |
Thread.sleep(1000); |
251 |
} |
252 |
LOG.info("junitlib failed to install properly"); |
253 |
return showNoDownloadDialog(name); |
254 |
} |
255 |
|
256 |
private UpdateUnit findJUnitLib() throws IOException { |
257 |
for (UpdateUnit unit : UpdateManager.getDefault().getUpdateUnits(UpdateManager.TYPE.MODULE)) { |
258 |
if (unit.getCodeName().equals("org.netbeans.modules.junitlib")) { |
259 |
return unit; |
260 |
} |
261 |
} |
262 |
return null; |
263 |
} |
264 |
|
265 |
private void initButtons() { |
266 |
if (libraryManager != null) { |
267 |
return ; |
268 |
} |
269 |
libraryManager = new JButton(); |
270 |
tryAgain = new JButton(); |
271 |
proxySettings = new JButton(); |
272 |
Mnemonics.setLocalizedText(tryAgain, tryagain_button()); |
273 |
Mnemonics.setLocalizedText(libraryManager, library_button()); |
274 |
Mnemonics.setLocalizedText(proxySettings, proxy_button()); |
275 |
proxySettings.addActionListener(new ActionListener() { |
276 |
@Override |
277 |
public void actionPerformed(ActionEvent e) { |
278 |
LOG.fine("show proxy options"); |
279 |
OptionsDisplayer.getDefault().open("General"); // NOI18N |
280 |
} |
281 |
}); |
282 |
} |
283 |
|
284 |
private Library showNoDownloadDialog(String name) throws Exception { |
285 |
DialogDescriptor networkProblem = new DialogDescriptor( |
286 |
problemPanel(nodownload_header(), nodownload_message()), // message |
287 |
resolve_title(), // title |
288 |
true, // modal |
289 |
null); |
290 |
initButtons(); |
291 |
networkProblem.setOptions(new Object[] {tryAgain, NotifyDescriptor.CANCEL_OPTION}); |
292 |
networkProblem.setAdditionalOptions(new Object[] {libraryManager}); |
293 |
networkProblem.setClosingOptions(new Object[] {libraryManager, tryAgain, NotifyDescriptor.CANCEL_OPTION}); |
294 |
networkProblem.setMessageType(NotifyDescriptor.WARNING_MESSAGE); |
295 |
Dialog networkProblemDialog = DialogDisplayer.getDefault().createDialog(networkProblem); |
296 |
networkProblemDialog.setVisible(true); |
297 |
Object answer = networkProblem.getValue(); |
298 |
if (NotifyDescriptor.CANCEL_OPTION.equals(answer) || answer.equals(-1) /* escape */ ) { |
299 |
LOG.fine("cancel no download dialog"); |
300 |
//throw new InterruptedException("user canceled download & install JUnit"); |
301 |
return null; |
302 |
} else if (tryAgain.equals(answer)) { |
303 |
LOG.fine("try again download()"); |
304 |
return download(name); |
305 |
} else if (libraryManager.equals(answer)) { |
306 |
LOG.fine("open library manager"); |
307 |
throw new Exception("junitlib failed/canceled to install properly, open library manager instaed"); |
308 |
} else { |
309 |
assert false : "Unknown " + answer; |
310 |
} |
311 |
assert false : "Unknown " + answer; |
312 |
return null; |
313 |
} |
314 |
|
315 |
private static JPanel searchingPanel(JLabel progressLabel, JComponent progressComponent) { |
316 |
JPanel panel = new JPanel(); |
317 |
progressLabel.setLabelFor(progressComponent); |
318 |
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(panel); |
319 |
panel.setLayout(layout); |
320 |
layout.setHorizontalGroup( |
321 |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
322 |
.addGroup(layout.createSequentialGroup() |
323 |
.addContainerGap() |
324 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) |
325 |
.addComponent(progressLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) |
326 |
.addComponent(progressComponent, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)) |
327 |
.addContainerGap()) |
328 |
); |
329 |
layout.setVerticalGroup( |
330 |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
331 |
.addGroup(layout.createSequentialGroup() |
332 |
.addGap(96, 96, 96) |
333 |
.addComponent(progressLabel) |
334 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
335 |
.addComponent(progressComponent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) |
336 |
.addContainerGap(109, Short.MAX_VALUE)) |
337 |
); |
338 |
return panel; |
339 |
} |
340 |
|
341 |
private static JPanel problemPanel(String header, String message) { |
342 |
JPanel panel = new JPanel(); |
343 |
JLabel jLabel1 = new javax.swing.JLabel(); |
344 |
JScrollPane jScrollPane1 = new javax.swing.JScrollPane(); |
345 |
JTextArea jTextArea1 = new javax.swing.JTextArea(); |
346 |
|
347 |
jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() | java.awt.Font.BOLD)); |
348 |
jLabel1.setText(header); |
349 |
|
350 |
jTextArea1.setColumns(20); |
351 |
jTextArea1.setEditable(false); |
352 |
jTextArea1.setLineWrap(true); |
353 |
jTextArea1.setWrapStyleWord(true); |
354 |
jTextArea1.setRows(5); |
355 |
jTextArea1.setText(message); |
356 |
jTextArea1.setOpaque(false); |
357 |
jScrollPane1.setViewportView(jTextArea1); |
358 |
|
359 |
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(panel); |
360 |
panel.setLayout(layout); |
361 |
layout.setHorizontalGroup( |
362 |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
363 |
.addGroup(layout.createSequentialGroup() |
364 |
.addContainerGap() |
365 |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
366 |
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 478, Short.MAX_VALUE) |
367 |
.addGroup(layout.createSequentialGroup() |
368 |
.addComponent(jLabel1) |
369 |
.addGap(107, 107, 107))) |
370 |
.addContainerGap()) |
371 |
); |
372 |
layout.setVerticalGroup( |
373 |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
374 |
.addGroup(layout.createSequentialGroup() |
375 |
.addContainerGap() |
376 |
.addComponent(jLabel1) |
377 |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
378 |
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE) |
379 |
.addGap(82, 82, 82)) |
380 |
); |
381 |
return panel; |
382 |
} |
93 |
} |
383 |
|
94 |
|
384 |
} |
95 |
} |