// ---------------------- AbstractPopup.java --------------------------- // package com.woven_media.gui.menu; /*********************************************************************** Copyright (C) 2008, Brent Allen Parrish This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. ************************************************************************/ import java.awt.Component; import java.awt.Toolkit; import java.awt.HeadlessException; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.io.IOException; // Swingx import javax.swing.JPopupMenu; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JMenuItem; /** * AbstractPopup class provides an abstraction for javax.swing.JPopup * menus. A Template Pattern form has been applied so common cut, copy and paste commands * can be easily implemented in classes that extend AbstractPopup class. Built-in * support for many data transfer operations have been fully-implemented. * AbstractPopup handles gaining access to the user's system clipboard * by following the recommendations in the Java 6 SE documentation * (Sun MicroSystems, Inc.) to check for an install of the java.lang.SecurityManager * when calling * getSystemClipboard(). If security checks are passed, the static member clipboard * is set and is made available to all classes extending AbstractPopup. * * @author Brent Allen Parrish * @version %I%, %G% * @since 1.0 * @see Template Pattern * @see java.lang.SecurityManager * @see com.woven_media.gui.menu.CutNPastePopup * @see com.woven_media.gui.menu.AbstractPopup */ public abstract class AbstractPopup extends JPopupMenu implements ActionListener { // Private members private Component host; private Component component; // Protected protected JMenuItem[] menuItems; protected static Clipboard clipboard; /** * Class constructor sets the java.awt.Component parent member * with a reference to this instance's parent component. A * java.awt.Component child reference to the component that the * popup will be attached to is set in the component * member. Calls the init method. * * @param host java.awt.Component reference * to the parent component. * @param component java.awt.Component reference * to the child component. * @since 1.0 */ public AbstractPopup(Component host, Component component) { this.host = host; this.component = component; init(); } /* * Initializes the menu building process and starts the security checks. * * @since 1.0 */ private void init() { buildMenu(); getClipboard(); } /** * Builds the popup by grabbing the resource strings for the * menu commands, setting the action commands and adding the * java.awt.event.ActionListener event handler. * * @since 1.0 */ protected void buildMenu() { // CutNPaste Popup menu menuItems = new JMenuItem[3]; // TODO: Localize the cut, copy & paste strings in a resource bundle. String cut = "Cut"; //rb.getString("popup.cut"); // I18N String copy = "Copy"; //rb.getString("popup.copy"); // I18N String paste = "Paste"; //rb.getString("popup.paste"); // I18N String[] labels = new String[] {cut, copy, paste}; String[] cmds = new String[] {"cut", "copy", "paste"}; for (int i = 0; i < labels.length; ++i) { menuItems[i] = new JMenuItem(labels[i]); menuItems[i].setActionCommand(cmds[i]); menuItems[i].addActionListener(this); add(menuItems[i]); } } /** * Checks to see if a java.lang.SecurityManager is installed. * * @return boolean value indicating whether access to the system clipboard is granted. * @see System#getSecurityManager * @since 1.0 */ protected boolean checkClipboardAccess() { SecurityManager security = System.getSecurityManager(); if (security != null) { try { security.checkSystemClipboardAccess(); return true; } catch(SecurityException se) { System.err.println(se.getCause()); System.err.println(se.getMessage()); se.printStackTrace(); return false; } } else { return true; } } /** * Sets the clipboard member if security checks are passed. * A java.awt.HeadLessException can be thrown by the * getSystemClipboard method if program is running within an * environment that does not support a mouse, video display or keyboard. * * @since 1.0 * @see java.awt.HeadlessException */ protected void getClipboard() { if(checkClipboardAccess()) { try { clipboard = host.getToolkit().getSystemClipboard(); } catch(HeadlessException he) { System.err.println(he.getCause()); System.err.println(he.getMessage()); he.printStackTrace(); } } } /** * Abstraction based on the Template Pattern that handles events * for cut, copy and paste commands. * * @param e java.awt.event.ActionEvent for capturing * action command strings. * @see java.awt.event.ActionListener * @see java.awt.event.ActionEvent * @see com.woven_media.gui.menu.AbstractPopup * @since 1.0 */ public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if(cmd.equals("cut")) cut(); if(cmd.equals("copy")) copy(); if(cmd.equals("paste"))paste(); } /** * Transfers data from the system clipboard. * * @since 1.0 */ protected abstract void paste(); /** * Copies selected text to the system clipboard. * * @since 1.0 */ protected abstract void copy(); /** * Clears text and transfers data to the system clipboard. * * @since 1.0 */ protected abstract void cut(); } // End class