View Javadoc

1   /*
2    * $Id: CopyAction.java,v 1.9 2007/07/04 19:42:49 edankert Exp $
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  package org.xmlhammer.gui.actions;
22  
23  import java.awt.Toolkit;
24  import java.awt.event.KeyEvent;
25  
26  import javax.swing.ImageIcon;
27  import javax.swing.JComponent;
28  import javax.swing.KeyStroke;
29  import javax.swing.text.JTextComponent;
30  
31  import org.bounce.RunnableAction;
32  import org.xmlhammer.gui.XMLHammer;
33  import org.xmlhammer.gui.output.ResultPanel;
34  
35  /***
36   * An action that can be used to copy text.
37   *
38   * @version	$Revision: 1.9 $, $Date: 2007/07/04 19:42:49 $
39   * @author Edwin Dankert <edankert@gmail.com>
40   */
41   public class CopyAction extends RunnableAction {
42      private static final long serialVersionUID = -2937163195189032857L;
43  
44      private XMLHammer parent = null;
45  	
46   	/***
47  	 * The constructor for the action which allows for copying.
48  	 *
49  	 * @param parent the XML Hammer
50  	 */
51   	public CopyAction( XMLHammer parent) {
52  		super( "Copy");
53  		
54          this.parent = parent;
55          
56  		putValue( MNEMONIC_KEY, new Integer( 'C'));
57  		putValue( ACCELERATOR_KEY, KeyStroke.getKeyStroke( KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
58  		putValue( SMALL_ICON, new ImageIcon( getClass().getResource( "/org/xmlhammer/gui/icons/etool16/copy_edit.gif")));
59  		putValue( SHORT_DESCRIPTION, "Copy");
60  
61  		setEnabled( false);
62   	}
63      
64      /***
65       * The implementation of the copy action, called 
66       * after a user action.
67       */
68      public void run() {
69          JComponent component = parent.getLastSelectedComponent();
70          
71          if ( component instanceof JTextComponent) {
72              ((JTextComponent)component).copy();
73          } else if (component instanceof ResultPanel) {
74              ((ResultPanel)component).copySelectedNodes();
75          }
76      }
77  }