1 package rero.client.functions; 2 3 import sleep.engine.*; 4 import sleep.runtime.*; 5 import sleep.interfaces.*; 6 import sleep.bridges.BridgeUtilities; 7 8 import rero.util.*; 9 import rero.client.*; 10 import rero.config.*; 11 import java.util.*; 12 import java.text.*; 13 14 import text.AttributedString; 15 16 import java.awt.*; 18 import text.TextSource; 20 import java.io.*; 21 import java.net.*; 22 23 import javax.swing.*; 24 25 public class UtilOperators extends Feature implements Loadable 26 { 27 public void init() 28 { 29 getCapabilities().getScriptCore().addBridge(this); 30 } 31 32 public boolean scriptLoaded(ScriptInstance script) 33 { 34 script.getScriptEnvironment().getEnvironment().put("&formatBytes", new formatBytes()); 35 36 script.getScriptEnvironment().getEnvironment().put("&duration", new duration()); 37 script.getScriptEnvironment().getEnvironment().put("&formatTime", new formatTime()); 38 script.getScriptEnvironment().getEnvironment().put("&formatTime2", new formatTime2()); 39 script.getScriptEnvironment().getEnvironment().put("&formatTime3", new formatTime3()); 40 script.getScriptEnvironment().getEnvironment().put("&formatDecimal", new formatDecimal()); 41 script.getScriptEnvironment().getEnvironment().put("&longip", new longip()); 42 43 script.getScriptEnvironment().getEnvironment().put("&ctime", new ctime()); 45 script.getScriptEnvironment().getEnvironment().put("&timeDateStamp", new timeDateStamp()); 46 script.getScriptEnvironment().getEnvironment().put("&timeStamp", new timeStamp()); 47 48 script.getScriptEnvironment().getEnvironment().put("&showInputDialog", new showInputDialog()); 49 script.getScriptEnvironment().getEnvironment().put("&showFileDialog", new showChooserDialog(JFileChooser.FILES_ONLY)); 50 script.getScriptEnvironment().getEnvironment().put("&showDirectoryDialog", new showChooserDialog(JFileChooser.DIRECTORIES_ONLY)); 51 52 script.getScriptEnvironment().getEnvironment().put("&setMappedColor", new setMappedColor()); 53 script.getScriptEnvironment().getEnvironment().put("&getMappedColor", new getMappedColor()); 54 script.getScriptEnvironment().getEnvironment().put("&saveColorMap", new saveColorMap()); 55 56 script.getScriptEnvironment().getEnvironment().put("&generateThemeScript", new generateTheme()); 57 58 script.getScriptEnvironment().getEnvironment().put("&loadFont", new loadFont()); 59 60 script.getScriptEnvironment().getEnvironment().put("&getScriptPath", new getScriptPath()); 61 script.getScriptEnvironment().getEnvironment().put("&getScriptResource", new getScriptResource()); 62 63 script.getScriptEnvironment().getEnvironment().put("&strip", new strip()); 64 script.getScriptEnvironment().getEnvironment().put("&versionString", new versionString()); 65 66 script.getScriptEnvironment().getEnvironment().put("&exit", new exit()); 67 script.getScriptEnvironment().getEnvironment().put("&use", new f2_use()); 68 69 script.getScriptEnvironment().getEnvironment().put("-ischannel", new isChannel()); 70 71 script.getScriptEnvironment().getEnvironment().put("&groupNicks", new groupUsers()); 72 script.getScriptEnvironment().getEnvironment().put("&fileCompleteAll", new fileCompleteAll()); 73 74 script.getScriptEnvironment().getEnvironment().put("&buildCP437String", new buildString()); 75 76 return true; 77 } 78 79 private static HashMap bridges = new HashMap(); 80 81 private static class f2_use implements Function 82 { 83 public Scalar evaluate(String n, ScriptInstance si, Stack l) 84 { 85 File parent; 86 String className; 87 88 if (l.size() == 2) 89 { 90 parent = BridgeUtilities.getFile(l); 91 className = BridgeUtilities.getString(l, ""); 92 } 93 else 94 { 95 File a = BridgeUtilities.getFile(l); 96 parent = a.getParentFile(); 97 className = a.getName(); 98 } 99 100 Class bridge; 101 102 try 103 { 104 if (parent != null) 105 { 106 URLClassLoader loader = new URLClassLoader(new URL[] { parent.toURL() }); 107 bridge = Class.forName(className, true, loader); 108 } 109 else 110 { 111 bridge = Class.forName(className); 112 } 113 114 Loadable temp; 115 116 if (bridges.get(bridge.toString()) == null) 117 { 118 temp = (Loadable)bridge.newInstance(); 119 bridges.put(bridge.toString(), temp); 120 } 121 else 122 { 123 temp = (Loadable)bridges.get(bridge.toString()); 124 } 125 126 temp.scriptLoaded(si); 127 } 128 catch (Exception ex) 129 { 130 si.getScriptEnvironment().flagError(ex.toString()); 131 } 132 133 return SleepUtils.getEmptyScalar(); 134 } 135 } 136 137 public boolean scriptUnloaded(ScriptInstance script) 138 { 139 return true; 140 } 141 142 private static class buildString implements Function 143 { 144 public Scalar evaluate(String name, ScriptInstance si, Stack locals) 145 { 146 String temp = BridgeUtilities.getString(locals, ""); 147 148 return SleepUtils.getScalar(ClientUtils.BuildCP437String(temp)); 149 } 150 } 151 152 private static class getScriptPath implements Function 153 { 154 public Scalar evaluate(String name, ScriptInstance si, Stack locals) 155 { 156 if (locals.size() < 1) 157 return SleepUtils.getEmptyScalar(); 158 159 File path = _getScriptPath(locals.pop().toString()); 160 161 if (path == null) 162 return SleepUtils.getEmptyScalar(); 163 164 return SleepUtils.getScalar(path.getAbsolutePath()); 165 } 166 } 167 168 private static File _getScriptPath(String script) 169 { 170 StringList temp = ClientState.getClientState().getStringList("script.files"); 171 Iterator i = temp.getList().iterator(); 172 173 while (i.hasNext()) 174 { 175 File f = new File(i.next().toString()); 176 177 if (f.getName().equals(script) || f.getAbsolutePath().equals(script)) 178 { 179 return f.getParentFile(); 180 } 181 } 182 183 return null; 184 } 185 186 private static class getScriptResource implements Function 187 { 188 public Scalar evaluate(String name, ScriptInstance si, Stack locals) 189 { 190 if (locals.size() < 2) 191 return SleepUtils.getEmptyScalar(); 192 193 File path = _getScriptPath(locals.pop().toString()); 194 195 while (!locals.isEmpty()) 196 { 197 path = new File(path, locals.pop().toString()); 198 } 199 200 return SleepUtils.getScalar(path.getAbsolutePath()); 201 } 202 } 203 204 private static class strip implements Function 205 { 206 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 207 { 208 return SleepUtils.getScalar(ClientUtils.strip(BridgeUtilities.getString(locals, ""))); 209 } 210 } 211 212 private static class generateTheme implements Function 213 { 214 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 215 { 216 if (locals.size() >= 1) 217 { 218 ClientUtils.generateThemeScript(locals.pop().toString()); 219 } 220 else 221 { 222 ClientUtils.generateThemeScript(null); 223 } 224 225 return SleepUtils.getEmptyScalar(); 226 } 227 } 228 229 private static class versionString implements Function 230 { 231 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 232 { 233 return SleepUtils.getScalar(ClientState.getClientState().getString("version.string", ClientDefaults.version_string)); 234 } 235 } 236 237 private static class loadFont implements Function 238 { 239 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 240 { 241 String a = locals.pop().toString(); 242 243 try 244 { 245 Font font = Font.createFont(Font.TRUETYPE_FONT, ClientState.getClientState().getResourceAsStream(a)); 246 return SleepUtils.getScalar(ClientUtils.encodeFont(font)); 247 } 248 catch (Exception ex) { si.getScriptEnvironment().flagError(ex.getMessage()); } 249 250 return SleepUtils.getEmptyScalar(); 251 } 252 } 253 254 private static class formatBytes implements Function 255 { 256 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 257 { 258 long a = BridgeUtilities.getLong(locals); 259 return SleepUtils.getScalar(ClientUtils.formatBytes(a)); 260 } 261 } 262 263 private static class formatTime implements Function 264 { 265 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 266 { 267 long a = BridgeUtilities.getLong(locals); 268 return SleepUtils.getScalar(ClientUtils.formatTime(a)); 269 } 270 } 271 272 private static class formatTime2 implements Function 273 { 274 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 275 { 276 long a = BridgeUtilities.getLong(locals); 277 return SleepUtils.getScalar(ClientUtils.formatTime2(a)); 278 } 279 } 280 281 private static class formatTime3 implements Function 282 { 283 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 284 { 285 long a = BridgeUtilities.getLong(locals); 286 return SleepUtils.getScalar(ClientUtils.formatTime3(a)); 287 } 288 } 289 290 private static class timeDateStamp implements Function 291 { 292 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 293 { 294 long a = BridgeUtilities.getLong(locals); 295 return SleepUtils.getScalar(ClientUtils.TimeDateStamp(a)); 296 } 297 } 298 299 private static class longip implements Function 300 { 301 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 302 { 303 String a = locals.pop().toString(); 304 return SleepUtils.getScalar(ClientUtils.longip(a)); 305 } 306 } 307 308 private class exit implements Function 309 { 310 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 311 { 312 getCapabilities().getGlobalCapabilities().QuitClient(); 313 return null; 314 } 315 } 316 317 private class showInputDialog implements Function 318 { 319 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 320 { 321 String message = "Your answer?"; 322 323 if (!locals.isEmpty()) 324 message = locals.pop().toString(); 325 326 String title = "Input Requested"; 327 328 if (!locals.isEmpty()) 329 title = locals.pop().toString(); 330 331 String a = JOptionPane.showInputDialog(getCapabilities().getGlobalCapabilities().getFrame(), message, title, JOptionPane.QUESTION_MESSAGE); 332 333 if (a == null) 334 return SleepUtils.getEmptyScalar(); 335 336 return SleepUtils.getScalar(a); 337 } 338 } 339 340 private class showChooserDialog implements Function 341 { 342 private int dirChooser; 343 private JFileChooser chooser = null; 344 345 public showChooserDialog(int dir) { dirChooser = dir; } 346 347 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 348 { 349 if (chooser == null) 350 chooser = new JFileChooser(); 351 352 chooser.setFileSelectionMode(dirChooser); 353 354 String title = "Select File"; 355 356 if (!locals.isEmpty()) 357 title = locals.pop().toString(); 358 359 chooser.setDialogTitle(title); 360 361 if (!locals.isEmpty()) 362 chooser.setCurrentDirectory(new File(locals.pop().toString())); 363 364 if (!locals.isEmpty()) 365 chooser.setApproveButtonText(locals.pop().toString()); 366 367 int returnVal = chooser.showOpenDialog(getCapabilities().getGlobalCapabilities().getFrame()); 368 369 if (returnVal == JFileChooser.APPROVE_OPTION) 370 return SleepUtils.getScalar(chooser.getSelectedFile().getAbsolutePath()); 371 372 return SleepUtils.getEmptyScalar(); 373 } 374 } 375 376 private static class ctime implements Function 377 { 378 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 379 { 380 return SleepUtils.getScalar(ClientUtils.ctime()); 381 } 382 } 383 384 private static class formatDecimal implements Function 385 { 386 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 387 { 388 long temp = BridgeUtilities.getLong(locals); 389 return SleepUtils.getScalar(ClientUtils.formatLongAsDecimal(temp)); 390 } 391 } 392 393 private static class timeStamp implements Function 394 { 395 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 396 { 397 return SleepUtils.getScalar(ClientUtils.TimeStamp()); 398 } 399 } 400 401 private static class setMappedColor implements Function 402 { 403 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 404 { 405 int a = BridgeUtilities.getInt(locals); 406 String b = locals.pop().toString(); 407 408 if (a < TextSource.colorTable.length && a >= 0) 409 TextSource.colorTable[a] = Color.decode(b); 410 411 return SleepUtils.getEmptyScalar(); 412 } 413 } 414 415 private static class getMappedColor implements Function 416 { 417 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 418 { 419 int a = BridgeUtilities.getInt(locals); 420 421 if (a < TextSource.colorTable.length && a > 0) 422 return SleepUtils.getScalar(TextSource.colorTable[a].getRGB()); 423 424 return SleepUtils.getEmptyScalar(); 425 } 426 } 427 428 private static class saveColorMap implements Function 429 { 430 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 431 { 432 TextSource.saveColorMap(); 433 return SleepUtils.getEmptyScalar(); 434 } 435 } 436 437 private static class fileCompleteAll implements Function 438 { 439 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 440 { 441 Scalar value = SleepUtils.getArrayScalar(); 442 Iterator i = ClientUtils.fileCompleteAll(BridgeUtilities.getString(locals, "")).iterator(); 443 while (i.hasNext()) 444 { 445 value.getArray().push(SleepUtils.getScalar(i.next().toString())); 446 } 447 448 return value; 449 } 450 } 451 452 private static class groupUsers implements Function 453 { 454 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 455 { 456 ScalarArray users = BridgeUtilities.getArray(locals); 457 int count = BridgeUtilities.getInt(locals, 4); 458 Scalar value = SleepUtils.getArrayScalar(); 459 460 StringBuffer rv = new StringBuffer (); 461 462 int x = 1; 463 Iterator i = users.scalarIterator(); 464 while (i.hasNext()) 465 { 466 String temp = i.next().toString(); 467 rv.append(temp); 468 if ((x % count) == 0 && x > 1) { rv.append("="); } 469 else { rv.append(","); } 470 471 x++; 472 } 473 474 if (rv.toString().length() > 1) 475 { 476 String groups[] = rv.toString().substring(0, rv.toString().length() - 1).split("="); 477 478 for (x = 0; x < groups.length; x++) 479 { 480 value.getArray().push(SleepUtils.getScalar(groups[x])); 481 } 482 } 483 484 return value; 485 } 486 } 487 488 private static class isChannel implements Predicate 489 { 490 public boolean decide(String pred, ScriptInstance si, Stack locals) 491 { 492 return ClientUtils.isChannel(BridgeUtilities.getString(locals, " ")); 493 } 494 } 495 496 private static class duration implements Function 497 { 498 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 499 { 500 long seconds = BridgeUtilities.getLong(locals, 0L); 501 StringBuffer rv = new StringBuffer (); 502 503 long minutes = 0, hours = 0, days = 0; 504 505 days = seconds / (60 * 60 * 24); 506 hours = seconds / (60 * 60) % 24; 507 minutes = (seconds / 60) % 60; 508 seconds = seconds % 60; 509 510 if (days > 0) { 511 rv.append(days); 512 513 if (days == 1) { rv.append("day, "); } 514 else { rv.append("days, "); } 515 } 516 517 if (hours > 0) { 518 rv.append(hours); 519 520 if (hours == 1) { rv.append("hour, "); } 521 else { rv.append("hours, "); } 522 } 523 524 if (minutes > 0) { 525 rv.append(minutes); 526 527 if (minutes == 1) { rv.append("minute, "); } 528 else { rv.append("minutes, "); } 529 } 530 531 rv.append(seconds); 532 533 if (seconds == 1) { rv.append("second"); } 534 else { rv.append("seconds"); } 535 536 return SleepUtils.getScalar(rv.toString()); 537 } 538 } 539 } 540 | Popular Tags |