Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  
  /*
   * Copyright (C) 2006 Sun Microsystems, Inc. All rights reserved. Use is
   * subject to license terms.
   */ 
  
  package org.jdesktop.application;
  
  import java.awt.AWTEvent;
An ActionMap class that defines cut/copy/paste/delete. This class only exists to paper over limitations in the standard JTextComponent cut/copy/paste/delete javax.swing.Actions. The standard cut/copy Actions don't keep their enabled property in sync with having the focus and (for copy) having a non-empty text selection. The standard paste Action's enabled property doesn't stay in sync with the current contents of the clipboard. The paste/copy/delete actions must also track the JTextComponent editable property. The new cut/copy/paste/delete are installed lazily, when a JTextComponent gets the focus, and before any other focus-change related work is done. See updateFocusOwner().

Author(s):
Hans Muller (Hans.Muller@Sun.COM)
Scott Violet (Scott.Violet@Sun.COM)
 
 class TextActions extends AbstractBean {
     private final ApplicationContext context;
     private final CaretListener textComponentCaretListener;
     private final PropertyChangeListener textComponentPCL;
     private final String markerActionKey = "TextActions.markerAction";
     private final javax.swing.Action markerAction;
     private boolean copyEnabled = false;    // see setCopyEnabled
     private boolean cutEnabled = false;     // see setCutEnabled
     private boolean pasteEnabled = false;   // see setPasteEnabled
     private boolean deleteEnabled = false;  // see setDeleteEnabled
 
     public TextActions(ApplicationContext context) {
         this. = context;
 	    public void actionPerformed(ActionEvent e) { } 
         };
     }
 
     private ApplicationContext getContext() {
         return ;
     }
 
     private JComponent getFocusOwner() {
 	return 	getContext().getFocusOwner();
     }
 
     private Clipboard getClipboard() {
 	return getContext().getClipboard();
     }
 
     /* Called by the KeyboardFocus PropertyChangeListener in ApplicationContext,
      * before any other focus-change related work is done.
      */
     void updateFocusOwner(JComponent oldOwnerJComponent newOwner) {
 	if (oldOwner instanceof JTextComponent) {
 	    JTextComponent text = (JTextComponent)oldOwner;
 	}
 	if (newOwner instanceof JTextComponent) {
 	    JTextComponent text = (JTextComponent)newOwner;
 	    maybeInstallTextActions(text);
 	    updateTextActions(text);
 	}
 	else if (newOwner == null) {
 	    setCopyEnabled(false);
 	    setCutEnabled(false);
 	    setPasteEnabled(false);
	    setDeleteEnabled(false);
	}
    }
    private final class ClipboardListener implements FlavorListener {
	public void flavorsChanged(FlavorEvent e) {
	    JComponent c = getFocusOwner();
	    if (c instanceof JTextComponent) {
	    }
	}
    }
    private final class TextComponentCaretListener implements CaretListener {
        public void caretUpdate(CaretEvent e) {
        }
    }
    private final class TextComponentPCL implements PropertyChangeListener {
	    String propertyName = e.getPropertyName();
	    if ((propertyName == null) || "editable".equals(propertyName)) {
	    }
	}
    }
    private void updateTextActions(JTextComponent text) {
	Caret caret = text.getCaret();
	boolean selection = (caret.getDot() != caret.getMark());
	boolean editable = text.isEditable();
	setCopyEnabled(selection);
	setCutEnabled(editable && selection);
	setDeleteEnabled(editable && selection);
	setPasteEnabled(editable && data);
    }
    // TBD: what if text.getActionMap is null, or if it's parent isn't the UI-installed actionMap
    private void maybeInstallTextActions(JTextComponent text) {
	ActionMap actionMap = text.getActionMap();
	if (actionMap.get() == null) {
	    actionMap.put();
	    ActionMap textActions = getContext().getActionMap(getClass(), this);
	    for(Object key : textActions.keys()) {
		actionMap.put(keytextActions.get(key));
	    }
	}
    }
    /* This method lifted from JTextComponent.java 
     */
    private int getCurrentEventModifiers() {
        int modifiers = 0;
        AWTEvent currentEvent = EventQueue.getCurrentEvent();
        if (currentEvent instanceof InputEvent) {
            modifiers = ((InputEvent)currentEvent).getModifiers();
        } 
	else if (currentEvent instanceof ActionEvent) {
            modifiers = ((ActionEvent)currentEvent).getModifiers();
        }
        return modifiers;
    }
    private void invokeTextAction(JTextComponent textString actionName) {
        ActionMap actionMap = text.getActionMap().getParent();
	long eventTime = EventQueue.getMostRecentEventTime();
	int eventMods = getCurrentEventModifiers();
	ActionEvent actionEvent = 
	    new ActionEvent(text.actionNameeventTimeeventMods);
	actionMap.get(actionName).actionPerformed(actionEvent);
    }
    @Action(enabledProperty = "cutEnabled")
    public void cut(ActionEvent e) {
	Object src = e.getSource();
	if (src instanceof JTextComponent) {
	    invokeTextAction((JTextComponent)src"cut");
	}
    }
    public boolean isCutEnabled() { return ; }
    public void setCutEnabled(boolean cutEnabled) { 
	boolean oldValue = this.
	this. = cutEnabled;
	firePropertyChange("cutEnabled"oldValuethis.);
    }
    @Action(enabledProperty = "copyEnabled")
    public void copy(ActionEvent e) {
	Object src = e.getSource();
	if (src instanceof JTextComponent) {
	    invokeTextAction((JTextComponent)src"copy");
	}
    }
    public boolean isCopyEnabled() { return ; }
    public void setCopyEnabled(boolean copyEnabled) { 
	boolean oldValue = this.
	this. = copyEnabled;
	firePropertyChange("copyEnabled"oldValuethis.);
    }
    @Action(enabledProperty = "pasteEnabled")
    public void paste(ActionEvent e) {
	Object src = e.getSource();
	if (src instanceof JTextComponent) {
	    invokeTextAction((JTextComponent)src"paste");
	}
    }
    public boolean isPasteEnabled() { return ; }
    public void setPasteEnabled(boolean pasteEnabled) { 
	boolean oldValue = this.
	this. = pasteEnabled;
	firePropertyChange("pasteEnabled"oldValuethis.);
    }
    @Action(enabledProperty = "deleteEnabled")
    public void delete(ActionEvent e) {
	Object src = e.getSource();
	if (src instanceof JTextComponent) {
	    /* The deleteNextCharAction is bound to the delete key in
	     * text components.  The name appears to be a misnomer,
	     * however it's really a compromise.  Calling the method
	     * by a more accurate name,
	     *   "IfASelectionExistsThenDeleteItOtherwiseDeleteTheNextCharacter"
	     * would be rather unwieldy.
	     */
	}
    }
    public boolean isDeleteEnabled() { return ; }
    public void setDeleteEnabled(boolean deleteEnabled) { 
	boolean oldValue = this.
	this. = deleteEnabled;
	firePropertyChange("deleteEnabled"oldValuethis.);
    }
New to GrepCode? Check out our FAQ X