home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 12.8 KB | 407 lines |
- package symantec.itools.awt;
-
- import java.awt.*;
- import java.awt.event.*;
- import java.util.Vector;
- import java.beans.PropertyVetoException;
- import java.beans.PropertyChangeListener;
- import java.beans.VetoableChangeListener;
- import java.beans.PropertyChangeEvent;
-
- // 03/03/97 RKM Fixed a bug in doTab, where it would tab to a hidden component
- // 03/13/97 RKM Added code in HandleEvent to handle return being hit in TextFields which
- // results in a Action being sent
- // 03/13/97 RKM Added ability to tab to TextComponents only, and defaulted it true for Mac
- // On platforms that support tabbing this is not needed, but on the Mac tabbing
- // would stop if you hit anything but a TextComponent - this seems a descent solution
- //
- // When encountering a non TextComponent on the Mac before defaulting to true:
- // Netscape - would not tab to the next component
- // Symantec - focus would be lost
- // Apple - tabbing works, but no focus is shown in non TextComponents and keys do nothing (ie List)
- // MW - same as Apple
- // 05/15/97 CAR Changed doTab to "deselect" the text of the text component being tabbed from
- // 06/01/97 RKM Updated to support Java 1.1
- // Made properties bound & constrained
- // Updated mechanism to keep track of contents of panel
- // Removed FKey to event binding mechanism (could I even support it?)
- // Removed tabbing functionality, since JDK 1.1 fixes this
- // 06/01/97 LAB Removed non-functioning deprecated methods. Changed vetos and changes to private
- // from protected. Updated the version to 1.1.
- // 07/29/97 CAR inner adaptor class implements java.io.Serializable
- // 08/13/97 RKM Removed tabbed - it did nothing
- // Removed setTabStop - it did nothing (with tabber removed)
- // Removed tabToTextComponentsOnly
- // Removed AutoTabState property
- // Removed all bound and constrained property stuff (it was no longer needed)
- // 08/19/97 CAR constructor now calls super.setLayout(null)
- // 08/19/97 BS don't add keylistener when the component added is a grid=>grid now functional
- // setCancelButton() and setDefaultButton() did not work, fixed message dispatching
- // 08/28/97 CAR corrected typo in componentRemoved()
- // 08/28/97 CAR when TAB is hit on last traversable item in panel focus goes back to first
- // traversable item in panel. CTRL+TAB transfers focus to next traversable item
- // outside of the panel.
-
- //
- //
-
- //???RKM??? I could not figure out an easy way to implement the tab hack that was previously here,
- // What it did was this, on lose focus it would save the event, if a key was pressed it would
- // remove the event, but if a key release was done (if it still had the event), it would
- // treat act as if a key had been pressed
-
- /**
- * A Panel extension which provides for
- * tabbing between components and supporting certain key accelerations
- * by posting events provided by the user. Can be used directly or extended.
- * When extending be sure to super()
- * during construction and to super.handleEvent(evt) from
- * handleEvent if you override it.
- * <p>
- * The tab focus order is based on the order in which the components were
- * added to the panel. Each component receives
- * focus in turn, but the mouse cursor is not relocated. Look at the Project
- * window to see the tab order of components within the KeyPressManagerPanel.
- * You can change the tab order by moving the component names in the Project
- * window list, or by adding them to the panel in a different order in your
- * source code.
- * <p>
- * Components respond to default events when they receive focus. For example,
- * the TextField component displays text input from the keyboard, and the
- * Button component issues an action event when it is clicked.
- * <p>
- * Use KeyPressManagerPanel to create a panel whose elements can be tabbed
- * through, and specifically to:
- * <UL>
- * <DT>╖ Create a subcontainer that organizes container space within an Applet,
- * Frame or Dialog container. This simplifies your component layout task.</DT>
- * <DT>╖ Hold other specialized Panel containers.</DT>
- * </UL>
- * @version 1.1, July 18, 1997
- * @author Symantec
- */
- public class KeyPressManagerPanel extends Panel implements java.io.Serializable
- {
- /**
- * Constructs a Panel which handles key press events.
- * By default, components added to this panel can be tabbed to. To change
- * this call method setAutoTabState() with false before adding components
- * that can't be tabbed to.
- */
- public KeyPressManagerPanel()
- {
- super.setLayout(null);
- resetKeyManager();
- }
-
- /**
- * Resets all KeyPressManager associations (default button, cancel
- * button, tab stop list).
- */
- public void resetKeyManager()
- {
- removeDefaultButton();
- removeCancelButton();
- }
-
- /**
- * Sets the button to press when the Enter or Return key
- * is pressed.
- * @param button the button to set as default
- * @see #removeDefaultButton
- */
- public void setDefaultButton(Button button)
- {
- removeDefaultButton();
- defaultButton = button;
- }
-
- /**
- * Removes Enter/Return key association with current default
- * button/event.
- * @see #setDefaultButton
- */
- public void removeDefaultButton()
- {
- defaultButton = null;
- }
-
- /**
- * @deprecated defaultButton will be sent action and focus will be set
- * @see #setDefaultButton(java.awt.Button)
- */
- public void setDefaultButton(Button button, Event evt, Container deliverTo, boolean bSetFocus)
- {
- setDefaultButton(button);
- }
-
- /**
- * Sets the button to press when the Escape key is pressed.
- * @param button the button to set as Cancel
- * @see #removeCancelButton
- */
- public void setCancelButton(Button button)
- {
- removeCancelButton();
- cancelButton = button;
- }
-
- /**
- * Removes Escape key association with current Cancel button/event.
- * @see #setCancelButton
- */
- public void removeCancelButton()
- {
- cancelButton = null;
- }
-
- /**
- * @deprecated CancelButton will be sent action and focus will be set.
- * @see #setCancelButton(java.awt.Button)
- */
- public void setCancelButton(Button button, Event evt, Container deliverTo, boolean bSetFocus)
- {
- setCancelButton(cancelButton);
- }
-
- //
- // Implementation
- //
-
- //???RKM??? As soon as we get to zips, fix this
- class A/*ction*/ implements ActionListener, java.io.Serializable
- {
- public void actionPerformed(ActionEvent evt)
- {
- sendAction(defaultButton);
- }
- }
-
- //???RKM??? As soon as we get to zips, fix this
- class K/*ey*/ extends KeyAdapter implements java.io.Serializable
- {
- //???RKM??? Switch to keyTyped when this works
- public void keyPressed(KeyEvent evt)
- //public void keyTyped(KeyEvent evt)
- {
- if (consumeKeyTyped(evt))
- evt.consume();
- }
- }
-
- /**
- * Inserts a component into this container at the given position, and
- * adds it to the layout manager using the specified constraints object.
- * <p>
- * This is a standard Java AWT method which is overridden to track
- * every add request to a container.
- *
- * @param component the component to add
- * @param constraints the positioning constraints for the layout manager
- * @param index the zero-relative index at which to add the component,
- * or -1 to append to the end
- * @see #remove
- */
- protected void addImpl(Component component, Object constraints, int index)
- {
- componentAdded(component);
-
- super.addImpl(component,constraints,index);
- }
-
- /**
- * Removes the component at the specified zero-relative index from this
- * container.
- * <p>
- * This is a standard Java AWT method which gets called to remove a
- * component from a container. When this happens the component's
- * removeNotify() will also get called to indicate component removal.
- *
- * @param index the zero-relative index of the component to remove
- * @see #removeAll
- * @see java.awt.Container#add
- */
- public void remove(int index)
- {
- componentRemoved(getComponent(index));
-
- super.remove(index);
- }
-
- /**
- * Removes all the components from this container.
- * <p>
- * This is a standard Java AWT method which gets called to remove all
- * the components from a container. When this happens each component's
- * removeNotify() will also get called to indicate component removal.
- *
- * @see #remove
- * @see java.awt.Container#add
- */
- public void removeAll()
- {
- //Make certain we remove them from our internal records
- Component[] components = getComponents();
- for (int i = 0;i < components.length;i++)
- componentRemoved(components[i]);
-
- super.removeAll();
- }
-
- /**
- * This helper method sends an action event to the specified button.
- */
- protected boolean sendAction(Button button)
- {
- if (button != null)
- {
- if (button.isEnabled())
- {
- //???RKM??? Should we check if it is visible
- button.requestFocus();
-
- //Send an acion event to the component
- button.dispatchEvent(new ActionEvent(button,ActionEvent.ACTION_PERFORMED,new String(button.getLabel())));
-
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * Internally called utility routine that handles ENTER and ESCAPE key processing.
- * @param evt the key event
- * @return true if an action event was sent to the default button
- */
- protected boolean consumeKeyTyped(KeyEvent evt)
- {
- switch (evt.getKeyCode())
- {
- case KeyEvent.VK_ENTER:
- {
- return sendAction(defaultButton);
- }
-
- case KeyEvent.VK_ESCAPE:
- {
- return sendAction(cancelButton);
- }
-
- case KeyEvent.VK_TAB:
- {
- return handleTabEvent(evt);
- }
- }//switch
-
- return false;
- }
-
- /**
- * Internally called utility routine that handles TAB key processing.
- * @param evt the key event
- * @return true if an event was processed
- */
- protected boolean handleTabEvent(KeyEvent evt)
- {
- Component[] ca = getComponents();
- Component lastTraversable = null;
- int i = ca.length - 1;
-
- do {
- if (ca[i].isFocusTraversable() == true)
- lastTraversable = ca[i];
- else
- --i;
- } while(ca[i].isFocusTraversable() != true && i >= 0);
-
- if (lastTraversable == null)
- return false;
-
- if((evt.getModifiers() & java.awt.event.InputEvent.CTRL_MASK)
- == java.awt.event.InputEvent.CTRL_MASK) {
- lastTraversable.transferFocus();
- return true;
- }
-
- if(evt.getComponent() == lastTraversable)
- {
- for(i = 0; i < ca.length; ++i) {
- if(ca[i].isFocusTraversable() == true) {
- ca[i].requestFocus();
- return true;
- }
- }
- }
-
- return false;
- }
-
- /**
- * Sets the tab stop if necessary, hooks up any listeners we need.
- * Called when any component is added to the panel
- * @param component component being added to the panel.
- * @see #componentRemoved
- */
- protected void componentAdded(Component component)
- {
- //If the component is already in the panel, don't process it
- if (symantec.itools.awt.util.Util.findComponent(this,component) != -1)
- return;
-
- if(component instanceof java.awt.TextField)
- {
- java.awt.TextField textField = (java.awt.TextField)component;
- textField.addActionListener(action);
- }
-
- if(!component.getClass().getName().equals("symantec.itools.db.awt.Grid"))
- component.addKeyListener(key);
-
- }
-
- /**
- * Utility routined automatically called before a component is removed from this panel.
- * It removes this panel's key event listeners and, for TextFields,
- * action event listeners.
- * @param component the component about to be removed from this panel
- */
- protected void componentRemoved(Component component)
- {
- //If the component is not in the panel, don't process it
- if (symantec.itools.awt.util.Util.findComponent(this,component) == -1)
- return;
-
- if(component instanceof java.awt.TextField)
- {
- java.awt.TextField textField = (java.awt.TextField)component;
- textField.removeActionListener(action);
- }
-
- if(!component.getClass().getName().equals("symantec.itools.db.awt.Grid"))
- component.removeKeyListener(key);
- }
-
- //Protected members
- /**
- * The button to press when the Enter/Return key is pressed.
- * @see #setDefaultButton
- */
- protected Button defaultButton;
- /**
- * The button to press when the Escape key is pressed.
- * @see #setCancelButton
- */
- protected Button cancelButton;
- /**
- * Listens for an action and posts a default button action when triggered.
- */
- protected A action = new A();
- /**
- * Listens for an key and if appropriate posts a default or cancel button action.
- */
- protected K key = new K();
- }
-
-