1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package com.sun.star.wizards.web;
24 
25 import com.sun.star.awt.VclWindowPeerAttribute;
26 import com.sun.star.awt.XWindowPeer;
27 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.wizards.common.SystemDialog;
29 
30 /**
31  * An abstract implementation of ErrorHandler, which
32  * uses a renderer method geMessageFor(Exception, Object, int, int)
33  * (in this class still abstract...)
34  * to render the errors, and displays
35  * error messeges.
36  */
37 public abstract class AbstractErrorHandler implements ErrorHandler
38 {
39 
40     XMultiServiceFactory xmsf;
41     XWindowPeer peer;
42 
AbstractErrorHandler(XMultiServiceFactory xmsf, XWindowPeer peer_)43     protected AbstractErrorHandler(XMultiServiceFactory xmsf, XWindowPeer peer_)
44     {
45         this.xmsf = xmsf;
46         peer = peer_;
47     }
48 
49     /**
50      * Implementation of ErrorHandler:
51      * shows a message box with the rendered error.
52      * @param arg identifies the error. This object is passed to the render method
53      * which returns the right error message.
54      * @return true/false for continue/abort.
55      */
error(Exception ex, Object arg, int ix, int errorType)56     public boolean error(Exception ex, Object arg, int ix, int errorType)
57     {
58         //ex.printStackTrace();
59         switch (errorType)
60         {
61             case ErrorHandler.ERROR_FATAL:
62                 return !showMessage(getMessageFor(ex, arg, ix, errorType), errorType);
63             case ErrorHandler.ERROR_PROCESS_FATAL:
64                 return !showMessage(getMessageFor(ex, arg, ix, errorType), errorType);
65             case ErrorHandler.ERROR_NORMAL_ABORT:
66                 return showMessage(getMessageFor(ex, arg, ix, errorType), errorType);
67             case ErrorHandler.ERROR_NORMAL_IGNORE:
68                 return showMessage(getMessageFor(ex, arg, ix, errorType), errorType);
69             case ErrorHandler.ERROR_QUESTION_CANCEL:
70                 return showMessage(getMessageFor(ex, arg, ix, errorType), errorType);
71             case ErrorHandler.ERROR_QUESTION_OK:
72                 return showMessage(getMessageFor(ex, arg, ix, errorType), errorType);
73             case ErrorHandler.ERROR_QUESTION_NO:
74                 return showMessage(getMessageFor(ex, arg, ix, errorType), errorType);
75             case ErrorHandler.ERROR_QUESTION_YES:
76                 return showMessage(getMessageFor(ex, arg, ix, errorType), errorType);
77             case ErrorHandler.ERROR_WARNING:
78                 return showMessage(getMessageFor(ex, arg, ix, errorType), errorType);
79             case ErrorHandler.ERROR_MESSAGE:
80                 return showMessage(getMessageFor(ex, arg, ix, errorType), errorType);
81         }
82         throw new IllegalArgumentException("unknown error type");
83     }
84 
85     /**
86      * @deprecated
87      * @param message
88      * @param errorType
89      * @return true if the ok/yes button is clicked, false otherwise.
90      */
showMessage(String message, int errorType)91     protected boolean showMessage(String message, int errorType)
92     {
93         return showMessage(xmsf, peer, message, errorType);
94     }
95 
96     /**
97      * display a message
98      * @deprecated
99      * @param xmsf
100      * @param message the message to display
101      * @param errorType an int constant from the ErrorHandler interface.
102      * @return
103      */
showMessage(XMultiServiceFactory xmsf, XWindowPeer peer, String message, int errorType)104     public static boolean showMessage(XMultiServiceFactory xmsf, XWindowPeer peer, String message, int errorType)
105     {
106         String serviceName = getServiceNameFor(errorType);
107         int attribute = getAttributeFor(errorType);
108         int b = SystemDialog.showMessageBox(xmsf, peer, serviceName, attribute, message);
109         return b == getTrueFor(errorType);
110     }
111 
showMessage(XMultiServiceFactory xmsf, XWindowPeer peer, String message, String dialogtype, int buttons, int defaultButton, int returnTrueOn)112     public static boolean showMessage(XMultiServiceFactory xmsf, XWindowPeer peer,
113             String message,
114             String dialogtype,
115             int buttons,
116             int defaultButton,
117             int returnTrueOn)
118     {
119         int b = SystemDialog.showMessageBox(xmsf, peer, dialogtype, defaultButton + buttons, message);
120         return b == returnTrueOn;
121     }
122 
123     /**
124      * normally ok(1) is the value for true.
125      * but a question dialog may use yes. so i use this method
126      * for each error type to get its type of "true" value.
127      * @param errorType
128      * @return
129      */
getTrueFor(int errorType)130     private static int getTrueFor(int errorType)
131     {
132         switch (errorType)
133         {
134             case ErrorHandler.ERROR_FATAL:
135             case ErrorHandler.ERROR_PROCESS_FATAL:
136             case ErrorHandler.ERROR_NORMAL_ABORT:
137             case ErrorHandler.ERROR_NORMAL_IGNORE:
138             case ErrorHandler.ERROR_QUESTION_CANCEL:
139             case ErrorHandler.ERROR_QUESTION_OK:
140 
141                 return 1;
142 
143             case ErrorHandler.ERROR_QUESTION_NO:
144             case ErrorHandler.ERROR_QUESTION_YES:
145 
146                 return 2;
147 
148             case ErrorHandler.ERROR_WARNING:
149             case ErrorHandler.ERROR_MESSAGE:
150 
151                 return 1;
152         }
153         throw new IllegalArgumentException("unkonown error type");
154     }
155 
156     /**
157      * @param errorType
158      * @return the Uno attributes for each error type.
159      */
getAttributeFor(int errorType)160     private static int getAttributeFor(int errorType)
161     {
162         switch (errorType)
163         {
164             case ErrorHandler.ERROR_FATAL:
165                 return VclWindowPeerAttribute.OK;
166             case ErrorHandler.ERROR_PROCESS_FATAL:
167                 return VclWindowPeerAttribute.OK;
168             case ErrorHandler.ERROR_NORMAL_ABORT:
169                 return VclWindowPeerAttribute.OK_CANCEL + VclWindowPeerAttribute.DEF_CANCEL;
170             case ErrorHandler.ERROR_NORMAL_IGNORE:
171                 return VclWindowPeerAttribute.OK_CANCEL + VclWindowPeerAttribute.DEF_OK;
172             case ErrorHandler.ERROR_QUESTION_CANCEL:
173                 return VclWindowPeerAttribute.OK_CANCEL + VclWindowPeerAttribute.DEF_CANCEL;
174             case ErrorHandler.ERROR_QUESTION_OK:
175                 return VclWindowPeerAttribute.OK_CANCEL + VclWindowPeerAttribute.DEF_OK;
176             case ErrorHandler.ERROR_QUESTION_NO:
177                 return VclWindowPeerAttribute.YES_NO + VclWindowPeerAttribute.DEF_NO;
178             case ErrorHandler.ERROR_QUESTION_YES:
179                 return VclWindowPeerAttribute.YES_NO + VclWindowPeerAttribute.DEF_YES;
180             case ErrorHandler.ERROR_WARNING:
181                 return VclWindowPeerAttribute.OK;
182             case ErrorHandler.ERROR_MESSAGE:
183                 return VclWindowPeerAttribute.OK;
184         }
185         throw new IllegalArgumentException("unkonown error type");
186     }
187 
188     /**
189      * @deprecated
190      * @param errorType
191      * @return the uno service name for each error type
192      */
getServiceNameFor(int errorType)193     private static String getServiceNameFor(int errorType)
194     {
195         switch (errorType)
196         {
197             case ErrorHandler.ERROR_FATAL:
198                 return "errorbox";
199             case ErrorHandler.ERROR_PROCESS_FATAL:
200                 return "errorbox";
201             case ErrorHandler.ERROR_NORMAL_ABORT:
202                 return "errorbox";
203             case ErrorHandler.ERROR_NORMAL_IGNORE:
204                 return "warningbox";
205             case ErrorHandler.ERROR_QUESTION_CANCEL:
206                 return "querybox";
207             case ErrorHandler.ERROR_QUESTION_OK:
208                 return "querybox";
209             case ErrorHandler.ERROR_QUESTION_NO:
210                 return "querybox";
211             case ErrorHandler.ERROR_QUESTION_YES:
212                 return "querybox";
213             case ErrorHandler.ERROR_WARNING:
214                 return "warningbox";
215             case ErrorHandler.ERROR_MESSAGE:
216                 return "infobox";
217         }
218         throw new IllegalArgumentException("unkonown error type");
219     }
220 
221     /**
222      * renders the error
223      * @param ex the exception
224      * @param arg a free argument
225      * @param ix a free argument
226      * @param type the error type (from the int constants
227      * in ErrorHandler interface)
228      * @return a Strings which will be displayed in the message box,
229      * and which describes the error, and the needed action from the user.
230      */
getMessageFor(Exception ex, Object arg, int ix, int type)231     protected abstract String getMessageFor(Exception ex, Object arg, int ix, int type);
232 }
233 
234 
235