net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFJReferenceEditor.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFJReferenceEditor.java

Source

// Description: Java 7 Swing Reference Display Widget.

/*
 *   CFLib
 *
 *   Copyright (c) 2014 Mark Sobkow
 *   
 *      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.
 *   
 * ***********************************************************************
 *
 *   Code manufactured by MSS Code Factory
 *
 *   $Revision: 1274 $
 */

package net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing;

import java.math.*;
import java.net.URL;
import java.sql.*;
import java.text.*;
import java.util.*;
import java.awt.*;

import javax.swing.*;

import net.sourceforge.msscodefactory.cflib.v1_11.CFLib.*;

import org.apache.commons.codec.binary.Base64;

/**
 *   CFJReferenceEditor Swing Reference Display Widget.
 */
public class CFJReferenceEditor extends JPanel {
    public final static String PICK_ICON_RESOURCE = "/images/PickReference.gif";
    public final static String VIEW_ICON_RESOURCE = "/images/ViewReference.gif";
    protected ICFLibAnyObj2 referencedObject = null;
    protected JTextField textFieldQualifiedName = null;
    protected Action actionPickReference = null;
    protected Action actionViewReference = null;
    protected JButton buttonPickReference = null;
    protected JButton buttonViewReference = null;

    public CFJReferenceEditor(Action pickReference, Action viewReference) {
        super();
        final String S_ProcName = "construct";
        if (pickReference == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 1,
                    "pickReference");
        }
        if (viewReference == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 2,
                    "viewReference");
        }
        actionPickReference = pickReference;
        actionPickReference.putValue(Action.NAME, null);
        actionPickReference.putValue(Action.SMALL_ICON, getPickIcon());
        actionViewReference = viewReference;
        actionViewReference.putValue(Action.NAME, null);
        actionViewReference.putValue(Action.SMALL_ICON, getViewIcon());
        referencedObject = null;

        JTextField textField = getTextFieldQualifiedName();
        add(textField);
        textField.setBounds(0, 0, 100, 25);

        JButton button = getButtonPickReference();
        add(button);
        button.setBounds(100, 0, 25, 25);

        button = getButtonViewReference();
        add(button);
        button.setBounds(125, 0, 25, 25);

        Dimension min = new Dimension(150, 25);
        this.setMinimumSize(min);
    }

    public ICFLibAnyObj2 getReferencedObject() {
        return (referencedObject);
    }

    public void setEnabled(boolean value) {
        super.setEnabled(value);
        JTextField textField = getTextFieldQualifiedName();
        if (textField != null) {
            textField.setEnabled(false);
            if (value) {
                textField.setBackground(Color.WHITE);
            } else {
                Container cont = getParent();
                if (cont != null) {
                    textField.setBackground(cont.getBackground());
                }
            }
            textField.setForeground(Color.BLACK);
            textField.setDisabledTextColor(Color.BLACK);
        }
        JButton button = getButtonPickReference();
        if (button != null) {
            button.setEnabled(value);
        }
        button = getButtonViewReference();
        if (button != null) {
            button.setEnabled(true);
        }
    }

    public void setReferencedObject(ICFLibAnyObj2 value) {
        referencedObject = value;
        JTextField textField = getTextFieldQualifiedName();
        if (referencedObject != null) {
            String qualifiedName = referencedObject.getObjQualifiedName();
            if (qualifiedName != null) {
                textField.setText(qualifiedName);
            } else {
                textField.setText("");
            }
        } else {
            textField.setText("");
        }
    }

    public ImageIcon getPickIcon() {
        ImageIcon icon;
        URL imgURL = getClass().getResource(PICK_ICON_RESOURCE);
        if (imgURL != null) {
            icon = new ImageIcon(imgURL, "Pick Reference");
        } else {
            icon = null;
        }
        return (icon);
    }

    public ImageIcon getViewIcon() {
        ImageIcon icon;
        URL imgURL = getClass().getResource(VIEW_ICON_RESOURCE);
        if (imgURL != null) {
            icon = new ImageIcon(imgURL, "View Reference");
        } else {
            icon = null;
        }
        return (icon);
    }

    public JTextField getTextFieldQualifiedName() {
        if (textFieldQualifiedName == null) {
            textFieldQualifiedName = new JTextField();
            textFieldQualifiedName.setForeground(Color.BLACK);
            textFieldQualifiedName.setDisabledTextColor(Color.BLACK);
        }
        return (textFieldQualifiedName);
    }

    public JButton getButtonPickReference() {
        if (buttonPickReference == null) {
            buttonPickReference = new JButton(actionPickReference);
        }
        return (buttonPickReference);
    }

    public JButton getButtonViewReference() {
        if (buttonViewReference == null) {
            buttonViewReference = new JButton(actionViewReference);
        }
        return (buttonViewReference);
    }

    public void doLayout() {
        Dimension sz = getSize();
        int y;
        if (sz.height > 25) {
            y = (sz.height - 25) / 2;
        } else {
            y = 0;
        }
        JTextField textField = getTextFieldQualifiedName();
        textField.setBounds(0, y, sz.width - 50, 25);

        JButton button = getButtonPickReference();
        button.setBounds(sz.width - 50, y, 25, 25);

        button = getButtonViewReference();
        button.setBounds(sz.width - 25, y, 25, 25);
    }

}