/** * */ package org.gcube.portlets.widgets.githubconnector.client.wizard; import java.util.ArrayList; import org.gcube.portlets.widgets.githubconnector.client.resource.GCResources; import org.gcube.portlets.widgets.githubconnector.client.util.GWTMessages; import com.google.gwt.core.client.Callback; import com.google.gwt.core.client.GWT; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.NativeEvent; import com.google.gwt.dom.client.Node; import com.google.gwt.dom.client.Style.Float; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.logical.shared.ResizeEvent; import com.google.gwt.event.logical.shared.ResizeHandler; import com.google.gwt.event.shared.EventBus; import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.event.shared.SimpleEventBus; import com.google.gwt.user.client.Command; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.Event.NativePreviewEvent; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.DeckPanel; import com.google.gwt.user.client.ui.DialogBox; import com.google.gwt.user.client.ui.DockPanel; import com.google.gwt.user.client.ui.FlowPanel; import com.google.gwt.user.client.ui.Widget; /** * * @author "Giancarlo Panichi" g.panichi@isti.cnr.it * */ public class WizardWindow extends DialogBox { private HandlerRegistration resizeHandlerRegistration; protected boolean WIZARD_RESIZABLE = false; protected boolean WIZARD_COLLAPSIBLE = true; protected ArrayList listeners; protected EventBus eventBus; protected String title; protected ArrayList cardStack = new ArrayList(); protected Button backButton; protected Button nextButton; protected String originalTitle; protected boolean checkBeforeClose = true; protected boolean nextCardFinish = false; protected Command nextButtonAction = null; protected Command previousButtonAction = null; protected DockPanel dockPanel; protected FlowPanel moveToolBar; // protected FillToolItem fillSpacingCardMoveToolBar; protected WizardMessages msgs; protected DeckPanel deckPanel; /** * Create a new Wizard Window with the specified title. * * @param title * the wizard window title. */ public WizardWindow(String title) { this(title, new SimpleEventBus()); } public WizardWindow(String title, EventBus eventBus) { super(); GWT.log("WizardWindow"); this.title = title; this.eventBus = eventBus; this.msgs = GWT.create(WizardMessages.class); initWindow(); initHandler(); addToolIcon(); listeners = new ArrayList(); deckPanel = new DeckPanel(); backButton = new Button("
" + "" + msgs.buttonBackLabel() + "" + "
"); backButton.setEnabled(false); backButton.setTabIndex(1001); backButton.getElement().getStyle().setFloat(Float.LEFT); backButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { if (previousButtonAction != null) previousButtonAction.execute(); else previousCard(); } }); nextButton = new Button("
" + msgs.buttonNextLabel() + "" + "
"); nextButton.setEnabled(false); nextButton.setTabIndex(1002); nextButton.getElement().getStyle().setFloat(Float.RIGHT); nextButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { if (nextButtonAction != null) nextButtonAction.execute(); else nextCard(); } }); moveToolBar = new FlowPanel(); moveToolBar.setWidth("100%"); moveToolBar.add(backButton); moveToolBar.add(nextButton); dockPanel = new DockPanel(); dockPanel.setSpacing(4); dockPanel.add(deckPanel, DockPanel.CENTER); dockPanel.add(moveToolBar, DockPanel.SOUTH); dockPanel.setWidth("100%"); setWidget(dockPanel); center(); } private void initHandler() { resizeHandlerRegistration = Window .addResizeHandler(new ResizeHandler() { @Override public void onResize(ResizeEvent event) { center(); } }); } protected void initWindow() { GWT.log(title); setModal(true); setGlassEnabled(true); setAnimationEnabled(true); setText(title); this.originalTitle = title; } public void addListener(WizardListener listener) { listeners.add(listener); } public void removeListener(WizardListener listener) { listeners.remove(listener); } /** * Shows the next available card. */ public void nextCard() { int index = deckPanel.getVisibleWidget(); Widget activeItem = deckPanel.getWidget(index); if (activeItem instanceof WizardCard) ((WizardCard) activeItem).dispose(); int cardPos = cardStack.indexOf(activeItem); // NEXT -> nextButton.setEnabled(true); backButton.setEnabled(true); int newPos = cardPos + 1; if (newPos == 0) { // we are moving forward from the first card backButton.setEnabled(false); } nextButtonAction = null; previousButtonAction = null; GWT.log("cardStack size:" + cardStack.size()); WizardCard card = cardStack.get(newPos); deckPanel.add(card); int showIndex = deckPanel.getWidgetIndex(card); deckPanel.showWidget(showIndex); if (card instanceof WizardCard) ((WizardCard) card).setup(); } /** * Shows the previous available card. */ public void previousCard() { int index = deckPanel.getVisibleWidget(); Widget activeItem = deckPanel.getWidget(index); if (activeItem instanceof WizardCard) ((WizardCard) activeItem).dispose(); int cardPos = cardStack.indexOf(activeItem); // BACK <- nextButton.setEnabled(true); backButton.setEnabled(true); int newPos = cardPos - 1; if (newPos == 0) { backButton.setEnabled(false); } nextButtonAction = null; previousButtonAction = null; WizardCard card = cardStack.get(newPos); deckPanel.add(card); int showIndex = deckPanel.getWidgetIndex(card); deckPanel.showWidget(showIndex); if (card instanceof WizardCard) ((WizardCard) card).setup(); } /** * Returns the number of available cards. * * @return */ public int getCardStackSize() { return cardStack.size(); } /** * Returns the current active card. * * @return */ public int getCurrentCard() { int index = deckPanel.getVisibleWidget(); Widget activeItem = deckPanel.getWidget(index); return cardStack.indexOf(activeItem); } public boolean checkBeforeClose() { return true; } public void close(boolean check) { checkBeforeClose = check; hide(); } /** * Sets the label of next button to "Finish" value and add a close command * to it. */ public void setNextButtonToFinish() { nextButton = new Button("
" + msgs.buttonFinishLabel() + "" + "
"); // nextButton.setIcon(GCResources.INSTANCE.wizardGo()); // nextButton.setIconAlign(IconAlign.RIGHT); nextButtonAction = new Command() { public void execute() { close(false); } }; } /** * Set the command for the next button. * * @param command * the command to execute. */ public void setNextButtonCommand(Command command) { nextButtonAction = command; } /** * Set the command for the previous button. * * @param command * the command to execute. */ public void setPreviousButtonCommand(Command command) { previousButtonAction = command; } /** * {@inheritDoc} */ @Override public void show() { super.show(); int index = deckPanel.getVisibleWidget(); GWT.log("Visible widget: " + index); Widget activeItem; if (index == -1) { if (deckPanel.getWidgetCount() > 0) { deckPanel.showWidget(0); activeItem = deckPanel.getWidget(0); if (activeItem instanceof WizardCard) ((WizardCard) activeItem).setup(); backButton.setEnabled(false); center(); } else { backButton.setEnabled(false); nextButton.setEnabled(false); } } else { deckPanel.showWidget(index); activeItem = deckPanel.getWidget(index); if (activeItem instanceof WizardCard) ((WizardCard) activeItem).setup(); } } @Override public void hide() { if (resizeHandlerRegistration != null) { resizeHandlerRegistration.removeHandler(); resizeHandlerRegistration = null; } super.hide(); } /** * Set the card list. * * @param cards */ public void setCards(ArrayList cards) { for (WizardCard card : cards) { addCard(card); } } /** * Adds a card to this wizard. * * @param card * the card to add. */ public void addCard(WizardCard card) { card.setWizardWindow(this); if (deckPanel.getVisibleWidget() > -1) { int index = deckPanel.getVisibleWidget(); deckPanel.remove(index); } deckPanel.add(card); cardStack.add(card); } /** * Remove a card to this wizard. * * @param card * the card to add. */ public void removeCard(WizardCard card) { deckPanel.remove(card); cardStack.remove(card); } /** * Enables the next button on the wizard. * * @param enable * true to enable the next button, * false otherwise. */ public void setEnableNextButton(boolean enable) { nextButton.setEnabled(enable); } /** * Enables the back button on the wizard. * * @param enable * true to enable the back button, * false otherwise. */ public void setEnableBackButton(boolean enable) { backButton.setEnabled(enable); } /** * Sets the next button label. * * @param text * the button label. */ protected void setNextButtonText(String text) { nextButton.setText(text); } /** * Sets the back button label. * * @param text * the button label. */ protected void setBackButtonText(String text) { backButton.setText(text); } /** * Sets visible next button. * * @param visible */ protected void setNextButtonVisible(boolean visible) { nextButton.setVisible(visible); } /** * Sets visible back button. * * @param visible */ protected void setBackButtonVisible(boolean visible) { backButton.setVisible(visible); } /** * Add a listener to the next button. * * @param listener * the listener to add. */ protected void addNextButtonListener(ClickHandler listener) { nextButton.addClickHandler(listener); } /** * @return the originalTitle */ public String getOriginalTitle() { return originalTitle; } /** * Returns the card list. * * @return teh card list. */ public ArrayList getCardStack() { return cardStack; } public void showErrorAndHide(final String title, final String message, final String details, final Throwable throwable) { GWTMessages.alert(title, message + " " + details, new Callback() { @Override public void onFailure(Void reason) { hide(); } @Override public void onSuccess(Void result) { hide(); } }); } private Node closeEventTarget = null; private void addToolIcon() { // get the "dialogTopRight" class td Element dialogTopRight = getCellElement(0, 2); // close button image html dialogTopRight.setInnerHTML("
" + "
"); // set the event target closeEventTarget = dialogTopRight.getChild(0).getChild(0); } @Override protected void onPreviewNativeEvent(NativePreviewEvent event) { NativeEvent nativeEvent = event.getNativeEvent(); if (!event.isCanceled() && (event.getTypeInt() == Event.ONCLICK) && isCloseEvent(nativeEvent)) { this.hide(); } super.onPreviewNativeEvent(event); } // see if the click target is the close button private boolean isCloseEvent(NativeEvent event) { return event.getEventTarget().equals(closeEventTarget); // compares // equality of // the // underlying // DOM elements } }