update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / rename / naming / AutomaticUsageRenamer.java
blob7d99901d3b84993e37dbc337f5434203e5aecc87
1 /*
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.
16 package com.intellij.refactoring.rename.naming;
18 import com.intellij.usages.RenameableUsage;
19 import com.intellij.util.IncorrectOperationException;
20 import org.jetbrains.annotations.NotNull;
21 import org.jetbrains.annotations.Nullable;
23 import java.util.*;
25 /**
26 * @author peter
28 public abstract class AutomaticUsageRenamer<T> {
29 private final String myOldName;
30 private final String myNewName;
31 private final Map<T, String> myRenames = new LinkedHashMap<T, String>();
32 private final List<T> myElements = new ArrayList<T>();
33 private final Map<T, List<RenameableUsage>> myReferences = new HashMap<T, List<RenameableUsage>>();
35 protected AutomaticUsageRenamer(List<? extends T> renamedElements, String oldName, String newName) {
36 myOldName = oldName;
37 myNewName = newName;
38 List<T> elements = new ArrayList<T>(renamedElements);
39 Collections.sort(elements, new Comparator<T>() {
40 private int compareNullable(@Nullable String s1, @Nullable String s2) {
41 if (s1 != null) {
42 return s2 == null ? 1 : s1.compareTo(s2);
44 return s2 == null ? 0 : -1;
47 public int compare(T o1, T o2) {
48 int i = compareNullable(getSourceName(o1), getSourceName(o2));
49 if (i != 0) return i;
50 return getName(o1).compareTo(getName(o2));
52 });
53 for (T element : elements) {
54 String suggestedNewName = suggestName(element);
55 if (!getName(element).equals(suggestedNewName)) {
56 myElements.add(element);
57 setRename(element, suggestedNewName);
62 public boolean hasAnythingToRename() {
63 for (final String s : myRenames.values()) {
64 if (s != null) return true;
66 return false;
69 public boolean isEmpty() {
70 return myRenames.isEmpty();
73 protected String getOldName() {
74 return myOldName;
77 public String getNewName() {
78 return myNewName;
81 protected boolean isChecked(T element) {
82 return myRenames.containsKey(element);
85 protected boolean isCheckedInitially(T element) {
86 return false;
89 protected boolean isNameAlreadySuggested(String newName) {
90 return myRenames.values().contains(newName);
93 public List<? extends T> getElements() {
94 return myElements;
97 @Nullable
98 /**
99 * Element source, path. For example, package. Taken into account while sorting.
101 public String getSourceName(T element) {
102 return null;
105 public String getNewElementName(T element) {
106 return myRenames.get(element);
109 public Map<? extends T,String> getRenames() {
110 return myRenames;
113 public void setRename(T element, @NotNull String replacement) {
114 myRenames.put(element, replacement);
117 public void doNotRename(T element) {
118 myRenames.remove(element);
121 @Nullable
122 public String getErrorText(T element) {
123 return null;
126 public final void doRename() throws IncorrectOperationException {
127 for (final Map.Entry<T, List<RenameableUsage>> entry : myReferences.entrySet()) {
128 final T element = entry.getKey();
129 final String newName = getNewElementName(element);
130 doRenameElement(element);
131 for (final RenameableUsage usage : entry.getValue()) {
132 usage.rename(newName);
137 protected abstract void doRenameElement(T element) throws IncorrectOperationException;
139 protected abstract String suggestName(T element);
141 protected abstract String getName(T element);
143 public abstract String getDialogTitle();
145 public abstract String getDialogDescription();
147 public abstract String getEntityName();