James Moger
2013-05-02 d5ee557ef1370b5b9953dca1c8d3b14d0bd68a98
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright 2013 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitblit.wicket.panels;
 
import java.text.MessageFormat;
 
import org.apache.wicket.Component;
import org.apache.wicket.Localizer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.ContextImage;
import org.apache.wicket.markup.html.panel.Fragment;
 
import com.gitblit.Constants.AccessPermission;
import com.gitblit.GitBlit;
import com.gitblit.Keys;
import com.gitblit.utils.StringUtils;
import com.gitblit.wicket.WicketUtils;
 
public class DetailedRepositoryUrlPanel extends BasePanel {
 
    private static final long serialVersionUID = 1L;
    public DetailedRepositoryUrlPanel(String wicketId, Localizer localizer, Component parent, String repository, String url) {
        this(wicketId, localizer, parent, repository, url, null);
    }
    
    public DetailedRepositoryUrlPanel(String wicketId, Localizer localizer, Component parent, String repository, String url, AccessPermission ap) {
        super(wicketId);
        
        String protocol = url.substring(0, url.indexOf(':'));
        String note;
        String permission;
        
        if (ap == null) {
            note = MessageFormat.format(localizer.getString("gb.externalPermissions", parent), protocol, repository);
            permission = "";
        } else {
            note = null;
            permission = ap.toString();
            String key;
            switch (ap) {
                case OWNER:
                case REWIND:
                    key = "gb.rewindPermission";
                    break;
                case DELETE:
                    key = "gb.deletePermission";
                    break;
                case CREATE:
                    key = "gb.createPermission";
                    break;
                case PUSH:
                    key = "gb.pushPermission";
                    break;
                case CLONE:
                    key = "gb.clonePermission";
                    break;
                default:
                    key = null;
                    note = localizer.getString("gb.viewAccess", parent);
                    break;
            }
            
            if (note == null) {
                String pattern = localizer.getString(key, parent);
                String description = MessageFormat.format(pattern, permission);
                String permissionPattern = localizer.getString("gb.yourProtocolPermissionIs", parent);
                note = MessageFormat.format(permissionPattern, protocol.toUpperCase(), repository, description);
            }
        }
        
        if (!StringUtils.isEmpty(url) && ((ap == null) || ap.atLeast(AccessPermission.CLONE))) {
            // valid repository url
            Fragment fragment = new Fragment("urlPanel", "repositoryUrlPanel", this);
            add(fragment);
            Label protocolLabel = new Label("repositoryProtocol", protocol + "://");
            WicketUtils.setHtmlTooltip(protocolLabel, note);
            fragment.add(protocolLabel);
            fragment.add(new Label("repositoryUrl", url.substring(url.indexOf("://") + 3)));
            Label permissionLabel = new Label("repositoryUrlPermission", permission);
            WicketUtils.setHtmlTooltip(permissionLabel, note);
            fragment.add(permissionLabel);
 
            if (StringUtils.isEmpty(url)) {
                fragment.add(new Label("copyFunction").setVisible(false));
            } else if (GitBlit.getBoolean(Keys.web.allowFlashCopyToClipboard, true)) {
                // clippy: flash-based copy & paste
                Fragment copyFragment = new Fragment("copyFunction", "clippyPanel", this);
                String baseUrl = WicketUtils.getGitblitURL(getRequest());
                ShockWaveComponent clippy = new ShockWaveComponent("clippy", baseUrl + "/clippy.swf");
                clippy.setValue("flashVars", "text=" + StringUtils.encodeURL(url));
                copyFragment.add(clippy);
                fragment.add(copyFragment);
            } else {
                // javascript: manual copy & paste with modal browser prompt dialog
                Fragment copyFragment = new Fragment("copyFunction", "jsPanel", this);
                ContextImage img = WicketUtils.newImage("copyIcon", "clippy.png");
                img.add(new JavascriptTextPrompt("onclick", "Copy to Clipboard (Ctrl+C, Enter)", url));
                copyFragment.add(img);
                fragment.add(copyFragment);
            }
        } else {
            // no Git url, there may be a message
            add(new Label("urlPanel", MessageFormat.format("<i>{0}</i>", note)).setEscapeModelStrings(false).setVisible(!StringUtils.isEmpty(note)));
        }
    }
}