FormWindow.java :  » Workflow-Engines » osbl-1_0 » org » osbl » client » wings » form » editor » Java Open Source

Java Open Source » Workflow Engines » osbl 1_0 
osbl 1_0 » org » osbl » client » wings » form » editor » FormWindow.java
package org.osbl.client.wings.form.editor;

import org.osbl.client.wings.shell.Window;
import org.osbl.client.wings.shell.Client;
import org.osbl.client.wings.form.DelegateEnvironment;
import org.osbl.client.wings.form.ObjectForm;
import org.osbl.client.wings.XButton;
import org.osbl.client.wings.XOptionPane;
import org.osbl.client.action.*;

import javax.swing.event.EventListenerList;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class FormWindow
    extends Window
{
    private EventListenerList listenerList = new EventListenerList();

    DelegateEnvironment delegateEnvironment = new DelegateEnvironment(this);

    ObjectForm form;

    public FormWindow(ObjectForm objectForm) {
        this.form = objectForm;
        delegateEnvironment.setActive(true);
        delegateEnvironment.setNestedEnvironment((DelegateEnvironment)objectForm.getEnvironment());
    }

    public ObjectForm getForm() {
        return form;
    }

    public final void addPopAction(ObjectAction action) {
        listenerList.add(ObjectAction.class, action);
        delegateEnvironment.addControl(new XButton(new FormWindow.PopAction(action)));
    }

    public final void removePopAction(ObjectAction action) {
        listenerList.remove(ObjectAction.class, action);
    }

    private void firePopEvent(ObjectActionEvent event) {
        Object[] listeners = listenerList.getListenerList();
        if (listeners.length == 0)
            return;

        for (int i = listeners.length - 2; i >= 0; i -= 2) {
            ObjectAction listener = (ObjectAction)listeners[i + 1];
            listener.actionPerformed(event);
        }
    }

    class PopAction
        extends AbstractAction
    {
        ObjectAction objectAction;

        public PopAction(ObjectAction objectAction) {
            this.objectAction = objectAction;
            putValue(Action.ACTION_COMMAND_KEY, objectAction.getValue(ObjectAction.ACTION_COMMAND_KEY));
            putValue(AuthorizedAction.PERMISSION_KEY, objectAction.getValue(AuthorizedAction.PERMISSION_KEY));
        }

        public void actionPerformed(ActionEvent e) {
            if (form.getEnvironment().getHasChanges()) {
                String name = Client.getInstance().getResourceProvider().getMessage(form.getType().getName());
                String message = Client.getInstance().getResourceProvider().getMessage("businessobject.messages.unsavedChanges");
                XOptionPane.showConfirmDialog(null, message, name, new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        if (XOptionPane.YES_ACTION == e.getActionCommand())
                            pop();
                    }
                });
            }
            else
                pop();
        }

        private void pop() {
            Client.getInstance().popWindow(FormWindow.this);
            ObjectActionEvent event = new ObjectActionEvent(this, form.getObject());
            firePopEvent(event);
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.