summaryrefslogtreecommitdiff
path: root/wizards/com/sun/star/wizards/web/AbstractErrorHandler.py
blob: 5f79384ef8395fbf68577769933f34df7deac126 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file incorporates work covered by the following license notice:
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements. See the NOTICE file distributed
#   with this work for additional information regarding copyright
#   ownership. The ASF licenses this file to you under the Apache
#   License, Version 2.0 (the "License"); you may not use this file
#   except in compliance with the License. You may obtain a copy of
#   the License at http://www.apache.org/licenses/LICENSE-2.0 .
#
from abc import abstractmethod

from .ErrorHandler import ErrorHandler
from ..common.SystemDialog import SystemDialog

from com.sun.star.awt.VclWindowPeerAttribute import OK, DEF_OK, OK_CANCEL, DEF_CANCEL
from com.sun.star.awt.VclWindowPeerAttribute import YES_NO, DEF_NO, DEF_YES

#import com.sun.star.awt.VclWindowPeerAttribute


# An abstract implementation of ErrorHandler, which
# uses a renderer method geMessageFor(Exception, Object, int, int)
# (in this class still abstract...)
# to render the errors, and displays
# error messeges.
class AbstractErrorHandler(ErrorHandler):

    xmsf = None
    peer = None

    def __init__(self, xmsf, peer_):
        self.xmsf = xmsf
        self.peer = peer_

    # Implementation of ErrorHandler:
    # shows a message box with the rendered error.
    # @param arg identifies the error. This object is passed to the render method
    # which returns the right error message.
    # @return true/false for continue/abort.
    def error(self, ex, arg, ix, errorType):
        if (errorType == ErrorHandler.ERROR_FATAL):
                return not self.showMessage(self.getMessageFor(ex, arg, ix, errorType), errorType)
        elif (errorType == ErrorHandler.ERROR_PROCESS_FATAL):
                return not self.showMessage(self.getMessageFor(ex, arg, ix, errorType), errorType)
        elif (errorType == ErrorHandler.ERROR_NORMAL_ABORT):
                return self.showMessage(self.getMessageFor(ex, arg, ix, errorType), errorType)
        elif (errorType == ErrorHandler.ERROR_NORMAL_IGNORE):
                return self.showMessage(self.getMessageFor(ex, arg, ix, errorType), errorType)
        elif (errorType == ErrorHandler.ERROR_QUESTION_CANCEL):
                return self.showMessage(self.getMessageFor(ex, arg, ix, errorType), errorType)
        elif (errorType == ErrorHandler.ERROR_QUESTION_OK):
                return self.showMessage(self.getMessageFor(ex, arg, ix, errorType), errorType)
        elif (errorType == ErrorHandler.ERROR_QUESTION_NO):
                return self.showMessage(self.getMessageFor(ex, arg, ix, errorType), errorType)
        elif (errorType == ErrorHandler.ERROR_QUESTION_YES):
                return self.showMessage(self.getMessageFor(ex, arg, ix, errorType), errorType)
        elif (errorType == ErrorHandler.ERROR_WARNING):
                return self.showMessage(self.getMessageFor(ex, arg, ix, errorType), errorType)
        elif (errorType == ErrorHandler.ERROR_MESSAGE):
                return self.showMessage(self.getMessageFor(ex, arg, ix, errorType), errorType)
        raise IllegalArgumentException("unknown error type")

    # @deprecated
    # @param message
    # @param errorType
    # @return true if the ok/yes button is clicked, false otherwise.
    def showMessage(self, message, errorType):
        return self.showMessage1(self.xmsf, self.peer, message, errorType)

    # display a message
    # @deprecated
    # @param xmsf
    # @param message the message to display
    # @param errorType an int constant from the ErrorHandler interface.
    # @return
    @classmethod
    def showMessage1(self, xmsf, peer, message, errorType):
        serviceName = self.getServiceNameFor(errorType)
        attribute = self.getAttributeFor(errorType)
        b = SystemDialog.showMessageBox(xmsf, serviceName, attribute, message, peer)
        return b == self.getTrueFor(errorType)

    @classmethod
    def showMessage2(self, xmsf, peer, message, dialogType, buttons, defaultButton, returnTrueOn):
        b = SystemDialog.showMessageBox(xmsf, dialogType, defaultButton + buttons, message, peer, )
        return b == returnTrueOn


    # normally ok(1) is the value for true.
    # but a question dialog may use yes. so i use this method
    # for each error type to get its type of "true" value.
    # @param errorType
    # @return
    @classmethod
    def getTrueFor(self, errorType):
        if (errorType in (ErrorHandler.ERROR_FATAL, ErrorHandler.ERROR_PROCESS_FATAL,
                          ErrorHandler.ERROR_NORMAL_ABORT, ErrorHandler.ERROR_NORMAL_IGNORE,
                          ErrorHandler.ERROR_QUESTION_CANCEL, ErrorHandler.ERROR_QUESTION_OK)):
                return 1
        elif (errorType in (ErrorHandler.ERROR_QUESTION_NO, ErrorHandler.ERROR_QUESTION_YES)):
                return 2
        elif (errorType in (ErrorHandler.ERROR_WARNING, ErrorHandler.ERROR_MESSAGE)):
                return 1
        raise IllegalArgumentException("unkonown error type")


    # @param errorType
    # @return the Uno attributes for each error type.
    @classmethod
    def getAttributeFor(self, errorType):
        if (errorType == ErrorHandler.ERROR_FATAL):
                return OK
        elif (errorType == ErrorHandler.ERROR_PROCESS_FATAL):
                return OK
        elif (errorType == ErrorHandler.ERROR_NORMAL_ABORT):
                return OK_CANCEL + DEF_CANCEL
        elif (errorType == ErrorHandler.ERROR_NORMAL_IGNORE):
                return OK_CANCEL + DEF_OK
        elif (errorType == ErrorHandler.ERROR_QUESTION_CANCEL):
                return OK_CANCEL + DEF_CANCEL
        elif (errorType == ErrorHandler.ERROR_QUESTION_OK):
                return OK_CANCEL + DEF_OK
        elif (errorType == ErrorHandler.ERROR_QUESTION_NO):
                return YES_NO + DEF_NO
        elif (errorType == ErrorHandler.ERROR_QUESTION_YES):
                return YES_NO + DEF_YES
        elif (errorType == ErrorHandler.ERROR_WARNING):
                return OK
        elif (errorType == ErrorHandler.ERROR_MESSAGE):
                return OK
        raise IllegalArgumentException("unkonown error type")

    # @deprecated
    # @param errorType
    # @return the uno service name for each error type
    @classmethod
    def getServiceNameFor(self, errorType):
        if (errorType == ErrorHandler.ERROR_FATAL):
                return "errorbox"
        elif (errorType == ErrorHandler.ERROR_PROCESS_FATAL):
                return "errorbox"
        elif (errorType == ErrorHandler.ERROR_NORMAL_ABORT):
                return "errorbox"
        elif (errorType == ErrorHandler.ERROR_NORMAL_IGNORE):
                return "warningbox"
        elif (errorType == ErrorHandler.ERROR_QUESTION_CANCEL):
                return "querybox"
        elif (errorType == ErrorHandler.ERROR_QUESTION_OK):
                return "querybox"
        elif (errorType == ErrorHandler.ERROR_QUESTION_NO):
                return "querybox"
        elif (errorType == ErrorHandler.ERROR_QUESTION_YES):
                return "querybox"
        elif (errorType == ErrorHandler.ERROR_WARNING):
                return "warningbox"
        elif (errorType == ErrorHandler.ERROR_MESSAGE):
                return "infobox"
        raise IllegalArgumentException("unkonown error type")

    # renders the error
    # @param ex the exception
    # @param arg a free argument
    # @param ix a free argument
    # @param type the error type (from the int constants
    # in ErrorHandler interface)
    # @return a Strings which will be displayed in the message box,
    # and which describes the error, and the needed action from the user.
    @abstractmethod
    def getMessageFor(self, ex, arg, ix, errorType):
        pass