Merge pull request #64 in ITERATE/cyberduck from feature/windows/9074 to master
[cyberduck.git] / source / ch / cyberduck / ui / cocoa / delegate / URLMenuDelegate.java
blobbd0ee35b7bbe131d5944ddd8d383176bd2027565
1 package ch.cyberduck.ui.cocoa.delegate;
3 /*
4 * Copyright (c) 2002-2010 David Kocher. All rights reserved.
6 * http://cyberduck.ch/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * Bug fixes, suggestions and comments should be sent to:
19 * dkocher@cyberduck.ch
22 import ch.cyberduck.binding.application.NSColor;
23 import ch.cyberduck.binding.application.NSFont;
24 import ch.cyberduck.binding.application.NSImage;
25 import ch.cyberduck.binding.application.NSMenu;
26 import ch.cyberduck.binding.application.NSMenuItem;
27 import ch.cyberduck.binding.foundation.NSArray;
28 import ch.cyberduck.binding.foundation.NSAttributedString;
29 import ch.cyberduck.binding.foundation.NSDictionary;
30 import ch.cyberduck.core.DescriptiveUrl;
31 import ch.cyberduck.core.LocaleFactory;
32 import ch.cyberduck.core.Path;
33 import ch.cyberduck.core.Session;
34 import ch.cyberduck.core.resources.IconCacheFactory;
35 import ch.cyberduck.ui.cocoa.Action;
36 import ch.cyberduck.ui.cocoa.TableCellAttributes;
38 import org.apache.commons.lang3.StringUtils;
39 import org.rococoa.Foundation;
40 import org.rococoa.ID;
41 import org.rococoa.Selector;
42 import org.rococoa.cocoa.foundation.NSInteger;
44 import java.util.ArrayList;
45 import java.util.Iterator;
46 import java.util.List;
48 /**
49 * @version $Id:$
51 public abstract class URLMenuDelegate extends AbstractMenuDelegate {
53 protected static final NSDictionary URL_FONT_ATTRIBUTES = NSDictionary.dictionaryWithObjectsForKeys(
54 NSArray.arrayWithObjects(NSFont.userFontOfSize(NSFont.smallSystemFontSize()), NSColor.darkGrayColor(),
55 TableCellAttributes.PARAGRAPH_STYLE_LEFT_ALIGNMENT_TRUNCATE_MIDDLE),
56 NSArray.arrayWithObjects(NSAttributedString.FontAttributeName, NSAttributedString.ForegroundColorAttributeName,
57 NSAttributedString.ParagraphStyleAttributeName)
60 protected abstract Session<?> getSession();
62 /**
63 * @return Path selected in the browser or current working directory.
65 protected abstract List<Path> getSelected();
67 @Override
68 public NSInteger numberOfItemsInMenu(NSMenu menu) {
69 final List<Path> selected = this.getSelected();
70 if(selected.isEmpty()) {
71 return new NSInteger(1);
73 // Number of URLs for a single path
74 int urls = this.getURLs(selected.iterator().next()).size();
75 if(0 == urls) {
76 return new NSInteger(1);
78 return new NSInteger(urls * 2);
81 protected abstract List<DescriptiveUrl> getURLs(Path selected);
83 @Override
84 public boolean menuUpdateItemAtIndex(NSMenu menu, NSMenuItem item, NSInteger index, boolean cancel) {
85 final List<Path> selected = this.getSelected();
86 if(selected.isEmpty() || this.getURLs(selected.iterator().next()).isEmpty()) {
87 item.setTitle(LocaleFactory.localizedString("None"));
88 item.setEnabled(false);
89 item.setAction(null);
90 item.setTarget(null);
91 item.setImage(null);
93 else {
94 boolean label = index.intValue() % 2 == 0;
95 if(label) {
96 item.setEnabled(true);
97 item.setTarget(this.id());
98 item.setAction(Foundation.selector("menuItemClicked:"));
99 item.setImage(IconCacheFactory.<NSImage>get().iconNamed("site.tiff", 16));
100 Iterator<Path> iter = selected.iterator();
101 final DescriptiveUrl url = this.getURLs(iter.next()).get(index.intValue() / 2);
102 item.setRepresentedObject(url.getUrl());
103 item.setTitle(url.getHelp());
104 if(url.getType().equals(DescriptiveUrl.Type.provider)) {
105 this.setShortcut(item, this.getKeyEquivalent(), this.getModifierMask());
107 else {
108 this.clearShortcut(item);
111 else {
112 // Dummy menu item to preview URL only
113 final List<DescriptiveUrl> target = this.getURLs(index, selected);
114 item.setAttributedTitle(NSAttributedString.attributedStringWithAttributes(
115 StringUtils.join(target, '\n'), URL_FONT_ATTRIBUTES));
118 return super.menuUpdateItemAtIndex(menu, item, index, cancel);
121 private List<DescriptiveUrl> getURLs(final NSInteger index, final List<Path> selected) {
122 List<DescriptiveUrl> list = new ArrayList<DescriptiveUrl>();
123 for(final Path file : selected) {
124 final List<DescriptiveUrl> urls = this.getURLs(file);
125 final DescriptiveUrl url = urls.get(index.intValue() / 2);
126 list.add(url);
128 return list;
131 @Action
132 public void menuClicked(final NSMenu menu) {
133 final List<DescriptiveUrl> selected = new ArrayList<DescriptiveUrl>();
134 for(Path file : this.getSelected()) {
135 selected.add(this.getURLs(file).iterator().next());
137 this.handle(selected);
140 @Action
141 public void menuItemClicked(final NSMenuItem item) {
142 this.handle(this.getURLs(item.menu().indexOfItem(item), this.getSelected()));
146 * @param selected URLs of selected files.
148 public abstract void handle(final List<DescriptiveUrl> selected);
150 @Override
151 public boolean validateMenuItem(final NSMenuItem item) {
152 final List<Path> selected = this.getSelected();
153 if(selected.isEmpty()) {
154 return false;
156 final Selector action = item.action();
157 if(action.equals(this.getDefaultAction())) {
158 return StringUtils.isNotBlank(item.representedObject());
160 return true;
163 @Override
164 protected ID getTarget() {
165 return this.id();
168 @Override
169 public Selector getDefaultAction() {
170 return Foundation.selector("menuClicked:");