Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ActionCut |
|
| 3.1666666666666665;3.167 |
1 | /* $Id: ActionCut.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 | * linus | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2007 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.Transferable; | |
44 | import java.awt.datatransfer.UnsupportedFlavorException; | |
45 | import java.awt.event.ActionEvent; | |
46 | import java.io.IOException; | |
47 | import java.util.Collection; | |
48 | ||
49 | import javax.swing.AbstractAction; | |
50 | import javax.swing.Action; | |
51 | import javax.swing.Icon; | |
52 | import javax.swing.event.CaretEvent; | |
53 | import javax.swing.event.CaretListener; | |
54 | import javax.swing.text.JTextComponent; | |
55 | ||
56 | import org.argouml.application.helpers.ResourceLoaderWrapper; | |
57 | import org.argouml.i18n.Translator; | |
58 | import org.tigris.gef.base.CutAction; | |
59 | import org.tigris.gef.base.Globals; | |
60 | ||
61 | /** | |
62 | * The Cut Action. | |
63 | */ | |
64 | public class ActionCut extends AbstractAction implements CaretListener { | |
65 | ||
66 | 900 | private static ActionCut instance = new ActionCut(); |
67 | ||
68 | private static final String LOCALIZE_KEY = "action.cut"; | |
69 | ||
70 | //////////////////////////////////////////////////////////////// | |
71 | // constructors | |
72 | ||
73 | /** | |
74 | * Constructor. | |
75 | */ | |
76 | public ActionCut() { | |
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 ActionCut 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 | if (removeFromDiagramAllowed()) { |
102 | 0 | CutAction cmd = |
103 | new CutAction(Translator.localize("action.cut")); | |
104 | 0 | cmd.actionPerformed(ae); |
105 | 0 | } |
106 | } else { | |
107 | 0 | textSource.cut(); |
108 | } | |
109 | 0 | if (isSystemClipBoardEmpty() |
110 | && Globals.clipBoard == null | |
111 | || Globals.clipBoard.isEmpty()) { | |
112 | 0 | ActionPaste.getInstance().setEnabled(false); |
113 | } else { | |
114 | 0 | ActionPaste.getInstance().setEnabled(true); |
115 | } | |
116 | 0 | } |
117 | ||
118 | /** | |
119 | * Disable cutting figs from a diagram to prevent issue 3480. | |
120 | * See also ActionPaste, which is also disabled for similar reasons. | |
121 | * | |
122 | * @return true if cut is allowed for the selected items | |
123 | */ | |
124 | private boolean removeFromDiagramAllowed() { | |
125 | 0 | return false; |
126 | } | |
127 | ||
128 | /* | |
129 | * @see javax.swing.event.CaretListener#caretUpdate(javax.swing.event.CaretEvent) | |
130 | */ | |
131 | public void caretUpdate(CaretEvent e) { | |
132 | 0 | if (e.getMark() != e.getDot()) { // there is a selection |
133 | 0 | setEnabled(true); |
134 | 0 | textSource = (JTextComponent) e.getSource(); |
135 | } else { | |
136 | 0 | Collection figSelection = |
137 | Globals.curEditor().getSelectionManager().selections(); | |
138 | 0 | if (figSelection == null || figSelection.isEmpty()) { |
139 | 0 | setEnabled(false); |
140 | } else { | |
141 | 0 | setEnabled(true); |
142 | } | |
143 | 0 | textSource = null; |
144 | } | |
145 | ||
146 | 0 | } |
147 | ||
148 | private boolean isSystemClipBoardEmpty() { | |
149 | // if there is a selection on the clipboard | |
150 | 0 | boolean hasContents = false; |
151 | 0 | Transferable content = |
152 | Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); | |
153 | 0 | DataFlavor[] flavors = content.getTransferDataFlavors(); |
154 | try { | |
155 | 0 | for (int i = 0; i < flavors.length; i++) { |
156 | 0 | if (content.getTransferData(flavors[i]) != null) { |
157 | 0 | hasContents = true; |
158 | 0 | break; |
159 | } | |
160 | } | |
161 | 0 | } catch (UnsupportedFlavorException ignorable) { |
162 | 0 | } catch (IOException ignorable) { |
163 | 0 | } |
164 | 0 | return !hasContents; |
165 | } | |
166 | ||
167 | } /* end class ActionCut */ |