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

Java Open Source » Testing » jacareto 
jacareto » jacareto » editor » ComponentPropertiesRecordableEditor.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.ComponentPropertiesRecordable;
import jacareto.struct.StructureElement;
import jacareto.system.Environment;
import jacareto.system.Language;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JPanel;

/**
 * An editor for component mode recordables.
 *
 * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
 * @version 1.0
 */
public class ComponentPropertiesRecordableEditor extends Editor {
    /** The structure element to edit. */
    private StructureElement element;

    /** The editor component. */
    private JPanel editorComponent;

    /** Components for the isEnabled property. */
    private JCheckBox isEnabledCheckBox;
    private JComboBox isEnabledComboBox;

    /** Components for the isVisible property. */
    private JCheckBox isVisibleCheckBox;
    private JComboBox isVisibleComboBox;
    private JCheckBox keepEnabledStateCheckBox;

    /**
     * Create a new editor.
     *
     * @param env the environment
     */
    public ComponentPropertiesRecordableEditor (Environment env) {
        super(env);

        Language language = getLanguage ();

        editorComponent = new JPanel(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(5, 5, 5, 5);
        c.anchor = GridBagConstraints.WEST;
        c.fill = GridBagConstraints.NONE;

        // The enabled field
        isEnabledCheckBox = new JCheckBox(language.getString (
                    "Recordables.ComponentPropertiesRecordable.Attributes.IsEnabled") + ":");
        isEnabledCheckBox.addActionListener (new ActionListener() {
                public void actionPerformed (ActionEvent e) {
                    if (getElement () != null) {
                        ((ComponentPropertiesRecordable) getElement ()).setApplyIsEnabled (isEnabledCheckBox.isSelected ());
                        isEnabledComboBox.setEnabled (isEnabledCheckBox.isSelected ());
                    }
                }
            });
        isEnabledComboBox = new JComboBox();
        isEnabledComboBox.addItem (language.getString ("General.True"));
        isEnabledComboBox.addItem (language.getString ("General.False"));
        isEnabledComboBox.addActionListener (new ActionListener() {
                public void actionPerformed (ActionEvent e) {
                    if (isUpdateOnChange && (getElement () != null)) {
                        ((ComponentPropertiesRecordable) getElement ()).setIsEnabled (isEnabledComboBox.getSelectedIndex () == 0);
                    }
                }
            });
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 20;
        c.gridwidth = 1;
        editorComponent.add (isEnabledCheckBox, c);
        c.gridx = 1;
        c.gridy = 0;
        c.weightx = 30;
        c.gridwidth = 1;
        editorComponent.add (isEnabledComboBox, c);

        // The visible field
        isVisibleCheckBox = new JCheckBox(language.getString (
                    "Recordables.ComponentPropertiesRecordable.Attributes.IsVisible") + ":");
        isVisibleCheckBox.addActionListener (new ActionListener() {
                public void actionPerformed (ActionEvent e) {
                    if (getElement () != null) {
                        ((ComponentPropertiesRecordable) getElement ()).setApplyIsVisible (isVisibleCheckBox.isSelected ());
                        isVisibleComboBox.setEnabled (isVisibleCheckBox.isSelected ());
                    }
                }
            });
        isVisibleComboBox = new JComboBox();
        isVisibleComboBox.addItem (language.getString ("General.True"));
        isVisibleComboBox.addItem (language.getString ("General.False"));
        isVisibleComboBox.addActionListener (new ActionListener() {
                public void actionPerformed (ActionEvent e) {
                    if (isUpdateOnChange && (getElement () != null)) {
                        ((ComponentPropertiesRecordable) getElement ()).setIsVisible (isVisibleComboBox.getSelectedIndex () == 0);
                    }
                }
            });
        c.gridx = 0;
        c.gridy = 1;
        c.weightx = 20;
        c.gridwidth = 1;
        editorComponent.add (isVisibleCheckBox, c);
        c.gridx = 1;
        c.gridy = 1;
        c.weightx = 30;
        c.gridwidth = 1;
        editorComponent.add (isVisibleComboBox, c);

        // The keep state field
        keepEnabledStateCheckBox = new JCheckBox(language.getString (
                    "Recordables.ComponentPropertiesRecordable.Attributes.KeepEnabledState"));
        keepEnabledStateCheckBox.addActionListener (new ActionListener() {
                public void actionPerformed (ActionEvent e) {
                    if (getElement () != null) {
                        ((ComponentPropertiesRecordable) getElement ()).setKeepState (keepEnabledStateCheckBox.isSelected ());
                    }
                }
            });
        c.gridx = 0;
        c.gridy = 2;
        editorComponent.add (keepEnabledStateCheckBox, c);
    }

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

    /**
     * Returns the editor component.
     *
     * @return the component
     */
    public Component getComponent () {
        return editorComponent;
    }

    /**
     * Returns the actual structure element to be edited.
     *
     * @return the element
     */
    public StructureElement getElement () {
        return element;
    }

    /**
     * Sets the structure element to edit.
     *
     * @param element the structure element
     */
    public void setElement (StructureElement element) {
        this.element = element;

        ComponentPropertiesRecordable recordable = (ComponentPropertiesRecordable) element;

        // isEnabled
        isEnabledCheckBox.setSelected (recordable.getApplyIsEnabled ());

        if (recordable.getIsEnabled ()) {
            isEnabledComboBox.setSelectedIndex (0);
        } else {
            isEnabledComboBox.setSelectedIndex (1);
        }

        isEnabledComboBox.setEnabled (recordable.getApplyIsEnabled ());

        // isVisible
        isVisibleCheckBox.setSelected (recordable.getApplyIsVisible ());

        if (recordable.getIsVisible ()) {
            isVisibleComboBox.setSelectedIndex (0);
        } else {
            isVisibleComboBox.setSelectedIndex (1);
        }

        isVisibleComboBox.setEnabled (recordable.getApplyIsVisible ());

        keepEnabledStateCheckBox.setSelected (recordable.getKeepState ());
    }
}
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.