1 22 23 package org.gjt.sp.jedit; 24 25 import java.io.*; 26 import java.net.URL ; 27 import java.util.*; 28 29 import org.gjt.sp.jedit.gui.InputHandler; 30 import org.gjt.sp.util.Log; 31 import org.gjt.sp.util.XMLUtilities; 32 33 144 public class ActionSet 145 { 146 151 public ActionSet() 152 { 153 actions = new Hashtable(); 154 loaded = true; 155 label = "<no label set; plugin bug>"; 156 } 158 167 public ActionSet(PluginJAR plugin, String [] cachedActionNames, 168 boolean[] cachedActionToggleFlags, URL uri) 169 { 170 this(); 171 this.plugin = plugin; 172 this.uri = uri; 173 if(cachedActionNames != null) 174 { 175 for(int i = 0; i < cachedActionNames.length; i++) 176 { 177 actions.put(cachedActionNames[i],placeholder); 178 jEdit.setTemporaryProperty(cachedActionNames[i] 179 + ".toggle",cachedActionToggleFlags[i] 180 ? "true" : "false"); 181 } 182 } 183 loaded = false; 184 } 186 192 public ActionSet(String label) 193 { 194 this(); 195 setLabel(label); 196 } 198 203 public String getLabel() 204 { 205 return label; 206 } 208 214 public void setLabel(String label) 215 { 216 if(label == null) 217 throw new NullPointerException (); 218 this.label = label; 219 } 221 226 public PluginJAR getPluginJAR() 227 { 228 return plugin; 229 } 231 237 public void addAction(EditAction action) 238 { 239 actions.put(action.getName(),action); 240 if(context != null) 241 { 242 context.actionNames = null; 243 context.actionHash.put(action.getName(),this); 244 } 245 } 247 253 public void removeAction(String name) 254 { 255 actions.remove(name); 256 if(context != null) 257 { 258 context.actionNames = null; 259 context.actionHash.remove(name); 260 } 261 } 263 268 public void removeAllActions() 269 { 270 if(context != null) 271 { 272 context.actionNames = null; 273 String [] actions = getActionNames(); 274 for(int i = 0; i < actions.length; i++) 275 { 276 context.actionHash.remove(actions[i]); 277 } 278 } 279 this.actions.clear(); 280 } 282 291 public EditAction getAction(String name) 292 { 293 Object obj = actions.get(name); 294 if(obj == placeholder) 295 { 296 load(); 297 obj = actions.get(name); 298 if(obj == placeholder) 299 { 300 Log.log(Log.WARNING,this,"Outdated cache"); 301 obj = null; 302 } 303 } 304 305 return (EditAction)obj; 306 } 308 313 public int getActionCount() 314 { 315 return actions.size(); 316 } 318 323 public String [] getActionNames() 324 { 325 String [] retVal = new String [actions.size()]; 326 Enumeration e = actions.keys(); 327 int i = 0; 328 while(e.hasMoreElements()) 329 { 330 retVal[i++] = (String )e.nextElement(); 331 } 332 return retVal; 333 } 335 341 public String [] getCacheableActionNames() 342 { 343 LinkedList retVal = new LinkedList(); 344 Enumeration e = actions.elements(); 345 while(e.hasMoreElements()) 346 { 347 Object obj = e.nextElement(); 348 if(obj == placeholder) 349 { 350 Log.log(Log.WARNING,this,"Action set not up " 353 + "to date"); 354 } 355 else if(obj instanceof BeanShellAction) 356 retVal.add(((BeanShellAction)obj).getName()); 357 } 358 return (String [])retVal.toArray(new String [retVal.size()]); 359 } 361 369 public EditAction[] getActions() 370 { 371 load(); 372 373 EditAction[] retVal = new EditAction[actions.size()]; 374 Enumeration e = actions.elements(); 375 int i = 0; 376 while(e.hasMoreElements()) 377 { 378 retVal[i++] = (EditAction)e.nextElement(); 379 } 380 return retVal; 381 } 383 389 public boolean contains(String action) 390 { 391 boolean retval = actions.containsKey(action); 392 return retval; 393 } 396 401 public int size() 402 { 403 return actions.size(); 404 } 406 public String toString() 408 { 409 return label; 410 } 412 423 public void initKeyBindings() 424 { 425 InputHandler inputHandler = jEdit.getInputHandler(); 426 427 Iterator iter = actions.entrySet().iterator(); 428 while(iter.hasNext()) 429 { 430 Map.Entry entry = (Map.Entry)iter.next(); 431 String name = (String )entry.getKey(); 432 433 String shortcut1 = jEdit.getProperty(name + ".shortcut"); 434 if(shortcut1 != null) 435 inputHandler.addKeyBinding(shortcut1,name); 436 437 String shortcut2 = jEdit.getProperty(name + ".shortcut2"); 438 if(shortcut2 != null) 439 inputHandler.addKeyBinding(shortcut2,name); 440 } 441 } 443 449 public void load() 450 { 451 if(loaded) 452 return; 453 454 loaded = true; 455 457 Reader stream = null; 458 459 try 460 { 461 Log.log(Log.DEBUG,this,"Loading actions from " + uri); 462 ActionListHandler ah = new ActionListHandler(uri.toString(),this); 463 XMLUtilities.parseXML(uri.openStream(), ah); 464 } 465 catch(IOException e) 466 { 467 Log.log(Log.ERROR,uri,e); 468 } 469 } 471 ActionContext context; 473 474 void getActionNames(List vec) 476 { 477 Enumeration e = actions.keys(); 478 while(e.hasMoreElements()) 479 vec.add(e.nextElement()); 480 } 482 484 private String label; 486 private Hashtable actions; 487 private PluginJAR plugin; 488 private URL uri; 489 private boolean loaded; 490 491 private static final Object placeholder = new Object (); 492 493 } 495 | Popular Tags |