Lines 64-77
Link Here
|
64 |
import org.netbeans.api.progress.ProgressUtils; |
64 |
import org.netbeans.api.progress.ProgressUtils; |
65 |
import org.netbeans.modules.progress.spi.RunOffEDTProvider; |
65 |
import org.netbeans.modules.progress.spi.RunOffEDTProvider; |
66 |
import org.netbeans.modules.progress.spi.RunOffEDTProvider.Progress; |
66 |
import org.netbeans.modules.progress.spi.RunOffEDTProvider.Progress; |
|
|
67 |
import org.netbeans.modules.progress.spi.RunOffEDTProvider.Progress2; |
67 |
import org.openide.DialogDescriptor; |
68 |
import org.openide.DialogDescriptor; |
68 |
import org.openide.DialogDisplayer; |
69 |
import org.openide.DialogDisplayer; |
69 |
import org.openide.NotifyDescriptor; |
70 |
import org.openide.NotifyDescriptor; |
70 |
import org.openide.util.Cancellable; |
71 |
import org.openide.util.*; |
71 |
import org.openide.util.Exceptions; |
|
|
72 |
import org.openide.util.NbBundle; |
73 |
import org.openide.util.Parameters; |
74 |
import org.openide.util.RequestProcessor; |
75 |
import org.openide.util.RequestProcessor.Task; |
72 |
import org.openide.util.RequestProcessor.Task; |
76 |
import org.openide.util.lookup.ServiceProvider; |
73 |
import org.openide.util.lookup.ServiceProvider; |
77 |
import org.openide.windows.WindowManager; |
74 |
import org.openide.windows.WindowManager; |
Lines 81-89
Link Here
|
81 |
* @author Jan Lahoda, Tomas Holy |
78 |
* @author Jan Lahoda, Tomas Holy |
82 |
*/ |
79 |
*/ |
83 |
@ServiceProvider(service=RunOffEDTProvider.class, position = 100) |
80 |
@ServiceProvider(service=RunOffEDTProvider.class, position = 100) |
84 |
public class RunOffEDTImpl implements RunOffEDTProvider, Progress { |
81 |
public class RunOffEDTImpl implements RunOffEDTProvider, Progress, Progress2 { |
85 |
|
82 |
|
86 |
private static final RequestProcessor WORKER = new RequestProcessor(ProgressUtils.class.getName()); |
83 |
private static final RequestProcessor WORKER = new RequestProcessor(ProgressUtils.class.getName()); |
|
|
84 |
private static final RequestProcessor TI_WORKER = new RequestProcessor("TI_" + ProgressUtils.class.getName(), 1, true); |
85 |
|
87 |
private static final Map<String, Long> CUMULATIVE_SPENT_TIME = new HashMap<String, Long>(); |
86 |
private static final Map<String, Long> CUMULATIVE_SPENT_TIME = new HashMap<String, Long>(); |
88 |
private static final Map<String, Long> MAXIMAL_SPENT_TIME = new HashMap<String, Long>(); |
87 |
private static final Map<String, Long> MAXIMAL_SPENT_TIME = new HashMap<String, Long>(); |
89 |
private static final Map<String, Integer> INVOCATION_COUNT = new HashMap<String, Integer>(); |
88 |
private static final Map<String, Integer> INVOCATION_COUNT = new HashMap<String, Integer>(); |
Lines 216-225
Link Here
|
216 |
} |
215 |
} |
217 |
} |
216 |
} |
218 |
} |
217 |
} |
|
|
218 |
|
219 |
@Override |
220 |
public void runOffEventThreadWithCustomDialogContent(Runnable operation, String dialogTitle, JPanel content, int waitCursorAfter, int dialogAfter) |
221 |
{ |
222 |
runOffEventThreadCustomDialogImpl(operation, dialogTitle, content, waitCursorAfter, dialogAfter); |
223 |
} |
219 |
|
224 |
|
|
|
225 |
@Override |
226 |
public void runOffEventThreadWithProgressDialog(final Runnable operation, final String operationDescr, |
227 |
ProgressHandle handle, boolean includeDetailLabel, int waitCursorAfter, int dialogAfter) { |
228 |
JPanel content = contentPanel(handle, includeDetailLabel); |
229 |
runOffEventThreadCustomDialogImpl(operation, operationDescr, content, waitCursorAfter, dialogAfter); |
230 |
} |
231 |
|
232 |
private void runOffEventThreadCustomDialogImpl(final Runnable operation, final String operationDescr, |
233 |
final JPanel contentPanel, int waitCursorAfter, int dialogAfter) { |
234 |
if (waitCursorAfter < 0) waitCursorAfter = 1000; |
235 |
if (dialogAfter < 0) dialogAfter = 2000; |
236 |
|
237 |
final CountDownLatch latch = new CountDownLatch(1); |
238 |
final AtomicReference<Dialog> d = new AtomicReference<Dialog>(); |
239 |
final AtomicReference<RequestProcessor.Task> t = new AtomicReference<RequestProcessor.Task>(); |
240 |
|
241 |
JDialog dialog = createModalDialog(operation, operationDescr, contentPanel, d, t, operation instanceof Cancellable); |
242 |
|
243 |
final Task rt = TI_WORKER.post(new Runnable() { |
244 |
|
245 |
public @Override void run() { |
246 |
try { |
247 |
operation.run(); |
248 |
} finally { |
249 |
latch.countDown(); |
250 |
|
251 |
SwingUtilities.invokeLater(new Runnable() { |
252 |
|
253 |
public @Override void run() { |
254 |
Dialog dd = d.get(); |
255 |
if (dd != null) { |
256 |
dd.setVisible(false); |
257 |
dd.dispose(); |
258 |
} |
259 |
} |
260 |
}); |
261 |
} |
262 |
} |
263 |
}); |
264 |
t.set(rt); |
265 |
|
266 |
Window window = null; |
267 |
Component glassPane = null; |
268 |
Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); |
269 |
if (focusOwner != null) { |
270 |
window = SwingUtilities.getWindowAncestor(focusOwner); |
271 |
if (window != null) { |
272 |
RootPaneContainer root = (RootPaneContainer) SwingUtilities.getAncestorOfClass(RootPaneContainer.class, focusOwner); |
273 |
glassPane = root.getGlassPane(); |
274 |
} |
275 |
} |
276 |
if (window == null || glassPane == null) { |
277 |
window = WindowManager.getDefault().getMainWindow(); |
278 |
glassPane = ((JFrame) window).getGlassPane(); |
279 |
} |
280 |
if (waitMomentarily(glassPane, null, waitCursorAfter, latch, window)) { |
281 |
return; |
282 |
} |
283 |
|
284 |
Cursor wait = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR); |
285 |
|
286 |
if (waitMomentarily(glassPane, wait, dialogAfter, latch, window)) { |
287 |
return; |
288 |
} |
289 |
|
290 |
d.set(dialog); |
291 |
if (EventQueue.isDispatchThread()) { |
292 |
EventQueue.invokeLater(new Runnable() { |
293 |
@Override |
294 |
public void run() { |
295 |
d.get().setVisible(true); |
296 |
} |
297 |
}); |
298 |
} else { |
299 |
d.get().setVisible(true); |
300 |
} |
301 |
} |
302 |
|
220 |
private static boolean waitMomentarily(Component glassPane, Cursor wait, int timeout, final CountDownLatch l, Window window) { |
303 |
private static boolean waitMomentarily(Component glassPane, Cursor wait, int timeout, final CountDownLatch l, Window window) { |
221 |
Cursor originalWindow = window.getCursor(); |
304 |
Cursor originalWindow = window.getCursor(); |
222 |
Cursor originalGlass = glassPane.getCursor(); |
305 |
Cursor originalGlass = glassPane.getCursor(); |
223 |
|
306 |
|
224 |
try { |
307 |
try { |
225 |
if (wait != null) { |
308 |
if (wait != null) { |
Lines 412-415
Link Here
|
412 |
} |
495 |
} |
413 |
|
496 |
|
414 |
} |
497 |
} |
|
|
498 |
|
499 |
private static JPanel contentPanel(final ProgressHandle handle, boolean includeDetail) { |
500 |
// top panel |
501 |
JPanel contentPanel = new JPanel(new GridBagLayout()); |
502 |
|
503 |
// main label |
504 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
505 |
gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0); |
506 |
gridBagConstraints.gridx = 0; |
507 |
gridBagConstraints.gridy = 0; |
508 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START; |
509 |
|
510 |
JLabel mainLabel = ProgressHandleFactory.createMainLabelComponent(handle); |
511 |
Font f = mainLabel.getFont(); |
512 |
if (f != null) { |
513 |
mainLabel.setFont(f.deriveFont(Font.BOLD)); |
514 |
} |
515 |
contentPanel.add(mainLabel, gridBagConstraints); |
516 |
|
517 |
// progress bar |
518 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
519 |
gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0); |
520 |
gridBagConstraints.gridx = 0; |
521 |
gridBagConstraints.gridy = 1; |
522 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
523 |
JComponent progressBar = ProgressHandleFactory.createProgressComponent(handle); |
524 |
contentPanel.add (progressBar, gridBagConstraints); |
525 |
|
526 |
if (includeDetail) { |
527 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
528 |
gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0); |
529 |
gridBagConstraints.gridx = 0; |
530 |
gridBagConstraints.gridy = 2; |
531 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START; |
532 |
JLabel details = ProgressHandleFactory.createDetailLabelComponent(handle); |
533 |
contentPanel.add(details, gridBagConstraints); |
534 |
} |
535 |
|
536 |
// empty panel - for correct resizing |
537 |
JPanel emptyPanel = new JPanel(); |
538 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
539 |
gridBagConstraints.gridx = 0; |
540 |
gridBagConstraints.gridy = includeDetail ? 3 : 2; |
541 |
gridBagConstraints.weighty = 2.0; |
542 |
gridBagConstraints.weightx = 2.0; |
543 |
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; |
544 |
contentPanel.add(emptyPanel, gridBagConstraints); |
545 |
|
546 |
return contentPanel; |
547 |
} |
548 |
|
549 |
private static JDialog createModalDialog( |
550 |
final Runnable operation, |
551 |
final String title, |
552 |
final JPanel content, |
553 |
final AtomicReference<Dialog> d, |
554 |
final AtomicReference<RequestProcessor.Task> task, |
555 |
final boolean cancelAvail) |
556 |
{ |
557 |
assert EventQueue.isDispatchThread(); |
558 |
|
559 |
JPanel panel = new JPanel(new GridBagLayout()); |
560 |
|
561 |
GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints(); |
562 |
gridBagConstraints.gridx = 0; |
563 |
gridBagConstraints.gridy = 0; |
564 |
gridBagConstraints.weightx = 1.0; |
565 |
gridBagConstraints.weighty = 1.0; |
566 |
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; |
567 |
|
568 |
panel.add(content, gridBagConstraints); |
569 |
|
570 |
if (cancelAvail) { |
571 |
JPanel buttonsPanel = new JPanel(); |
572 |
buttonsPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); |
573 |
String cancelButton = NbBundle.getMessage(RunOffEDTImpl.class, "RunOffAWT.BTN_Cancel"); //NOI18N |
574 |
JButton cancel = new JButton(cancelButton); |
575 |
cancel.addActionListener(new ActionListener() { |
576 |
|
577 |
@Override |
578 |
public void actionPerformed(ActionEvent e) { |
579 |
if (operation instanceof Cancellable) { |
580 |
((Cancellable) operation).cancel(); |
581 |
task.get().cancel(); |
582 |
d.get().setVisible(false); |
583 |
d.get().dispose(); |
584 |
} |
585 |
} |
586 |
}); |
587 |
buttonsPanel.add(cancel); |
588 |
gridBagConstraints = new java.awt.GridBagConstraints(); |
589 |
gridBagConstraints.gridx = 0; |
590 |
gridBagConstraints.gridy = 1; |
591 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
592 |
gridBagConstraints.weightx = 1.0; |
593 |
panel.add(buttonsPanel, gridBagConstraints); |
594 |
} |
595 |
|
596 |
Frame mainWindow = WindowManager.getDefault().getMainWindow(); |
597 |
final JDialog result = new JDialog(mainWindow, title, true); |
598 |
result.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); |
599 |
result.setSize(400, 150); |
600 |
result.setContentPane(panel); |
601 |
result.setLocationRelativeTo(WindowManager.getDefault().getMainWindow()); |
602 |
return result; |
603 |
} |
415 |
} |
604 |
} |