1 package ch
.cyberduck
.ui
.cocoa
.delegate
;
4 * Copyright (c) 2002-2010 David Kocher. All rights reserved.
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
;
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();
63 * @return Path selected in the browser or current working directory.
65 protected abstract List
<Path
> getSelected();
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();
76 return new NSInteger(1);
78 return new NSInteger(urls
* 2);
81 protected abstract List
<DescriptiveUrl
> getURLs(Path selected
);
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);
94 boolean label
= index
.intValue() % 2 == 0;
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());
108 this.clearShortcut(item
);
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);
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
);
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
);
151 public boolean validateMenuItem(final NSMenuItem item
) {
152 final List
<Path
> selected
= this.getSelected();
153 if(selected
.isEmpty()) {
156 final Selector action
= item
.action();
157 if(action
.equals(this.getDefaultAction())) {
158 return StringUtils
.isNotBlank(item
.representedObject());
164 protected ID
getTarget() {
169 public Selector
getDefaultAction() {
170 return Foundation
.selector("menuClicked:");