1 22 23 package macos; 24 25 import com.apple.eawt.*; 27 import com.apple.eio.*; 28 import com.apple.cocoa.application.*; 29 import com.apple.cocoa.foundation.*; 30 import java.util.*; 31 import java.io.File ; 32 import javax.swing.*; 33 import org.gjt.sp.jedit.*; 34 import org.gjt.sp.jedit.browser.*; 35 import org.gjt.sp.jedit.gui.*; 36 import org.gjt.sp.jedit.msg.*; 37 import org.gjt.sp.jedit.options.GlobalOptions; 38 import org.gjt.sp.util.Log; 39 41 public class Delegate extends ApplicationAdapter 42 { 43 private final NSSelector actionSel = new NSSelector("doAction", new Class [] {}); 45 46 private List filenames = new LinkedList(); 47 49 public Delegate() 51 { 52 if (jEdit.getBooleanProperty("MacOSPlugin.useScreenMenuBar", 53 jEdit.getBooleanProperty("MacOSPlugin.default.useScreenMenuBar")) 54 ) 55 System.setProperty("apple.laf.useScreenMenuBar","true"); 56 else 57 System.setProperty("apple.laf.useScreenMenuBar","false"); 58 } 60 62 public void handleAbout(ApplicationEvent event) 64 { 65 event.setHandled(true); 66 new AboutDialog(jEdit.getActiveView()); 67 } 69 public void handleFileCodes(BufferUpdate msg) 71 { 72 Buffer buffer = msg.getBuffer(); 73 74 if (!buffer.isDirty() && msg.getWhat() == BufferUpdate.DIRTY_CHANGED) 76 { 77 try { 78 FileManager.setFileTypeAndCreator(buffer.getPath(), 79 buffer.getIntegerProperty("MacOSPlugin.type", 80 jEdit.getIntegerProperty("MacOSPlugin.default.type",0)), 81 buffer.getIntegerProperty("MacOSPlugin.creator", 82 jEdit.getIntegerProperty("MacOSPlugin.default.creator",0))); 83 } catch (Exception e) { 84 } 86 } 87 else if (msg.getWhat() == BufferUpdate.CREATED) 89 { 90 if ("true".equals( 91 jEdit.getProperty("MacOSPlugin.preserveCodes"))) 92 { 93 try { 94 int type = FileManager.getFileType(buffer.getPath()); 95 int creator = FileManager.getFileCreator(buffer.getPath()); 96 97 if (type != 0) 98 buffer.setIntegerProperty("MacOSPlugin.type",type); 99 if (creator != 0) 100 buffer.setIntegerProperty("MacOSPlugin.creator",creator); 101 } catch (Exception e) { 102 } 104 } 105 } 106 } 108 public void handleOpenFile(ApplicationEvent event) 110 { 111 filenames.add(event.getFilename()); 112 event.setHandled(true); 113 } 115 public void handleOpenFile(ViewUpdate msg) 117 { 118 if(msg.getWhat() == ViewUpdate.CREATED) 119 { 120 Iterator i = filenames.iterator(); 121 while (i.hasNext()) 122 jEdit.openFile(msg.getView(),(String )i.next()); 123 MacOSPlugin.started = true; 124 NSApplication app = NSApplication.sharedApplication(); 125 app.setServicesProvider(new Delegate()); 126 } 127 } 129 public void handlePreferences(ApplicationEvent event) 131 { 132 event.setHandled(true); 133 new GlobalOptions(jEdit.getActiveView()); 134 } 136 140 146 148 150 public NSMenu applicationDockMenu(NSApplication sender) 152 { 153 NSMenu dockMenu; 154 BufferMenu bufMenu; 155 MacrosMenu macMenu; 156 RecentMenu recMenu; 157 RecentDirMenu dirMenu; 158 NSMenuItem showCurrItem; 159 NSMenuItem showCurrDirItem; 160 NSMenuItem newViewItem; 161 162 NSMenuItem miBuff = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.buffers.label"),null,""); 164 miBuff.setSubmenu(bufMenu = new BufferMenu()); 165 166 NSMenuItem miRec = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recent.label"),null,""); 168 miRec.setSubmenu(recMenu = new RecentMenu()); 169 170 NSMenuItem miDir = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recentDir.label"),null,""); 172 miDir.setSubmenu(dirMenu = new RecentDirMenu()); 173 174 NSMenuItem miMac = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.macros.label"),null,""); 176 miMac.setSubmenu(macMenu = new MacrosMenu()); 177 178 dockMenu = new NSMenu(); 179 newViewItem = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.newView"),actionSel,""); 180 newViewItem.setTarget(new NewViewAction()); 181 dockMenu.addItem(newViewItem); 182 dockMenu.addItem(new NSMenuItem().separatorItem()); 183 showCurrItem = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.showCurrent"),actionSel,""); 184 dockMenu.addItem(showCurrItem); 185 showCurrDirItem = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.showCurrentDir"),actionSel,""); 186 dockMenu.addItem(showCurrDirItem); 187 dockMenu.addItem(new NSMenuItem().separatorItem()); 188 dockMenu.addItem(miBuff); 189 dockMenu.addItem(miRec); 190 dockMenu.addItem(miDir); 191 if (jEdit.getViewCount() == 0) 194 miMac.setEnabled(false); 195 196 bufMenu.updateMenu(); 197 recMenu.updateMenu(); 198 dirMenu.updateMenu(); 199 macMenu.updateMenu(); 200 201 View view = jEdit.getActiveView(); 202 if (view != null) 203 { 204 File buff = new File (view.getBuffer().getPath()); 205 if (buff.exists()) 206 { 207 showCurrItem.setTarget(new ShowFileAction(buff.getPath())); 208 showCurrDirItem.setTarget(new ShowFileAction(buff.getParent())); 209 } 210 } 211 else 212 { 213 showCurrItem.setEnabled(false); 214 showCurrDirItem.setEnabled(false); 215 } 216 217 return dockMenu; 218 } 220 public void applicationOpenFiles(NSApplication sender, NSArray filenames) 222 { 223 int count = filenames.count(); 224 for (int i=0; i<count; i++) 225 { 226 File file = new File ((String )filenames.objectAtIndex(i)); 227 Buffer buffer; 228 229 View view = jEdit.getActiveView(); 230 if(view == null) 231 view = PerspectiveManager.loadPerspective(true); 232 233 if (file.isDirectory()) 234 { 235 VFSBrowser.browseDirectory(jEdit.getActiveView(),file.getPath()); 236 return; 237 } 238 239 if (jEdit.openFile(view,file.getPath()) == null) 240 Log.log(Log.ERROR,this,"Error opening file."); 241 } 242 } 244 public boolean applicationShouldHandleReopen(NSApplication theApplication, boolean flag) 246 { 247 SwingUtilities.invokeLater(new Runnable () 248 { 249 public void run() 250 { 251 if (jEdit.getViewCount() == 0) 252 new NewViewAction().doAction(); 253 } 254 }); 255 256 return false; 257 } 259 public boolean applicationShouldTerminate(NSApplication sender) 261 { 262 SwingUtilities.invokeLater(new Runnable () 263 { 264 public void run() 265 { 266 jEdit.exit(jEdit.getActiveView(),true); 267 } 268 }); 269 return false; 270 } 271 273 275 277 public String openFile(NSPasteboard pboard, String userData) 279 { 280 if (jEdit.getViewCount() == 0) 281 return null; 282 283 NSData data = pboard.dataForType("NSFilenamesPboardType"); 284 String [] error = new String [1]; 285 int[] format = new int[1]; 286 NSArray filenames = (NSArray)NSPropertyListSerialization.propertyListFromData(data, 287 NSPropertyListSerialization.PropertyListImmutable, 288 format, 289 error); 290 int count = filenames.count(); 291 for (int i=0; i<count; i++) 292 { 293 File file = new File ((String )filenames.objectAtIndex(i)); 294 if (file.isDirectory()) 295 VFSBrowser.browseDirectory(jEdit.getActiveView(),file.getPath()); 296 else 297 jEdit.openFile(jEdit.getActiveView(),file.getPath()); 298 } 299 300 return null; 301 } 303 public String insertSelection(NSPasteboard pboard, String userData) 305 { 306 String string = pboard.stringForType("NSStringPboardType"); 307 if (jEdit.getViewCount() > 0) 308 { 309 View view = jEdit.getActiveView(); 310 view.getBuffer().insert(view.getTextArea().getCaretPosition(),string); 311 } 312 return null; 313 } 315 public String openSelection(NSPasteboard pboard, String userData) 317 { 318 String string = pboard.stringForType("NSStringPboardType"); 319 if (jEdit.getViewCount() == 0) 320 new NewViewAction().doAction(); 321 jEdit.newFile(jEdit.getActiveView()).insert(0,pboard.stringForType("NSStringPboardType")); 322 return null; 323 } 325 327 329 class BufferMenu extends NSMenu 331 { 332 public BufferMenu() 333 { 334 super(); 335 } 336 337 public void updateMenu() 338 { 339 NSMenuItem item; 340 for (int i=0; i<numberOfItems(); i++) 341 removeItemAtIndex(0); 342 343 Buffer[] buffs = jEdit.getBuffers(); 344 for (int i=0; i < buffs.length; i++) 345 { 346 if (!buffs[i].isUntitled()) 347 { 348 item = new NSMenuItem(buffs[i].getName(),actionSel,""); 349 item.setTarget(new ShowFileAction(buffs[i].getPath())); 350 if (!new File (buffs[i].getPath()).exists()) 353 item.setEnabled(false); 354 addItem(item); 355 } 356 } 357 358 if (numberOfItems() == 0) 359 { 360 item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.buffers.none"),null,""); 361 item.setEnabled(false); 362 addItem(item); 363 } 364 } 365 } 367 class MacrosMenu extends NSMenu 369 { 370 public MacrosMenu() 371 { 372 super(); 373 } 374 375 public void updateMenu() 376 { 377 Vector macroVector = Macros.getMacroHierarchy(); 378 NSMenuItem item; 379 File file; 380 int max = macroVector.size(); 381 382 int length = numberOfItems(); 383 for (int i=0; i<length; i++) 384 removeItemAtIndex(0); 385 386 if (max == 0) 387 { 388 item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.macros.none"),null,""); 389 item.setEnabled(false); 390 addItem(item); 391 return; 392 } 393 394 createMenu(this,macroVector); 395 } 396 397 public void createMenu(NSMenu menu, Vector vector) 398 { 399 for(int i=0; i < vector.size(); i++) 400 { 401 Object obj = vector.elementAt(i); 402 if(obj instanceof Macros.Macro) 403 { 404 Macros.Macro macro = (Macros.Macro)obj; 405 NSMenuItem item = new NSMenuItem(macro.getLabel(),actionSel,""); 406 item.setTarget(new MacroAction(macro)); 407 menu.addItem(item); 408 } 409 else if(obj instanceof Vector) 410 { 411 Vector subvector = (Vector)obj; 412 String name = (String )subvector.elementAt(0); 413 NSMenu submenu = new NSMenu(); 414 createMenu(submenu,subvector); 415 if(submenu.numberOfItems() > 0) 416 { 417 NSMenuItem submenuitem = new NSMenuItem(name,null,""); 418 submenuitem.setSubmenu(submenu); 419 menu.addItem(submenuitem); 420 } 421 } 422 } 423 } 424 } 426 class RecentMenu extends NSMenu 428 { 429 public RecentMenu() 430 { 431 super(); 432 } 433 434 public void updateMenu() 435 { 436 List recent = BufferHistory.getHistory(); 437 NSMenuItem item; 438 File file; 439 int max = recent.size(); 440 int min = max - 20; 441 442 int length = numberOfItems(); 443 for (int i=0; i<length; i++) 444 removeItemAtIndex(0); 445 446 if (max == 0) 447 { 448 item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recent.none"),null,""); 449 item.setEnabled(false); 450 addItem(item); 451 return; 452 } 453 454 if (min < 0) 455 min = 0; 456 457 for (int i=max-1; i >= min ; i--) 458 { 459 file = new File (((BufferHistory.Entry)recent.get(i)).path); 460 item = new NSMenuItem(file.getName(),actionSel,""); 461 item.setTarget(new ShowFileAction(file.getPath())); 462 if (!file.exists()) 463 item.setEnabled(false); 464 addItem(item); 465 } 466 } 467 } 469 class RecentDirMenu extends NSMenu 471 { 472 public RecentDirMenu() 473 { 474 super(); 475 } 476 477 public void updateMenu() 478 { 479 HistoryModel model = HistoryModel.getModel("vfs.browser.path"); 480 NSMenuItem item; 481 File file; 482 int max = model.getSize(); 483 484 int length = numberOfItems(); 485 for (int i=0; i<length; i++) 486 removeItemAtIndex(0); 487 488 if (max == 0) 489 { 490 item = new NSMenuItem(jEdit.getProperty("MacOSPlugin.menu.recentDir.none"),null,""); 491 item.setEnabled(false); 492 addItem(item); 493 return; 494 } 495 496 for (int i=0; i < max ; i++) 497 { 498 file = new File (model.getItem(i)); 499 item = new NSMenuItem(file.getName(),actionSel,""); 500 item.setTarget(new ShowFileAction(file.getPath())); 501 if (!file.exists()) 502 item.setEnabled(false); 503 addItem(item); 504 } 505 } 506 } 508 class MacroAction 510 { 511 private Macros.Macro macro; 512 513 public MacroAction(Macros.Macro macro) 514 { 515 this.macro = macro; 516 } 517 518 public void doAction() 519 { 520 macro.invoke(jEdit.getActiveView()); 521 } 522 } 524 class NewViewAction 526 { 527 public void doAction() 528 { 529 SwingUtilities.invokeLater(new Runnable () 530 { 531 public void run() 532 { 533 if (jEdit.getViewCount() == 0) 534 PerspectiveManager.loadPerspective(true); 535 else 536 jEdit.newView(jEdit.getActiveView()); 537 } 538 }); 539 } 540 } 542 class ShowFileAction 544 { 545 private String path; 546 547 public ShowFileAction(String path) 548 { 549 this.path = path; 550 } 551 552 public void doAction() 553 { 554 MacOSActions.showInFinder(path); 555 } 556 } 558 } 560 | Popular Tags |