KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > CVSResourceTransfer


1 /*******************************************************************************
2  * Copyright (c) 2004, 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
12 package org.eclipse.team.internal.ccvs.ui;
13
14 import java.io.*;
15
16 import org.eclipse.swt.dnd.*;
17 import org.eclipse.team.internal.ccvs.core.ICVSRemoteFile;
18 import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
19 import org.eclipse.team.internal.ccvs.core.resources.RemoteFile;
20 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories;
21
22
23 /**
24  *
25  * @author Eugene Kuleshov
26  */

27 public final class CVSResourceTransfer extends ByteArrayTransfer {
28     
29     public static final String JavaDoc TYPE_NAME = "CVS-resource-transfer-format"; //$NON-NLS-1$
30

31     public static int TYPE = registerType(TYPE_NAME);
32
33     private static CVSResourceTransfer instance = new CVSResourceTransfer();
34
35     
36     private CVSResourceTransfer() {
37     }
38
39     public static CVSResourceTransfer getInstance() {
40         return instance;
41     }
42
43     
44     protected int[] getTypeIds() {
45         return new int[] { TYPE };
46     }
47
48     protected String JavaDoc[] getTypeNames() {
49         return new String JavaDoc[] { TYPE_NAME };
50     }
51
52     
53     /*
54      * (non-Javadoc)
55      *
56      * @see org.eclipse.swt.dnd.Transfer#javaToNative(java.lang.Object,org.eclipse.swt.dnd.TransferData)
57      */

58     public void javaToNative(Object JavaDoc object, TransferData transferData) {
59         if (!isSupportedType(transferData)) {
60             DND.error(DND.ERROR_INVALID_DATA);
61         }
62
63         final byte[] bytes = toByteArray((ICVSRemoteFile) object);
64         if (bytes != null) {
65             super.javaToNative(bytes, transferData);
66         }
67     }
68
69     /*
70      * (non-Javadoc)
71      *
72      * @see org.eclipse.swt.dnd.Transfer#nativeToJava(org.eclipse.swt.dnd.TransferData)
73      */

74     protected Object JavaDoc nativeToJava(TransferData transferData) {
75         byte[] bytes = (byte[]) super.nativeToJava(transferData);
76         return fromByteArray(bytes);
77     }
78
79     
80     public Object JavaDoc fromByteArray(byte[] bytes) {
81         final DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
82
83         try {
84             String JavaDoc location = in.readUTF();
85             String JavaDoc filePath = in.readUTF();
86             String JavaDoc fileRevision = in.readUTF();
87             
88             ICVSRepositoryLocation repositoryLocation = KnownRepositories.getInstance().getRepository(location);
89             RemoteFile file = RemoteFile.create( filePath, repositoryLocation);
90             file.setRevision(fileRevision);
91             file.setReadOnly(true);
92             return file;
93         } catch (Exception JavaDoc ex) {
94             return null;
95         }
96     }
97
98     public byte[] toByteArray(ICVSRemoteFile file) {
99         ByteArrayOutputStream bos = new ByteArrayOutputStream();
100         DataOutputStream dos = new DataOutputStream(bos);
101         try {
102             dos.writeUTF(file.getRepository().getLocation(false));
103             dos.writeUTF(file.getRepositoryRelativePath());
104             dos.writeUTF(file.getRevision());
105             return bos.toByteArray();
106         } catch (Exception JavaDoc ex) {
107             // ex.printStackTrace();
108
return null;
109         }
110     }
111
112 }
113
Popular Tags