View Javadoc

1   // Copyright (C) 2000, 2001, 2002, 2003 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.console.common;
23  
24  import java.text.MessageFormat;
25  
26  
27  /**
28   * Exception that can be displayed through the user interface.
29   * Supports internationalised text through {@link Resources}.
30   *
31   * @author Philip Aston
32   */
33  public class DisplayMessageConsoleException extends ConsoleException {
34  
35    /**
36     * Constructor.
37     *
38     * @param resources Resources to use.
39     * @param resourceKey Resource key that specifies message.
40     */
41    public DisplayMessageConsoleException(Resources resources,
42                                          String resourceKey) {
43      super(getMessage(resources, resourceKey));
44    }
45  
46    /**
47     * Constructor.
48     *
49     * @param resources Resources to use.
50     * @param resourceKey Resource key that specifies message.
51     * @param e Nested exception.
52     */
53    public DisplayMessageConsoleException(Resources resources,
54                                          String resourceKey,
55                                          Exception e) {
56      super(getMessage(resources, resourceKey), e);
57    }
58  
59    /**
60     * Constructor.
61     *
62     * @param resources Resources to use.
63     * @param resourceKey Resource key that specifies message.
64     * @param arguments Message arguments.
65     */
66    public DisplayMessageConsoleException(Resources resources,
67                                          String resourceKey,
68                                          Object[] arguments) {
69      super(MessageFormat.format(getMessage(resources, resourceKey), arguments));
70    }
71  
72    /**
73     * Constructor.
74     *
75     * @param resources Resources to use.
76     * @param resourceKey Resource key that specifies message.
77     * @param arguments Message arguments.
78     * @param e Nested exception.
79     */
80    public DisplayMessageConsoleException(Resources resources,
81                                          String resourceKey,
82                                          Object[] arguments,
83                                          Exception e) {
84      super(MessageFormat.format(getMessage(resources, resourceKey), arguments),
85            e);
86    }
87  
88    private static String getMessage(Resources resources, String resourceKey) {
89  
90      final String resourceValue = resources.getString(resourceKey, false);
91  
92      if (resourceValue != null) {
93        return resourceValue;
94      }
95      else {
96        return "No message found for key \"" + resourceKey + "\"";
97      }
98    }
99  }
100