Fix transcript in transfer window.
[cyberduck.git] / source / ch / cyberduck / ui / cocoa / delegate / EditMenuDelegate.java
blob3310c5817fdb4183a5fce46622d3b6730509d555
1 package ch.cyberduck.ui.cocoa.delegate;
3 /*
4 * Copyright (c) 2006 David Kocher. All rights reserved.
5 * http://cyberduck.ch/
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;
39 /**
40 * @version $Id$
42 public abstract class EditMenuDelegate extends AbstractMenuDelegate {
44 /**
45 * Last selected extension
47 private String extension = null;
49 @Override
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();
57 final int count;
58 if(null == file) {
59 count = EditorFactory.instance().getEditors().size();
61 else {
62 count = EditorFactory.instance().getEditors(file.getName()).size();
64 if(0 == count) {
65 return new NSInteger(1);
67 return new NSInteger(count);
70 protected abstract Path getEditable();
72 /**
73 * Caching last selected extension to build menu.
75 * @return True if menu is built.
77 @Override
78 protected boolean isPopulated() {
79 final Path selected = this.getEditable();
80 if(selected != null && Objects.equals(extension, selected.getExtension())) {
81 return true;
83 if(selected != null) {
84 extension = selected.getExtension();
86 else {
87 extension = null;
89 return false;
92 @Override
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();
99 else {
100 editors = EditorFactory.instance().getEditors(selected.getName());
102 if(editors.size() == 0) {
103 item.setTitle(LocaleFactory.localizedString("No external editor available"));
104 return false;
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"));
112 else {
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());
118 else {
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);
126 @Override
127 protected String getKeyEquivalent() {
128 return "k";
131 @Override
132 protected int getModifierMask() {
133 return NSEvent.NSCommandKeyMask;
136 @Override
137 protected Selector getDefaultAction() {
138 return Foundation.selector("editButtonClicked:");