/*
 * Copyright 2011 JBoss Inc 
 *
 * Licensed 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
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbpm.formbuilder.client.command;

import org.jbpm.formapi.client.CommonGlobals;
import org.jbpm.formbuilder.client.bus.UndoRedoEvent;
import org.jbpm.formbuilder.client.bus.UndoRedoHandler;
import org.jbpm.formbuilder.client.bus.UndoableEvent;
import org.jbpm.formbuilder.client.bus.UndoableHandler;
import org.jbpm.formbuilder.client.options.UndoRedoManager;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.MenuItem;
import com.gwtent.reflection.client.Reflectable;

/**
 * Handles the redo action
 */
@Reflectable
public class EditFormRedoCommand implements BaseCommand {

    private final UndoRedoManager mgr = UndoRedoManager.getInstance();
    private MenuItem item = null;
    
    public EditFormRedoCommand() {
        EventBus bus = CommonGlobals.getInstance().getEventBus();
        bus.addHandler(UndoableEvent.TYPE, new UndoableHandler() {
            @Override
            public void onEvent(UndoableEvent event) {  
                checkEnabled();
            }
            @Override
            public void doAction(UndoableEvent event) {  }
            @Override
            public void undoAction(UndoableEvent event) {  }
        });
        bus.addHandler(UndoRedoEvent.TYPE, new UndoRedoHandler() {
            @Override
            public void onEvent(UndoRedoEvent event) {
                checkEnabled();
            }
         });
    }
    
    @Override
    public void execute() {
        if (mgr.canRedo()) {
            mgr.redo();
        }
        checkEnabled();
    }

    @Override
    public void setEmbeded(String profile) {
        //shouldn't be disabled when embedded
    }

    @Override
    public void setItem(MenuItem item) {
        this.item = item;
        checkEnabled();
    }

    private void checkEnabled() {
        if (this.item != null) {
            this.item.setEnabled(mgr.canRedo());
        }
    }
}
