Avoid creating duplicate strings if no format but params passed
[fedora-idea.git] / platform / util / src / com / intellij / CommonBundle.java
blobe64c62028df499c9f41441ee282f0a181fc59758
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.
17 package com.intellij;
19 import com.intellij.util.ui.UIUtil;
20 import org.jetbrains.annotations.NonNls;
21 import org.jetbrains.annotations.Nullable;
22 import org.jetbrains.annotations.PropertyKey;
24 import java.lang.ref.Reference;
25 import java.lang.ref.SoftReference;
26 import java.text.MessageFormat;
27 import java.util.MissingResourceException;
28 import java.util.ResourceBundle;
30 /**
31 * Created by IntelliJ IDEA.
32 * User: yole
33 * Date: 11.08.2005
34 * Time: 18:06:12
35 * To change this template use File | Settings | File Templates.
37 public class CommonBundle {
38 @NonNls private static final String BUNDLE = "messages.CommonBundle";
39 private static Reference<ResourceBundle> ourBundle;
40 public static boolean assertKeyIsFound = false;
42 private CommonBundle() {}
44 public static String message(@PropertyKey(resourceBundle = BUNDLE) String key, Object... params) {
45 return message(getCommonBundle(), key, params);
48 private static ResourceBundle getCommonBundle() {
49 ResourceBundle bundle = null;
50 if (ourBundle != null) bundle = ourBundle.get();
51 if (bundle == null) {
52 bundle = ResourceBundle.getBundle(BUNDLE);
53 ourBundle = new SoftReference<ResourceBundle>(bundle);
55 return bundle;
58 public static String messageOrDefault(@Nullable final ResourceBundle bundle, final String key, final @Nullable String defaultValue, final Object... params) {
59 if (bundle == null) return defaultValue;
61 String value;
62 try {
63 value = bundle.getString(key);
65 catch (MissingResourceException e) {
66 if (defaultValue != null) {
67 value = defaultValue;
68 } else {
69 value = "!" + key + "!";
70 if (assertKeyIsFound) {
71 assert false: key + " is not found";
76 value = UIUtil.replaceMnemonicAmpersand(value);
78 if (params.length > 0 && value.indexOf('{')>=0) {
79 return MessageFormat.format(value, params);
82 return value;
85 public static String message(final ResourceBundle bundle, final String key, final Object... params) {
86 return messageOrDefault(bundle, key, null, params);
89 public static String getCancelButtonText() {
90 return message("button.cancel");
93 public static String getBackgroundButtonText() {
94 return message("button.background");
97 public static String getHelpButtonText() {
98 return message("button.help");
101 public static String getErrorTitle() {
102 return message("title.error");
105 public static String getWarningTitle() {
106 return message("title.warning");
109 public static String getLoadingTreeNodeText() {
110 return CommonBundle.message("tree.node.loading");
113 public static String getOkButtonText(){
114 return message("button.ok");
117 public static String getYesButtonText(){
118 return CommonBundle.message("button.yes");
121 public static String getNoButtonText(){
122 return CommonBundle.message("button.no");
125 public static String getContinueButtonText(){
126 return CommonBundle.message("button.continue");
130 public static String getYesForAllButtonText() {
131 return CommonBundle.message("button.yes.for.all");
134 public static String getCloseButtonText() {
135 return CommonBundle.message("button.close");
138 public static String getNoForAllButtonText() {
139 return CommonBundle.message("button.no.for.all");
142 public static String getApplyButtonText() {
143 return CommonBundle.message("button.apply");