001// Copyright 2006, 2008 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.util;
016
017import org.apache.tapestry5.ComponentEventCallback;
018
019/**
020 * A {@link org.apache.tapestry5.ComponentEventCallback} used for notification events. Event handler methods may return
021 * true (to abort the event) or false (to allow the event to continue bubbling up), but all other values are forbidden.
022 */
023public class NotificationEventCallback implements ComponentEventCallback
024{
025    private final String eventType;
026
027    private final String completeId;
028
029    public NotificationEventCallback(String eventType, String completeId)
030    {
031        this.eventType = eventType;
032        this.completeId = completeId;
033    }
034
035    public boolean handleResult(Object result)
036    {
037        if (result instanceof Boolean) return ((Boolean) result);
038
039        throw new IllegalArgumentException(
040                String.format("Event '%s' from %s received an event handler method return value of %s. This type of event does not support return values from event handler methods.", eventType, completeId, String
041                        .valueOf(result)));
042    }
043
044}