KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > binarytree > TreeDisplay


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.binarytree;
32
33 public class TreeDisplay {
34
35   private TreeApplet applet;
36   private Tree tree;
37
38
39   public TreeDisplay() {
40   }
41
42
43     public TreeDisplay(TreeApplet applet) {
44       this.applet = applet;
45       tree = null;
46     }
47     
48     
49     public void displayMessage(String JavaDoc s) {
50     applet.receiveMessage(s);
51     }
52     
53     public void displayMessage(String JavaDoc s, java.awt.Color JavaDoc c) {
54       applet.receiveMessage(s, c);
55     }
56
57     
58     // Create a tree with random nodes
59
public void createTree(int size, boolean AC) {
60     // Reset the current tree to create another
61
deleteTree();
62     String JavaDoc[] keys = randomKeys(size);
63     addRandom(keys, AC);
64     }
65
66     
67     public void add(String JavaDoc key, String JavaDoc value, boolean AC) {
68     if (tree == null) {
69         try {
70         tree = (Tree)org.objectweb.proactive.ProActive.newActive(Tree.class.getName(), new Object JavaDoc[]{key, value, org.objectweb.proactive.ProActive.getStubOnThis()});
71         applet.receiveMessage("Creating initial tree",
72                       new java.awt.Color JavaDoc(0, 150, 0));
73         }
74         catch (Exception JavaDoc e) {
75         applet.receiveMessage("Waiting, program is lauching...",
76                       java.awt.Color.red);
77         e.printStackTrace();
78         }
79     }
80     else {
81         tree.insert(key, value, AC);
82     }
83     applet.displayTree();
84     }
85
86
87     public void deleteTree() {
88     if (tree != null) {
89         tree.delete();
90         tree = null;
91         applet.receiveMessage("Current tree erased!", java.awt.Color.red);
92     }
93     }
94
95     
96     public Tree getTree() {
97     return tree;
98     }
99     
100     
101     public ObjectWrapper search(String JavaDoc key) {
102     if (tree == null)
103         return null;
104     return tree.search(key);
105     }
106
107
108     // Return a specified number of random keys
109
public java.util.ArrayList JavaDoc getRandomKeys(int number) {
110     java.util.ArrayList JavaDoc result = new java.util.ArrayList JavaDoc();
111     java.util.ArrayList JavaDoc keys = getKeys();
112     if (keys.size() < number)
113         number = keys.size();
114     while (number > 0) {
115         java.util.Random JavaDoc random = new java.util.Random JavaDoc();
116         int index = random.nextInt(keys.size());
117         result.add(keys.get(index));
118         keys.remove(index);
119         number--;
120     }
121     return result;
122     }
123
124
125     // Return the key list of the tree.
126
public java.util.ArrayList JavaDoc getKeys() {
127     java.util.ArrayList JavaDoc keys = new java.util.ArrayList JavaDoc();
128     if (tree != null) {
129         keys = tree.getKeys();
130     }
131     return keys;
132     }
133
134
135     // Change Automatic Continuation state
136
public void enableAC() {
137     if (tree == null)
138         return;
139     tree.enableAC();
140     }
141
142
143     public void disableAC() {
144     if (tree == null)
145         return;
146     tree.disableAC();
147     }
148
149     /********************************************
150      * Methods to create a random tree *
151      ********************************************/

152
153   
154     private String JavaDoc[] randomKeys(int number) {
155     java.util.TreeSet JavaDoc keys = new java.util.TreeSet JavaDoc();
156     for (int i=0; i<number; i++) {
157         while (!keys.add(randomWord(4))) {
158         }
159     }
160     int i = 0;
161     String JavaDoc[] result = new String JavaDoc[number];
162     java.util.Iterator JavaDoc it = keys.iterator();
163     while (it.hasNext()) {
164         result[i++] = (String JavaDoc)it.next();
165     }
166     return result;
167     }
168     
169
170     private void addRandom(String JavaDoc[] t, boolean AC) {
171     if (t.length == 0)
172         return;
173     else if (t.length == 1)
174         add(t[0], randomWord(4), AC);
175     else if (t.length == 2) {
176         add(t[0], randomWord(4), AC);
177         add(t[1], randomWord(4), AC);
178     }
179     else {
180         add(t[(int)t.length/2], randomWord(4), AC);
181         addRandom(firstHalf(t), AC);
182         addRandom(secondHalf(t), AC);
183     }
184     }
185
186
187     private String JavaDoc randomWord(int size) {
188     java.util.Random JavaDoc random = new java.util.Random JavaDoc();
189     String JavaDoc result = "";
190     for (int i=0; i < size; i++) {
191         int code = random.nextInt(35);
192         char[] c = {Character.forDigit(code,35)};
193         result = result.concat(String.copyValueOf(c));
194     }
195     return result;
196     }
197
198
199     private String JavaDoc[] firstHalf(String JavaDoc[] t) {
200     String JavaDoc[] firstHalf = new String JavaDoc[(int)(t.length/2)];
201     for (int i=0; i<(t.length/2); i++) {
202         firstHalf[i] = t[i];
203     }
204     return firstHalf;
205     }
206     
207     private String JavaDoc[] secondHalf(String JavaDoc[] t) {
208     int size = t.length - ((int)(t.length/2) + 1);
209     String JavaDoc[] secondHalf = new String JavaDoc[size];
210     int j = 0;
211     for (int i=((int)(t.length/2) + 1); i<t.length; i++) {
212         secondHalf[j++] = t[i];
213     }
214     return secondHalf;
215     }
216 }
217
Popular Tags