KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > swt > lib > EntryTransfer


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy, Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26 package org.objectweb.util.explorer.swt.lib;
27
28 import java.io.ByteArrayInputStream JavaDoc;
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.ObjectInputStream JavaDoc;
32 import java.io.ObjectOutputStream JavaDoc;
33
34 import org.eclipse.swt.dnd.ByteArrayTransfer;
35 import org.eclipse.swt.dnd.TransferData;
36 import org.objectweb.util.explorer.api.Entry;
37 import org.objectweb.util.trace.TraceSystem;
38
39 /**
40  *
41  *
42  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>,
43  * <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>.
44  *
45  * @version 0.1
46  */

47 public class EntryTransfer
48      extends ByteArrayTransfer
49 {
50
51     //==================================================================
52
//
53
// Internal States.
54
//
55
// ==================================================================
56

57     private static final String JavaDoc MYTYPENAME = "EntryType";
58     private static final int MYTYPEID = registerType(MYTYPENAME);
59     private static EntryTransfer instance_ = new EntryTransfer();
60     
61     // ==================================================================
62
//
63
// Constructors.
64
//
65
// ==================================================================
66

67     // ==================================================================
68
//
69
// Internal methods.
70
//
71
// ==================================================================
72

73     /* (non-Javadoc)
74      * @see org.eclipse.swt.dnd.Transfer#getTypeIds()
75      */

76     protected int[] getTypeIds() {
77         return new int[] {MYTYPEID};
78     }
79
80     /* (non-Javadoc)
81      * @see org.eclipse.swt.dnd.Transfer#getTypeNames()
82      */

83     protected String JavaDoc[] getTypeNames() {
84         return new String JavaDoc[]{MYTYPENAME};
85     }
86     
87     // ==================================================================
88
//
89
// Public methods for ByteArrayTransfer abstract class.
90
//
91
// ==================================================================
92

93     public static EntryTransfer getInstance () {
94         return instance_;
95     }
96     
97     public void javaToNative (Object JavaDoc object, TransferData transferData) {
98         if (object == null || !(object instanceof Entry)) return;
99         if (isSupportedType(transferData)) {
100             Entry entry = (Entry) object;
101             try {
102                 ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
103                 ObjectOutputStream JavaDoc writeOut = new ObjectOutputStream JavaDoc(out);
104                 //writeOut.writeObject(entry);
105
writeOut.writeObject(entry.getName());
106                 byte[] buffer = out.toByteArray();
107                 writeOut.close();
108
109                 super.javaToNative(buffer, transferData);
110                 
111             } catch (IOException JavaDoc e) {
112                 TraceSystem.get("explorer").debug("[" + getClass().getName() + "] IOException: " + e.getMessage());
113                 e.printStackTrace();
114             }
115         }
116     }
117
118     public Object JavaDoc nativeToJava(TransferData transferData){
119         if (isSupportedType(transferData)) {
120             
121             byte[] buffer = (byte[])super.nativeToJava(transferData);
122             if (buffer == null) return null;
123             
124             Entry[] myData = new Entry[0];
125             try {
126                 ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(buffer);
127                 ObjectInputStream JavaDoc readIn = new ObjectInputStream JavaDoc(in);
128                 String JavaDoc name = (String JavaDoc) readIn.readObject();
129                 readIn.close();
130             } catch (IOException JavaDoc ex) {
131                 return null;
132             } catch (ClassNotFoundException JavaDoc e) {
133                 e.printStackTrace();
134                 return null;
135             }
136             return myData;
137         }
138
139         return null;
140     }
141 }
142
Popular Tags