1 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 s) { 50 applet.receiveMessage(s); 51 } 52 53 public void displayMessage(String s, java.awt.Color c) { 54 applet.receiveMessage(s, c); 55 } 56 57 58 public void createTree(int size, boolean AC) { 60 deleteTree(); 62 String [] keys = randomKeys(size); 63 addRandom(keys, AC); 64 } 65 66 67 public void add(String key, String value, boolean AC) { 68 if (tree == null) { 69 try { 70 tree = (Tree)org.objectweb.proactive.ProActive.newActive(Tree.class.getName(), new Object []{key, value, org.objectweb.proactive.ProActive.getStubOnThis()}); 71 applet.receiveMessage("Creating initial tree", 72 new java.awt.Color (0, 150, 0)); 73 } 74 catch (Exception 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 key) { 102 if (tree == null) 103 return null; 104 return tree.search(key); 105 } 106 107 108 public java.util.ArrayList getRandomKeys(int number) { 110 java.util.ArrayList result = new java.util.ArrayList (); 111 java.util.ArrayList keys = getKeys(); 112 if (keys.size() < number) 113 number = keys.size(); 114 while (number > 0) { 115 java.util.Random random = new java.util.Random (); 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 public java.util.ArrayList getKeys() { 127 java.util.ArrayList keys = new java.util.ArrayList (); 128 if (tree != null) { 129 keys = tree.getKeys(); 130 } 131 return keys; 132 } 133 134 135 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 152 153 154 private String [] randomKeys(int number) { 155 java.util.TreeSet keys = new java.util.TreeSet (); 156 for (int i=0; i<number; i++) { 157 while (!keys.add(randomWord(4))) { 158 } 159 } 160 int i = 0; 161 String [] result = new String [number]; 162 java.util.Iterator it = keys.iterator(); 163 while (it.hasNext()) { 164 result[i++] = (String )it.next(); 165 } 166 return result; 167 } 168 169 170 private void addRandom(String [] 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 randomWord(int size) { 188 java.util.Random random = new java.util.Random (); 189 String 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 [] firstHalf(String [] t) { 200 String [] firstHalf = new String [(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 [] secondHalf(String [] t) { 208 int size = t.length - ((int)(t.length/2) + 1); 209 String [] secondHalf = new String [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
|