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
.NSColor
;
22 import ch
.cyberduck
.binding
.application
.NSFont
;
23 import ch
.cyberduck
.binding
.application
.NSImage
;
24 import ch
.cyberduck
.binding
.application
.NSMenu
;
25 import ch
.cyberduck
.binding
.application
.NSMenuItem
;
26 import ch
.cyberduck
.binding
.foundation
.NSArray
;
27 import ch
.cyberduck
.binding
.foundation
.NSAttributedString
;
28 import ch
.cyberduck
.binding
.foundation
.NSDictionary
;
29 import ch
.cyberduck
.core
.AbstractHostCollection
;
30 import ch
.cyberduck
.core
.BookmarkNameProvider
;
31 import ch
.cyberduck
.core
.HistoryCollection
;
32 import ch
.cyberduck
.core
.Host
;
33 import ch
.cyberduck
.core
.LocaleFactory
;
34 import ch
.cyberduck
.core
.UserDateFormatterFactory
;
35 import ch
.cyberduck
.core
.preferences
.Preferences
;
36 import ch
.cyberduck
.core
.preferences
.PreferencesFactory
;
37 import ch
.cyberduck
.core
.resources
.IconCacheFactory
;
38 import ch
.cyberduck
.ui
.cocoa
.Action
;
39 import ch
.cyberduck
.ui
.cocoa
.MainController
;
40 import ch
.cyberduck
.ui
.cocoa
.TableCellAttributes
;
42 import org
.apache
.log4j
.Logger
;
43 import org
.rococoa
.Foundation
;
44 import org
.rococoa
.Selector
;
45 import org
.rococoa
.cocoa
.foundation
.NSInteger
;
47 import java
.util
.Date
;
52 public class HistoryMenuDelegate
extends CollectionMenuDelegate
<Host
> {
53 private static final Logger log
= Logger
.getLogger(HistoryMenuDelegate
.class);
55 protected static final NSDictionary TIMESTAMP_FONT_ATTRIBUTES
= NSDictionary
.dictionaryWithObjectsForKeys(
56 NSArray
.arrayWithObjects(NSFont
.userFontOfSize(NSFont
.smallSystemFontSize()), NSColor
.darkGrayColor(),
57 TableCellAttributes
.PARAGRAPH_STYLE_LEFT_ALIGNMENT_TRUNCATE_MIDDLE
),
58 NSArray
.arrayWithObjects(NSAttributedString
.FontAttributeName
, NSAttributedString
.ForegroundColorAttributeName
,
59 NSAttributedString
.ParagraphStyleAttributeName
)
62 private AbstractHostCollection collection
63 = HistoryCollection
.defaultCollection();
65 private MenuCallback callback
;
67 private final Preferences preferences
68 = PreferencesFactory
.get();
70 public HistoryMenuDelegate() {
71 this(new MenuCallback() {
73 public void selected(final NSMenuItem sender
) {
74 MainController
.newDocument().mount(HistoryCollection
.defaultCollection().lookup(sender
.representedObject()));
79 public HistoryMenuDelegate(final MenuCallback callback
) {
80 this(HistoryCollection
.defaultCollection(), callback
);
83 public HistoryMenuDelegate(final AbstractHostCollection collection
, final MenuCallback callback
) {
85 this.callback
= callback
;
89 public NSInteger
numberOfItemsInMenu(NSMenu menu
) {
90 if(this.isPopulated()) {
91 // If you return a negative value, the number of items is left unchanged
92 // and menu:updateItem:atIndex:shouldCancel: is not called.
93 return new NSInteger(-1);
95 if(collection
.size() > 0) {
96 // The number of history plus a delimiter and the 'Clear' menu
97 return new NSInteger(collection
.size() * 2 + 2);
99 return new NSInteger(1);
103 public Host
itemForIndex(final NSInteger index
) {
104 return collection
.get(index
.intValue() / 2);
108 * @return False if no more updates needed.
111 public boolean menuUpdateItemAtIndex(NSMenu menu
, NSMenuItem item
, NSInteger row
, boolean cancel
) {
112 final int size
= collection
.size();
114 item
.setTitle(LocaleFactory
.localizedString("No recently connected servers available"));
115 item
.setTarget(null);
116 item
.setAction(null);
118 item
.setEnabled(false);
119 // No more menu updates.
122 else if(row
.intValue() < size
* 2) {
123 boolean label
= row
.intValue() % 2 == 0;
124 final Host h
= this.itemForIndex(row
);
126 item
.setTitle(BookmarkNameProvider
.toString(h
));
127 item
.setTarget(this.id());
128 item
.setAction(this.getDefaultAction());
129 item
.setRepresentedObject(h
.getUuid());
130 item
.setEnabled(true);
131 item
.setImage(IconCacheFactory
.<NSImage
>get().iconNamed(h
.getProtocol().icon(), preferences
.getInteger("bookmark.menu.icon.size")));
134 // Dummy menu item with timestamp
135 final Date timestamp
= h
.getTimestamp();
136 if(null != timestamp
) {
137 item
.setAttributedTitle(NSAttributedString
.attributedStringWithAttributes(
138 UserDateFormatterFactory
.get().getLongFormat(timestamp
.getTime()), TIMESTAMP_FONT_ATTRIBUTES
));
141 item
.setAttributedTitle(NSAttributedString
.attributedStringWithAttributes(
142 LocaleFactory
.localizedString("Unknown"), TIMESTAMP_FONT_ATTRIBUTES
));
146 else if(row
.intValue() == size
* 2) {
147 menu
.removeItemAtIndex(row
);
148 menu
.insertItem_atIndex(this.seperator(), row
);
150 else if(row
.intValue() == size
* 2 + 1) {
151 item
.setTitle(LocaleFactory
.localizedString("Clear Menu"));
152 item
.setTarget(this.id());
153 item
.setAction(Foundation
.selector("clearMenuItemClicked:"));
154 item
.setEnabled(true);
156 return super.menuUpdateItemAtIndex(menu
, item
, row
, cancel
);
160 public void menuItemClicked(NSMenuItem sender
) {
161 if(log
.isDebugEnabled()) {
162 log
.debug(String
.format("Menu item clicked %s", sender
));
164 callback
.selected(sender
);
168 public void clearMenuItemClicked(NSMenuItem sender
) {
169 // Delete all bookmark files
174 public Selector
getDefaultAction() {
175 return Foundation
.selector("menuItemClicked:");