001    /*
002     * $Id: TextContextMenuSource.java,v 1.5 2006/05/14 08:19:45 dmouse Exp $
003     *
004     * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005     * Santa Clara, California 95054, U.S.A. All rights reserved.
006     *
007     * This library is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU Lesser General Public
009     * License as published by the Free Software Foundation; either
010     * version 2.1 of the License, or (at your option) any later version.
011     * 
012     * This library is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015     * Lesser General Public License for more details.
016     * 
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this library; if not, write to the Free Software
019     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020     */
021    package org.jdesktop.swingx.plaf;
022    
023    import java.util.Map;
024    
025    import javax.swing.ActionMap;
026    import javax.swing.JComponent;
027    import javax.swing.JPasswordField;
028    import javax.swing.text.DefaultEditorKit;
029    import javax.swing.text.JTextComponent;
030    
031    /**
032     * @author Jeanette Winzenburg
033      */
034    public class TextContextMenuSource extends ContextMenuSource{
035    
036        String UNDO = "Undo";
037        String CUT = "Cut";
038        String COPY = "Copy";
039        String PASTE = "Paste";
040        String DELETE = "Delete";
041        String SELECT_ALL = "Select All";
042      
043        String[] keys = { DefaultEditorKit.cutAction, DefaultEditorKit.copyAction,
044                DefaultEditorKit.pasteAction, DefaultEditorKit.deleteNextCharAction, 
045                null, // separator
046                DefaultEditorKit.selectAllAction };
047    
048        String[] defaultValues = { CUT, COPY, PASTE, DELETE, null, SELECT_ALL,
049        };
050    
051        public String[] getKeys() {
052            return keys;
053        }
054    
055        public void updateActionEnabled(JComponent component, ActionMap map) {
056            if (!(component instanceof JTextComponent)) return;
057            JTextComponent textComponent = (JTextComponent) component;
058            boolean selectedText = textComponent.getSelectionEnd()
059                    - textComponent.getSelectionStart() > 0;
060            boolean containsText = textComponent.getDocument().getLength() > 0;
061            boolean editable = textComponent.isEditable();
062            boolean copyProtected = (textComponent instanceof JPasswordField);
063            boolean dataOnClipboard = textComponent.getToolkit()
064                    .getSystemClipboard().getContents(null) != null;
065            map.get(DefaultEditorKit.cutAction).setEnabled(
066                    !copyProtected && editable && selectedText);
067            map.get(DefaultEditorKit.copyAction).setEnabled(
068                    !copyProtected && selectedText);
069            map.get(DefaultEditorKit.pasteAction).setEnabled(
070                    editable && dataOnClipboard);
071            map.get(DefaultEditorKit.deleteNextCharAction).setEnabled(
072                    editable && selectedText);
073            map.get(DefaultEditorKit.selectAllAction).setEnabled(containsText);
074    
075        }
076    
077    
078        protected void initNames(Map<String, String> names) {
079            for (int i = 0; i < keys.length; i++) {
080                if (keys[i] != null) {
081                    names.put(keys[i],  getValue(keys[i], defaultValues[i]));
082                }
083            }
084        }
085    
086    
087        /**
088         * @return resource prefix
089         */
090        protected String getResourcePrefix() {
091            return "DefaultEditorKit.";
092        }
093    
094    }