KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > scenario > util > isac > gui > tree > dnd > ScenarioTreeTransfer


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2004 France Telecom R&D
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * CLIF
20 *
21 * Contact: clif@objectweb.org
22 */

23 package org.objectweb.clif.scenario.util.isac.gui.tree.dnd;
24
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.DataInputStream JavaDoc;
28 import java.io.DataOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import org.apache.log4j.Category;
33 import org.eclipse.swt.dnd.ByteArrayTransfer;
34 import org.eclipse.swt.dnd.TransferData;
35 import org.objectweb.clif.scenario.util.isac.util.tree.ScenarioNode;
36
37 /**
38  * This class is used to serialize the scenario node, and to do some transfert
39  * when drag & drop operations are used
40  *
41  * @author JC Meillaud
42  * @author A Peyrard
43  */

44 /**
45  * Class for serializing behaviorfulltree to/from a byte array
46  * @author JC Meillaud & A Peyrard
47  */

48 public class ScenarioTreeTransfer extends ByteArrayTransfer {
49     static Category cat = Category.getInstance(ScenarioTreeTransfer.class.getName());
50     private static ScenarioTreeTransfer instance = new ScenarioTreeTransfer();
51     private static final String JavaDoc TYPE_NAME = "ScenarioTree-transfer-format";
52     private static final int TYPEID = registerType(TYPE_NAME);
53
54     /**
55      * Returns the singleton bft transfer instance.
56      */

57     public static ScenarioTreeTransfer getInstance() {
58         cat.debug("-> getInstance") ;
59         return instance;
60     }
61
62     /**
63      * Avoid explicit instantiation
64      */

65     private ScenarioTreeTransfer() {
66         cat.debug("-> constructor") ;
67     }
68
69     protected ScenarioNode fromByteArray(byte[] bytes) {
70         cat.debug("-> fromByteArray") ;
71         if (bytes == null)
72             return null ;
73         DataInputStream JavaDoc in =
74             new DataInputStream JavaDoc(new ByteArrayInputStream JavaDoc(bytes));
75
76         try {
77             ScenarioNode tree = readScenarioTree(in);
78             if (tree != null)
79                 return tree;
80         } catch (IOException JavaDoc e) {
81             // do nothing
82
}
83         return null;
84     }
85
86     /**
87      * Method declared on Transfer.
88      * @return A table containing the value of accepted types
89      */

90     protected int[] getTypeIds() {
91         cat.debug("-> getTypeIds") ;
92         return new int[] { TYPEID };
93     }
94
95     /**
96      * Method declared on Transfer.
97      * @return A table containing the name of accepted types
98      */

99     protected String JavaDoc[] getTypeNames() {
100         cat.debug("-> getTypeNames") ;
101         return new String JavaDoc[] { TYPE_NAME };
102     }
103
104     /**
105      * Method declared on Transfer.
106      * @param object
107      * @param transferData
108      */

109     protected void javaToNative(Object JavaDoc object, TransferData transferData) {
110         cat.debug("-> javaToNative") ;
111         byte[] bytes = toByteArray((ScenarioNode) object);
112         if (bytes != null)
113             super.javaToNative(bytes, transferData);
114     }
115
116     /**
117      * Method declared on Transfer.
118      */

119     protected Object JavaDoc nativeToJava(TransferData transferData) {
120         cat.debug("-> nativeToJava") ;
121         byte[] bytes = (byte[]) super.nativeToJava(transferData);
122         return fromByteArray(bytes);
123     }
124
125     /**
126      * Reads and returns a single BehaviorFullTree from the given stream.
127      */

128     private ScenarioNode readScenarioTree(DataInputStream JavaDoc dataIn)
129         throws IOException JavaDoc {
130         cat.debug("-> readScenarioTree") ;
131         /**
132          * ScenarioNode trees serialization format is as follows:
133          * (String) key of the node
134          * (int) number of childs
135          * (ScenarioNode) child 1
136          * ... repeat for each child
137          */

138         String JavaDoc key = dataIn.readUTF();
139         int n = dataIn.readInt();
140         ScenarioNode newParent = new ScenarioNode(key);
141         for (int i = 0; i < n; i++) {
142             newParent.addChild(readScenarioTree(dataIn));
143         }
144         return newParent;
145     }
146
147     protected byte[] toByteArray(ScenarioNode tree) {
148         cat.debug("-> toByteArray") ;
149         /**
150          * Transfer data is a ScenarioTree. Serialized version is:
151          * (ScenarioTree) tree 1
152          * see writeScenarioTree for the ScenarioNodes tree format.
153          */

154         ByteArrayOutputStream JavaDoc byteOut = new ByteArrayOutputStream JavaDoc();
155         DataOutputStream JavaDoc out = new DataOutputStream JavaDoc(byteOut);
156
157         byte[] bytes = null;
158
159         try {
160             writeScenarioTree(tree, out);
161             out.close();
162             bytes = byteOut.toByteArray();
163         } catch (IOException JavaDoc e) {
164             //when in doubt send nothing
165
}
166         return bytes;
167     }
168
169     /**
170      * Writes the given ScenarioNode to the stream.
171      */

172     private void writeScenarioTree(ScenarioNode tree, DataOutputStream JavaDoc dataOut)
173         throws IOException JavaDoc {
174         cat.debug("-> writeScenarioTree") ;
175         /**
176          * ScenarioNodes tree serialization format is as follows:
177          * (String) key of the tree
178          * (int) number of childs
179          * (ScenarioNode) child 1
180          * ... repeat for each child
181          */

182         dataOut.writeUTF(tree.getKey());
183         Vector JavaDoc children = tree.getChildren();
184         dataOut.writeInt(children.size());
185         for (int i = 0; i < children.size(); i++) {
186             writeScenarioTree((ScenarioNode) children.elementAt(i), dataOut);
187         }
188     }
189 }
190
191     
Popular Tags