FileDocCategorySizeDatePackage
DateBeanCustomizer_batch.javaAPI DocExample6235Sat Sep 12 03:01:00 BST 1998borland.samples.beans.customizer

DateBeanCustomizer_batch.java

/*
 * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
 * 
 * This SOURCE CODE FILE, which has been provided by Borland as part
 * of a Borland product for use ONLY by licensed users of the product,
 * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
 *
 * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
 * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
 * THE PRODUCT.
 *
 * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
 * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
 * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
 * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
 * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
 * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
 * CODE FILE.
 */
//
// batch mode customizer allows the user to make changes in the customizer UI
// and then uses an <apply> button to signal that it's time to fire the property
// changed events.  This type of mechanism is often paired with a "revert" mechanism
// to fall back to the default state of the bean at the time the customizer was
// loaded
//
package borland.samples.beans.customizer;

import java.awt.*;
import java.beans.*;
import com.sun.java.swing.*;
import java.awt.event.*;
import borland.jbcl.control.*;

public class DateBeanCustomizer_batch extends JPanel implements Customizer{
  // variables used by the Customizer
  PropertyChangeSupport pcs = null;
  DateBean bean = null;
  Integer oldStyle = null;
  Boolean oldUseMonthString = null;

  // variables used for UI design
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel southPanel = new JPanel();
  FlowLayout fL1 = new FlowLayout();
  JPanel buttonPanel = new JPanel();
  GridLayout gL1 = new GridLayout();
  JButton apply = new JButton();
  JLabel title = new JLabel();
  JPanel centerPanel = new JPanel();
  BorderLayout bL2 = new BorderLayout();
  ChoicePanel choices = new ChoicePanel();
  JPanel eastPanel = new JPanel();
  JCheckBox useMS = new JCheckBox();
  JButton revert = new JButton();

  public DateBeanCustomizer_batch() {
    // all the neat stuff lives in setObject()
  }

  public void setObject(Object in) {
    // jbInit() is fired from here instead of the constructor because it
    // is assumed in the Beans Spec that setObject() is called first and is
    // handled before any re-parenting of the Customizer by an IDE
    try { jbInit(); } catch (Exception e) { e.printStackTrace(); }

    if(in instanceof DateBean){
      bean = (DateBean)in;

      // capture old state values for use by revert
      oldStyle = new Integer(bean.getStyle());
      oldUseMonthString = new Boolean(bean.getUseMonthString());

      // seed the customizer with copies the right settings for the current copy
      choices.setSelectedItem(bean.getStyle());
      useMS.setSelected(bean.getUseMonthString());
      }
    else
      System.out.println("how'd that happen?");
  }

  // called when changing the useMonthString property
  private void changeUseMonthStringProperty(Boolean newValue){
    Boolean currentStyle = new Boolean(bean.getUseMonthString());
    bean.setUseMonthString(newValue.booleanValue());
    if(pcs != null)
      pcs.firePropertyChange("useMonthString", currentStyle, newValue);

  }

  // called when changing the style property
  private void changeStyleProperty(Integer newValue){
    Integer currentStyle = new Integer(bean.getStyle());
    bean.setStyle(newValue.intValue());
    if(pcs != null)
      pcs.firePropertyChange("style", currentStyle, newValue);
  }

  // Constructs the UI for the Customizer
  private void jbInit() throws Exception {
    this.setLayout(borderLayout1);
    southPanel.setLayout(fL1);
    fL1.setAlignment(2);
    gL1.setColumns(1);
    gL1.setHgap(5);
    apply.setText("Apply");
    apply.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        apply_actionPerformed(e);
      }
    });
    title.setText("Date Bean Customizer");
    useMS.setText("Use Month String");
    revert.setText("Revert");
    revert.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        revert_actionPerformed(e);
      }
    });
    centerPanel.setLayout(bL2);
    buttonPanel.setLayout(gL1);
    this.add(southPanel, BorderLayout.SOUTH);
    southPanel.add(buttonPanel, null);
    buttonPanel.add(apply, null);
    buttonPanel.add(revert, null);
    this.add(title, BorderLayout.NORTH);
    this.add(centerPanel, BorderLayout.CENTER);
    centerPanel.add(choices, BorderLayout.CENTER);
    centerPanel.add(eastPanel, BorderLayout.EAST);
    eastPanel.add(useMS, null);
  }

  // Use Insets to make UI stand out when reparented by an IDE
  public Insets getInsets(){ return new Insets(5,5,5,5); }

  // Apply takes fires the proper change method based on the selected items
  // in the Customizers' UI
  void apply_actionPerformed(ActionEvent e) {
    if(bean != null){
      changeStyleProperty(new Integer(choices.getSelectedItem()));
      changeUseMonthStringProperty(new Boolean(useMS.isSelected()));
    }
  }

  // Revert returns the bean to it's state prior to the launch of the Customizer
  // and updates the Customizer UI to reflect that change
  void revert_actionPerformed(ActionEvent e) {
    changeStyleProperty(oldStyle);
    choices.setSelectedItem(oldStyle.intValue());

    changeUseMonthStringProperty(oldUseMonthString);
    useMS.setSelected(oldUseMonthString.booleanValue());
  }

  // required by implementation of Customizer Interface
  public void addPropertyChangeListener(PropertyChangeListener parm1) {
    if(pcs == null)
      pcs = new PropertyChangeSupport(this);
    pcs.addPropertyChangeListener(parm1);
  }

  // required by implementation of Customizer Interface
  public void removePropertyChangeListener(PropertyChangeListener parm1) {
    if(pcs != null)
      pcs.removePropertyChangeListener(parm1);
  }
}