summaryrefslogtreecommitdiff
path: root/plugins/java-i18n/src/com/intellij/refactoring/inline/InlinePropertyHandler.java
blob: 151b8e605f8c47336b16714928a5148774d1b6f4 (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
/*
 * Copyright 2000-2014 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.refactoring.inline;

import com.intellij.lang.properties.IProperty;
import com.intellij.lang.properties.PropertiesBundle;
import com.intellij.lang.properties.references.PropertyReference;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.refactoring.HelpID;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.listeners.RefactoringEventData;
import com.intellij.refactoring.listeners.RefactoringEventListener;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import com.intellij.refactoring.util.RefactoringMessageDialog;
import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.FilteringIterator;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

/**
 * @author gregsh
 */
public class InlinePropertyHandler extends JavaInlineActionHandler {
  public static final String REFACTORING_NAME = PropertiesBundle.message("inline.property.refactoring");
  public static final String REFACTORING_ID = "refactoring.inline.property";

  public boolean canInlineElement(PsiElement element) {
    if (element instanceof PsiJavaToken && ((PsiJavaToken)element).getTokenType() == JavaTokenType.STRING_LITERAL) {
      PsiReference[] references = element.getParent().getReferences();
      return ContainerUtil.find(references, FilteringIterator.instanceOf(PropertyReference.class)) != null;
    }
    return element instanceof IProperty;
  }

  public void inlineElement(final Project project, Editor editor, PsiElement psiElement) {
    if (!(psiElement instanceof IProperty)) return;

    IProperty property = (IProperty)psiElement;
    final String propertyValue = property.getValue();
    if (propertyValue == null) return;

    final List<PsiElement> occurrences = Collections.synchronizedList(ContainerUtil.<PsiElement>newArrayList());
    final Collection<PsiFile> containingFiles = Collections.synchronizedSet(new HashSet<PsiFile>());
    containingFiles.add(psiElement.getContainingFile());
    boolean result = ReferencesSearch.search(psiElement).forEach(
      new Processor<PsiReference>() {
        public boolean process(final PsiReference psiReference) {
          PsiElement element = psiReference.getElement();
          PsiElement parent = element.getParent();
          if (parent instanceof PsiExpressionList && parent.getParent() instanceof PsiMethodCallExpression) {
            if (((PsiExpressionList)parent).getExpressions().length == 1) {
              occurrences.add(parent.getParent());
              containingFiles.add(element.getContainingFile());
              return true;
            }
          }
          return false;
        }
      }
    );

    if (!result) {
      CommonRefactoringUtil.showErrorHint(project, editor, "Property has non-method usages", REFACTORING_NAME, null);
    }
    if (occurrences.isEmpty()) {
      CommonRefactoringUtil.showErrorHint(project, editor, "Property has no usages", REFACTORING_NAME, null);
      return;
    }

    if (!ApplicationManager.getApplication().isUnitTestMode()) {
      String occurrencesString = RefactoringBundle.message("occurrences.string", occurrences.size());
      String question = PropertiesBundle.message("inline.property.confirmation", property.getName(), propertyValue) + " " + occurrencesString;
      RefactoringMessageDialog dialog = new RefactoringMessageDialog(REFACTORING_NAME, question, HelpID.INLINE_VARIABLE,
                                                                     "OptionPane.questionIcon", true, project);
      dialog.show();
      if (!dialog.isOK()) {
        return;
      }
    }

    final RefactoringEventData data = new RefactoringEventData();
    data.addElement(psiElement.copy());

    new WriteCommandAction.Simple(project, REFACTORING_NAME, containingFiles.toArray(new PsiFile[containingFiles.size()])) {
      @Override
      protected void run() throws Throwable {
        project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(REFACTORING_ID, data);
        PsiLiteral stringLiteral = (PsiLiteral)JavaPsiFacade.getInstance(getProject()).getElementFactory().
          createExpressionFromText("\"" + StringUtil.escapeStringCharacters(propertyValue) + "\"", null);
        for (PsiElement occurrence : occurrences) {
          occurrence.replace(stringLiteral.copy());
        }
        project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(REFACTORING_ID, null);
      }
    }.execute();
  }
}