2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.intellij
.ide
.util
.projectWizard
;
19 import com
.intellij
.ide
.IdeBundle
;
20 import com
.intellij
.openapi
.application
.ApplicationManager
;
21 import com
.intellij
.openapi
.progress
.ProgressIndicator
;
22 import com
.intellij
.openapi
.progress
.ProgressManager
;
23 import com
.intellij
.openapi
.progress
.util
.ProgressIndicatorBase
;
24 import com
.intellij
.openapi
.ui
.Messages
;
25 import com
.intellij
.openapi
.util
.Ref
;
26 import com
.intellij
.util
.concurrency
.SwingWorker
;
27 import com
.intellij
.util
.ui
.UIUtil
;
28 import org
.jetbrains
.annotations
.NonNls
;
32 import java
.awt
.event
.ActionEvent
;
33 import java
.awt
.event
.ActionListener
;
38 public abstract class AbstractStepWithProgress
<Result
> extends ModuleWizardStep
{
40 @NonNls private static final String PROGRESS_PANEL
= "progress_panel";
41 @NonNls private static final String RESULTS_PANEL
= "results_panel";
42 private JPanel myPanel
;
44 private JLabel myTitleLabel
;
45 private JLabel myProgressLabel
;
46 private JLabel myProgressLabel2
;
47 private ProgressIndicator myProgressIndicator
= null;
48 private final String myPromptStopSearch
;
50 public AbstractStepWithProgress(final String promptStopSearching
) {
51 myPromptStopSearch
= promptStopSearching
;
54 public final JComponent
getComponent() {
55 if (myPanel
== null) {
56 myPanel
= new JPanel(new CardLayout());
57 myPanel
.setBorder(BorderFactory
.createEtchedBorder());
59 myPanel
.add(createProgressPanel(), PROGRESS_PANEL
);
60 myPanel
.add(createResultsPanel(), RESULTS_PANEL
);
65 protected abstract JComponent
createResultsPanel();
67 protected abstract String
getProgressText();
69 protected abstract boolean shouldRunProgress();
71 protected abstract Result
calculate();
73 protected abstract void onFinished(Result result
, boolean canceled
);
75 private JPanel
createProgressPanel() {
76 final JPanel progressPanel
= new JPanel(new GridBagLayout());
77 myTitleLabel
= new JLabel();
78 myTitleLabel
.setFont(UIManager
.getFont("Label.font").deriveFont(Font
.BOLD
));
79 progressPanel
.add(myTitleLabel
, new GridBagConstraints(0, GridBagConstraints
.RELATIVE
, 2, 1, 1.0, 0.0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.HORIZONTAL
, new Insets(8, 10, 5, 10), 0, 0));
81 myProgressLabel
= new JLabel();
82 progressPanel
.add(myProgressLabel
, new GridBagConstraints(0, GridBagConstraints
.RELATIVE
, 1, 1, 1.0, 0.0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.HORIZONTAL
, new Insets(8, 10, 0, 10), 0, 0));
84 myProgressLabel2
= new JLabel();
85 progressPanel
.add(myProgressLabel2
, new GridBagConstraints(0, GridBagConstraints
.RELATIVE
, 1, 1, 1.0, 1.0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.HORIZONTAL
, new Insets(8, 10, 0, 10), 0, 0));
87 JButton stopButton
= new JButton(IdeBundle
.message("button.stop.searching"));
88 stopButton
.addActionListener(new ActionListener() {
89 public void actionPerformed(ActionEvent e
) {
93 progressPanel
.add(stopButton
, new GridBagConstraints(1, GridBagConstraints
.RELATIVE
, 1, 2, 0.0, 1.0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.NONE
, new Insets(10, 0, 0, 10), 0, 0));
97 private void cancelSearch() {
98 if (myProgressIndicator
!= null) {
99 myProgressIndicator
.cancel();
103 private synchronized boolean isProgressRunning() {
104 return myProgressIndicator
!= null && myProgressIndicator
.isRunning();
108 public void updateStep() {
109 if (shouldRunProgress()) {
113 showCard(RESULTS_PANEL
);
117 protected void runProgress() {
118 final MyProgressIndicator progress
= new MyProgressIndicator();
119 progress
.setModalityProgress(null);
120 final String title
= getProgressText();
122 myTitleLabel
.setText(title
);
124 showCard(PROGRESS_PANEL
);
125 myProgressIndicator
= progress
;
127 public Object
construct() {
128 final Ref
<Result
> result
= Ref
.create(null);
129 ProgressManager
.getInstance().runProcess(new Runnable() {
131 result
.set(calculate());
137 public void finished() {
138 myProgressIndicator
= null;
139 ApplicationManager
.getApplication().invokeLater(new Runnable() {
141 final Result result
= (Result
)get();
142 onFinished(result
, progress
.isCanceled());
143 showCard(RESULTS_PANEL
);
150 private void showCard(final String id
) {
151 ((CardLayout
)myPanel
.getLayout()).show(myPanel
, id
);
152 myPanel
.revalidate();
155 public boolean validate() {
156 if (isProgressRunning()) {
157 final int answer
= Messages
.showDialog(getComponent(), myPromptStopSearch
,
158 IdeBundle
.message("title.question"), new String
[] {IdeBundle
.message("action.continue.searching"), IdeBundle
.message("action.stop.searching")}, 0, Messages
.getWarningIcon());
159 if (answer
== 1) { // terminate
167 public void onStepLeaving() {
168 if (isProgressRunning()) {
173 protected class MyProgressIndicator
extends ProgressIndicatorBase
{
174 public void setText(String text
) {
175 updateLabel(myProgressLabel
, text
);
179 public void setText2(String text
) {
180 updateLabel(myProgressLabel2
, text
);
181 super.setText2(text
);
184 private void updateLabel(final JLabel label
, final String text
) {
185 UIUtil
.invokeLaterIfNeeded(new Runnable() {