Package DisplayProject

Source Code of DisplayProject.TableJButton$TableJButtonUI

/*
Copyright (c) 2003-2008 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package DisplayProject;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.AbstractButton;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import DisplayProject.controls.ReadOnlyButtonModel;
import DisplayProject.factory.PushButtonFactory;
import Framework.CloneHelper;
import Framework.ImageData;

import com.sun.java.swing.plaf.windows.WindowsButtonUI;

/**
* A TableJButton is a standard Swing button that can be used as a table cell editor if the need arises.<p>
* Note that by default in Swing, pressing the mouse button on a JButton, moving the mouse a few pixels
* and releasing the mouse is considered a drag gesture and will not fire the actionPerformed listener.
* As this behaviour is different to forte, the mouse events generated have been massaged into something
* more paletable for a Forte mouse user.
*
* @author Peter + Tim
*/
@SuppressWarnings("serial")
public class TableJButton extends JButton implements TableCellRenderer, PropertyChangeListener {
  /**
   * Hold the image value associated with this button. This will be non-null only for PictureButtons
   */
  private ImageData imageValue;
  /**
   * Whether this button is "editable", that is pressable without changing the display. (Hence the difference
   * between editable and enabled is that enabled = false sets the button to grey and doesn't fire events, but
   * editable = false doesn't change the button but still prevents events being fired.
   */
    private boolean editable = true;
   
  public TableJButton() {
    super();
    // TF:17 Sep 2009:implements status line text (moved here from the factory)
        this.addMouseListener(StatusTextListener.sharedInstance());
      // TF:15/12/2009:DET-141:Set this up to allow inheriting of popup menu
      this.setInheritsPopupMenu(true);
  }
 
  public void setImageValue(ImageData imageValue) {
    if (imageValue == this.imageValue) {
      // Do nothing
      return;
    }
    // TF:25/03/2009:Disassociate ourselves from the old image value if there is one
    if (this.imageValue != null) {
      this.imageValue.removePropertyChangeListener("value", this);
    }
    this.imageValue = imageValue;
    if (imageValue == null) {
      this.setIcon(null);
    }
    else {
      // Monitor the value of the imageValue, in case it changes via a "setValue"
      imageValue.addPropertyChangeListener("value", this);
      this.setIcon(imageValue.asIconImage());
    }
  }
 
  public ImageData getImageValue() {
    return imageValue;
  }
 
  @Override
  public void setIcon(Icon icon) {
    // Set the imageValue as well
    if (icon == null) {
      this.imageValue = null;
      this.setIcon_internal(null);
    }
    else {
      if (imageValue == null) {
        imageValue = new ImageData();
        // Associate this button listener with the value change event
        imageValue.addPropertyChangeListener("value", this);
      }
      this.imageValue.setValue((ImageIcon)icon);
      // No need to call setIcon_internal here, as this will trigger the property change event
    }
  }
 
  private void setIcon_internal(Icon icon) {
    super.setIcon(icon);
   
    if (icon != null) {
      Insets insets = this.getInsets();
          this.setMinimumSize(new Dimensionicon.getIconWidth()+insets.left+insets.right,
                            icon.getIconHeight()+insets.top+insets.bottom));
          this.setPreferredSize(this.getMinimumSize());
    }
  }

  public void propertyChange(PropertyChangeEvent evt) {
    if (this.imageValue != null) {
      this.setIcon_internal(imageValue.asIconImage());
    }
  }
 
    /**
     * Implement the TableCellRenderer interface
     */
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {

        return this;
    }

    @Override                                  
    public void setText(String text) {
        if (text != null){
            int index = text.indexOf('&');
            if (index >= 0) {
              // got the mnemonic, extract it
              if (index < text.length() - 1) {
                char mnemonic = text.charAt(index + 1);
                // DK:12/12/2008:do not add mnemonic if '&' symbol is just doubled
                if (mnemonic != '&') {
                  super.setMnemonic(mnemonic);         
                }         
              }
              // Only replace the first one, in case they've done eg "&&"
              String value = text.replaceFirst("&", "");
              super.setText(value);
            } else {
                super.setText(text);
            }
        }
        else {
            super.setText(text);
        }
        revalidate();
    }

    /**
     * Process the mouse events in a Forte style manner.
     */
    protected void processMouseEvent(MouseEvent e) {
        if (e.getID() == MouseEvent.MOUSE_RELEASED && e.getClickCount() == 1) {
            // The mouse button is being released as per normal, and it's the first click. Process it as per normal.
            super.processMouseEvent(e);

            // If the release occured within the bounds of this component, we want to simulate a click as well
            if (this.contains(e.getX(), e.getY())) {
                super.processMouseEvent(new MouseEvent(e.getComponent(),
                                                        MouseEvent.MOUSE_CLICKED,
                                                        e.getWhen(),
                                                        e.getModifiers(),
                                                        e.getX(),
                                                        e.getY(),
                                                        e.getClickCount(),
                                                        e.isPopupTrigger(),
                                                        e.getButton()));
            }
        }
        else if (e.getID() == MouseEvent.MOUSE_CLICKED && e.getClickCount() == 1) {
            // Normal clicks are ignored to prevent duplicate events from normal, non-moved events
        }
        else {
            // Otherwise, just process as per normal.
            super.processMouseEvent(e);
        }
    } 

    /**
     * In Forte, the minimum size of a button is a fraction bigger than the ones
     * in Java, so we'll adjust this here.
     */
    @Override
    public Dimension getMinimumSize() {
        GridCell cell = GridField.getConstraints(this);
        Dimension d = getUI().getMinimumSize(this);

        // If we're natural or to_parent then the minimum size is
        // the UI minimum size
        // TF:06/01/2009:JCT-625:Added the case of partnerships in here
        Dimension requestedMinSize = super.getMinimumSize();
        switch (cell.widthPolicy) {
          case Constants.SP_NATURAL:
          case Constants.SP_TO_PARENT:
          case Constants.SP_TO_PARTNER:
            // TF:02/05/2008:Corrected the minimum size for icon only
            if (getIcon() != null && "".equals(getText())) {
              d.width += 2;
            }
            else {
              d.width += 8;
            }
            break;
          default:
            d.width = requestedMinSize.width;
        }
       
        switch (cell.heightPolicy) {
        case Constants.SP_NATURAL:
        case Constants.SP_TO_PARENT:
        case Constants.SP_TO_PARTNER:
            // TF:02/05/2008:Corrected the minimum size for icon only
            if (getIcon() != null && "".equals(getText())) {
              d.height += 2;
            }
            else {
              d.height += 3;
            }
            break;
          default:
            d.height = requestedMinSize.height;
        }
        return d;
    }

    /**
     * Set this component to be editable or not. An editable toggle field allows its value to be
     * changed whereas a non-editable one does not allow it's value to be changed by the user, but
     * can still be changed programmatically. This is needed so that view-only components will still
     * fire mouse events like click and child click, whereas simply disabling the component would
     * not allow these events to be fired.
     * @param editable
     */
    public void setEditable(boolean editable) {
      if (this.editable != editable) {
        this.editable = editable;
        // Invoke setModel so we set the model to be the correct sort of model
        if (editable) {
          ButtonModel model = getModel();
          if (model instanceof ReadOnlyButtonModel) {
            this.setModel(((ReadOnlyButtonModel)model).getOriginalModel());
          }
        }
        else {
          ButtonModel model = getModel();
          if (!(model instanceof ReadOnlyButtonModel)) {
            this.setModel(new ReadOnlyButtonModel(this, model));
          }
        }
        this.firePropertyChange("editable", !editable, editable);
      }
  }
   
    /**
     * Return true if the the component's value can be changed by the user.
     * @return
     */
    public boolean isEditable() {
    return editable;
  }

    /**
     * Set the model. This will also set the editable property of the control.
     */
    @Override
    public void setModel(ButtonModel newModel) {
      if (newModel instanceof ReadOnlyButtonModel) {
        editable = false;
      }
      else {
        editable = true;
      }
    super.setModel(newModel);
    }
   

  /**
   * Override the tooltip text so that if it's being set to "" then we actually set the
   * value to null to remove the tooltip text, as this is how Forte behaved.
   */
  @Override
  public void setToolTipText(String pText) {
    if (pText == null || pText.equals("")) {
      super.setToolTipText(null);
    }
    else {
      super.setToolTipText(pText);
    }
  }

    public JButton cloneComponent(){
        JButton clone = PushButtonFactory.newInstance(getName());
        CloneHelper.cloneComponent(this, clone, new String[]{"UI", // class javax.swing.plaf.ButtonUI
                "UIClassID", // class java.lang.String
                "accessibleContext", // class javax.accessibility.AccessibleContext
                "action", // interface javax.swing.Action
                "actionCommand", // class java.lang.String
                "actionListeners", // class [Ljava.awt.event.ActionListener;
                "actionMap", // class javax.swing.ActionMap
                //"alignmentX", // float
                //"alignmentY", // float
                "ancestorListeners", // class [Ljavax.swing.event.AncestorListener;
                "autoscrolls", // boolean
                //"background", // class java.awt.Color
                "border", // interface javax.swing.border.Border
                //"borderPainted", // boolean
                "changeListeners", // class [Ljavax.swing.event.ChangeListener;
                "component", // null
                "componentCount", // int
                //"componentPopupMenu", // class javax.swing.JPopupMenu
                "components", // class [Ljava.awt.Component;
                "containerListeners", // class [Ljava.awt.event.ContainerListener;
                "contentAreaFilled", // boolean
                "debugGraphicsOptions", // int
                //"defaultButton", // boolean
                //"defaultCapable", // boolean
                "disabledIcon", // interface javax.swing.Icon
                "disabledSelectedIcon", // interface javax.swing.Icon
                //"displayedMnemonicIndex", // int
                //"doubleBuffered", // boolean
                //"enabled", // boolean
                "focusCycleRoot", // boolean
                "focusPainted", // boolean
                "focusTraversalKeys", // null
                "focusTraversalPolicy", // class java.awt.FocusTraversalPolicy
                "focusTraversalPolicyProvider", // boolean
                "focusTraversalPolicySet", // boolean
                //"focusable", // boolean
                //"font", // class java.awt.Font
                //"foreground", // class java.awt.Color
                "graphics", // class java.awt.Graphics
                //"height", // int
                //"horizontalAlignment", // int
                //"horizontalTextPosition", // int
                //"icon", // interface javax.swing.Icon
                //"iconTextGap", // int
                //"inheritsPopupMenu", // boolean
                "inputMap", // null
                "inputVerifier", // class javax.swing.InputVerifier
                //"insets", // class java.awt.Insets
                "itemListeners", // class [Ljava.awt.event.ItemListener;
                //"label", // class java.lang.String
                "layout", // interface java.awt.LayoutManager
                "managingFocus", // boolean
                //"margin", // class java.awt.Insets
                //"maximumSize", // class java.awt.Dimension
                //"minimumSize", // class java.awt.Dimension
                //"mnemonic", // int
                "model", // interface javax.swing.ButtonModel
                "multiClickThreshhold", // long
                // "name", // class java.lang.String
                "nextFocusableComponent", // class java.awt.Component
                //"opaque", // boolean
                //"optimizedDrawingEnabled", // boolean
                "paintingTile", // boolean
                "preferredSize", // class java.awt.Dimension
                "pressedIcon", // interface javax.swing.Icon
                "registeredKeyStrokes", // class [Ljavax.swing.KeyStroke;
                //"requestFocusEnabled", // boolean
                //"rolloverEnabled", // boolean
                "rolloverIcon", // interface javax.swing.Icon
                "rolloverSelectedIcon", // interface javax.swing.Icon
                "rootPane", // class javax.swing.JRootPane
                //"selected", // boolean
                "selectedIcon", // interface javax.swing.Icon
                "selectedObjects", // class [Ljava.lang.Object;
                //"text", // class java.lang.String
                //"toolTipText", // class java.lang.String
                "topLevelAncestor", // class java.awt.Container
                "transferHandler", // class javax.swing.TransferHandler
                "validateRoot", // boolean
                //"verifyInputWhenFocusTarget", // boolean
                //"verticalAlignment", // int
                //"verticalTextPosition", // int
                "vetoableChangeListeners", // class [Ljava.beans.VetoableChangeListener;
                //"visible", // boolean
                "visibleRect", // class java.awt.Rectangle
                //"width", // int
                //"x", // int
                //"y" // int
                });
        return clone;
    }

    @Override
    public void updateUI() {
        setUI(new TableJButtonUI());
        invalidate();
    }   


    public class TableJButtonUI extends WindowsButtonUI {

        @Override
        protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect) {
            AbstractButton b = (AbstractButton) c;                          
            ButtonModel model = b.getModel();
            Icon icon = b.getIcon();
            Icon tmpIcon = null;

        if(icon == null) {
            return;
        }

          if(!model.isEnabled()) {
            if(model.isSelected()) {
              tmpIcon = (Icon) b.getDisabledSelectedIcon();
            } else {
              tmpIcon = (Icon) b.getDisabledIcon();
            }
          } else if(model.isPressed() && model.isArmed()) {
            tmpIcon = (Icon) b.getPressedIcon();
            if(tmpIcon != null) {
              // revert back to 0 offset
              clearTextShiftOffset();
            }
          } else if(b.isRolloverEnabled() && model.isRollover()) {
            if(model.isSelected()) {
              tmpIcon = (Icon) b.getRolloverSelectedIcon();
            } else {
              tmpIcon = (Icon) b.getRolloverIcon();
            }
          } else if(model.isSelected()) {
            tmpIcon = (Icon) b.getSelectedIcon();
          }

          if(tmpIcon != null) {
            icon = tmpIcon;
          }
          else {
            // TF:Mar 11, 2010:Changed this to create the transparent icon only if necessary
              int width = icon.getIconWidth();
              int height = icon.getIconHeight();
              BufferedImage buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
              icon.paintIcon(c, buf.createGraphics(),0,0);
              /*
               * for transparent icon
               */
              //PM:25/4/08 move code to common method
              UIutils.transparentImage(buf, UIutils.Gray2);
              icon = new ImageIcon(buf);
          }

          if(model.isPressed() && model.isArmed()) {
            icon.paintIcon(c, g, iconRect.x + getTextShiftOffset(),
                iconRect.y + getTextShiftOffset());
            } else {
                icon.paintIcon(c, g, iconRect.x, iconRect.y);
            }
        }

    }
}
TOP

Related Classes of DisplayProject.TableJButton$TableJButtonUI

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.