KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > plugins > Policy


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.plugins;
12
13 import java.util.*;
14 import org.eclipse.core.runtime.*;
15 import org.eclipse.osgi.util.NLS;
16
17 public class Policy {
18     private static String JavaDoc bundleName = "org.eclipse.core.internal.plugins.messages"; //$NON-NLS-1$
19
private static ResourceBundle bundle;
20
21     /*
22      * Returns a resource bundle, creating one if it none is available.
23      */

24     private static ResourceBundle getResourceBundle() {
25         // thread safety
26
ResourceBundle tmpBundle = bundle;
27         if (tmpBundle != null)
28             return tmpBundle;
29         // always create a new classloader to be passed in
30
// in order to prevent ResourceBundle caching
31
return bundle = ResourceBundle.getBundle(bundleName, Locale.getDefault());
32     }
33
34     /**
35      * Lookup the message with the given ID in this catalog
36      */

37     public static String JavaDoc bind(String JavaDoc id) {
38         return bind(id, (String JavaDoc[]) null);
39     }
40
41     /**
42      * Lookup the message with the given ID in this catalog and bind its
43      * substitution locations with the given string.
44      */

45     public static String JavaDoc bind(String JavaDoc id, String JavaDoc binding) {
46         return bind(id, new String JavaDoc[] {binding});
47     }
48
49     /**
50      * Lookup the message with the given ID in this catalog and bind its
51      * substitution locations with the given strings.
52      */

53     public static String JavaDoc bind(String JavaDoc id, String JavaDoc binding1, String JavaDoc binding2) {
54         return bind(id, new String JavaDoc[] {binding1, binding2});
55     }
56
57     /**
58      * Lookup the message with the given ID in this catalog and bind its
59      * substitution locations with the given string values.
60      */

61     public static String JavaDoc bind(String JavaDoc id, String JavaDoc[] bindings) {
62         if (id == null)
63             return "No message available"; //$NON-NLS-1$
64
String JavaDoc message = null;
65         try {
66             message = getResourceBundle().getString(id);
67         } catch (MissingResourceException e) {
68             // If we got an exception looking for the message, fail gracefully by just returning
69
// the id we were looking for. In most cases this is semi-informative so is not too bad.
70
return "Missing message: " + id + " in: " + bundleName; //$NON-NLS-1$ //$NON-NLS-2$
71
}
72         if (bindings == null)
73             return message;
74         return NLS.bind(message, bindings);
75     }
76
77     public static IProgressMonitor monitorFor(IProgressMonitor monitor) {
78         if (monitor == null)
79             return new NullProgressMonitor();
80         return monitor;
81     }
82
83     public static IProgressMonitor subMonitorFor(IProgressMonitor monitor, int ticks) {
84         if (monitor == null)
85             return new NullProgressMonitor();
86         if (monitor instanceof NullProgressMonitor)
87             return monitor;
88         return new SubProgressMonitor(monitor, ticks);
89     }
90
91     public static IProgressMonitor subMonitorFor(IProgressMonitor monitor, int ticks, int style) {
92         if (monitor == null)
93             return new NullProgressMonitor();
94         if (monitor instanceof NullProgressMonitor)
95             return monitor;
96         return new SubProgressMonitor(monitor, ticks, style);
97     }
98
99     /**
100      * Print a debug message to the console. If the given boolean is <code>true</code> then
101      * pre-pend the message with the current date.
102      */

103     public static void debug(boolean includeDate, String JavaDoc message) {
104         if (includeDate)
105             message = new Date(System.currentTimeMillis()) + " - " + message; //$NON-NLS-1$
106
System.out.println(message);
107     }
108 }
109
Popular Tags