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
;
19 import com
.intellij
.ide
.IdeBundle
;
20 import com
.intellij
.psi
.ElementDescriptionUtil
;
21 import com
.intellij
.psi
.PsiDirectoryContainer
;
22 import com
.intellij
.psi
.PsiElement
;
23 import com
.intellij
.util
.containers
.FactoryMap
;
24 import com
.intellij
.util
.containers
.HashMap
;
25 import org
.jetbrains
.annotations
.NonNls
;
27 import java
.text
.MessageFormat
;
33 public class DeleteUtil
{
34 private DeleteUtil() {}
36 public static void appendMessage(int count
, @NonNls String propertyKey
, StringBuffer buffer
) {
38 if (buffer
.length() > 0) {
39 buffer
.append(" ").append(IdeBundle
.message("prompt.delete.and")).append(" ");
43 buffer
.append(IdeBundle
.message(propertyKey
, count
));
47 public static String
generateWarningMessage(String messageTemplate
, final PsiElement
[] elements
) {
48 if (elements
.length
== 1) {
49 String name
= ElementDescriptionUtil
.getElementDescription(elements
[0], DeleteNameDescriptionLocation
.INSTANCE
);
50 String type
= ElementDescriptionUtil
.getElementDescription(elements
[0], DeleteTypeDescriptionLocation
.SINGULAR
);
51 return MessageFormat
.format(messageTemplate
, type
+ " \"" + name
+ "\"");
54 FactoryMap
<String
, Integer
> countMap
= new FactoryMap
<String
, Integer
>() {
55 protected Integer
create(final String key
) {
59 Map
<String
, String
> pluralToSingular
= new HashMap
<String
, String
>();
60 int directoryCount
= 0;
61 String containerType
= null;
63 for (final PsiElement elementToDelete
: elements
) {
64 String type
= ElementDescriptionUtil
.getElementDescription(elementToDelete
, DeleteTypeDescriptionLocation
.PLURAL
);
65 pluralToSingular
.put(type
, ElementDescriptionUtil
.getElementDescription(elementToDelete
, DeleteTypeDescriptionLocation
.SINGULAR
));
66 int oldCount
= countMap
.get(type
).intValue();
67 countMap
.put(type
, oldCount
+1);
68 if (elementToDelete
instanceof PsiDirectoryContainer
) {
70 directoryCount
+= ((PsiDirectoryContainer
) elementToDelete
).getDirectories().length
;
74 StringBuffer buffer
= new StringBuffer();
75 for (Map
.Entry
<String
, Integer
> entry
: countMap
.entrySet()) {
76 if (buffer
.length() > 0) {
77 if (buffer
.length() > 0) {
78 buffer
.append(" ").append(IdeBundle
.message("prompt.delete.and")).append(" ");
81 final int count
= entry
.getValue().intValue();
83 buffer
.append(count
).append(" ");
85 buffer
.append(pluralToSingular
.get(entry
.getKey()));
88 buffer
.append(entry
.getKey());
91 if (entry
.getKey().equals(containerType
)) {
92 buffer
.append(" ").append(IdeBundle
.message("prompt.delete.directory.paren", directoryCount
));
95 return MessageFormat
.format(messageTemplate
, buffer
.toString());