KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > services > ActionPasteType


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.windows.services;
21
22 import org.openide.filesystems.FileObject;
23 import org.openide.filesystems.FileStateInvalidException;
24 import org.openide.filesystems.Repository;
25 import org.openide.loaders.DataFolder;
26 import org.openide.loaders.DataObject;
27 import org.openide.loaders.LoaderTransfer;
28 import org.openide.util.datatransfer.PasteType;
29 import org.openide.cookies.InstanceCookie;
30
31 import javax.swing.*;
32 import java.awt.datatransfer.Transferable JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.util.*;
35
36 /**
37  * PasteType for Action instances. PasteType impl. that uses {@link org.openide.loaders.DataShadow} for copying and
38  * {@link DataObject#move(org.openide.loaders.DataFolder)} .
39  *
40  * @author Radek Matous
41  */

42 final class ActionPasteType {
43 /**
44  *
45  */

46     static PasteType getPasteType(final DataFolder targetFolder, final Transferable JavaDoc transfer) {
47         final FileObject folder = targetFolder.getPrimaryFile();
48         PasteType retVal = null;
49
50         try {
51             /*Copy/Cut/Paste is allowed just on SystemFileSystem*/
52             if (folder.getFileSystem() == Repository.getDefault().getDefaultFileSystem()) {
53                 final int[] pasteOperations = new int[]{LoaderTransfer.CLIPBOARD_COPY, LoaderTransfer.CLIPBOARD_CUT};
54
55                 for (int i = 0; i < pasteOperations.length; i++) {
56                     final DataObject[] dataObjects = LoaderTransfer.getDataObjects(transfer, pasteOperations[i]);
57                     if (dataObjects != null) {
58                         if (canBePasted(dataObjects, targetFolder, pasteOperations[i])) {
59                             retVal = new PasteTypeImpl(Arrays.asList(dataObjects), targetFolder, pasteOperations[i]);
60                             break;
61                         }
62                     }
63                 }
64             }
65         } catch (FileStateInvalidException e) {/*just null is returned if folder.getFileSystem fires ISE*/}
66
67         return retVal;
68     }
69
70     private static boolean canBePasted(final DataObject[] dataObjects, final DataFolder targetFolder, final int operation) throws FileStateInvalidException {
71         final Set<DataObject> pasteableDataObjects = new HashSet<DataObject> ();
72         final FileObject folder = targetFolder.getPrimaryFile();
73         
74         DataObject[] folderChildren = targetFolder.getChildren();
75         
76         for (int j = 0; j < dataObjects.length; j++) {
77             final DataObject dataObject = dataObjects[j];
78             final FileObject fo = dataObject.getPrimaryFile ();
79             
80             if (!isAction(dataObject) || fo.getFileSystem() != Repository.getDefault().getDefaultFileSystem()) {
81                 break;
82             }
83
84             final boolean isCopyPaste = operation == LoaderTransfer.CLIPBOARD_COPY && dataObject.isCopyAllowed();
85             final boolean isCutPaste = operation == LoaderTransfer.CLIPBOARD_CUT && dataObject.isMoveAllowed() &&
86                     !(fo.getParent() == folder);//prevents from cutting into the same folder where it was
87

88             if (isCopyPaste || isCutPaste) {
89                 
90                 boolean isDuplicate = false;
91                 for( int i=0; i<folderChildren.length; i++ ) {
92                     if( 0 == folderChildren[i].getName().compareTo( dataObject.getName() ) ) {
93                         isDuplicate = true;
94                         break;
95                     }
96                 }
97                 if( !isDuplicate )
98                     pasteableDataObjects.add(dataObject);
99             }
100         }
101         return (pasteableDataObjects.size() == dataObjects.length);
102     }
103
104     private static boolean isAction(DataObject dataObject) {
105         boolean retVal = false;
106         InstanceCookie.Of ic = (InstanceCookie.Of)dataObject.getCookie(InstanceCookie.Of.class);
107         if (ic != null && ic.instanceOf(Action.class)) {
108             retVal = true;
109         }
110         return retVal;
111     }
112
113     private final static class PasteTypeImpl extends PasteType {
114         final private DataFolder targetFolder;
115         final private Collection/*<DataObject>*/ sourceDataObjects;
116         final private int pasteOperation;
117
118     
119         private PasteTypeImpl(final Collection/*<DataObject>*/ sourceDataObjects, final DataFolder targetFolder, final int pasteOperation) {
120             this.targetFolder = targetFolder;
121             this.sourceDataObjects = sourceDataObjects;
122             this.pasteOperation = pasteOperation;
123         }
124
125         public Transferable JavaDoc paste() throws IOException JavaDoc {
126             if (targetFolder != null) {
127                 for (Iterator iterator = sourceDataObjects.iterator(); iterator.hasNext();) {
128                     DataObject dataObject = (DataObject) iterator.next();
129                     boolean isValid = dataObject != null && dataObject.isValid();
130                     
131                     if (isValid && pasteOperation == LoaderTransfer.CLIPBOARD_COPY) {
132                         dataObject.createShadow(targetFolder);
133                     }
134                     
135                     if (isValid && pasteOperation == LoaderTransfer.CLIPBOARD_CUT) {
136                         dataObject.move(targetFolder);
137                     }
138                                         
139                 }
140             }
141             return null;
142         }
143     }
144 }
Popular Tags