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
.BookmarkNameProvider
;
30 import ch
.cyberduck
.core
.HistoryCollection
;
31 import ch
.cyberduck
.core
.Host
;
32 import ch
.cyberduck
.core
.LocaleFactory
;
33 import ch
.cyberduck
.core
.UserDateFormatterFactory
;
34 import ch
.cyberduck
.core
.resources
.IconCacheFactory
;
35 import ch
.cyberduck
.ui
.cocoa
.BrowserController
;
36 import ch
.cyberduck
.ui
.cocoa
.MainController
;
37 import ch
.cyberduck
.ui
.cocoa
.TableCellAttributes
;
39 import org
.apache
.commons
.lang3
.StringUtils
;
40 import org
.apache
.log4j
.Logger
;
41 import org
.rococoa
.Foundation
;
42 import org
.rococoa
.Selector
;
43 import org
.rococoa
.cocoa
.foundation
.NSInteger
;
45 import java
.util
.Date
;
50 public abstract class HistoryMenuDelegate
extends CollectionMenuDelegate
<Host
> {
51 private static final Logger log
= Logger
.getLogger(HistoryMenuDelegate
.class);
53 protected static final NSDictionary TIMESTAMP_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 private HistoryCollection collection
;
62 public HistoryMenuDelegate() {
63 super(HistoryCollection
.defaultCollection());
64 collection
= HistoryCollection
.defaultCollection();
68 public NSInteger
numberOfItemsInMenu(NSMenu menu
) {
69 if(this.isPopulated()) {
70 // If you return a negative value, the number of items is left unchanged
71 // and menu:updateItem:atIndex:shouldCancel: is not called.
72 return new NSInteger(-1);
74 if(collection
.size() > 0) {
75 // The number of history plus a delimiter and the 'Clear' menu
76 return new NSInteger(collection
.size() * 2 + 2);
78 return new NSInteger(1);
82 * @return False if no more updates needed.
85 public boolean menuUpdateItemAtIndex(NSMenu menu
, NSMenuItem item
, NSInteger index
, boolean cancel
) {
89 final int size
= collection
.size();
91 item
.setTitle(LocaleFactory
.localizedString("No recently connected servers available"));
95 item
.setEnabled(false);
96 // No more menu updates.
99 else if(index
.intValue() < size
* 2) {
100 boolean label
= index
.intValue() % 2 == 0;
101 Host h
= collection
.get(index
.intValue() / 2);
103 item
.setTitle(BookmarkNameProvider
.toString(h
));
104 item
.setAction(this.getDefaultAction());
105 item
.setRepresentedObject(h
.getUuid());
106 item
.setTarget(this.id());
107 item
.setEnabled(true);
108 item
.setImage(IconCacheFactory
.<NSImage
>get().iconNamed(h
.getProtocol().icon(), 16));
111 // Dummy menu item with timestamp
112 Date timestamp
= h
.getTimestamp();
113 if(null != timestamp
) {
114 item
.setAttributedTitle(NSAttributedString
.attributedStringWithAttributes(
115 UserDateFormatterFactory
.get().getLongFormat(timestamp
.getTime()), TIMESTAMP_FONT_ATTRIBUTES
));
118 item
.setAttributedTitle(NSAttributedString
.attributedStringWithAttributes(
119 LocaleFactory
.localizedString("Unknown"), TIMESTAMP_FONT_ATTRIBUTES
));
123 else if(index
.intValue() == size
* 2) {
124 // How to change an existing item to a separator item?
125 item
.setTitle(StringUtils
.EMPTY
);
126 item
.setTarget(null);
127 item
.setAction(null);
129 item
.setEnabled(false);
131 else if(index
.intValue() == size
* 2 + 1) {
132 item
.setTitle(LocaleFactory
.localizedString("Clear Menu"));
133 item
.setAction(Foundation
.selector("clearMenuItemClicked:"));
134 item
.setTarget(this.id());
135 item
.setEnabled(true);
137 return super.menuUpdateItemAtIndex(menu
, item
, index
, cancel
);
140 public void historyMenuItemClicked(NSMenuItem sender
) {
141 log
.debug("historyMenuItemClicked:" + sender
);
142 BrowserController controller
= MainController
.newDocument();
143 controller
.mount(collection
.lookup(sender
.representedObject()));
146 public void clearMenuItemClicked(NSMenuItem sender
) {
147 // Delete all bookmark files
152 protected Selector
getDefaultAction() {
153 return Foundation
.selector("historyMenuItemClicked:");