Let WebLink alter image and name depending on what it links from.

By returning a WebLinkInfo from WebLinks instead of the four separate
values, plugins implementing the extension points:
PatchSetWeblink, FileWeblink, BranchWeblink and/or ProjectWeblink
can alter imageUrl, linkName and target depending on what it links
from.

Change-Id: I23716673713db3955d9f1743dee95a87a9db2978
This commit is contained in:
Sven Selberg 2014-09-24 10:52:21 +02:00 committed by Edwin Kempin
parent 76235c76e6
commit a85e64d411
9 changed files with 65 additions and 94 deletions

View File

@ -1753,6 +1753,7 @@ PatchSetWebLinks will appear to the right of the commit-SHA1 in the UI.
----
import com.google.gerrit.extensions.annotations.Listen;
import com.google.gerrit.extensions.webui.PatchSetWebLink;;
import com.google.gerrit.extensions.webui.WebLinkTarget;
@Listen
public class MyWeblinkPlugin implements PatchSetWebLink {
@ -1762,23 +1763,11 @@ public class MyWeblinkPlugin implements PatchSetWebLink {
private String imageUrl = "http://placehold.it/16x16.gif";
@Override
public String getLinkName() {
return name;
}
@Override
public String getPatchSetUrl(String project, String commit) {
return String.format(placeHolderUrlProjectCommit, project, commit);
}
@Override
public String getImageUrl() {
return imageUrl;
}
@Override
public String getTarget() {
return "_blank";
public WebLinkInfo getPathSetWebLink(String projectName, String commit) {
return new WebLinkInfo(name,
imageUrl,
String.format(placeHolderUrlProjectCommit, project, commit),
WebLinkTarget.BLANK);
}
}
----

View File

@ -15,16 +15,17 @@
package com.google.gerrit.extensions.webui;
import com.google.gerrit.extensions.annotations.ExtensionPoint;
import com.google.gerrit.extensions.common.WebLinkInfo;
@ExtensionPoint
public interface BranchWebLink extends WebLink {
public interface BranchWebLink {
/**
* URL to branch in external service.
*
* @param projectName Name of the project
* @param branchName Name of the branch
* @return url to branch in external service.
* @return WebLinkInfo that links to branch in external service.
*/
String getBranchUrl(String projectName, String branchName);
WebLinkInfo getBranchWebLink(String projectName, String branchName);
}

View File

@ -15,9 +15,10 @@
package com.google.gerrit.extensions.webui;
import com.google.gerrit.extensions.annotations.ExtensionPoint;
import com.google.gerrit.extensions.common.WebLinkInfo;
@ExtensionPoint
public interface FileWebLink extends WebLink {
public interface FileWebLink {
/**
* URL to file in external service.
@ -25,7 +26,7 @@ public interface FileWebLink extends WebLink {
* @param projectName Name of the project
* @param revision Name of the revision (e.g. branch or commit ID)
* @param fileName Name of the file
* @return url to project in external service.
* @return WebLinkInfo that links to project in external service.
*/
String getFileUrl(String projectName, String revision, String fileName);
WebLinkInfo getFileWebLink(String projectName, String revision, String fileName);
}

View File

@ -14,16 +14,17 @@
package com.google.gerrit.extensions.webui;
import com.google.gerrit.extensions.annotations.ExtensionPoint;
import com.google.gerrit.extensions.common.WebLinkInfo;
@ExtensionPoint
public interface PatchSetWebLink extends WebLink {
public interface PatchSetWebLink {
/**
* URL to patch set in external service.
*
* @param projectName Name of the project
* @param commit Commit of the patch set
* @return url to patch set in external service.
* @return WebLinkInfo that links to patch set in external service.
*/
String getPatchSetUrl(final String projectName, final String commit);
WebLinkInfo getPathSetWebLink(final String projectName, final String commit);
}

View File

@ -15,15 +15,16 @@
package com.google.gerrit.extensions.webui;
import com.google.gerrit.extensions.annotations.ExtensionPoint;
import com.google.gerrit.extensions.common.WebLinkInfo;
@ExtensionPoint
public interface ProjectWebLink extends WebLink {
public interface ProjectWebLink {
/**
* URL to project in external service.
*
* @param projectName Name of the project
* @return url to project in external service.
* @return WebLinkInfo that links to project in external service.
*/
String getProjectUrl(String projectName);
WebLinkInfo getProjectWeblink(String projectName);
}

View File

@ -1,45 +0,0 @@
// Copyright (C) 2014 The Android Open Source Project
//
// 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.google.gerrit.extensions.webui;
public interface WebLink {
public static class Target {
public final static String BLANK = "_blank";
public final static String SELF = "_self";
public final static String PARENT = "_parent";
public final static String TOP = "_top";
}
/**
* The link-name displayed in UI.
*
* @return name of link or title of the link if image URL is available.
*/
String getLinkName();
/**
* URL of image to be displayed
*
* @return URL to image for link or null for using a text-only link.
* Recommended image size is 16x16.
*/
String getImageUrl();
/**
* Target window in which the link should be opened (e.g. "_blank", "_self".).
*
* @return link target, if null the link is opened in the current window
*/
String getTarget();
}

View File

@ -0,0 +1,36 @@
// Copyright (C) 2014 The Android Open Source Project
//
// 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.google.gerrit.extensions.webui;
/**
* Class that holds target defaults for WebLink anchors.
*/
public class WebLinkTarget {
/**
* Opens the link in a new window or tab
*/
public final static String BLANK = "_blank";
/**
* Opens the link in the frame it was clicked.
*/
public final static String SELF = "_self";
/**
* Opens link in parent frame.
*/
public final static String PARENT = "_parent";
/**
* Opens link in the full body of the window.
*/
public final static String TOP = "_top";
}

View File

@ -50,10 +50,7 @@ public class WebLinks {
public List<WebLinkInfo> getPatchSetLinks(String project, String commit) {
List<WebLinkInfo> links = new ArrayList<>(4);
for (PatchSetWebLink webLink : patchSetLinks) {
links.add(new WebLinkInfo(webLink.getLinkName(),
webLink.getImageUrl(),
webLink.getPatchSetUrl(project, commit),
webLink.getTarget()));
links.add(webLink.getPathSetWebLink(project, commit));
}
return links;
}
@ -62,13 +59,9 @@ public class WebLinks {
String file) {
List<WebLinkInfo> links = new ArrayList<>(4);
for (FileWebLink webLink : fileLinks) {
String name = webLink.getLinkName();
String url = webLink.getFileUrl(project, revision, file);
if (!Strings.isNullOrEmpty(name) && !Strings.isNullOrEmpty(url)) {
links.add(new WebLinkInfo(name,
webLink.getImageUrl(),
url,
webLink.getTarget()));
WebLinkInfo info = webLink.getFileWebLink(project, revision, file);
if (!Strings.isNullOrEmpty(info.name) && !Strings.isNullOrEmpty(info.url)) {
links.add(info);
}
}
return links;
@ -77,10 +70,7 @@ public class WebLinks {
public Iterable<WebLinkInfo> getProjectLinks(String project) {
List<WebLinkInfo> links = Lists.newArrayList();
for (ProjectWebLink webLink : projectLinks) {
links.add(new WebLinkInfo(webLink.getLinkName(),
webLink.getImageUrl(),
webLink.getProjectUrl(project),
webLink.getTarget()));
links.add(webLink.getProjectWeblink(project));
}
return links;
}
@ -88,10 +78,7 @@ public class WebLinks {
public Iterable<WebLinkInfo> getBranchLinks(String project, String branch) {
List<WebLinkInfo> links = Lists.newArrayList();
for (BranchWebLink webLink : branchLinks) {
links.add(new WebLinkInfo(webLink.getLinkName(),
webLink.getImageUrl(),
webLink.getBranchUrl(project, branch),
webLink.getTarget()));
links.add(webLink.getBranchWebLink(project, branch));
}
return links;
}

@ -1 +1 @@
Subproject commit a2e56f0f76dac45a5084c28a27e24ba039b57e09
Subproject commit 15efdb1f7e6e5f6e4db2791b8753cd57e6d5c790