ContainerEventRecordableEditor.java :  » Testing » jacareto » jacareto » editor » Java Open Source

Java Open Source » Testing » jacareto 
jacareto » jacareto » editor » ContainerEventRecordableEditor.java
/*
 * Jacareto Copyright (c) 2002-2005
 * Applied Computer Science Research Group, Darmstadt University of
 * Technology, Institute of Mathematics & Computer Science,
 * Ludwigsburg University of Education, and Computer Based
 * Learning Research Group, Aachen University. All rights reserved.
 *
 * Jacareto is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * Jacareto is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Jacareto; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package jacareto.editor;


import jacareto.record.ContainerEventRecordable;
import jacareto.struct.StructureElement;
import jacareto.system.Environment;
import jacareto.system.Language;
import jacareto.toolkit.event.TextValueListener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;

import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;

/**
 * <p>
 * An editor for container event recordables.
 * </p>
 *
 * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
 * @version 1.01
 */
public class ContainerEventRecordableEditor extends ComponentEventRecordableEditor {
    /** The text field for the container's name. */
    private JTextField containerField;

    /** The text field for the child's name. */
    private JTextField childField;

    /** The combo box for the event type (=id). */
    private JComboBox typeBox;

    /**
     * Create a new container event recordable editor.
     *
     * @param env the environment
     */
    public ContainerEventRecordableEditor (Environment env) {
        super(env);

        Language language = getLanguage ();

        // The container field.
        containerField = addTextFieldRow (language.getString ("Events.ContainerEvent.Container"), 20);
        containerField.getDocument ().addDocumentListener (new TextValueListener() {
                public void textValueChanged (DocumentEvent e) {
                    if (isUpdateOnChange && (getElement () != null)) {
                        ((ContainerEventRecordable) getElement ()).setContainerName (containerField.getText ());
                    }
                }
            });

        // The child field.
        childField = addTextFieldRow (language.getString ("Events.ContainerEvent.Child"), 20);
        childField.getDocument ().addDocumentListener (new TextValueListener() {
                public void textValueChanged (DocumentEvent e) {
                    if (isUpdateOnChange && (getElement () != null)) {
                        ((ContainerEventRecordable) getElement ()).setChildName (childField.getText ());
                    }
                }
            });

        // The type box
        typeBox = addComboBoxRow (language.getString ("Events.ContainerEvent.Type"));
        typeBox.addItem (language.getString ("Events.ContainerEvent.Added"));
        typeBox.addItem (language.getString ("Events.ContainerEvent.Removed"));
        typeBox.addActionListener (new ActionListener() {
                public void actionPerformed (ActionEvent e) {
                    ContainerEventRecordable cRecordable = (ContainerEventRecordable) getElement ();

                    if (isUpdateOnChange && (cRecordable != null)) {
                        switch (typeBox.getSelectedIndex ()) {
                            case 0:
                                cRecordable.setID (ContainerEvent.COMPONENT_ADDED);

                                break;

                            case 1:
                                cRecordable.setID (ContainerEvent.COMPONENT_REMOVED);

                                break;
                        }
                    }
                }
            });
    }

    /**
     * Returns whether this editor is responsible for a given structure element. This editor is
     * responsible for all container event recordables.
     *
     * @param element the structure element
     *
     * @return <code>true</code> if <i>element</i> is an container event recordable and not
     *         <code>null</code>, otherwise <code>false</code>
     */
    public boolean handlesElement (StructureElement element) {
        return (element != null) && (element instanceof ContainerEventRecordable);
    }

    /**
     * Sets the structure element to edit.
     *
     * @param element DOCUMENT ME!
     */
    public void setElement (StructureElement element) {
        super.setElement (element);

        ContainerEventRecordable cRecordable = (ContainerEventRecordable) element;
        containerField.setText (cRecordable.getContainerName ());
        childField.setText (cRecordable.getChildName ());

        switch (cRecordable.getID ()) {
            case ContainerEvent.COMPONENT_ADDED:
                typeBox.setSelectedIndex (0);

                break;

            case ContainerEvent.COMPONENT_REMOVED:
                typeBox.setSelectedIndex (1);

                break;
        }
    }
}
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.