summaryrefslogtreecommitdiff
path: root/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/ui/SelectTemplateDialog.java
blob: b74715ecf07a0c0f31f4ec583ce18c5370e77461 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package com.intellij.structuralsearch.plugin.ui;

import com.intellij.codeInsight.template.TemplateContextType;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Splitter;
import com.intellij.structuralsearch.MatchOptions;
import com.intellij.structuralsearch.SSRBundle;
import com.intellij.structuralsearch.plugin.replace.ui.ReplaceConfiguration;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by IntelliJ IDEA.
 * User: Maxim.Mossienko
 * Date: Apr 23, 2004
 * Time: 5:03:52 PM
 * To change this template use File | Settings | File Templates.
 */
public class SelectTemplateDialog extends DialogWrapper {
  private final boolean showHistory;
  private Editor searchPatternEditor;
  private Editor replacePatternEditor;
  private final boolean replace;
  private final Project project;
  private final ExistingTemplatesComponent existingTemplatesComponent;

  private MySelectionListener selectionListener;
  private CardLayout myCardLayout;
  private JPanel myPreviewPanel;
  @NonNls private static final String PREVIEW_CARD = "Preview";
  @NonNls private static final String SELECT_TEMPLATE_CARD = "SelectCard";

  public SelectTemplateDialog(Project project, boolean showHistory, boolean replace) {
    super(project, false);

    this.project = project;
    this.showHistory = showHistory;
    this.replace = replace;
    existingTemplatesComponent = ExistingTemplatesComponent.getInstance(this.project);

    setTitle(SSRBundle.message(this.showHistory ? "used.templates.history.dialog.title" : "existing.templates.dialog.title"));
    init();

    if (this.showHistory) {
      final int selection = existingTemplatesComponent.getHistoryList().getSelectedIndex();
      if (selection != -1) {
        setPatternFromList(selection);
      }
    }
    else {
      final TreePath selection = existingTemplatesComponent.getPatternTree().getSelectionPath();
      if (selection != null) {
        setPatternFromNode((DefaultMutableTreeNode)selection.getLastPathComponent());
      }
      else {
        showPatternPreviewFromConfiguration(null);
      }
    }

    setupListeners();
  }

  class MySelectionListener implements TreeSelectionListener, ListSelectionListener {
    public void valueChanged(TreeSelectionEvent e) {
      if (e.getNewLeadSelectionPath() != null) {
        setPatternFromNode(
          (DefaultMutableTreeNode)e.getNewLeadSelectionPath().getLastPathComponent()
        );
      }
    }

    public void valueChanged(ListSelectionEvent e) {
      if (e.getValueIsAdjusting() || e.getLastIndex() == -1) return;
      int selectionIndex = existingTemplatesComponent.getHistoryList().getSelectedIndex();
      if (selectionIndex != -1) {
        setPatternFromList(selectionIndex);
      }
    }
  }

  private void setPatternFromList(int index) {
    showPatternPreviewFromConfiguration(
      (Configuration)existingTemplatesComponent.getHistoryList().getModel().getElementAt(index)
    );
  }

  protected JComponent createCenterPanel() {
    final JPanel centerPanel = new JPanel(new BorderLayout());
    Splitter splitter;

    centerPanel.add(BorderLayout.CENTER, splitter = new Splitter(false, 0.3f));
    centerPanel.add(splitter);

    splitter.setFirstComponent(
      showHistory ?
      existingTemplatesComponent.getHistoryPanel() :
      existingTemplatesComponent.getTemplatesPanel()
    );
    final JPanel panel;
    splitter.setSecondComponent(
      panel = new JPanel(new BorderLayout())
    );

    searchPatternEditor = UIUtil.createEditor(
      EditorFactory.getInstance().createDocument(""),
      project,
      false,
      true,
      ContainerUtil.findInstance(TemplateContextType.EP_NAME.getExtensions(), TemplateContextType.class)
    );

    JComponent centerComponent;

    if (replace) {
      replacePatternEditor = UIUtil.createEditor(
        EditorFactory.getInstance().createDocument(""),
        project,
        false,
        true,
        ContainerUtil.findInstance(TemplateContextType.EP_NAME.getExtensions(), TemplateContextType.class)
      );
      centerComponent = new Splitter(true);
      ((Splitter)centerComponent).setFirstComponent(searchPatternEditor.getComponent());
      ((Splitter)centerComponent).setSecondComponent(replacePatternEditor.getComponent());
    }
    else {
      centerComponent = searchPatternEditor.getComponent();
    }

    myCardLayout = new CardLayout();
    myPreviewPanel = new JPanel(myCardLayout);
    myPreviewPanel.add(centerComponent, PREVIEW_CARD);
    JPanel selectPanel = new JPanel(new GridBagLayout());
    GridBagConstraints gb = new GridBagConstraints(0,0,0,0,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE, new Insets(0,0,0,0),0,0);
    selectPanel.add(new JLabel(SSRBundle.message("selecttemplate.template.label.please.select.template")), gb);
    myPreviewPanel.add(selectPanel, SELECT_TEMPLATE_CARD);

    panel.add(BorderLayout.CENTER, myPreviewPanel);

    panel.add(BorderLayout.NORTH, new JLabel(SSRBundle.message("selecttemplate.template.preview")));
    return centerPanel;
  }

  public void dispose() {
    EditorFactory.getInstance().releaseEditor(searchPatternEditor);
    if (replacePatternEditor != null) EditorFactory.getInstance().releaseEditor(replacePatternEditor);
    removeListeners();
    super.dispose();
  }

  public JComponent getPreferredFocusedComponent() {
    return showHistory ?
           existingTemplatesComponent.getHistoryList() :
           existingTemplatesComponent.getPatternTree();
  }

  protected String getDimensionServiceKey() {
    return "#com.intellij.structuralsearch.plugin.ui.SelectTemplateDialog";
  }

  private void setupListeners() {
    existingTemplatesComponent.setOwner(this);
    selectionListener = new MySelectionListener();

    if (showHistory) {
      existingTemplatesComponent.getHistoryList().getSelectionModel().addListSelectionListener(
        selectionListener
      );
    }
    else {
      existingTemplatesComponent.getPatternTree().getSelectionModel().addTreeSelectionListener(
        selectionListener
      );
    }
  }

  private void removeListeners() {
    existingTemplatesComponent.setOwner(null);
    if (showHistory) {
      existingTemplatesComponent.getHistoryList().getSelectionModel().removeListSelectionListener(
        selectionListener
      );
    }
    else {
      existingTemplatesComponent.getPatternTree().getSelectionModel().removeTreeSelectionListener(selectionListener);
    }
  }

  private void setPatternFromNode(DefaultMutableTreeNode node) {
    if (node == null) return;
    final Object userObject = node.getUserObject();
    final Configuration configuration;

    // root could be without search template
    if (userObject instanceof Configuration) {
      configuration = (Configuration)userObject;
    }
    else {
      configuration = null;
    }

    showPatternPreviewFromConfiguration(configuration);
  }

  private void showPatternPreviewFromConfiguration(@Nullable final Configuration configuration) {
    if (configuration == null) {
      myCardLayout.show(myPreviewPanel, SELECT_TEMPLATE_CARD);
      return;
    }
    else {
      myCardLayout.show(myPreviewPanel, PREVIEW_CARD);
    }
    final MatchOptions matchOptions = configuration.getMatchOptions();

    UIUtil.setContent(
      searchPatternEditor,
      matchOptions.getSearchPattern(),
      0,
      searchPatternEditor.getDocument().getTextLength(),
      project
    );

    searchPatternEditor.putUserData(SubstitutionShortInfoHandler.CURRENT_CONFIGURATION_KEY, configuration);

    if (replace) {
      String replacement;

      if (configuration instanceof ReplaceConfiguration) {
        replacement = ((ReplaceConfiguration)configuration).getOptions().getReplacement();
      }
      else {
        replacement = configuration.getMatchOptions().getSearchPattern();
      }

      UIUtil.setContent(
        replacePatternEditor,
        replacement,
        0,
        replacePatternEditor.getDocument().getTextLength(),
        project
      );

      replacePatternEditor.putUserData(SubstitutionShortInfoHandler.CURRENT_CONFIGURATION_KEY, configuration);
    }
  }

  @NotNull public Configuration[] getSelectedConfigurations() {
    if (showHistory) {
      Object[] selectedValues = existingTemplatesComponent.getHistoryList().getSelectedValues();
      if (selectedValues == null) {
        return new Configuration[0];
      }
      Collection<Configuration> configurations = new ArrayList<Configuration>();
      for (Object selectedValue : selectedValues) {
        if (selectedValue instanceof Configuration) {
          configurations.add((Configuration)selectedValue);
        }
      }
      return configurations.toArray(new Configuration[configurations.size()]);
    }
    else {
      TreePath[] paths = existingTemplatesComponent.getPatternTree().getSelectionModel().getSelectionPaths();
      if (paths == null) {
        return new Configuration[0];
      }
      Collection<Configuration> configurations = new ArrayList<Configuration>();
      for (TreePath path : paths) {

        DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
        final Object userObject = node.getUserObject();
        if (userObject instanceof Configuration) {
          configurations.add((Configuration)userObject);
        }
      }
      return configurations.toArray(new Configuration[configurations.size()]);
    }
  }
}