All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.randelshofer.quaqua.QuaquaTextComponentPopupHandler Maven / Gradle / Ivy

Go to download

A Mavenisation of the Quaqua Mac OSX Swing Look and Feel (Java library) Quaqua Look and Feel (C) 2003-2010, Werner Randelshofer. Mavenisation by Matt Gumbley, DevZendo.org - for problems with Mavenisation, see Matt; for issues with Quaqua, see the Quaqua home page. For full license details, see http://randelshofer.ch/quaqua/license.html

The newest version!
/*
 * @(#)QuaquaTextComponentPopupHandler.java  
 *
 * Copyright (c) 2006-2010 Werner Randelshofer, Immensee, Switzerland.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */

package ch.randelshofer.quaqua;

import ch.randelshofer.quaqua.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
/**
 * TextComponentHandler displays a popup menu on a JTextComponent with the
 * cut/copy and paste actions.
 * The Quaqua text component UI's register a shared instance of
 * QuaquaTextComponentPopupHandler as a mouse listener on all JTextComponent's.
 *
 * @author Werner Randelshofer.
 * @version $Id: QuaquaTextComponentPopupHandler.java 361 2010-11-21 11:19:20Z wrandelshofer $
 */
public class QuaquaTextComponentPopupHandler extends MouseAdapter {
    private JPopupMenu popupMenu;
    private AbstractAction cutAction;
    private AbstractAction copyAction;
    private AbstractAction pasteAction;
    
    /** Creates a new instance. */
    public QuaquaTextComponentPopupHandler() {
        popupMenu = new JPopupMenu();
        popupMenu.add(cutAction = new DefaultEditorKit.CutAction());
        popupMenu.add(copyAction = new DefaultEditorKit.CopyAction());
        popupMenu.add(pasteAction = new DefaultEditorKit.PasteAction());
        
        cutAction.putValue(Action.NAME, UIManager.getString("TextComponent.cut"));
        copyAction.putValue(Action.NAME, UIManager.getString("TextComponent.copy"));
        pasteAction.putValue(Action.NAME, UIManager.getString("TextComponent.paste"));
    }
    
    public void mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger()) {
            showPopup(e);
        }
    }
    
    public void mousePressed(MouseEvent e) {
        if (e.isPopupTrigger()) {
            showPopup(e);
        }
    }
    
    protected void showPopup(MouseEvent e) {
        JTextComponent src = (JTextComponent) e.getSource();
        
        boolean isFocusable = Methods.invokeGetter(src, "isFocusable", true);
        
        if (src.getClientProperty("Quaqua.TextComponent.showPopup") != Boolean.FALSE &&
                src.isEnabled() &&
                isFocusable &&
                Methods.invokeGetter(src,"getComponentPopupMenu",null) == null) {
            cutAction.setEnabled(! (src instanceof JPasswordField) &&
                    src.getSelectionEnd() > src.getSelectionStart() &&
                    src.isEditable()
                    );
            copyAction.setEnabled(! (src instanceof JPasswordField) &&
                    src.getSelectionEnd() > src.getSelectionStart()
                    );
            pasteAction.setEnabled(src.isEditable()
            );
            src.requestFocus();
            popupMenu.show(src, e.getX(), e.getY());
            e.consume();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy