001/* A simple message handler that throws exceptions.
002
003 Copyright (c) 2012-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026 */
027package ptolemy.util;
028
029///////////////////////////////////////////////////////////////////
030//// SimpleMessageHandler
031
032/**
033 This is a message handler that reports errors in a graphical dialog box.
034
035 <p>See ptolemy.gui.GraphicalMessageHandler</p>
036
037 @author  Christopher Brooks
038 @version $Id$
039 @since Ptolemy II 10.0
040b @Pt.ProposedRating Red (cxh)
041 @Pt.AcceptedRating Red (cxh)
042 */
043public class SimpleMessageHandler extends MessageHandler {
044
045    ///////////////////////////////////////////////////////////////////
046    ////                         protected methods                 ////
047
048    /** Throw a RuntimeException.
049     *  @param info The message.
050     */
051    @Override
052    protected void _error(String info) {
053        throw new RuntimeException(info);
054    }
055
056    /** Show the specified message and throwable information.
057     *  If the throwable is an instance of CancelException, then nothing
058     *  is shown.
059     *
060     *  @param info The message.
061     *  @param throwable The throwable.
062     *  @see CancelException
063     */
064    @Override
065    protected void _error(String info, Throwable throwable) {
066        if (throwable instanceof CancelException) {
067            return;
068        }
069        // Print out the exception so that if MoMLSimpleApplication
070        // throws an exception, we see it on stdout.
071        throwable.printStackTrace();
072        throw new RuntimeException(info, throwable);
073    }
074
075    /** Display the warning message.  In this base class, the
076     *  the default handler merely prints the warning to stderr.
077     *  @param info The message.
078     */
079    @Override
080    protected void _message(String info) {
081        System.err.println(info);
082    }
083
084    /** Show the specified message.  In this base class, the message
085     *  is printed to standard error.
086     *  <p>Derived classes might show the specified message in a modal
087     *  dialog.  If the user clicks on the "Cancel" button, then throw
088     *  an exception.  This gives the user the option of not
089     *  continuing the execution, something that is particularly
090     *  useful if continuing execution will result in repeated
091     *  warnings.
092     *  @param info The message.
093     *  @exception CancelException If the user clicks on the "Cancel" button.
094     */
095    @Override
096    protected void _warning(String info) throws CancelException {
097        _error(info);
098    }
099
100    /** Display the warning message and throwable information.  In
101     *  this base class, the the default handler merely prints the
102     *  warning to stderr.
103     *  @param info The message.
104     *  @param throwable The Throwable.
105     *  @exception CancelException If the user clicks on the "Cancel" button.
106     */
107    @Override
108    protected void _warning(String info, Throwable throwable)
109            throws CancelException {
110        _error(info, throwable);
111    }
112
113    /** Ask the user a yes/no question, and return true if the answer
114     *  is yes.  In this base class, this prints the question on standard
115     *  output and looks for the reply on standard input.
116     *  @param question The yes/no question to be asked.
117     *  @return True if the answer is yes.
118     */
119    @Override
120    protected boolean _yesNoQuestion(String question) {
121        System.out.print(question);
122        System.out.print(" (yes or no) ");
123        return false;
124    }
125
126    /** Ask the user a question with three possible answers;
127     *  return true if the answer is the first one and false if
128     *  the answer is the second one; throw an exception if the
129     *  user selects the third one.
130     *  @param question The question.
131     *  @param trueOption The option for which to return true.
132     *  @param falseOption The option for which to return false.
133     *  @param exceptionOption The option for which to throw an exception.
134     *  @return Always return false.
135     *  @exception ptolemy.util.CancelException If the user selects the third option.
136     */
137    @Override
138    protected boolean _yesNoCancelQuestion(String question, String trueOption,
139            String falseOption, String exceptionOption)
140            throws ptolemy.util.CancelException {
141        System.out.print(question + " (" + trueOption + " or " + falseOption
142                + " or " + exceptionOption + ") ");
143        return false;
144    }
145}