Merge pull request #64 in ITERATE/cyberduck from feature/windows/9074 to master
[cyberduck.git] / source / ch / cyberduck / ui / cocoa / delegate / HistoryMenuDelegate.java
blob4240310a013f128d40a4dc5730ee10f6dd0091d0
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.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;
49 /**
50 * @version $Id$
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() {
72 @Override
73 public void selected(final NSMenuItem sender) {
74 MainController.newDocument().mount(HistoryCollection.defaultCollection().lookup(sender.representedObject()));
76 });
79 public HistoryMenuDelegate(final MenuCallback callback) {
80 this(HistoryCollection.defaultCollection(), callback);
83 public HistoryMenuDelegate(final AbstractHostCollection collection, final MenuCallback callback) {
84 super(collection);
85 this.callback = callback;
88 @Override
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);
102 @Override
103 public Host itemForIndex(final NSInteger index) {
104 return collection.get(index.intValue() / 2);
108 * @return False if no more updates needed.
110 @Override
111 public boolean menuUpdateItemAtIndex(NSMenu menu, NSMenuItem item, NSInteger row, boolean cancel) {
112 final int size = collection.size();
113 if(size == 0) {
114 item.setTitle(LocaleFactory.localizedString("No recently connected servers available"));
115 item.setTarget(null);
116 item.setAction(null);
117 item.setImage(null);
118 item.setEnabled(false);
119 // No more menu updates.
120 return false;
122 else if(row.intValue() < size * 2) {
123 boolean label = row.intValue() % 2 == 0;
124 final Host h = this.itemForIndex(row);
125 if(label) {
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")));
133 else {
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));
140 else {
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);
159 @Action
160 public void menuItemClicked(NSMenuItem sender) {
161 if(log.isDebugEnabled()) {
162 log.debug(String.format("Menu item clicked %s", sender));
164 callback.selected(sender);
167 @Action
168 public void clearMenuItemClicked(NSMenuItem sender) {
169 // Delete all bookmark files
170 collection.clear();
173 @Override
174 public Selector getDefaultAction() {
175 return Foundation.selector("menuItemClicked:");