home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-10-07 | 4.5 KB | 169 lines |
- package com.symantec.itools.swing.actions;
-
- import java.beans.*;
- import com.sun.java.swing.*;
-
- public class JActionButton
- extends JButton
- {
- public static final int DISPLAY_ACTION_ICON = 1;
- public static final int DISPLAY_ACTION_NAME = 2;
- public static final int DISPLAY_ACTION_ICON_AND_NAME = 3;
-
- public JActionButton()
- {
- setHorizontalTextPosition(RIGHT);
- setVerticalTextPosition(CENTER);
- }
-
- public JActionButton(com.sun.java.swing.Action newAction)
- {
- this();
- setAction(newAction);
- }
-
- //
- // Properties
- //
-
- // Action
-
- public com.sun.java.swing.Action getAction()
- {
- return m_Action;
- }
-
- public void setAction(com.sun.java.swing.Action newAction)
- {
- //Handle bad arg
- if (newAction == null)
- throw new IllegalArgumentException();
-
- //Do nothing if no change
- if (newAction == m_Action)
- return;
-
- //Release last action
- if (m_Action != null)
- {
- removeActionListener(m_Action);
- m_Action.removePropertyChangeListener(m_ActionPropertyChangeListener);
- }
-
- m_Action = newAction;
-
- addActionListener(m_Action);
-
- m_ActionPropertyChangeListener = createActionChangeListener();
- m_Action.addPropertyChangeListener(m_ActionPropertyChangeListener);
-
- updateMenuItem();
- }
-
- // Display
-
- public int getDisplay()
- {
- return m_Display;
- }
-
- public void setDisplay(int newDisplay)
- {
- if (m_Display != newDisplay)
- {
- m_Display = newDisplay;
-
- updateMenuItem();
- }
- }
-
- //
- // Implementation
- //
-
- protected PropertyChangeListener createActionChangeListener()
- {
- return new ActionChangedListener();
- }
-
- protected class ActionChangedListener
- implements PropertyChangeListener
- {
- public void propertyChange(PropertyChangeEvent e)
- {
- String propertyName = e.getPropertyName();
- if (propertyName.equals(Action.NAME))
- {
- String text = (String) e.getNewValue();
- setText(text);
- }
- else if (propertyName.equals("enabled"))
- {
- Boolean enabledState = (Boolean) e.getNewValue();
- setEnabled(enabledState.booleanValue());
- }
- else if (propertyName.equals(Action.SMALL_ICON))
- {
- Icon icon = (Icon) e.getNewValue();
- setIcon(icon);
- invalidate();
- repaint();
- }
- }
- }
-
- public void setIcon(Icon newIcon)
- {
- if (newIcon == null)
- {
- super.setIcon(null);
- setDisabledIcon(null);
- }
- else
- {
- super.setIcon(newIcon);
- if (!(newIcon instanceof com.sun.java.swing.ImageIcon))
- {
- if (newIcon instanceof com.symantec.itools.swing.icons.ImageIcon)
- {
- setDisabledIcon
- (new com.sun.java.swing.ImageIcon
- (GrayFilter.createDisabledImage
- (((com.symantec.itools.swing.icons.ImageIcon)newIcon).getImage())));
- }
- }
- }
- }
-
- protected void updateMenuItem()
- {
- if (m_Action == null)
- {
- setText("");
- setIcon(null);
- setEnabled(true);
- }
- else
- {
- if (m_Display == DISPLAY_ACTION_ICON_AND_NAME || m_Display == DISPLAY_ACTION_NAME)
- setText((String)m_Action.getValue(Action.NAME));
- else
- setText("");
-
- if (m_Display == DISPLAY_ACTION_ICON_AND_NAME || m_Display == DISPLAY_ACTION_ICON)
- setIcon((Icon)m_Action.getValue(Action.SMALL_ICON));
- else
- setIcon(null);
-
- setEnabled(m_Action.isEnabled());
- }
-
- invalidate();
- repaint();
- }
-
- protected com.sun.java.swing.Action m_Action = null;
- protected PropertyChangeListener m_ActionPropertyChangeListener = null;
- protected int m_Display = DISPLAY_ACTION_ICON_AND_NAME;
- }
-