1 package ch
.cyberduck
.ui
.cocoa
;
4 * Copyright (c) 2008 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
.core
.DescriptiveUrl
;
22 import ch
.cyberduck
.core
.Local
;
23 import ch
.cyberduck
.binding
.application
.NSColor
;
24 import ch
.cyberduck
.binding
.application
.NSFont
;
25 import ch
.cyberduck
.binding
.foundation
.NSAttributedString
;
26 import ch
.cyberduck
.binding
.foundation
.NSMutableAttributedString
;
27 import ch
.cyberduck
.binding
.foundation
.NSNumber
;
28 import ch
.cyberduck
.binding
.foundation
.NSRange
;
30 import org
.apache
.commons
.lang3
.StringUtils
;
31 import org
.rococoa
.cocoa
.foundation
.NSUInteger
;
34 * From http://developer.apple.com/qa/qa2006/qa1487.html
38 public final class HyperlinkAttributedStringFactory
{
40 private HyperlinkAttributedStringFactory() {
44 public static NSAttributedString
create(final DescriptiveUrl url
) {
45 if(url
.equals(DescriptiveUrl
.EMPTY
)) {
46 return NSAttributedString
.attributedString(StringUtils
.EMPTY
);
48 return create(url
.getUrl());
53 * @return Clickable and underlined string to put into textfield.
55 public static NSAttributedString
create(final String url
) {
57 return NSAttributedString
.attributedString(StringUtils
.EMPTY
);
59 return create(url
, url
);
62 public static NSAttributedString
create(final String title
, final Local file
) {
64 return NSAttributedString
.attributedString(title
);
66 return create(NSMutableAttributedString
.create(title
,
67 BundleController
.TRUNCATE_MIDDLE_ATTRIBUTES
), file
.getAbsolute());
70 public static NSAttributedString
create(final String title
, final String url
) {
72 return NSAttributedString
.attributedString(title
);
74 return create(NSMutableAttributedString
.create(title
,
75 BundleController
.TRUNCATE_MIDDLE_ATTRIBUTES
), url
);
79 * @param value Existing attributes
80 * @param hyperlink URL
81 * @return Clickable and underlined string to put into text field.
83 private static NSAttributedString
create(final NSMutableAttributedString value
, final String hyperlink
) {
84 final NSRange range
= NSRange
.NSMakeRange(new NSUInteger(0), value
.length());
86 value
.addAttributeInRange(NSMutableAttributedString
.LinkAttributeName
,
88 // make the text appear in blue
89 value
.addAttributeInRange(NSMutableAttributedString
.ForegroundColorAttributeName
,
90 NSColor
.blueColor(), range
);
92 value
.addAttributeInRange(NSMutableAttributedString
.FontAttributeName
,
93 NSFont
.systemFontOfSize(NSFont
.smallSystemFontSize()), range
);
94 // next make the text appear with an underline
95 value
.addAttributeInRange(NSMutableAttributedString
.UnderlineStyleAttributeName
,
96 NSNumber
.numberWithInt(NSMutableAttributedString
.SingleUnderlineStyle
), range
);