KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > spi > impl > ParametersPanel


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  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.refactoring.spi.impl;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import javax.swing.*;
27 import javax.swing.border.EmptyBorder JavaDoc;
28 import javax.swing.event.ChangeEvent JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import org.netbeans.api.javahelp.Help;
31 import org.netbeans.api.progress.ProgressHandle;
32 import org.netbeans.api.progress.ProgressHandleFactory;
33 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
34 import org.netbeans.modules.refactoring.api.Problem;
35 import org.netbeans.modules.refactoring.api.ProblemDetails;
36 import org.netbeans.modules.refactoring.api.RefactoringSession;
37 import org.netbeans.modules.refactoring.spi.impl.UndoWatcher;
38 import org.netbeans.modules.refactoring.spi.ui.*;
39 import org.openide.DialogDescriptor;
40 import org.openide.DialogDisplayer;
41 import org.openide.ErrorManager;
42 import org.openide.util.*;
43 import org.netbeans.modules.refactoring.api.ProgressListener;
44 import org.netbeans.modules.refactoring.api.ProgressEvent;
45 import org.netbeans.modules.refactoring.spi.impl.ProblemComponent.CallbackAction;
46 import org.openide.awt.Mnemonics;
47
48
49 /** Main panel for refactoring parameters dialog. This panel is automatically displayed
50  * by refactoring action. It handles all the generic logic for displaying progress,
51  * checking for problems (during parameters validation and refactoring preparation) and
52  * accepting/canceling the refactoring. Refactoring-specific parameters panel is
53  * requested from {@link RefactoringUI} implementation (passed to this panel by {@link
54  * AbstractRefactoringAction}) and then displayed in the upper part of this panel.
55  * Refactoring-specific panel can use setPreviewEnabled method to enable/disable
56  * button that accepts the parameters. The button is disabled by default.
57  *
58  * @author Martin Matula, Jan Becicka
59  */

60 public class ParametersPanel extends JPanel implements ProgressListener, ChangeListener JavaDoc, InvalidationListener {
61     // refactoring elements that will be returned as a result of showDialog method
62
private RefactoringSession result;
63     
64     // corresponding implementation of RefactoringUI
65
private final RefactoringUI rui;
66     // refactoring-specific panel returned from RefactoringUI.getPanel
67
private final JPanel customPanel;
68     private final CustomRefactoringPanel customComponent;
69     
70     // parent dialog
71
private transient JDialog dialog = null;
72     // disabled components that should be reenabled by a call to setPanelEnabled
73
private ArrayList JavaDoc components = null;
74     
75     private Problem problem;
76     
77     private ErrorPanel errorPanel;
78     
79     private final int PRE_CHECK = 0;
80     private final int INPUT_PARAMETERS = 1;
81     private final int POST_CHECK = 2;
82     private final int PROBLEM = 3;
83     private final int CHECK_PARAMETERS = 4;
84     
85     private transient int currentState = INPUT_PARAMETERS;
86
87     private boolean cancelRequest = false;
88     
89     /** Enables/disables Preview button of dialog. Can be used by refactoring-specific
90      * parameters panel to disable accepting the parameters when needed (e.g. if
91      * not all parameters were entered). When the dialog is displayed, the button
92      * is disabled by default.
93      * @param enabled <code>true</code> to enable preview button, <code>false</code>
94      * to disable it.
95      */

96     public void setPreviewEnabled(boolean enabled) {
97         RefactoringPanel.checkEventThread();
98         next.setEnabled(enabled);
99     }
100     
101     /** Creates ParametersPanel
102      * @param rui Implementation of RefactoringUI for desired refactoring.
103      */

104     public ParametersPanel(RefactoringUI rui) {
105         RefactoringPanel.checkEventThread();
106         this.rui = rui;
107         initComponents();
108         innerPanel.setBorder(null);
109         label.setText(" ");//NOI18N
110
this.customComponent = rui.getPanel(this);
111         if (this.customComponent!=null) {
112             this.customPanel = (JPanel) this.customComponent.getComponent();
113         } else {
114             customPanel = null;
115         }
116         errorPanel = new ErrorPanel(rui);
117         calculatePrefferedSize();
118         setButtonsEnabled(false);
119         //cancel.setEnabled(false);
120
}
121     
122     
123     private void calculatePrefferedSize() {
124         Dimension cpDim = new Dimension(0,0);
125         Dimension ppDim = progressPanel.getPreferredSize();
126         Dimension epDim = new Dimension(0,0);
127         if (customPanel != null) {
128             cpDim = customPanel.getPreferredSize();
129         }
130         if (errorPanel != null) {
131             epDim = errorPanel.getPreferredSize();
132         }
133         
134         setPreferredSize(new Dimension(Math.max(Math.max(cpDim.width, ppDim.width),epDim.width) , Math.max(cpDim.height, epDim.height) + ppDim.height));
135         //validate();
136
if (dialog != null && rui.isQuery()) {
137             dialog.setSize(getPreferredSize());
138         }
139     }
140     
141     /** This method is called from within the constructor to
142      * initialize the form.
143      * WARNING: Do NOT modify this code. The content of this method is
144      * always regenerated by the Form Editor.
145      */

146     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
147
private void initComponents() {
148
149         progressPanel = new javax.swing.JPanel JavaDoc();
150         innerPanel = new javax.swing.JPanel JavaDoc();
151         label = new javax.swing.JLabel JavaDoc();
152         controlsPanel = new javax.swing.JPanel JavaDoc();
153         buttonsPanel = new javax.swing.JPanel JavaDoc();
154         back = new javax.swing.JButton JavaDoc();
155         previewButton = new javax.swing.JButton JavaDoc();
156         previewButton.setVisible(false);
157         next = new javax.swing.JButton JavaDoc();
158         cancel = new javax.swing.JButton JavaDoc();
159         help = new javax.swing.JButton JavaDoc();
160         containerPanel = new javax.swing.JPanel JavaDoc();
161         pleaseWait = new javax.swing.JLabel JavaDoc();
162
163         setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
164         java.util.ResourceBundle JavaDoc bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/refactoring/spi/impl/Bundle"); // NOI18N
165
setName(bundle.getString("LBL_FindUsagesDialog")); // NOI18N
166
setLayout(new java.awt.BorderLayout JavaDoc());
167
168         progressPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 12, 0, 11));
169         progressPanel.setOpaque(false);
170         progressPanel.setLayout(new java.awt.BorderLayout JavaDoc());
171
172         innerPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color JavaDoc(128, 128, 128)));
173         innerPanel.setLayout(new java.awt.BorderLayout JavaDoc());
174
175         label.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
176         innerPanel.add(label, java.awt.BorderLayout.WEST);
177
178         progressPanel.add(innerPanel, java.awt.BorderLayout.CENTER);
179
180         controlsPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 0, 5, 0));
181         controlsPanel.setLayout(new java.awt.BorderLayout JavaDoc());
182
183         buttonsPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
184         buttonsPanel.setLayout(new java.awt.GridLayout JavaDoc(1, 0, 4, 0));
185
186         back.setMnemonic(java.util.ResourceBundle.getBundle("org/netbeans/modules/refactoring/spi/impl/Bundle").getString("MNEM_Back").charAt(0));
187         org.openide.awt.Mnemonics.setLocalizedText(back, org.openide.util.NbBundle.getBundle("org/netbeans/modules/refactoring/spi/impl/Bundle").getString("CTL_Back")); // NOI18N
188
back.addActionListener(new java.awt.event.ActionListener JavaDoc() {
189             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
190                 backActionPerformed(evt);
191             }
192         });
193         buttonsPanel.add(back);
194         back.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Back")); // NOI18N
195

196         previewButton.setMnemonic(org.openide.util.NbBundle.getMessage(ParametersPanel.class, "MNEM_Preview").charAt(0));
197         previewButton.setLabel(org.openide.util.NbBundle.getMessage(ParametersPanel.class, "CTL_PreviewAll")); // NOI18N
198
previewButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
199             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
200                 preview(evt);
201             }
202         });
203         buttonsPanel.add(previewButton);
204
205         org.openide.awt.Mnemonics.setLocalizedText(next, org.openide.util.NbBundle.getBundle("org/netbeans/modules/refactoring/spi/impl/Bundle").getString("CTL_Finish")); // NOI18N
206
next.addActionListener(new java.awt.event.ActionListener JavaDoc() {
207             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
208                 refactor(evt);
209             }
210         });
211         buttonsPanel.add(next);
212         next.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_finish")); // NOI18N
213

214         org.openide.awt.Mnemonics.setLocalizedText(cancel, org.openide.util.NbBundle.getBundle("org/netbeans/modules/refactoring/spi/impl/Bundle").getString("CTL_Cancel")); // NOI18N
215
cancel.addActionListener(new java.awt.event.ActionListener JavaDoc() {
216             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
217                 cancelActionPerformed(evt);
218             }
219         });
220         buttonsPanel.add(cancel);
221         cancel.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_cancel")); // NOI18N
222

223         org.openide.awt.Mnemonics.setLocalizedText(help, org.openide.util.NbBundle.getBundle("org/netbeans/modules/refactoring/spi/impl/Bundle").getString("CTL_Help")); // NOI18N
224
help.addActionListener(new java.awt.event.ActionListener JavaDoc() {
225             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
226                 helpActionPerformed(evt);
227             }
228         });
229         buttonsPanel.add(help);
230         help.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_help")); // NOI18N
231

232         controlsPanel.add(buttonsPanel, java.awt.BorderLayout.EAST);
233
234         progressPanel.add(controlsPanel, java.awt.BorderLayout.SOUTH);
235
236         add(progressPanel, java.awt.BorderLayout.SOUTH);
237
238         containerPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
239         containerPanel.setLayout(new java.awt.BorderLayout JavaDoc());
240
241         pleaseWait.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
242         org.openide.awt.Mnemonics.setLocalizedText(pleaseWait, org.openide.util.NbBundle.getBundle("org/netbeans/modules/refactoring/spi/impl/Bundle").getString("LBL_PleaseWait")); // NOI18N
243
containerPanel.add(pleaseWait, java.awt.BorderLayout.CENTER);
244         pleaseWait.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ParametersPanel.class, "LBL_PleaseWait")); // NOI18N
245

246         add(containerPanel, java.awt.BorderLayout.CENTER);
247
248         getAccessibleContext().setAccessibleName(bundle.getString("LBL_FindUsagesDialog")); // NOI18N
249
getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_FindUsagesDialog")); // NOI18N
250
}// </editor-fold>//GEN-END:initComponents
251

252     private void preview(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_preview
253
refactor(true);
254 }//GEN-LAST:event_preview
255

256     private void helpActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_helpActionPerformed
257
Help help = (Help) Lookup.getDefault().lookup(Help.class);
258         help.showHelp(getHelpCtx());
259     }//GEN-LAST:event_helpActionPerformed
260

261     private void backActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_backActionPerformed
262
placeCustomPanel();
263     }//GEN-LAST:event_backActionPerformed
264

265     private void cancelActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_cancelActionPerformed
266
synchronized (this) {
267             rui.getRefactoring().cancelRequest();
268             result = null;
269             dialog.setVisible(false);
270             cancelRequest = true;
271         }
272     }//GEN-LAST:event_cancelActionPerformed
273
private void refactor(final boolean previewAll) {
274         if (currentState == PRE_CHECK) {
275             //next is "Next>"
276
placeCustomPanel();
277             return;
278         }
279         
280         if (currentState == PROBLEM && previewAll) {
281             Cancellable doCloseParent = new Cancellable() {
282                 public boolean cancel() {
283                     cancelActionPerformed(null);
284                     return true;
285                 }
286             };
287             currentProblemAction.showDetails(new CallbackAction(rui), doCloseParent);
288             return;
289         }
290         
291         //next is Finish
292

293         setPanelEnabled(false);
294         cancel.setEnabled(true);
295         
296         RequestProcessor rp = new RequestProcessor();
297         final int inputState = currentState;
298         
299         if (currentState != PRE_CHECK) {
300             if (rui instanceof RefactoringUIBypass && ((RefactoringUIBypass) rui).isRefactoringBypassRequired()) {
301                 try{
302                     ((RefactoringUIBypass)rui).doRefactoringBypass();
303                 } catch (final IOException JavaDoc ioe) {
304                     currentState = POST_CHECK;
305                     SwingUtilities.invokeLater(new Runnable JavaDoc() {
306                         public void run() {
307                             placeErrorPanel(new Problem(true, ioe.getMessage()));
308                         }});
309                 } finally {
310                     if (inputState == currentState) {
311                         setVisibleLater(false);
312                     }
313                     return;
314                 }
315             } else if (currentState != POST_CHECK && currentState != CHECK_PARAMETERS) {
316                 result = RefactoringSession.create(rui.getName());
317                 //setParameters and prepare is done asynchronously
318
rp.post(new Prepare());
319             } else if(currentState == CHECK_PARAMETERS) {
320                 rp.post(new Prepare());
321             }
322         }
323         
324         //refactoring is done asynchronously
325

326         rp.post(new Runnable JavaDoc() {
327             public void run() {
328                 //inputState != currentState means, that panels changed and dialog will not be closed
329
if (inputState == currentState) {
330                     try {
331                           if (!previewAll) {
332                             UndoWatcher.watch(result, ParametersPanel.this);
333                             result.addProgressListener(ParametersPanel.this);
334                             result.doRefactoring(true);
335                             UndoWatcher.stopWatching(ParametersPanel.this);
336                         }
337                     } finally {
338                         if (!previewAll) {
339                             result = null;
340                         }
341                         if (inputState == currentState) {
342                             setVisibleLater(false);
343                         }
344                     }
345                 }
346             }
347         });
348     }
349         
350     private void refactor(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_refactor
351
refactor(rui.isQuery());
352 }//GEN-LAST:event_refactor
353

354     
355     // Variables declaration - do not modify//GEN-BEGIN:variables
356
private javax.swing.JButton JavaDoc back;
357     private javax.swing.JPanel JavaDoc buttonsPanel;
358     public javax.swing.JButton JavaDoc cancel;
359     private javax.swing.JPanel JavaDoc containerPanel;
360     private javax.swing.JPanel JavaDoc controlsPanel;
361     private javax.swing.JButton JavaDoc help;
362     private javax.swing.JPanel JavaDoc innerPanel;
363     private javax.swing.JLabel JavaDoc label;
364     private javax.swing.JButton JavaDoc next;
365     private javax.swing.JLabel JavaDoc pleaseWait;
366     private javax.swing.JButton JavaDoc previewButton;
367     private javax.swing.JPanel JavaDoc progressPanel;
368     // End of variables declaration//GEN-END:variables
369

370     /**
371      * dialog is closed asynchronously on the AWT event thread
372      */

373     private void setVisibleLater(final boolean visible) {
374         SwingUtilities.invokeLater(new Runnable JavaDoc() {
375             public void run() {
376                 dialog.setVisible(visible);
377             }
378         });
379     }
380     
381     // disables/re-enables components in the custom panel
382
private void setPanelEnabled(boolean enabled) {
383         RefactoringPanel.checkEventThread();
384         setButtonsEnabled(enabled);
385         if (enabled) {
386             if (components == null) return;
387             for (Iterator JavaDoc it = components.iterator(); it.hasNext();) {
388                 ((Component) it.next()).setEnabled(true);
389             }
390             components = null;
391         } else {
392             if (components != null) return;
393             components = new ArrayList JavaDoc();
394             disableComponents(customPanel);
395         }
396     }
397     
398     // disables all components in the custom panel
399
private void disableComponents(Container c) {
400         RefactoringPanel.checkEventThread();
401         Component children[] = c.getComponents();
402         for (int i = 0; i < children.length; i++) {
403             if (children[i].isEnabled()) {
404                 children[i].setEnabled(false);
405                 components.add(children[i]);
406             }
407             if (children[i] instanceof Container) {
408                 disableComponents((Container) children[i]);
409             }
410         }
411     }
412     
413     /** Method used by AbstractRefactoringAction to display refactoring parameters
414      * dialog. Constructs a dialog consisting of this panel and Preview and Cancel
415      * buttons. Let's user to enter refactoring parameters.
416      * @return Collection of refactoring elements returned from the refactoring
417      * operation or <code>null</code> if the operation was cancelled.
418      */

419     public synchronized RefactoringSession showDialog() {
420         RefactoringPanel.checkEventThread();
421         if (rui != null) {
422             rui.getRefactoring().addProgressListener(this);
423         }
424         String JavaDoc title = (customPanel != null && customPanel.getName()!=null && !"".equals(customPanel.getName()))?customPanel.getName() : rui.getName();
425         DialogDescriptor descriptor = new DialogDescriptor(this, title, true, new Object JavaDoc[]{}, null, 0, null, null);
426         
427         dialog = (JDialog) DialogDisplayer.getDefault().createDialog(descriptor);
428         dialog.validate();
429         if (customPanel!=null) {
430             dialog.getAccessibleContext().setAccessibleName(rui.getName());
431             dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ParametersPanel.class, "ACSD_FindUsagesDialog"));
432         }
433         
434         setCancelStuff();
435         
436         dialog.pack();
437         
438         RequestProcessor.Task task = RequestProcessor.getDefault().post(new Runnable JavaDoc() {
439             public void run() {
440                 try {
441                     problem = rui.getRefactoring().preCheck();
442                 } catch (RuntimeException JavaDoc e) {
443                     setVisibleLater(false);
444                     throw e;
445                 }
446                 if (problem != null) {
447                     currentState = PRE_CHECK;
448                     if (!problem.isFatal()) {
449                         customComponent.initialize();
450                     }
451                     SwingUtilities.invokeLater(new Runnable JavaDoc() {
452                         public void run() {
453                             placeErrorPanel(problem);
454                             dialog.setVisible(true);
455                         }
456                     });
457                 } else {
458                     if (customPanel != null)
459                         customComponent.initialize();
460                         SwingUtilities.invokeLater(new Runnable JavaDoc() {
461                             public void run() {
462                                 placeCustomPanel();
463                             }
464                         });
465                     if (!rui.hasParameters()) {
466                         result = RefactoringSession.create(rui.getName());
467                         try {
468                             rui.getRefactoring().prepare(result);
469                         } finally {
470                             setVisibleLater(false);
471                         }
472                     }
473                     
474                 }
475             }
476         });
477         
478         dialog.setVisible(true);
479         dialog.dispose();
480         
481         if (rui != null) {
482             rui.getRefactoring().removeProgressListener(this);
483         }
484         if (!cancelRequest)
485             task.waitFinished();
486         return result;
487     }
488     
489     private final void setCancelStuff() {
490         KeyStroke k = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
491         Object JavaDoc actionKey = "cancel"; // NOI18N
492
getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(k, actionKey);
493         Action cancelAction = new AbstractAction() {
494             public void actionPerformed(ActionEvent ev) {
495                 if (cancel.isEnabled())
496                     cancelActionPerformed(ev);
497             }
498         };
499         
500         getRootPane().getActionMap().put(actionKey, cancelAction);
501         
502         dialog.addWindowListener(new WindowAdapter() {
503             public void windowClosing(WindowEvent evt) {
504                 if (cancel.isEnabled())
505                     cancelActionPerformed(null);
506             }
507         });
508     }
509     
510     private ProblemDetails getDetails(Problem problem) {
511         if (problem.getNext()==null) {
512             return problem.getDetails();
513         }
514         return null;
515     }
516     private ProblemDetails currentProblemAction;
517     private void placeErrorPanel(Problem problem) {
518         containerPanel.removeAll();
519         errorPanel = new ErrorPanel(problem, rui);
520         errorPanel.setBorder(new EmptyBorder JavaDoc(new Insets(12, 12, 11, 11)));
521         containerPanel.add(errorPanel, BorderLayout.CENTER);
522         
523         next.setEnabled(!problem.isFatal());
524         dialog.getRootPane().setDefaultButton(next);
525         if (currentState == PRE_CHECK ) {
526             //calculatePrefferedSize();
527
Mnemonics.setLocalizedText(next, NbBundle.getMessage(ParametersPanel.class,"CTL_Next"));
528             back.setVisible(false);
529         } else {
530             ProblemDetails details = getDetails(problem);
531             if (details!=null) {
532                 currentState=PROBLEM;
533                 Mnemonics.setLocalizedText(previewButton, details.getDetailsHint());
534                 previewButton.setVisible(true);
535                 previewButton.setEnabled(true);
536                 currentProblemAction = details;
537             }
538             back.setVisible(true);
539             back.setEnabled(true);
540             dialog.getRootPane().setDefaultButton(back);
541         }
542         cancel.setEnabled(true);
543         previewButton.setEnabled(!problem.isFatal());
544         repaint();
545     }
546     
547     private void placeCustomPanel() {
548         if (customPanel == null) return;
549         Mnemonics.setLocalizedText(next, NbBundle.getMessage(ParametersPanel.class, rui.isQuery()?"CTL_Find": "CTL_Finish"));
550         Mnemonics.setLocalizedText(previewButton, NbBundle.getMessage(ParametersPanel.class, "CTL_PreviewAll"));
551         customPanel.setBorder(new EmptyBorder JavaDoc(new Insets(12, 12, 11, 11)));
552         containerPanel.removeAll();
553         containerPanel.add(customPanel, BorderLayout.CENTER);
554         back.setVisible(false);
555         previewButton.setVisible(!rui.isQuery());
556         currentState = INPUT_PARAMETERS;
557         setPanelEnabled(true);
558         cancel.setEnabled(true);
559         dialog.getRootPane().setDefaultButton(next);
560         //Initial errors are ignored by on-line error checker
561
//stateChanged(null);
562
customPanel.requestFocus();
563         repaint();
564     }
565     
566     private ProgressBar progressBar;
567     private ProgressHandle progressHandle;
568     private boolean isIndeterminate;
569     
570     /** Implementation of ProgressListener.start method. Displays progress bar and
571      * sets progress label and progress bar bounds.
572      * @param event Event object.
573      */

574     public void start(final ProgressEvent event) {
575         SwingUtilities.invokeLater(new Runnable JavaDoc() {
576             public void run() {
577                 progressPanel.remove(innerPanel);
578                 progressBar = ProgressBar.create(progressHandle = ProgressHandleFactory.createHandle("")); //NOI18N
579
progressPanel.add(progressBar, BorderLayout.CENTER);
580                 //progressPanel.validate();
581
if (event.getCount()==-1) {
582                     isIndeterminate = true;
583                     progressHandle.start();
584                     progressHandle.switchToIndeterminate();
585                 } else {
586                     isIndeterminate = false;
587                     progressHandle.start(event.getCount());
588                 }
589                 
590                 String JavaDoc text;
591                 switch (event.getOperationType()) {
592                     case AbstractRefactoring.PARAMETERS_CHECK:
593                         text = NbBundle.getMessage(ParametersPanel.class, "LBL_ParametersCheck");
594                         break;
595                     case AbstractRefactoring.PREPARE:
596                         text = NbBundle.getMessage(ParametersPanel.class, "LBL_Prepare");
597                         break;
598                     case AbstractRefactoring.PRE_CHECK:
599                         text = NbBundle.getMessage(ParametersPanel.class, "LBL_PreCheck");
600                         break;
601                     default:
602                         text = NbBundle.getMessage(ParametersPanel.class, "LBL_Usages");
603                         break;
604                 }
605                 progressBar.setString(text); //NOI18N
606

607                 progressPanel.setVisible(true);
608                 
609                 setButtonsEnabled(false);
610             }
611         });
612     }
613     
614     /** Implementation of ProgressListener.step method. Increments progress bar value
615      * by 1.
616      * @param event Event object.
617      */

618     public void step(final ProgressEvent event) {
619         SwingUtilities.invokeLater(new Runnable JavaDoc() {
620             public void run() {
621                 try {
622                     progressHandle.progress(isIndeterminate ? -2 : event.getCount());
623                 } catch (Throwable JavaDoc e) {
624                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
625                 }
626             }
627         });
628     }
629     
630     /** Implementation of ProgressListener.stop method. Sets progress bar value to
631      * its maximum.
632      * @param event Event object.
633      */

634     public void stop(ProgressEvent event) {
635        SwingUtilities.invokeLater(new Runnable JavaDoc() {
636             public void run() {
637                 progressHandle.finish();
638                 progressPanel.remove(progressBar);
639                 progressPanel.add(innerPanel, BorderLayout.CENTER);
640                 //progressPanel.validate();
641
//setButtonsEnabled(true);
642
//validate();
643
}
644         });
645     }
646     
647     public void stateChanged(ChangeEvent JavaDoc e) {
648         showProblem(rui.checkParameters());
649     }
650     
651     private void showProblem(Problem problem) {
652         if (problem == null) {
653             label.setText(" "); // NOI18N
654
innerPanel.setBorder(null);
655             next.setEnabled(true);
656             previewButton.setEnabled(true);
657             return;
658         }
659         innerPanel.setBorder(new javax.swing.border.LineBorder JavaDoc(java.awt.Color.gray));
660         progressPanel.setVisible(true);
661         if (problem.isFatal()) {
662             displayError(problem.getMessage());
663         } else {
664             displayWarning(problem.getMessage());
665         }
666     }
667     
668     private void displayError(String JavaDoc error) {
669         next.setEnabled(false);
670         previewButton.setEnabled(false);
671         label.setText("<html><font color=\"red\">" + NbBundle.getMessage(ParametersPanel.class, "LBL_Error") + ": </font>" + error + "</html>"); //NOI18N
672
}
673     
674     private void displayWarning(String JavaDoc warning) {
675         next.setEnabled(true);
676         previewButton.setEnabled(true);
677         label.setText("<html><font color=\"red\">" + NbBundle.getMessage(ParametersPanel.class, "LBL_Warning") + ": </font>" + warning + "</html>"); //NOI18N
678
}
679     
680     private class Prepare implements Runnable JavaDoc {
681         public void run() {
682             if (currentState != POST_CHECK && currentState != CHECK_PARAMETERS) {
683                 problem = rui.setParameters();
684                 if (problem != null) {
685                     currentState = CHECK_PARAMETERS;
686                     try {
687                         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
688                             public void run() {
689                                 placeErrorPanel(problem);
690                             }});
691                     } catch (Exception JavaDoc ie) {
692                         throw (RuntimeException JavaDoc) new RuntimeException JavaDoc().initCause(ie);
693                     }
694                     return;
695                 }
696             }
697             
698             try {
699                 problem = rui.getRefactoring().prepare(result);
700             } catch (RuntimeException JavaDoc e) {
701                 setVisibleLater(false);
702                 throw e;
703             }
704     
705             if (problem != null) {
706                 currentState = POST_CHECK;
707                 SwingUtilities.invokeLater(new Runnable JavaDoc() {
708                     public void run() {
709                         placeErrorPanel(problem);
710                     }});
711             }
712         }
713     }
714     
715     private void setButtonsEnabled(boolean enabled) {
716         next.setEnabled(enabled);
717         //cancel.setEnabled(enabled);
718
back.setEnabled(enabled);
719         previewButton.setEnabled(enabled);
720     }
721     
722     public HelpCtx getHelpCtx() {
723         return rui.getHelpCtx();
724     }
725     
726     public void invalidateObject() {
727     }
728     
729     private static class ProgressBar extends JPanel {
730         
731         private JLabel label;
732         
733         private static ProgressBar create(ProgressHandle handle) {
734             ProgressBar instance = new ProgressBar();
735             instance.setLayout(new BorderLayout());
736             instance.label = new JLabel();
737             instance.label.setBorder(new EmptyBorder JavaDoc(0, 0, 2, 0));
738             instance.add(instance.label, BorderLayout.NORTH);
739             JComponent progress = ProgressHandleFactory.createProgressComponent(handle);
740             instance.add(progress, BorderLayout.CENTER);
741             return instance;
742         }
743         
744         public void setString(String JavaDoc value) {
745             label.setText(value);
746         }
747         
748         private ProgressBar() {
749         }
750         
751     }
752     
753 }
754
Popular Tags