1 package ch
.cyberduck
.ui
.cocoa
.delegate
;
4 * Copyright (c) 2006 David Kocher. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to:
18 * dkocher@cyberduck.ch
21 import ch
.cyberduck
.binding
.application
.NSEvent
;
22 import ch
.cyberduck
.binding
.application
.NSImage
;
23 import ch
.cyberduck
.binding
.application
.NSMenu
;
24 import ch
.cyberduck
.binding
.application
.NSMenuItem
;
25 import ch
.cyberduck
.core
.LocaleFactory
;
26 import ch
.cyberduck
.core
.Path
;
27 import ch
.cyberduck
.core
.editor
.EditorFactory
;
28 import ch
.cyberduck
.core
.local
.Application
;
29 import ch
.cyberduck
.core
.resources
.IconCacheFactory
;
31 import org
.apache
.commons
.lang3
.StringUtils
;
32 import org
.rococoa
.Foundation
;
33 import org
.rococoa
.Selector
;
34 import org
.rococoa
.cocoa
.foundation
.NSInteger
;
36 import java
.util
.List
;
37 import java
.util
.Objects
;
42 public abstract class EditMenuDelegate
extends AbstractMenuDelegate
{
45 * Last selected extension
47 private String extension
= null;
50 public NSInteger
numberOfItemsInMenu(NSMenu menu
) {
51 if(this.isPopulated()) {
52 // If you return a negative value, the number of items is left unchanged
53 // and menu:updateItem:atIndex:shouldCancel: is not called.
54 return new NSInteger(-1);
56 final Path file
= this.getEditable();
59 count
= EditorFactory
.instance().getEditors().size();
62 count
= EditorFactory
.instance().getEditors(file
.getName()).size();
65 return new NSInteger(1);
67 return new NSInteger(count
);
70 protected abstract Path
getEditable();
73 * Caching last selected extension to build menu.
75 * @return True if menu is built.
78 protected boolean isPopulated() {
79 final Path selected
= this.getEditable();
80 if(selected
!= null && Objects
.equals(extension
, selected
.getExtension())) {
83 if(selected
!= null) {
84 extension
= selected
.getExtension();
93 public boolean menuUpdateItemAtIndex(NSMenu menu
, NSMenuItem item
, NSInteger index
, boolean cancel
) {
94 final Path selected
= this.getEditable();
95 final List
<Application
> editors
;
96 if(null == selected
) {
97 editors
= EditorFactory
.instance().getEditors();
100 editors
= EditorFactory
.instance().getEditors(selected
.getName());
102 if(editors
.size() == 0) {
103 item
.setTitle(LocaleFactory
.localizedString("No external editor available"));
106 final Application application
= editors
.get(index
.intValue());
107 item
.setRepresentedObject(application
.getIdentifier());
108 final String editor
= editors
.get(index
.intValue()).getName();
109 if(StringUtils
.isBlank(editor
)) {
110 item
.setTitle(LocaleFactory
.localizedString("Unknown"));
113 item
.setTitle(editor
);
115 if(null != selected
&& application
.getIdentifier().equalsIgnoreCase(EditorFactory
.instance().getEditor(selected
.getName()).getIdentifier())) {
116 this.setShortcut(item
, this.getKeyEquivalent(), this.getModifierMask());
119 this.clearShortcut(item
);
121 item
.setImage(IconCacheFactory
.<NSImage
>get().applicationIcon(application
, 16));
122 item
.setAction(Foundation
.selector("editMenuClicked:"));
123 return super.menuUpdateItemAtIndex(menu
, item
, index
, cancel
);
127 protected String
getKeyEquivalent() {
132 protected int getModifierMask() {
133 return NSEvent
.NSCommandKeyMask
;
137 protected Selector
getDefaultAction() {
138 return Foundation
.selector("editButtonClicked:");