summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/ui/ReplacePromptDialog.java
blob: 3cf239f46632bdb156913a685855fdb570cc7934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.intellij.ui;

import com.intellij.find.FindManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class ReplacePromptDialog extends DialogWrapper {
  private final boolean myIsMultiple;
  @Nullable private final FindManager.MalformedReplacementStringException myException;

  public ReplacePromptDialog(boolean isMultipleFiles, String title, Project project) {
    this(isMultipleFiles, title, project, null);
  }

  public ReplacePromptDialog(boolean isMultipleFiles, String title, Project project, @Nullable FindManager.MalformedReplacementStringException exception) {
    super(project, true);
    myIsMultiple = isMultipleFiles;
    myException = exception;
    setButtonsAlignment(SwingConstants.CENTER);
    setTitle(title);
    init();
  }

  @NotNull
  @Override
  protected Action[] createActions(){
    DoAction replaceAction = new DoAction(UIBundle.message("replace.prompt.replace.button"), FindManager.PromptResult.OK);
    replaceAction.putValue(DEFAULT_ACTION,Boolean.TRUE);
    if (myException == null) {
      if (myIsMultiple){
        setCancelButtonText(UIBundle.message("replace.prompt.review.action"));
        return new Action[]{
          replaceAction,
          createSkipAction(),
          new DoAction(UIBundle.message("replace.prompt.all.in.this.file.button"), FindManager.PromptResult.ALL_IN_THIS_FILE),
          new DoAction(UIBundle.message("replace.prompt.all.files.action"), FindManager.PromptResult.ALL_FILES),
          getCancelAction()
        };
      }
      else {
        return new Action[]{
          replaceAction,
          createSkipAction(),
          new DoAction(UIBundle.message("replace.prompt.all.button"), FindManager.PromptResult.ALL),
          getCancelAction()
        };
      }
    } else {
      return new Action[] {
        createSkipAction(),
        getCancelAction()
      };
    }
  }

  private DoAction createSkipAction() {
    return new DoAction(UIBundle.message("replace.prompt.skip.button"), FindManager.PromptResult.SKIP);
  }

  @Override
  public JComponent createNorthPanel() {
    JPanel panel = new JPanel(new BorderLayout());
    panel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
    Icon icon = Messages.getQuestionIcon();
    if (icon != null){
      JLabel iconLabel = new JLabel(icon);
      panel.add(iconLabel, BorderLayout.WEST);
    }
    JLabel label = new JLabel(getMessage());
    label.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 10));
    label.setForeground(JBColor.foreground());
    panel.add(label, BorderLayout.CENTER);
    return panel;
  }

  protected String getMessage() {
    return myException == null ? UIBundle.message("replace.prompt.replace.occurrence.label") : myException.getMessage();
  }

  @Override
  public JComponent createCenterPanel() {
    return null;
  }

  @Override
  protected String getDimensionServiceKey() {
    return "ReplaceDuplicatesPrompt";
  }

  private class DoAction extends AbstractAction {
    private final int myExitCode;

    public DoAction(String name,int exitCode) {
      putValue(Action.NAME, name);
      myExitCode = exitCode;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      close(myExitCode);
    }
  }
}