Java tutorial
/** * Copyright (C) 2010 BonitaSoft S.A. * BonitaSoft, 31 rue Gustave Eiffel - 38000 Grenoble * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2.0 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.bonitasoft.console.client.view.identity; import java.util.HashMap; import java.util.List; import org.bonitasoft.console.client.BonitaUUID; import org.bonitasoft.console.client.CSSClassManager; import org.bonitasoft.console.client.ItemUpdates; import org.bonitasoft.console.client.SimpleFilter; import org.bonitasoft.console.client.common.data.AsyncHandler; import org.bonitasoft.console.client.controller.ViewToken; import org.bonitasoft.console.client.events.ModelChangeEvent; import org.bonitasoft.console.client.i18n.ConsoleConstants; import org.bonitasoft.console.client.identity.Group; import org.bonitasoft.console.client.identity.GroupFilter; import org.bonitasoft.console.client.model.MessageDataSource; import org.bonitasoft.console.client.model.identity.GroupDataSource; import org.bonitasoft.console.client.view.AbstractItemList; import org.bonitasoft.console.client.view.CustomDialogBox; import org.bonitasoft.console.client.view.CustomMenuBar; import org.bonitasoft.console.client.view.GroupSelectorWidget; import org.bonitasoft.console.client.view.ItemFilterEditor; import org.bonitasoft.console.client.view.ItemSelectionWidget; import org.bonitasoft.console.client.view.SimpleFilterEditor; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.logical.shared.CloseEvent; import com.google.gwt.event.logical.shared.CloseHandler; import com.google.gwt.user.client.Command; import com.google.gwt.user.client.History; import com.google.gwt.user.client.ui.FlowPanel; import com.google.gwt.user.client.ui.HTMLTable.Cell; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.PopupPanel; /** * @author Nicolas Chabanoles * */ public class GroupsListWidget extends AbstractItemList<BonitaUUID, Group, GroupFilter> { protected CustomDialogBox addUpdateItemDialogBox; protected ConfirmationDialogbox confirmationDialogbox; protected final HashMap<BonitaUUID, ItemSelectionWidget<BonitaUUID>> myItemSelectionWidgets = new HashMap<BonitaUUID, ItemSelectionWidget<BonitaUUID>>(); protected final HashMap<BonitaUUID, Label> myItemNameWidgets = new HashMap<BonitaUUID, Label>(); protected final HashMap<BonitaUUID, Label> myItemLabelWidgets = new HashMap<BonitaUUID, Label>(); protected final HashMap<BonitaUUID, Label> myItemDescriptionWidgets = new HashMap<BonitaUUID, Label>(); protected final HashMap<BonitaUUID, Label> myItemPathWidgets = new HashMap<BonitaUUID, Label>(); /** * Default constructor. * * @param aRoleDataSource */ public GroupsListWidget(MessageDataSource aMessageDataSource, GroupDataSource aGroupDataSource) { super(aMessageDataSource, aGroupDataSource.getItemSelection(), aGroupDataSource, 20, 20, 5); myBonitaDataSource.addModelChangeListener(GroupDataSource.ITEM_LIST_PROPERTY, this); myBonitaDataSource.addModelChangeListener(GroupDataSource.ITEM_UPDATED_PROPERTY, this); newerButtonTop.setHTML(constants.previousPageLinkLabel()); olderButtonTop.setHTML(constants.nextPageLinkLabel()); newerButtonBottom.setHTML(constants.previousPageLinkLabel()); olderButtonBottom.setHTML(constants.nextPageLinkLabel()); initView(); createWidgetsForItemsAndDisplay(); addUpdateItemDialogBox = new CustomDialogBox(false, true); } @Override protected FlowPanel buildBottomNavBar() { return buildTopNavBar(); } @Override protected FlowPanel buildTopNavBar() { final FlowPanel theFirstCell = new FlowPanel(); final CustomMenuBar theTopMenu = new CustomMenuBar(); theTopMenu.addItem(constants.add(), new Command() { public void execute() { addItem(); } }); theTopMenu.addItem(constants.delete(), new Command() { public void execute() { //add a ConfirmationDialogbox when you delete Group. if (myItemSelection.getSize() > 0) { confirmationDialogbox = new ConfirmationDialogbox(constants.deleteGroupsDialogbox(), patterns.deleteGroupsWarn(myItemSelection.getSelectedItems().size()), constants.okButton(), constants.cancelButton()); confirmationDialogbox.addCloseHandler(new CloseHandler<PopupPanel>() { public void onClose(CloseEvent<PopupPanel> event) { if (confirmationDialogbox.getConfirmation()) { deleteSelectedItems(); } } }); } else { if (myMessageDataSource != null) { myMessageDataSource.addWarningMessage(messages.noGroupSelected()); } } } }); Label theRefreshLink = new Label(constants.refresh()); theRefreshLink.setStylePrimaryName(CSSClassManager.LINK_LABEL); theRefreshLink.addClickHandler(new ClickHandler() { public void onClick(ClickEvent aEvent) { myBonitaDataSource.reload(); } }); // Create the Selector Widget theFirstCell.add(new GroupSelectorWidget((GroupDataSource) myBonitaDataSource)); theFirstCell.add(theTopMenu); theFirstCell.add(theRefreshLink); return theFirstCell; } protected void deleteSelectedItems() { if (myItemSelection.getSize() > 0) { myBonitaDataSource.deleteItems(myItemSelection.getSelectedItems(), new AsyncHandler<ItemUpdates<Group>>() { public void handleFailure(Throwable anT) { myItemSelection.clearSelection(); } public void handleSuccess(ItemUpdates<Group> anResult) { myItemSelection.clearSelection(); } }); } else { myMessageDataSource.addWarningMessage(messages.noGroupSelected()); } } // protected void updateItem(final BonitaUUID anItem) { // if (anItem != null) { // final GroupEditorWidget theGroupEditorPanel = new GroupEditorWidget((GroupDataSource) myBonitaDataSource, anItem); // theGroupEditorPanel.addCancelClickHandler(new ClickHandler() { // // public void onClick(ClickEvent event) { // addUpdateItemDialogBox.hide(); // } // }); // theGroupEditorPanel.addSaveHandler(new AsyncHandler<Void>() { // // public void handleFailure(Throwable t) { // } // // public void handleSuccess(Void result) { // addUpdateItemDialogBox.hide(); // Integer theItemRow = myItemTableRow.get(anItem); // if (theItemRow != null) { // Group theItem = myBonitaDataSource.getItem(anItem); // myInnerTable.setWidget(theItemRow, 0, myItemSelectionWidgets.get(theItem.getUUID())); // myInnerTable.setWidget(theItemRow, 1, myItemNameWidgets.get(theItem.getUUID())); // myInnerTable.setWidget(theItemRow, 2, myItemLabelWidgets.get(theItem.getUUID())); // myInnerTable.setWidget(theItemRow, 3, myItemPathWidgets.get(theItem.getUUID())); // myInnerTable.setWidget(theItemRow, 4, myItemDescriptionWidgets.get(theItem.getUUID())); // } // } // }); // addUpdateItemDialogBox.clear(); // addUpdateItemDialogBox.add(theGroupEditorPanel); // addUpdateItemDialogBox.setText(constants.groupUpdate()); // addUpdateItemDialogBox.center(); // addUpdateItemDialogBox.show(); // theGroupEditorPanel.setFocus(); // } // } protected void addItem() { final GroupEditorWidget theGroupEditorPanel = new GroupEditorWidget((GroupDataSource) myBonitaDataSource, null); theGroupEditorPanel.addCancelClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { addUpdateItemDialogBox.hide(); } }); theGroupEditorPanel.addSaveHandler(new AsyncHandler<Void>() { public void handleFailure(Throwable t) { } public void handleSuccess(Void result) { addUpdateItemDialogBox.hide(); } }); addUpdateItemDialogBox.clear(); addUpdateItemDialogBox.add(theGroupEditorPanel); addUpdateItemDialogBox.setText(constants.groupCreation()); addUpdateItemDialogBox.center(); theGroupEditorPanel.setFocus(); } @Override protected void createWidgetsForItemsAndDisplay() { // Do not create object. Use HTML instead. myVisibleItems = ((GroupDataSource) myBonitaDataSource).getVisibleItems(); if (myVisibleItems != null) { hideLoading(); myInnerTable.removeStyleName(LOADING_STYLE); for (BonitaUUID theItemUUID : myVisibleItems) { if (!myItemTableRow.containsKey(theItemUUID)) { // theItem.addModelChangeListener(CaseItem.LABELS_PROPERTY, this); createWidgetsForItem(theItemUUID); } } // Update the UI. update(myVisibleItems); } else { displayLoading(); myInnerTable.addStyleName(LOADING_STYLE); } } protected void createWidgetsForItem(BonitaUUID anItem) { Group theItem = myBonitaDataSource.getItem(anItem); myItemSelectionWidgets.put(anItem, new ItemSelectionWidget<BonitaUUID>(myItemSelection, anItem)); myItemNameWidgets.put(anItem, new Label(theItem.getName())); myItemLabelWidgets.put(anItem, new Label(theItem.getLabel())); myItemDescriptionWidgets.put(anItem, new Label(theItem.getDescription())); myItemPathWidgets.put(anItem, new Label(Group.buildGroupPath(theItem))); } protected void updateWidgetsForItem(Group anItem) { myItemNameWidgets.get(anItem.getUUID()).setText(anItem.getName()); myItemLabelWidgets.get(anItem.getUUID()).setText(anItem.getLabel()); myItemDescriptionWidgets.get(anItem.getUUID()).setText(anItem.getDescription()); myItemPathWidgets.get(anItem.getUUID()).setText(Group.buildGroupPath(anItem)); } public void notifyItemClicked(BonitaUUID anItem, final ClickEvent anEvent) { Cell theCell = myInnerTable.getCellForEvent(anEvent); if (theCell.getCellIndex() > 0 && anItem != null) { History.newItem(ViewToken.GroupEditor.name() + ConsoleConstants.TOKEN_SEPARATOR + anItem.getValue()); } } @Override protected void update(List<BonitaUUID> anItemList) { updateListSize(anItemList); if (myTopNavBar != null && !myTopNavBar.isAttached()) { // Create the navigation row (Top). myInnerTable.setWidget(0, 0, myTopNavBar); myInnerTable.getFlexCellFormatter().setColSpan(0, 0, myColumnNumber); } // Add the column titles myInnerTable.setWidget(1, 1, new Label(constants.groupPathElement())); myInnerTable.setWidget(1, 2, new Label(constants.label())); myInnerTable.setWidget(1, 3, new Label(constants.groupPath())); myInnerTable.setWidget(1, 4, new Label(constants.description())); // Set CSS style. myInnerTable.getRowFormatter().setStylePrimaryName(1, ITEM_LIST_CONTENT_ROW_TITLE_STYLE); fillInContentRow(anItemList); if (myBottomNavBar != null && !myBottomNavBar.isAttached()) { // Create the navigation row (Bottom). int theBottomNavBarPosition = myInnerTable.getRowCount(); myInnerTable.setWidget(theBottomNavBarPosition, 0, myBottomNavBar); myInnerTable.getFlexCellFormatter().setColSpan(theBottomNavBarPosition, 0, myColumnNumber); } } protected void fillInContentRow(List<BonitaUUID> anItemList) { int theRowOffset = 2; int nbItemDisplayed = 0; int theCurrentRowIndex = theRowOffset + nbItemDisplayed; Group theItem; for (BonitaUUID theItemUUID : anItemList) { theCurrentRowIndex = theRowOffset + nbItemDisplayed; theItem = myBonitaDataSource.getItem(theItemUUID); // Add a new row to the table, then set each of its columns. // layout widgets myInnerTable.setWidget(theCurrentRowIndex, 0, myItemSelectionWidgets.get(theItem.getUUID())); myInnerTable.setWidget(theCurrentRowIndex, 1, myItemNameWidgets.get(theItem.getUUID())); myInnerTable.setWidget(theCurrentRowIndex, 2, myItemLabelWidgets.get(theItem.getUUID())); myInnerTable.setWidget(theCurrentRowIndex, 3, myItemPathWidgets.get(theItem.getUUID())); myInnerTable.setWidget(theCurrentRowIndex, 4, myItemDescriptionWidgets.get(theItem.getUUID())); // Set CSS style. if (theCurrentRowIndex % 2 == 0) { myInnerTable.getRowFormatter().setStylePrimaryName(theCurrentRowIndex, ITEM_LIST_CONTENT_ROW_STYLE); myInnerTable.getRowFormatter().addStyleName(theCurrentRowIndex, ITEM_LIST_CONTENT_ROW_STYLE + EVEN_STYLE_SUFFIX); } else { myInnerTable.getRowFormatter().setStylePrimaryName(theCurrentRowIndex, ITEM_LIST_CONTENT_ROW_STYLE); } myInnerTable.getRowFormatter().getElement(theCurrentRowIndex).setTitle(getContentRowTooltip()); // Keep link between the item and the row. linkItemWithRow(theItem.getUUID(), theCurrentRowIndex); nbItemDisplayed++; } fillWithEmptyRows(theRowOffset, theRowOffset + nbItemDisplayed, myColumnNumber); } protected String getContentRowTooltip() { return constants.clickToEdit(); } @Override public void modelChange(ModelChangeEvent anEvent) { // The event may come from a subscription made by my super class. super.modelChange(anEvent); if (GroupDataSource.ITEM_LIST_PROPERTY.equals(anEvent.getPropertyName())) { createWidgetsForItemsAndDisplay(); } else if (SimpleFilterEditor.FILTER_UPDATED_PROPERTY.equals(anEvent.getPropertyName())) { final String theSearchPattern = ((SimpleFilter) anEvent.getNewValue()).getSearchPattern(); myBonitaDataSource.getItemFilter().setSearchPattern(theSearchPattern); myBonitaDataSource.reload(); } else if (GroupDataSource.ITEM_UPDATED_PROPERTY.equals(anEvent.getPropertyName())) { BonitaUUID theItemUUID = (BonitaUUID) anEvent.getSource(); if (myItemSelectionWidgets.containsKey(theItemUUID)) { Group theItem = (Group) anEvent.getNewValue(); updateWidgetsForItem(theItem); } } } @Override protected ItemFilterEditor<GroupFilter> buildFilterEditor() { SimpleFilterEditor<GroupFilter> theEditor = new SimpleFilterEditor<GroupFilter>(myMessageDataSource, myBonitaDataSource.getItemFilter(), constants.filterGroupsToolTip()); theEditor.addModelChangeListener(SimpleFilterEditor.FILTER_UPDATED_PROPERTY, this); return theEditor; } }