Coverage Report - org.argouml.uml.ui.ActionCopy
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionCopy
27%
8/29
7%
1/14
2.8
 
 1  
 /* $Id: ActionCopy.java 17881 2010-01-12 21:09:28Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2006 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies.  This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.uml.ui;
 40  
 
 41  
 import java.awt.Toolkit;
 42  
 import java.awt.datatransfer.DataFlavor;
 43  
 import java.awt.datatransfer.UnsupportedFlavorException;
 44  
 import java.awt.event.ActionEvent;
 45  
 import java.io.IOException;
 46  
 
 47  
 import javax.swing.AbstractAction;
 48  
 import javax.swing.Action;
 49  
 import javax.swing.Icon;
 50  
 import javax.swing.event.CaretEvent;
 51  
 import javax.swing.event.CaretListener;
 52  
 import javax.swing.text.JTextComponent;
 53  
 
 54  
 import org.argouml.application.helpers.ResourceLoaderWrapper;
 55  
 import org.argouml.i18n.Translator;
 56  
 import org.tigris.gef.base.CmdCopy;
 57  
 import org.tigris.gef.base.Globals;
 58  
 
 59  
 /**
 60  
  * The Copy Action.
 61  
  */
 62  
 public class ActionCopy extends AbstractAction implements CaretListener {
 63  
 
 64  
     ////////////////////////////////////////////////////////////////
 65  
     // static variables
 66  
 
 67  900
     private static ActionCopy instance = new ActionCopy();
 68  
 
 69  
     private static final String LOCALIZE_KEY = "action.copy";
 70  
 
 71  
     ////////////////////////////////////////////////////////////////
 72  
     // constructors
 73  
     /**
 74  
      * Constructor.
 75  
      */
 76  
     public ActionCopy() {
 77  900
         super(Translator.localize(LOCALIZE_KEY));
 78  900
         Icon icon = ResourceLoaderWrapper.lookupIcon(LOCALIZE_KEY);
 79  900
         if (icon != null) {
 80  900
             putValue(Action.SMALL_ICON, icon);
 81  
         }
 82  900
         putValue(
 83  
                  Action.SHORT_DESCRIPTION,
 84  
                  Translator.localize(LOCALIZE_KEY) + " ");
 85  900
     }
 86  
 
 87  
     /**
 88  
      * @return the singleton
 89  
      */
 90  
     public static ActionCopy getInstance() {
 91  6320
         return instance;
 92  
     }
 93  
 
 94  
     private JTextComponent textSource;
 95  
 
 96  
     /*
 97  
      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 98  
      */
 99  
     public void actionPerformed(ActionEvent ae) {
 100  0
         if (textSource != null) {
 101  0
             textSource.copy();
 102  0
             Globals.clipBoard = null;
 103  
         } else {
 104  0
             CmdCopy cmd = new CmdCopy();
 105  0
             cmd.doIt();
 106  
         }
 107  0
         if (isSystemClipBoardEmpty()
 108  
             && (Globals.clipBoard == null
 109  
                 || Globals.clipBoard.isEmpty())) {
 110  0
             ActionPaste.getInstance().setEnabled(false);
 111  
         } else {
 112  0
             ActionPaste.getInstance().setEnabled(true);
 113  
         }
 114  0
     }
 115  
 
 116  
     /*
 117  
      * @see javax.swing.event.CaretListener#caretUpdate(javax.swing.event.CaretEvent)
 118  
      */
 119  
     public void caretUpdate(CaretEvent e) {
 120  0
         if (e.getMark() != e.getDot()) { // there is a selection
 121  0
             setEnabled(true);
 122  0
             textSource = (JTextComponent) e.getSource();
 123  
         } else {
 124  0
             setEnabled(false);
 125  0
             textSource = null;
 126  
         }
 127  0
     }
 128  
 
 129  
     private boolean isSystemClipBoardEmpty() {
 130  
         try {
 131  0
             Object text =
 132  
                 Toolkit.getDefaultToolkit().getSystemClipboard()
 133  
                     .getContents(null).getTransferData(DataFlavor.stringFlavor);
 134  0
             return text == null;
 135  0
         } catch (IOException ignorable) {
 136  0
         } catch (UnsupportedFlavorException ignorable) {
 137  0
         }
 138  0
         return true;
 139  
     }
 140  
 
 141  
 } /* end class ActionCopy */