org.rimudb.editor.swing.OkCancelPanel.java Source code

Java tutorial

Introduction

Here is the source code for org.rimudb.editor.swing.OkCancelPanel.java

Source

/*
 * Copyright (c) 2008-2011 Simon Ritchie.
 * All rights reserved. 
 * 
 * This program is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU Lesser General Public License as published 
 * by the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program.  If not, see http://www.gnu.org/licenses/>.
 */
package org.rimudb.editor.swing;

import java.awt.event.*;

import javax.swing.*;
import javax.swing.plaf.*;

import org.apache.commons.logging.*;

/**
 * This panel provides a formatted Ok and Cancel button.
 */
public class OkCancelPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private static Log log = LogFactory.getLog(OkCancelPanel.class);
    public static final int UNASSIGNED_BUTTONS = 0;
    public static final int OK_BUTTON = 1;
    public static final int CANCEL_BUTTON = 2;
    public static final int OK_CANCEL_BUTTON = 3;
    private static final String uiClassID = "OkCancelPanelUI";
    private String okButtonText = null;
    private String cancelButtonText = null;
    private JButton okButton = null;
    private JButton cancelButton = null;
    private int options = UNASSIGNED_BUTTONS;

    /**
     * Construct the panel.
     */
    public OkCancelPanel() {
        this(OK_CANCEL_BUTTON);
    }

    /**
     * Construct the panel.
     */
    public OkCancelPanel(String okButtonText) {
        this(OK_CANCEL_BUTTON, okButtonText);
    }

    /**
     * Construct the panel.
     */
    public OkCancelPanel(int options) {
        this(options, "OK", "Cancel");
    }

    /**
     * Construct the panel.
     */
    public OkCancelPanel(int options, String okButtonText) {
        this(options, okButtonText, "Cancel");
    }

    /**
     * Construct the panel.
     */
    public OkCancelPanel(int options, String okButtonText, String cancelButtonText) {
        super();
        this.options = options;
        this.okButtonText = okButtonText;
        this.cancelButtonText = cancelButtonText;
        updateUI();
    }

    /**
     * Add action listeners.
     */
    public void addActionListener(ActionListener listener) {
        getOkButton().addActionListener(listener);
        getCancelButton().addActionListener(listener);
    }

    /**
     * Remove action listeners.
     */
    public void removeActionListener(ActionListener listener) {
        getOkButton().removeActionListener(listener);
        getCancelButton().removeActionListener(listener);
    }

    /**
     * Gets the okButton
     * @return Returns a JButton
     */
    public JButton getOkButton() {
        if (okButton == null) {
            okButton = new JButton(okButtonText);
            okButton.setDefaultCapable(true);
        }
        return okButton;
    }

    /**
     * Gets the cancelButton
     * @return Returns a JButton
     */
    public JButton getCancelButton() {
        if (cancelButton == null) {
            cancelButton = new JButton(cancelButtonText);
        }
        return cancelButton;
    }

    /**
     * Return the options selected
     */
    public int getOptions() {
        return options;
    }

    /** 
     * Used to install UI Delegate 
     */
    public void updateUI() {
        setUI(registerUIDelegate());
    }

    /** 
     * Register the UI Delegate 
     */
    protected ComponentUI registerUIDelegate() {
        ComponentUI compUI = null;

        Object obj = UIManager.get(uiClassID);
        if (obj == null) {
            // The default delegate class name
            Class basicDelegateClassName = org.rimudb.editor.swing.plaf.basic.BasicOkCancelPanelUI.class;
            String lafName = UIManager.getLookAndFeel().getID();

            // Look for a delegate for this LAF
            String packageName = basicDelegateClassName.getPackage().getName();
            int pos = packageName.indexOf("basic");
            String delegatePackageName = packageName.substring(0, pos);

            String lafDelegateClassName = delegatePackageName + lafName.toLowerCase() + "." + lafName
                    + "OkCancelPanelUI";

            try {
                // Try the specific delegate
                compUI = (ComponentUI) (Class.forName(lafDelegateClassName)).newInstance();
            } catch (Exception e1) {
                try {
                    // Default to the default delegate
                    compUI = (ComponentUI) basicDelegateClassName.newInstance();
                } catch (Exception e2) {
                    log.error("in OkCancelPanel.registerUIDelegate", e2);
                }
            }
        } else {
            compUI = (ComponentUI) obj;
        }
        return compUI;
    }

    /**
     *   This method gives the UI Manager a constant to use to look up in the UI Defaults table
     *   to find the class name of the UI Delegate for the installed L&F.
     *   @return string
     */
    public String getUIClassID() {
        return uiClassID;
    }

}