1 package rero.gui.script; 2 3 import rero.gui.*; 4 import rero.gui.windows.*; 5 import rero.gui.input.*; 6 import rero.ircfw.*; 7 8 import sleep.engine.*; 9 import sleep.runtime.*; 10 import sleep.interfaces.*; 11 import sleep.bridges.BridgeUtilities; 12 13 import java.awt.*; 14 import java.awt.datatransfer.*; 15 16 import javax.swing.*; 17 import java.util.*; 18 19 import text.*; 20 import text.list.*; 21 22 import rero.util.ClientUtils; 23 24 public class WindowOperators implements Predicate, Function, Loadable 25 { 26 protected IRCSession session; 27 28 public WindowOperators(IRCSession _session) 29 { 30 session = _session; 31 } 32 33 public boolean scriptLoaded(ScriptInstance script) 34 { 35 String [] contents = new String [] { 36 37 "&setWindowPrompt", 38 "&getWindowPrompt", 39 40 "&setWindowTitle", 41 "&getWindowTitle", 42 43 "&renameWindow", 44 45 "&getSelectedText", 46 "&cutSelectedText", 47 "©SelectedText", 48 "&pasteText", 49 "&replaceSelectedText", 50 51 "&setInputText", 52 "&getInputText", 53 54 "&getSelectedUsers", 55 "&getSelectedUser", 56 57 "&getClipboardText", 58 "&setClipboardText", 59 60 "&scrollWindow", 61 62 "-iswindow", 63 "-isspecial", 64 "&refreshWindow" 65 }; 66 67 for (int x = 0; x < contents.length; x++) 68 { 69 script.getScriptEnvironment().getEnvironment().put(contents[x], this); 70 } 71 72 script.getScriptEnvironment().getEnvironment().put("&getCursorPosition", new getCursorPosition()); 73 script.getScriptEnvironment().getEnvironment().put("&setCursorPosition", new setCursorPosition()); 74 75 script.getScriptEnvironment().getEnvironment().put("&setButtonColor", new setButtonColor()); 76 script.getScriptEnvironment().getEnvironment().put("&getButtonColor", new getButtonColor()); 77 return true; 78 } 79 80 private class getButtonColor implements Function 81 { 82 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 83 { 84 String window = BridgeUtilities.getString(locals, ""); 85 if (session.getWindow(window) != null) 86 return SleepUtils.getScalar(session.getWindow(window).getButton().getForeground().getRGB()); 87 88 return SleepUtils.getEmptyScalar(); 89 } 90 } 91 92 private class setButtonColor implements Function 93 { 94 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 95 { 96 String window = BridgeUtilities.getString(locals, ""); 97 if (session.getWindow(window) != null) 98 session.getWindow(window).getButton().setForeground(Color.decode(locals.pop().toString())); 99 100 return SleepUtils.getEmptyScalar(); 101 } 102 } 103 104 private class getCursorPosition implements Function 105 { 106 public Scalar evaluate(String function, ScriptInstance script, Stack locals) 107 { 108 String window = BridgeUtilities.getString(locals, ""); 109 if (session.getWindow(window) != null && session.getWindow(window).isLegalWindow()) 110 { 111 return SleepUtils.getScalar(session.getWindow(window).getInput().getCaretPosition()); 112 } 113 114 return SleepUtils.getEmptyScalar(); 115 } 116 } 117 118 private class setCursorPosition implements Function 119 { 120 public Scalar evaluate(String function, ScriptInstance script, Stack locals) 121 { 122 String window = BridgeUtilities.getString(locals, ""); 123 if (session.getWindow(window) != null && session.getWindow(window).isLegalWindow()) 124 { 125 session.getWindow(window).getInput().setCaretPosition( BridgeUtilities.getInt(locals, 0) ); 126 } 127 128 return SleepUtils.getEmptyScalar(); 129 } 130 } 131 132 public boolean scriptUnloaded(ScriptInstance script) 133 { 134 return true; 135 } 136 137 public Scalar evaluate(final String function, final ScriptInstance script, Stack locals) 138 { 139 if (function.equals("&getSelectedText")) 140 { 141 return SleepUtils.getScalar(session.getActiveWindow().getInput().getSelectedText()); 142 } 143 else if (function.equals("&renameWindow") && locals.size() == 2) 144 { 145 String a = locals.pop().toString(); 146 String b = locals.pop().toString(); 147 148 session.renameWindow(a, b); 149 150 return SleepUtils.getEmptyScalar(); 151 } 152 else if (function.equals("&scrollWindow") && locals.size() == 2) 153 { 154 String a = locals.pop().toString(); 155 int b = BridgeUtilities.getInt(locals, 0); 156 157 if (session.getWindow(a) != null && session.getWindow(a).isLegalWindow()) 158 { 159 session.getWindow(a).getDisplay().scroll(b); 160 } 161 } 162 else if (function.equals("&getWindowTitle") && locals.size() == 1) 163 { 164 String window = locals.pop().toString(); 165 166 if (session.getWindow(window) != null) 167 { 168 return SleepUtils.getScalar(session.getWindow(window).getTitle()); 169 } 170 171 return SleepUtils.getEmptyScalar(); 172 } 173 else if (function.equals("&getWindowTitle") && locals.size() == 0) 174 { 175 return SleepUtils.getScalar(SessionManager.getGlobalCapabilities().getFrame().getTitle()); 176 } 177 else if (function.equals("&getWindowPrompt") && locals.size() == 1) 178 { 179 String window = locals.pop().toString(); 180 181 if (session.getWindow(window) != null) 182 { 183 return SleepUtils.getScalar(session.getWindow(window).getInput().getIndent()); 184 } 185 return SleepUtils.getEmptyScalar(); 186 } 187 else if (function.equals("&getInputText") && locals.size() == 1) 188 { 189 String window = locals.pop().toString(); 190 if (session.getWindow(window) != null) 191 { 192 return SleepUtils.getScalar(session.getWindow(window).getInput().getText()); 193 } 194 } 195 else if (function.equals("&getSelectedUser") && locals.size() == 1) 196 { 197 String window = locals.pop().toString(); 198 if (session.getWindow(window) != null) 199 { 200 ListElement element = ((ChannelWindow)session.getWindow(window)).getListbox().getSelectedElement(); 201 if (element != null) 202 { 203 User user = (User)element.getSource(); 204 return SleepUtils.getScalar(user.getNick()); 205 } 206 } 207 208 return SleepUtils.getEmptyScalar(); 209 } 210 else if (function.equals("&getSelectedUsers") && locals.size() == 1) 211 { 212 String window = locals.pop().toString(); 213 if (session.getWindow(window) != null && session.getWindow(window) instanceof ChannelWindow) 214 { 215 Set returnValue = new HashSet(); 216 217 Iterator list = ((ChannelWindow)session.getWindow(window)).getListbox().getSelectedElements().iterator(); 218 while (list.hasNext()) 219 { 220 ListElement element = (ListElement)list.next(); 221 222 User user = (User)element.getSource(); 223 returnValue.add(user.getNick()); 224 } 225 226 return SleepUtils.getArrayWrapper(returnValue); 227 } 228 229 return SleepUtils.getEmptyScalar(); 230 } 231 else if (function.equals("&getClipboardText")) 232 { 233 Clipboard cb = null; 234 235 if (Toolkit.getDefaultToolkit().getSystemSelection() != null) 236 { 237 cb = Toolkit.getDefaultToolkit().getSystemSelection(); 238 } 239 else if (Toolkit.getDefaultToolkit().getSystemClipboard() != null) 240 { 241 cb = Toolkit.getDefaultToolkit().getSystemClipboard(); 242 } 243 244 try 245 { 246 if (cb != null) 247 return SleepUtils.getScalar(cb.getContents(this).getTransferData(DataFlavor.stringFlavor).toString()); 248 } 249 catch (Exception ex) { ex.printStackTrace(); } 250 251 return SleepUtils.getEmptyScalar(); 252 } 253 else if (function.equals("&setClipboardText")) 254 { 255 String sel = BridgeUtilities.getString(locals, ""); 256 257 StringSelection selected = new StringSelection(sel); 258 259 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selected, selected); 260 261 if (Toolkit.getDefaultToolkit().getSystemSelection() != null) 262 { 263 Toolkit.getDefaultToolkit().getSystemSelection().setContents(selected, selected); 264 } 265 } 266 else 267 { 268 final Stack tempLocals = new Stack(); 269 270 while (!locals.isEmpty()) 271 { 272 tempLocals.push(locals.pop().toString()); 273 } 274 275 ClientUtils.invokeLater(new Runnable () 276 { 277 public void run() 278 { 279 safeEvaluate(function, script, tempLocals); 280 } 281 }); 282 } 283 284 return SleepUtils.getEmptyScalar(); 285 } 286 287 public void safeEvaluate(String function, ScriptInstance script, Stack locals) 288 { 289 String text = "", window = ""; 290 291 if (locals.size() == 1) 292 { 293 text = locals.pop().toString(); 294 } 295 else if (locals.size() == 2) 296 { 297 text = locals.pop().toString(); 298 window = locals.pop().toString(); 299 } 300 301 if (function.equals("&cutSelectedText")) 302 { 303 session.getActiveWindow().getInput().cut(); 304 } 305 else if (function.equals("©SelectedText")) 306 { 307 session.getActiveWindow().getInput().copy(); 308 } 309 else if (function.equals("&pasteText")) 310 { 311 session.getActiveWindow().getInput().paste(); 312 } 313 else if (function.equals("&replaceSelectedText")) 314 { 315 session.getActiveWindow().getInput().replaceSelection(text); 316 } 317 else if (function.equals("&setWindowTitle")) 318 { 319 if (window.length() > 0 && session.getWindow(window) != null) 320 { 321 session.getWindow(window).setTitle(text); 322 } 323 else 324 { 325 SessionManager.getGlobalCapabilities().getFrame().setTitle(text); 326 } 327 } 328 else if (function.equals("&setWindowPrompt")) 329 { 330 if (session.getWindow(window) != null) 331 { 332 session.getWindow(window).getInput().setIndent(text); 333 } 334 } 335 else if (function.equals("&refreshWindow")) 336 { 337 if (session.getWindow(text) != null) 338 { 339 session.getWindow(text).touch(); 340 } 341 } 342 else if (function.equals("&setInputText")) 343 { 344 if (session.getWindow(window) != null) 345 { 346 session.getWindow(window).getInput().setText(text); 347 } 348 } 349 } 350 351 public boolean decide(String predicate, ScriptInstance script, Stack terms) 352 { 353 if (terms.size() != 1) 354 { 355 return false; 356 } 357 358 String channel = ((Scalar)terms.pop()).getValue().toString(); 359 360 if (predicate.equals("-iswindow")) 361 { 362 return session.isWindow(channel); 363 } 364 365 if (predicate.equals("-isspecial")) 366 { 367 return !session.getWindow(channel).isLegalWindow(); 368 } 369 370 return false; 371 } 372 } 373 | Popular Tags |