KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > resources > actions > CopyAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.navigator.resources.actions;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.swt.SWTError;
23 import org.eclipse.swt.dnd.Clipboard;
24 import org.eclipse.swt.dnd.DND;
25 import org.eclipse.swt.dnd.FileTransfer;
26 import org.eclipse.swt.dnd.TextTransfer;
27 import org.eclipse.swt.dnd.Transfer;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.actions.SelectionListenerAction;
31 import org.eclipse.ui.part.ResourceTransfer;
32
33 /**
34  * Standard action for copying the currently selected resources to the clipboard.
35  * <p>
36  * This class may be instantiated; it is not intended to be subclassed.
37  * </p>
38  *
39  * @since 2.0
40  */

41 /*package*/class CopyAction extends SelectionListenerAction {
42
43     /**
44      * The id of this action.
45      */

46     public static final String JavaDoc ID = PlatformUI.PLUGIN_ID + ".CopyAction"; //$NON-NLS-1$
47

48     /**
49      * The shell in which to show any dialogs.
50      */

51     private Shell shell;
52
53     /**
54      * System clipboard
55      */

56     private Clipboard clipboard;
57
58     /**
59      * Associated paste action. May be <code>null</code>
60      */

61     private PasteAction pasteAction;
62
63     /**
64      * Creates a new action.
65      *
66      * @param shell the shell for any dialogs
67      * @param clipboard a platform clipboard
68      */

69     public CopyAction(Shell shell, Clipboard clipboard) {
70         super("Copy"); // TODO ResourceNavigatorMessages.CopyAction_title); //$NON-NLS-1$
71
Assert.isNotNull(shell);
72         Assert.isNotNull(clipboard);
73         this.shell = shell;
74         this.clipboard = clipboard;
75         setToolTipText("Copy Tooltip"); // TODO ResourceNavigatorMessages.CopyAction_toolTip); //$NON-NLS-1$
76
setId(CopyAction.ID);
77         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, "CopyHelpId"); //$NON-NLS-1$
78
// TODO INavigatorHelpContextIds.COPY_ACTION);
79
}
80
81     /**
82      * Creates a new action.
83      *
84      * @param shell the shell for any dialogs
85      * @param clipboard a platform clipboard
86      * @param pasteAction a paste action
87      *
88      * @since 2.0
89      */

90     public CopyAction(Shell shell, Clipboard clipboard, PasteAction pasteAction) {
91         this(shell, clipboard);
92         this.pasteAction = pasteAction;
93     }
94
95     /**
96      * The <code>CopyAction</code> implementation of this method defined
97      * on <code>IAction</code> copies the selected resources to the
98      * clipboard.
99      */

100     public void run() {
101         List JavaDoc selectedResources = getSelectedResources();
102         IResource[] resources = (IResource[]) selectedResources
103                 .toArray(new IResource[selectedResources.size()]);
104
105         // Get the file names and a string representation
106
final int length = resources.length;
107         int actualLength = 0;
108         String JavaDoc[] fileNames = new String JavaDoc[length];
109         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
110         for (int i = 0; i < length; i++) {
111             IPath location = resources[i].getLocation();
112             // location may be null. See bug 29491.
113
if (location != null) {
114                 fileNames[actualLength++] = location.toOSString();
115             }
116             if (i > 0) {
117                 buf.append("\n"); //$NON-NLS-1$
118
}
119             buf.append(resources[i].getName());
120         }
121         // was one or more of the locations null?
122
if (actualLength < length) {
123             String JavaDoc[] tempFileNames = fileNames;
124             fileNames = new String JavaDoc[actualLength];
125             for (int i = 0; i < actualLength; i++) {
126                 fileNames[i] = tempFileNames[i];
127             }
128         }
129         setClipboard(resources, fileNames, buf.toString());
130
131         // update the enablement of the paste action
132
// workaround since the clipboard does not suppot callbacks
133
if (pasteAction != null && pasteAction.getStructuredSelection() != null) {
134             pasteAction.selectionChanged(pasteAction.getStructuredSelection());
135         }
136     }
137
138     /**
139      * Set the clipboard contents. Prompt to retry if clipboard is busy.
140      *
141      * @param resources the resources to copy to the clipboard
142      * @param fileNames file names of the resources to copy to the clipboard
143      * @param names string representation of all names
144      */

145     private void setClipboard(IResource[] resources, String JavaDoc[] fileNames,
146             String JavaDoc names) {
147         try {
148             // set the clipboard contents
149
if (fileNames.length > 0) {
150                 clipboard.setContents(new Object JavaDoc[] { resources, fileNames,
151                         names },
152                         new Transfer[] { ResourceTransfer.getInstance(),
153                                 FileTransfer.getInstance(),
154                                 TextTransfer.getInstance() });
155             } else {
156                 clipboard.setContents(new Object JavaDoc[] { resources, names },
157                         new Transfer[] { ResourceTransfer.getInstance(),
158                                 TextTransfer.getInstance() });
159             }
160         } catch (SWTError e) {
161             if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) {
162                 throw e;
163             }
164             if (MessageDialog
165                     .openQuestion(
166                             shell,
167                             "Problem with copy title", // TODO ResourceNavigatorMessages.CopyToClipboardProblemDialog_title, //$NON-NLS-1$
168
"Problem with copy.")) { //$NON-NLS-1$
169
setClipboard(resources, fileNames, names);
170             }
171         }
172     }
173
174     /**
175      * The <code>CopyAction</code> implementation of this
176      * <code>SelectionListenerAction</code> method enables this action if
177      * one or more resources of compatible types are selected.
178      */

179     protected boolean updateSelection(IStructuredSelection selection) {
180         if (!super.updateSelection(selection)) {
181             return false;
182         }
183
184         if (getSelectedNonResources().size() > 0) {
185             return false;
186         }
187
188         List JavaDoc selectedResources = getSelectedResources();
189         if (selectedResources.size() == 0) {
190             return false;
191         }
192
193         boolean projSelected = selectionIsOfType(IResource.PROJECT);
194         boolean fileFoldersSelected = selectionIsOfType(IResource.FILE
195                 | IResource.FOLDER);
196         if (!projSelected && !fileFoldersSelected) {
197             return false;
198         }
199
200         // selection must be homogeneous
201
if (projSelected && fileFoldersSelected) {
202             return false;
203         }
204
205         // must have a common parent
206
IContainer firstParent = ((IResource) selectedResources.get(0))
207                 .getParent();
208         if (firstParent == null) {
209             return false;
210         }
211
212         Iterator JavaDoc resourcesEnum = selectedResources.iterator();
213         while (resourcesEnum.hasNext()) {
214             IResource currentResource = (IResource) resourcesEnum.next();
215             if (!currentResource.getParent().equals(firstParent)) {
216                 return false;
217             }
218             // resource location must exist
219
if (currentResource.getLocation() == null) {
220                 return false;
221             }
222         }
223
224         return true;
225     }
226
227 }
228
229
Popular Tags