Java tutorial
package com.r1soft.backup.server.web.dashboard; import org.joda.time.DateTime; import org.joda.time.Days; import org.zkoss.zk.ui.Components; import org.zkoss.zk.ui.Page; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zk.ui.event.Events; import org.zkoss.zkplus.databind.AnnotateDataBinder; import org.zkoss.zul.A; import org.zkoss.zul.Caption; import org.zkoss.zul.Cell; import org.zkoss.zul.Div; import org.zkoss.zul.Hbox; import org.zkoss.zul.Label; import com.r1soft.backup.server.facade.LicenseFacade; import com.r1soft.backup.server.facade.LicenseInfoSpec; import com.r1soft.backup.server.i18n.I18NStrings; import com.r1soft.backup.server.om.PersistenceException; import com.r1soft.backup.server.web.ELFunctions; import com.r1soft.backup.server.web.EventConstants; import com.r1soft.backup.server.web.GlobalUpdater; import com.r1soft.backup.server.web.session.SessionUtil; public class LicenseStatusPortlet extends DashboardPortlet { private static final long serialVersionUID = 1L; private static final String ERROR_SCLASS = "r1-dashboard-large-bold-text r1-bold-red"; private static final String NEUTRAL_SCLASS = "r1-dashboard-large-bold-text"; private static final String EXPANDED_STATE_KEY = "licenseStatusDashboardWidgetExpandedState"; LicenseInfoSpec licenseInfo = null; protected boolean expanded = false; protected EventListener eventListener = new EventListener() { @Override public void onEvent(Event event) throws Exception { if (event.getData() instanceof LicenseInfoSpec) { LicenseStatusPortlet.this.licenseInfo = (LicenseInfoSpec) event.getData(); updateLicenseInfo(); } } }; @Override public void onCreate() { super.onCreate(); Components.wireVariables(this, this); Components.addForwards(this, this); licenseInfo = LicenseFacade.getLicenseInfoSpec(); String expandedState = (String) SessionUtil.getUserData(EXPANDED_STATE_KEY); if (expandedState != null) expanded = Boolean.parseBoolean(expandedState); updateLicenseInfo(); GlobalUpdater.subscribeToEventQueue(EventConstants.LICENSING_EVENT_QUEUE_NAME, eventListener); } @Override public void onPageDetached(Page page) { super.onPageDetached(page); GlobalUpdater.unsubscribeFromEventQueue(EventConstants.LICENSING_EVENT_QUEUE_NAME, eventListener); } public void onClick$licenseNowButton(Event event) { Events.postEvent(new Event("onActivationSetup", null, null)); } @Override protected String getPanelTitleI18NKey() { if (licenseInfo == null) licenseInfo = LicenseFacade.getLicenseInfoSpec(); return licenseInfo.getTrial() ? I18NStrings.TRIAL_TIME_LEFT : I18NStrings.LICENSES; } @Override protected Caption buildCaption() { if (licenseInfo == null) licenseInfo = LicenseFacade.getLicenseInfoSpec(); if (binder == null) binder = new AnnotateDataBinder(this); Caption caption = new Caption(); caption.setHflex("1"); Div titleDiv = new Div(); titleDiv.setZclass(CAPTION_TITLE_ZCLASS); titleDiv.setHflex("1"); Hbox hbox = new Hbox(); hbox.setHflex("1"); hbox.setPack("stretch"); Label titleLabel = new Label(); hbox.appendChild(titleLabel); binder.addBinding(titleLabel, "value", "titleLabel"); binder.bindBean("titleLabel", ELFunctions.getLabel(getPanelTitleI18NKey())); String expandedState = (String) SessionUtil.getUserData(EXPANDED_STATE_KEY); if (expandedState != null) expanded = Boolean.parseBoolean(expandedState); final A expandLink = new A(ELFunctions.getLabel(expanded ? I18NStrings.LESS : I18NStrings.MORE)); expandLink.addEventListener("onClick", new EventListener() { @Override public void onEvent(Event arg0) throws Exception { expanded = !expanded; try { SessionUtil.setUserData(EXPANDED_STATE_KEY, Boolean.toString(expanded)); } catch (PersistenceException e) { // Don't actually care, user just won't have the same expanded state } expandLink.setLabel(ELFunctions.getLabel(expanded ? I18NStrings.LESS : I18NStrings.MORE)); binder.bindBean("expanded", expanded); binder.loadAll(); } }); binder.addBinding(expandLink, "visible", "panelOpen"); Cell expandLinkCell = new Cell(); expandLinkCell.setAlign("right"); expandLinkCell.appendChild(expandLink); hbox.appendChild(expandLinkCell); titleDiv.appendChild(hbox); caption.appendChild(titleDiv); return caption; } protected void updateLicenseInfo() { binder.bindBean("expanded", expanded); binder.bindBean("titleLabel", ELFunctions.getLabel(getPanelTitleI18NKey())); if (licenseInfo.getTrial()) { DateTime expiry = new DateTime(licenseInfo.getExpireDate()); DateTime now = new DateTime(); binder.bindBean("daysLeft", Days.daysBetween(new DateTime(now), new DateTime(expiry)).getDays()); } int physicalServersRemainingCount = licenseInfo.getLicenseAgentCount() - licenseInfo.getCurrentAgentCount(); double physicalServerRatio = physicalServersRemainingCount > 0 ? (((double) licenseInfo.getCurrentAgentCount())) / ((double) licenseInfo.getLicenseAgentCount()) * 100D : 100D; binder.bindBean("physicalServersCount", licenseInfo.getCurrentAgentCount()); binder.bindBean("physicalServersSclass", (licenseInfo.getCurrentAgentCount() > licenseInfo.getLicenseAgentCount() ? ERROR_SCLASS : NEUTRAL_SCLASS)); binder.bindBean("physicalServersRemainingCount", physicalServersRemainingCount); binder.bindBean("physicalServersRemainingSclass", physicalServersRemainingCount < 0 ? "r1-bold-red" : "r1-bold"); binder.bindBean("physicalServersRemainingLabelSclass", physicalServersRemainingCount < 0 ? "r1-bold-red" : ""); binder.bindBean("physicalServerRatio", physicalServerRatio); binder.bindBean("physicalServerRatioZclass", physicalServersRemainingCount < 0 ? "r1-dual-progressmeter-solid-red" : "r1-dual-progressmeter-green-blue"); int virtualServersRemainingCount = licenseInfo.getLicenseVirtualAgentCount() - licenseInfo.getCurrentVirtualAgentCount(); double virtualServerRatio = virtualServersRemainingCount > 0 ? (((double) licenseInfo.getCurrentVirtualAgentCount())) / ((double) licenseInfo.getLicenseVirtualAgentCount()) * 100D : 100D; binder.bindBean("virtualServersCount", licenseInfo.getCurrentVirtualAgentCount()); binder.bindBean("virtualServersSclass", (licenseInfo.getCurrentVirtualAgentCount() > licenseInfo.getLicenseVirtualAgentCount() ? ERROR_SCLASS : NEUTRAL_SCLASS)); binder.bindBean("virtualServersRemainingCount", virtualServersRemainingCount); binder.bindBean("virtualServersRemainingSclass", virtualServersRemainingCount < 0 ? "r1-bold-red" : "r1-bold"); binder.bindBean("virtualServersRemainingLabelSclass", virtualServersRemainingCount < 0 ? "r1-bold-red" : ""); binder.bindBean("virtualServerRatio", virtualServerRatio); binder.bindBean("virtualServerRatioZclass", virtualServersRemainingCount < 0 ? "r1-dual-progressmeter-solid-red" : "r1-dual-progressmeter-green-blue"); binder.bindBean("spec", licenseInfo); binder.loadAll(); } }