下面列出了怎么用com.vaadin.server.ExternalResource的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void attach() {
super.attach();
service = createService();
String url = service.getAuthorizationUrl(null);
gplusLoginButton = new Link("Login with Google", new ExternalResource(url));
gplusLoginButton.addStyleName(ValoTheme.LINK_LARGE);
VaadinSession.getCurrent().addRequestHandler(this);
setContent(new MVerticalLayout(gplusLoginButton).alignAll(
Alignment.MIDDLE_CENTER).withFullHeight());
setModal(true);
setWidth("300px");
setHeight("200px");
}
public AboutWindow() {
MHorizontalLayout content = new MHorizontalLayout().withMargin(true).withFullWidth();
this.setContent(content);
Image about = new Image("", new ExternalResource(StorageUtils.generateAssetRelativeLink(WebResourceIds._about)));
MVerticalLayout rightPanel = new MVerticalLayout();
ELabel versionLbl = ELabel.h2(String.format("MyCollab Community Edition %s", Version.getVersion())).withFullWidth();
ELabel javaNameLbl = new ELabel(String.format("%s, %s", System.getProperty("java.vm.name"),
System.getProperty("java.runtime.version"))).withFullWidth();
Label homeFolderLbl = new Label("Home folder: " + AppContextUtil.getSpringBean(ServerConfiguration.class).getHomeDir().getAbsolutePath());
WebBrowser browser = Page.getCurrent().getWebBrowser();
ELabel osLbl = new ELabel(String.format("%s, %s", System.getProperty("os.name"), browser.getBrowserApplication())).withFullWidth();
Div licenseDiv = new Div().appendChild(new Text("Powered by: "))
.appendChild(new A("https://www.mycollab.com")
.appendText("MyCollab")).appendChild(new Text(". Open source under GPL license"));
ELabel licenseLbl = ELabel.html(licenseDiv.write()).withFullWidth();
Label copyRightLbl = ELabel.html(String.format("© %s - %s MyCollab Ltd. All rights reserved", "2011",
LocalDate.now().getYear() + "")).withFullWidth();
rightPanel.with(versionLbl, javaNameLbl, osLbl, homeFolderLbl, licenseLbl, copyRightLbl).withAlign(copyRightLbl, Alignment.BOTTOM_LEFT);
content.with(about, rightPanel).expand(rightPanel);
}
public static Component projectLogoComp(String projectShortname, Integer projectId, String projectAvatarId, int size) {
AbstractComponent wrapper;
if (!StringUtils.isBlank(projectAvatarId)) {
wrapper = new Image(null, new ExternalResource(StorageUtils.getResourcePath
(String.format("%s/%s_%d.png", PathUtils.getProjectLogoPath(AppUI.getAccountId(), projectId),
projectAvatarId, size))));
} else {
ELabel projectIcon = new ELabel(projectShortname.substring(0, 1)).withStyleName(WebThemes.TEXT_ELLIPSIS, ValoTheme.LABEL_LARGE, "center");
projectIcon.setWidth(size, Sizeable.Unit.PIXELS);
projectIcon.setHeight(size, Sizeable.Unit.PIXELS);
wrapper = new MVerticalLayout(projectIcon).withAlign(projectIcon, Alignment.MIDDLE_CENTER).withMargin(false);
}
wrapper.setWidth(size, Sizeable.Unit.PIXELS);
wrapper.setHeight(size, Sizeable.Unit.PIXELS);
wrapper.addStyleName(WebThemes.CIRCLE_BOX);
wrapper.setDescription(UserUIContext.getMessage(GenericI18Enum.OPT_CHANGE_IMAGE));
return wrapper;
}
public static Component clientLogoComp(Client client, int size) {
AbstractComponent wrapper;
if (!StringUtils.isBlank(client.getAvatarid())) {
wrapper = new Image(null, new ExternalResource(StorageUtils.getEntityLogoPath(AppUI.getAccountId(), client.getAvatarid(), 100)));
} else {
String clientName = client.getName();
clientName = (clientName.length() > 3) ? clientName.substring(0, 3) : clientName;
ELabel projectIcon = new ELabel(clientName).withStyleName(WebThemes.TEXT_ELLIPSIS, "center");
wrapper = new VerticalLayout();
((VerticalLayout) wrapper).addComponent(projectIcon);
((VerticalLayout) wrapper).setComponentAlignment(projectIcon, Alignment.MIDDLE_CENTER);
}
wrapper.setWidth(size, Sizeable.Unit.PIXELS);
wrapper.setHeight(size, Sizeable.Unit.PIXELS);
wrapper.addStyleName(WebThemes.CIRCLE_BOX);
wrapper.setDescription(UserUIContext.getMessage(GenericI18Enum.OPT_CHANGE_IMAGE));
return wrapper;
}
public AccountModuleImpl() {
addStyleName("module");
ControllerRegistry.addController(new UserAccountController(this));
MHorizontalLayout topPanel = new MHorizontalLayout().withFullWidth().withMargin(true).withStyleName(WebThemes.BORDER_BOTTOM).withId("tab-content-header");
AccountSettingBreadcrumb breadcrumb = ViewManager.getCacheComponent(AccountSettingBreadcrumb.class);
MButton helpBtn = new MButton(UserUIContext.getMessage(GenericI18Enum.ACTION_HELP))
.withIcon(VaadinIcons.ACADEMY_CAP).withStyleName(WebThemes.BUTTON_LINK);
ExternalResource helpRes = new ExternalResource("https://docs.mycollab.com/user-guide/account-management/");
BrowserWindowOpener helpOpener = new BrowserWindowOpener(helpRes);
helpOpener.extend(helpBtn);
topPanel.with(breadcrumb, helpBtn).withAlign(helpBtn, Alignment.TOP_RIGHT);
tabSheet = new VerticalTabsheet();
tabSheet.getContentWrapper().addStyleName("content-height");
tabSheet.setSizeFull();
tabSheet.addToggleNavigatorControl();
CssLayout contentWrapper = tabSheet.getContentWrapper();
contentWrapper.addComponentAsFirst(topPanel);
this.buildComponents();
this.setContent(tabSheet);
}
private void initLayoutHeader() {
headerLayout.setWidth("100%");
headerLayout.setHeight("80px");
headerLayout.setId("header");
Link rdfunit = new Link("",
new ExternalResource("http://rdfunit.aksw.org/"));
rdfunit.setIcon(new ThemeResource("images/logo-rdfunit.png"));
headerLayout.addComponent(rdfunit);
Link aksw = new Link("",
new ExternalResource("http://aksw.org/"));
aksw.setIcon(new ThemeResource("images/logo-aksw.png"));
aksw.addStyleName("align-right");
headerLayout.addComponent(aksw);
}
@Override
public String getUrl() {
Resource resource = component.getResource();
if (resource instanceof ExternalResource)
return ((ExternalResource) resource).getURL();
return null;
}
@Override
public void setMimeType(String mimeType) {
this.mimeType = mimeType;
if (resource != null) {
((ExternalResource) resource).setMIMEType(mimeType);
}
}
@Override
protected void createResource() {
try {
URL context = new URL(ControllerUtils.getLocationWithoutParams());
resource = new ExternalResource(new URL(context, path));
} catch (MalformedURLException e) {
throw new RuntimeException("Can't create RelativePathResource", e);
}
}
@Override
public void setMimeType(String mimeType) {
this.mimeType = mimeType;
if (resource != null) {
((ExternalResource) resource).setMIMEType(mimeType);
}
}
@Override
public Link addCommitteePageLink(final ViewRiksdagenCommittee data) {
final Link pageLink = new Link(COMMITTEE
+ data.getEmbeddedId().getDetail(), new ExternalResource(PAGE_PREFIX
+ UserViews.COMMITTEE_VIEW_NAME + PAGE_SEPARATOR + data.getEmbeddedId().getOrgCode()));
pageLink.setId(ViewAction.VISIT_COMMITTEE_VIEW.name() + PAGE_SEPARATOR
+ data.getEmbeddedId().getOrgCode());
pageLink.setIcon(VaadinIcons.GROUP);
return pageLink;
}
@Override
public Link addMinistryPageLink(final ViewRiksdagenMinistry data) {
final Link pageLink = new Link(MINISTRY + data.getNameId(),
new ExternalResource(PAGE_PREFIX + UserViews.MINISTRY_VIEW_NAME + PAGE_SEPARATOR
+ data.getNameId()));
pageLink.setId(ViewAction.VISIT_MINISTRY_VIEW.name() + PAGE_SEPARATOR
+ data.getNameId());
pageLink.setIcon(VaadinIcons.GROUP);
return pageLink;
}
@Override
public Link addPartyPageLink(final ViewRiksdagenParty data) {
final Link pageLink = new Link(PARTY + data.getPartyName(),
new ExternalResource(PAGE_PREFIX + UserViews.PARTY_VIEW_NAME + PAGE_SEPARATOR
+ data.getPartyId()));
pageLink.setId(ViewAction.VISIT_PARTY_VIEW.name() + PAGE_SEPARATOR
+ data.getPartyId());
pageLink.setIcon(VaadinIcons.GROUP);
return pageLink;
}
@Override
public Link createAdminPagingLink(final String label,final String page, final String pageId, final String pageNr) {
final Link pageLink = new Link(label,
new ExternalResource(PAGE_PREFIX + page + PAGE_SEPARATOR
+ "[" + pageNr + "]"));
pageLink.setId(page +"ShowPage" + PAGE_SEPARATOR
+ pageNr);
pageLink.setIcon(VaadinIcons.SERVER);
return pageLink;
}
@Override
public Link createLoginPageLink() {
final Link pageLink = new Link("Login", new ExternalResource(
LINK_SEPARATOR + CommonsViews.MAIN_VIEW_NAME + PAGE_SEPARATOR + ApplicationPageMode.LOGIN));
pageLink.setId(ViewAction.VISIT_LOGIN.name());
pageLink.setIcon(VaadinIcons.SIGN_IN);
return pageLink;
}
@Override
public Link createMainViewPageLink() {
final Link pageLink = new Link(MAIN_VIEW_LINK_TEXT, new ExternalResource(
LINK_SEPARATOR + CommonsViews.MAIN_VIEW_NAME));
pageLink.setId(ViewAction.VISIT_MAIN_VIEW.name());
pageLink.setIcon(VaadinIcons.STAR);
return pageLink;
}
@Override
public Link createPoliticianPageLink(final PersonData personData) {
final Link pageLink = new Link(POLITICIAN
+ personData.getFirstName() + ' '
+ personData.getLastName(), new ExternalResource(PAGE_PREFIX
+ UserViews.POLITICIAN_VIEW_NAME + PAGE_SEPARATOR + personData.getId()));
pageLink.setId(ViewAction.VISIT_POLITICIAN_VIEW.name() + PAGE_SEPARATOR
+ personData.getId());
pageLink.setIcon(VaadinIcons.BUG);
return pageLink;
}
@Override
public Link createRegisterPageLink() {
final Link pageLink = new Link("Register", new ExternalResource(
LINK_SEPARATOR + CommonsViews.MAIN_VIEW_NAME + PAGE_SEPARATOR + ApplicationPageMode.REGISTER));
pageLink.setId(ViewAction.VISIT_REGISTER.name());
pageLink.setIcon(VaadinIcons.RANDOM);
return pageLink;
}
@Override
public Link createUserHomeViewPageLink() {
final Link pageLink = new Link("User account:", new ExternalResource(PAGE_PREFIX
+ UserViews.USERHOME_VIEW_NAME));
pageLink.setId(ViewAction.VISIT_USER_HOME_VIEW.name());
pageLink.setIcon(VaadinIcons.USER);
return pageLink;
}
@Override
public void build(String title, MyBeanItem<? extends Object> beanItem)
{
super.build(title, beanItem);
// add link to capabilities
Property<?> endPointProp = beanItem.getItemProperty(PROP_ENDPOINT);
if (endPointProp != null)
{
String baseUrl = ((String)endPointProp.getValue()).substring(1);
String href = baseUrl + "?service=SOS&version=2.0&request=GetCapabilities";
Link link = new Link("Link to capabilities", new ExternalResource(href), "_blank", 0, 0, null);
this.addComponent(link, 0);
}
}
public static Component editableProjectLogoComp(String projectShortname, Integer projectId, String projectAvatarId, int size) {
MCssLayout wrapper = new MCssLayout();
if (CurrentProjectVariables.canWrite(ProjectRolePermissionCollections.PROJECT)) {
wrapper.addStyleName(WebThemes.CURSOR_POINTER);
wrapper.setDescription(UserUIContext.getMessage(GenericI18Enum.OPT_CHANGE_IMAGE));
wrapper.addLayoutClickListener((LayoutEvents.LayoutClickListener) layoutClickEvent ->
UI.getCurrent().addWindow(new ProjectLogoUploadWindow(projectShortname, projectId, projectAvatarId))
);
}
if (!StringUtils.isBlank(projectAvatarId)) {
Image image = new Image(null, new ExternalResource(StorageUtils.getResourcePath
(String.format("%s/%s_%d.png", PathUtils.getProjectLogoPath(AppUI.getAccountId(), projectId),
projectAvatarId, size))));
image.addStyleName(WebThemes.CIRCLE_BOX);
wrapper.addComponent(image);
} else {
ELabel projectIcon = new ELabel(projectShortname.substring(0, 1)).withStyleName(WebThemes.TEXT_ELLIPSIS, ValoTheme.LABEL_LARGE, "center", WebThemes.CIRCLE_BOX).withDescription(projectShortname);
projectIcon.setWidth(size, Sizeable.Unit.PIXELS);
projectIcon.setHeight(size, Sizeable.Unit.PIXELS);
wrapper.addComponent(projectIcon);
}
wrapper.setWidth(size, Sizeable.Unit.PIXELS);
wrapper.setHeight(size, Sizeable.Unit.PIXELS);
return wrapper;
}
@Override
public void sourceGenerationStarted(final Source source, TestGenerationType generationType) {
UI.getCurrent().access(() -> {
Link uriLink = new Link(source.getUri(), new ExternalResource(source.getUri()));
uriLink.setTargetName("_blank");
resultsTable.addItem(new Object[]{
source.getClass().getSimpleName(), uriLink, new Label("-"), new Label("-")}, source);
resultsTable.setCurrentPageFirstItemIndex(resultsTable.getCurrentPageFirstItemIndex() + 1);
CommonAccessUtils.pushToClient();
});
}
private void initLayout() {
this.setMargin(true);
this.addComponent(new Label("<h2>RDFUnit - A Unit Testing Suite for RDF</h2>", ContentMode.HTML));
this.addComponent(new Label(
"<p><strong>Welcome to the RDFUnit Demo</strong></p>" +
"<p>RDFUnit is a testing framework that can verify your data against a schema / vocabulary or custom SPARQL test cases.</p>" +
"<p>Please note that you cannot use all features of RDFUnit from this UI. For example, you cannot define your own tests cases or test SPARQL Endpoints directly. " +
"For more thorough testing please try the command line (CLI) version.</p>",
ContentMode.HTML));
Link homepage = new Link("Homepage", new ExternalResource("http://rdfunit.aksw.org/"));
Link github = new Link("Github page", new ExternalResource("https://github.com/AKSW/RDFUnit"));
Link report = new Link("Report", new ExternalResource("http://svn.aksw.org/papers/2014/WWW_Databugger/public.pdf"));
HorizontalLayout links = new HorizontalLayout();
this.addComponent(links);
links.setDefaultComponentAlignment(Alignment.BOTTOM_LEFT);
links.addComponent(new Label("To learn more about RDFUnit you can navigate to our: ", ContentMode.HTML));
links.addComponent(homepage);
links.addComponent(new Label(" / "));
links.addComponent(github);
links.addComponent(new Label(" / "));
links.addComponent(report);
}
@Override
public void setUrl(String url) {
component.setResource(new ExternalResource(url));
}
@Override
protected void createResource() {
resource = new ExternalResource(url);
((ExternalResource) resource).setMIMEType(mimeType);
}
/**
* Method to create a link.
*
* @param id
* of the link
* @param name
* of the link
* @param resource
* path of the link
* @param icon
* of the link
* @param targetOpen
* specify how the link should be open (f. e. new windows = _blank)
* @param style
* chosen style of the link. Might be {@code null} if no style should
* be used
* @return a link UI component
*/
public static Link getLink(final String id, final String name, final String resource, final FontAwesome icon,
final String targetOpen, final String style) {
final Link link = new Link(name, new ExternalResource(resource));
link.setId(id);
link.setIcon(icon);
link.setDescription(name);
link.setTargetName(targetOpen);
if (style != null) {
link.setStyleName(style);
}
return link;
}
@Secured({ "ROLE_ADMIN" })
@Override
public Layout createContent(final String parameters, final MenuBar menuBar, final Panel panel) {
final VerticalLayout content = createPanelContent();
final String pageId = getPageId(parameters);
getMenuItemFactory().createMainPageMenuBar(menuBar);
final BrowserFrame browser = new BrowserFrame(ADMIN_MONITORING, new ExternalResource(MONITORING_CONTEXT_PATH));
browser.setSizeFull();
content.addComponent(browser);
content.setExpandRatio(browser, ContentRatio.FULL_SIZE);
getPageActionEventHelper().createPageEvent(ViewAction.VISIT_ADMIN_MONITORING_VIEW, ApplicationEventGroup.ADMIN,
NAME, null, pageId);
return content;
}
private void buildShortcutIconPanel() {
FormContainer formContainer = new FormContainer();
MHorizontalLayout layout = new MHorizontalLayout().withFullWidth().withMargin(new MarginInfo(true, false, true, false));
MVerticalLayout leftPanel = new MVerticalLayout().withMargin(false);
ELabel logoDesc = ELabel.html(UserUIContext.getMessage(FileI18nEnum.OPT_FAVICON_FORMAT_DESCRIPTION)).withFullWidth();
leftPanel.with(logoDesc).withWidth("250px");
MVerticalLayout rightPanel = new MVerticalLayout().withMargin(false);
final Image favIconRes = new Image("", new ExternalResource(StorageUtils.getFavIconPath(billingAccount.getId(),
billingAccount.getFaviconpath())));
MHorizontalLayout buttonControls = new MHorizontalLayout().withMargin(new MarginInfo(true, false, false, false));
buttonControls.setDefaultComponentAlignment(Alignment.BOTTOM_LEFT);
final UploadField favIconUploadField = new UploadField() {
private static final long serialVersionUID = 1L;
@Override
protected void updateDisplayComponent() {
byte[] imageData = this.getValue();
String mimeType = this.getLastMimeType();
if (mimeType.equals("image/jpeg")) {
imageData = ImageUtil.convertJpgToPngFormat(imageData);
if (imageData == null) {
throw new UserInvalidInputException(UserUIContext.getMessage(FileI18nEnum.ERROR_INVALID_SUPPORTED_IMAGE_FORMAT));
} else {
mimeType = "image/png";
}
}
if (mimeType.equals("image/png")) {
try {
AccountFavIconService favIconService = AppContextUtil.getSpringBean(AccountFavIconService.class);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageData));
String newFavIconPath = favIconService.upload(UserUIContext.getUsername(), image, AppUI.getAccountId());
favIconRes.setSource(new ExternalResource(StorageUtils.getFavIconPath(billingAccount.getId(),
newFavIconPath)));
UIUtils.reloadPage();
} catch (IOException e) {
throw new MyCollabException(e);
}
} else {
throw new UserInvalidInputException(UserUIContext.getMessage(FileI18nEnum.ERROR_UPLOAD_INVALID_SUPPORTED_IMAGE_FORMAT));
}
}
};
favIconUploadField.setButtonCaption(UserUIContext.getMessage(GenericI18Enum.ACTION_CHANGE));
favIconUploadField.addStyleName("upload-field");
favIconUploadField.setSizeUndefined();
favIconUploadField.setVisible(UserUIContext.canBeYes(RolePermissionCollections.ACCOUNT_THEME));
MButton resetButton = new MButton(UserUIContext.getMessage(GenericI18Enum.BUTTON_RESET), clickEvent -> {
BillingAccountService billingAccountService = AppContextUtil.getSpringBean(BillingAccountService.class);
billingAccount.setFaviconpath(null);
billingAccountService.updateWithSession(billingAccount, UserUIContext.getUsername());
UIUtils.reloadPage();
}).withStyleName(WebThemes.BUTTON_OPTION);
resetButton.setVisible(UserUIContext.canBeYes(RolePermissionCollections.ACCOUNT_THEME));
buttonControls.with(resetButton, favIconUploadField);
rightPanel.with(favIconRes, buttonControls);
layout.with(leftPanel, rightPanel).expand(rightPanel);
formContainer.addSection("Favicon", layout);
this.with(formContainer);
}
public UrlLink(String urlLink) {
this.setResource(new ExternalResource(urlLink));
this.setCaption(urlLink);
this.setTargetName("_blank");
}
/**
* Generates help/documentation links from within management UI.
*
* @param i18n
* the i18n
* @param uri
* to documentation site
*
* @return generated link
*/
public static Link getHelpLink(final VaadinMessageSource i18n, final String uri) {
final Link link = new Link("", new ExternalResource(uri));
link.setTargetName("_blank");
link.setIcon(FontAwesome.QUESTION_CIRCLE);
link.setDescription(i18n.getMessage("tooltip.documentation.link"));
return link;
}