org.archiviststoolkit.editor.rde.RdeTextField.java Source code

Java tutorial

Introduction

Here is the source code for org.archiviststoolkit.editor.rde.RdeTextField.java

Source

/*
 * Archivists' Toolkit(TM) Copyright  2005-2007 Regents of the University of California, New York University, & Five Colleges, Inc.  
 * All rights reserved. 
 *   
 * This software is free. You can redistribute it and / or modify it under the terms of the Educational Community License (ECL) 
 * version 1.0 (http://www.opensource.org/licenses/ecl1.php) 
 *
 * This software 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 ECL license for more details about permissions and limitations. 
 *
 *
 * Archivists' Toolkit(TM) 
 * http://www.archiviststoolkit.org 
 * info@archiviststoolkit.org 
 *
 * Created by JFormDesigner on Fri Jul 18 10:57:13 EDT 2008
 */

package org.archiviststoolkit.editor.rde;

import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import javax.swing.*;
import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;
import org.archiviststoolkit.structure.ATFieldInfo;
import org.archiviststoolkit.structure.DefaultValues;
import org.archiviststoolkit.model.ResourcesComponents;
import org.archiviststoolkit.model.RDEScreenPanelItems;
import org.archiviststoolkit.exceptions.RDEPopulateException;
import org.archiviststoolkit.exceptions.RDEPanelCreationException;

public class RdeTextField extends RdePanel {

    private boolean sticky = false;

    public RdeTextField(RdePanelContainer parentPanel, ATFieldInfo fieldInfo) {
        super(parentPanel, fieldInfo);
        initComponents();
        if (fieldInfo != null) {
            this.label.setText(fieldInfo.getFieldLabel());
        }
    }

    public void populateComponent(ResourcesComponents component) throws RDEPopulateException {
        try {
            fieldInfo.getSetMethod().invoke(component, textField.getText());
        } catch (IllegalAccessException e) {
            throw new RDEPopulateException(e);
        } catch (InvocationTargetException e) {
            throw new RDEPopulateException(e);
        }
    }

    public void clearFields() {
        if (!sticky) {
            textField.setText("");
        }
    }

    public void setStickyLabels() {
        setLabelColor(sticky, label);
    }

    public void initializeStickyLabels(Collection<RDEScreenPanelItems> panelItems)
            throws RDEPanelCreationException {
        for (RDEScreenPanelItems panelItem : panelItems) {
            if (panelItem.getPropertyName().equals(this.fieldInfo.getFieldName())) {
                this.sticky = panelItem.getSticky();
            } else {
                throw new RDEPanelCreationException(panelItem.getPropertyName() + "is not supported here");
            }
        }
    }

    // Method to set the default value
    public void setDefaultValues() {
        DefaultValues defaultValue = getDefaultValue();
        if (defaultValue != null && (!sticky || showDefaultValues)) {
            textField.setText(defaultValue.getStringValue());
        }
        showDefaultValues = false;
    }

    private void textFieldFocusGained(FocusEvent e) {
        parentPanel.componentFocusGained(e, this);
    }

    private void labelMouseClicked(MouseEvent e) {
        if (e.getClickCount() == 1 && e.isControlDown()) {
            sticky = !sticky;
            setStickyLabels();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner non-commercial license
        separator3 = new JSeparator();
        label = new JLabel();
        textField = new JTextField();
        CellConstraints cc = new CellConstraints();

        //======== this ========
        setBackground(new Color(200, 205, 232));
        setLayout(new FormLayout(
                new ColumnSpec[] { FormFactory.DEFAULT_COLSPEC, FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
                        new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW) },
                new RowSpec[] { FormFactory.DEFAULT_ROWSPEC, FormFactory.LINE_GAP_ROWSPEC,
                        FormFactory.DEFAULT_ROWSPEC }));

        //---- separator3 ----
        separator3.setBackground(new Color(220, 220, 232));
        separator3.setForeground(new Color(147, 131, 86));
        separator3.setFont(new Font("Trebuchet MS", Font.PLAIN, 13));
        add(separator3, cc.xywh(1, 1, 3, 1));

        //---- label ----
        label.setText("Label");
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                labelMouseClicked(e);
            }
        });
        add(label, cc.xy(1, 3));

        //---- textField ----
        textField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                textFieldFocusGained(e);
            }
        });
        add(textField, cc.xy(3, 3));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner non-commercial license
    private JSeparator separator3;
    private JLabel label;
    private JTextField textField;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}