001/*
002 * #%L
003 * GwtMaterial
004 * %%
005 * Copyright (C) 2015 - 2017 GwtMaterialDesign
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 * 
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package gwt.material.design.client.events;
021
022import com.google.gwt.event.shared.HandlerRegistration;
023import com.google.gwt.user.client.ui.IsWidget;
024import com.google.gwt.user.client.ui.Widget;
025
026import java.util.ArrayList;
027import java.util.List;
028
029public class DefaultHandlerRegistry implements HandlerRegistry {
030
031    private final Widget widget;
032    private List<HandlerRegistration> handlers;
033    private HandlerRegistration attachHandler;
034
035    private boolean clearOnUnload = true;
036    private boolean loaded = false;
037
038    public DefaultHandlerRegistry(IsWidget widget) {
039        this(widget, true);
040    }
041
042    public DefaultHandlerRegistry(IsWidget widget, boolean clearOnUnload) {
043        this(widget.asWidget(), clearOnUnload);
044    }
045
046    public DefaultHandlerRegistry(Widget widget, boolean clearOnUnload) {
047        assert widget != null : "Widget cannot be null";
048        this.widget = widget;
049        this.clearOnUnload = clearOnUnload;
050
051        load();
052    }
053
054    public void load() {
055        assert !loaded : "Cannot load an already loaded handler registry.";
056        attachHandler = widget.asWidget().addAttachHandler(event -> {
057            // Detach event
058            if(clearOnUnload && !event.isAttached()) {
059                clearHandlers();
060            }
061        });
062    }
063
064    public void unload() {
065        if(attachHandler != null) {
066            attachHandler.removeHandler();
067            attachHandler = null;
068        }
069
070        clearHandlers();
071        loaded = false;
072    }
073
074    @Override
075    public HandlerRegistration registerHandler(HandlerRegistration handler) {
076        if(handlers == null) {
077            handlers = new ArrayList<>();
078        }
079
080        handlers.add(handler);
081        return handler;
082    }
083
084    @Override
085    public void removeHandler(HandlerRegistration registration) {
086        if (registration != null) {
087            registration.removeHandler();
088        }
089    }
090
091    @Override
092    public void clearHandlers() {
093        if(handlers != null) {
094            for(HandlerRegistration handler : handlers) {
095                removeHandler(handler);
096            }
097            handlers.clear();
098        }
099    }
100
101    public void setClearOnUnload(boolean clearOnUnload) {
102        this.clearOnUnload = clearOnUnload;
103    }
104
105    public boolean isClearOnUnload() {
106        return clearOnUnload;
107    }
108
109    @Override
110    public Widget asWidget() {
111        return widget;
112    }
113}