github-connector/src/main/java/org/gcube/portlets/widgets/githubconnector/client/wizard/WizardWindow.java

490 lines
10 KiB
Java

/**
*
*/
package org.gcube.portlets.widgets.githubconnector.client.wizard;
import java.util.ArrayList;
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.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.Command;
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.MenuBar;
import com.google.gwt.user.client.ui.Widget;
/**
*
* @author "Giancarlo Panichi" <a
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class WizardWindow extends DialogBox {
protected boolean WIZARD_RESIZABLE = false;
protected boolean WIZARD_COLLAPSIBLE = true;
protected String title;
protected ArrayList<WizardCard> cardStack = new ArrayList<WizardCard>();
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 ArrayList<WizardListener> listeners;
protected EventBus eventBus;
protected MenuBar cardMoveToolBar;
// 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();
listeners = new ArrayList<WizardListener>();
deckPanel=new DeckPanel();
cardMoveToolBar = new MenuBar();
// Create a command that will execute on menu item selection
ScheduledCommand menuCommandBack = new ScheduledCommand() {
public void execute() {
if (previousButtonAction != null)
previousButtonAction.execute();
else
previousCard();
}
};
ScheduledCommand menuCommandNext = new ScheduledCommand() {
public void execute() {
if (nextButtonAction != null)
nextButtonAction.execute();
else
nextCard();
}
};
// cardMoveToolBar.setSpacing(2);
// cardMoveToolBar.addStyleName(ThemeStyles.get().style().borderTop());
backButton = new Button(msgs.buttonBackLabel());
// backButton.setIcon(GCResources.INSTANCE.wizardPrevious());
backButton.setEnabled(false);
backButton.setTabIndex(1001);
SafeHtml backItem=new SafeHtmlBuilder().appendEscaped(
msgs.buttonBackLabel()).toSafeHtml();
cardMoveToolBar.addItem(backItem,menuCommandBack);
nextButton = new Button(msgs.buttonNextLabel());
// backButton.setIcon(GCResources.INSTANCE.wizardPrevious());
nextButton.setEnabled(false);
nextButton.setTabIndex(1002);
SafeHtml nextItem=new SafeHtmlBuilder().appendEscaped(
msgs.buttonNextLabel()).toSafeHtml();
cardMoveToolBar.addItem(nextItem,menuCommandNext);
dockPanel = new DockPanel();
dockPanel.setSpacing(4);
dockPanel.add(deckPanel , DockPanel.CENTER);
dockPanel.add(cardMoveToolBar, DockPanel.SOUTH);
dockPanel.setWidth("100%");
setWidget(dockPanel);
}
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.setText(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);
} else {
backButton.setEnabled(false);
nextButton.setEnabled(false);
}
} else {
deckPanel.showWidget(index);
activeItem=deckPanel.getWidget(index);
if (activeItem instanceof WizardCard)
((WizardCard) activeItem).setup();
}
}
/**
* Set the card list.
*
* @param cards
*/
public void setCards(ArrayList<WizardCard> 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
* <code>true</code> to enable the next button,
* <code>false</code> otherwise.
*/
public void setEnableNextButton(boolean enable) {
nextButton.setEnabled(enable);
}
/**
* Enables the back button on the wizard.
*
* @param enable
* <code>true</code> to enable the back button,
* <code>false</code> 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<WizardCard> 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<Void, Void>() {
@Override
public void onFailure(Void reason) {
hide();
}
@Override
public void onSuccess(Void result) {
hide();
}
});
}
}