KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > navigator > 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.views.navigator;
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.internal.views.navigator.ResourceNavigatorMessages;
32 import org.eclipse.ui.part.ResourceTransfer;
33
34 /**
35  * Standard action for copying the currently selected resources to the clipboard.
36  * <p>
37  * This class may be instantiated; it is not intended to be subclassed.
38  * </p>
39  *
40  * @since 2.0
41  */

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

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

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

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

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

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

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

91     public CopyAction(Shell shell, Clipboard clipboard, PasteAction pasteAction) {
92         this(shell, clipboard);
93         this.pasteAction = pasteAction;
94     }
95
96    
97     /* (non-Javadoc)
98      * @see org.eclipse.jface.action.Action#run()
99      */

100     public void run() {
101          /**
102          * The <code>CopyAction</code> implementation of this method defined
103          * on <code>IAction</code> copies the selected resources to the
104          * clipboard.
105          */

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

150     private void setClipboard(IResource[] resources, String JavaDoc[] fileNames,
151             String JavaDoc names) {
152         try {
153             // set the clipboard contents
154
if (fileNames.length > 0) {
155                 clipboard.setContents(new Object JavaDoc[] { resources, fileNames,
156                         names },
157                         new Transfer[] { ResourceTransfer.getInstance(),
158                                 FileTransfer.getInstance(),
159                                 TextTransfer.getInstance() });
160             } else {
161                 clipboard.setContents(new Object JavaDoc[] { resources, names },
162                         new Transfer[] { ResourceTransfer.getInstance(),
163                                 TextTransfer.getInstance() });
164             }
165         } catch (SWTError e) {
166             if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) {
167                 throw e;
168             }
169             if (MessageDialog
170                     .openQuestion(
171                             shell,
172                             ResourceNavigatorMessages.CopyToClipboardProblemDialog_title, ResourceNavigatorMessages.CopyToClipboardProblemDialog_message)) {
173                 setClipboard(resources, fileNames, names);
174             }
175         }
176     }
177
178    
179     /* (non-Javadoc)
180      * @see org.eclipse.ui.actions.BaseSelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
181      */

182     protected boolean updateSelection(IStructuredSelection selection) {
183         
184          /**
185          * The <code>CopyAction</code> implementation of this
186          * <code>SelectionListenerAction</code> method enables this action if
187          * one or more resources of compatible types are selected.
188          */

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