/
ApplicationResources.java
80 lines (72 loc) · 2.61 KB
/
ApplicationResources.java
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
/*******************************************************************************
* JetUML - A desktop application for fast UML diagramming.
*
* Copyright (C) 2020, 2021 by McGill University.
*
* See: https://github.com/prmr/JetUML
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses.
*******************************************************************************/
package ca.mcgill.cs.jetuml.application;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import ca.mcgill.cs.jetuml.JetUML;
import ca.mcgill.cs.jetuml.annotations.Singleton;
/**
* A single access point for all application resources.
*
* Although ResourceBundles are cached internally, this class defines
* a singleton to make its nature as a single access point more
* explicit.
*
* The instance is exported as a public field instead of a static method
* to makes its more convenient and clear when statically imported.
*/
@Singleton
public final class ApplicationResources
{
public static final ApplicationResources RESOURCES = new ApplicationResources();
private static final String ERROR_MESSAGE = "[Resource cannot be found]";
private ResourceBundle aResouceBundle = ResourceBundle.getBundle(JetUML.class.getName());
private ApplicationResources() {}
/**
* Returns a resource string for the key. If the key cannot be found or is not a string,
* returns a string that indicates an error.
*
* @param pKey The key to look up.
* @return The corresponding resource string.
* @pre pKey != null
*/
public String getString(String pKey)
{
assert pKey != null;
try
{
return aResouceBundle.getString(pKey);
}
catch( MissingResourceException | ClassCastException exception )
{
return ERROR_MESSAGE;
}
}
/**
* @param pKey The key to check. Cannot be null.
* @return True if the key is found in the resources.
*/
public boolean containsKey(String pKey)
{
assert pKey != null;
return aResouceBundle.containsKey(pKey);
}
}