Java tutorial
/* * Copyright 2010, 2011 Renaud Brub * * This file is part of PIGE. * * PIGE 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 3 of the License, or * (at your option) any later version. * * PIGE 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 PIGE. If not, see <http://www.gnu.org/licenses/>. */ package ca.qc.cegepoutaouais.tge.pige.client.ui.dialog; import ca.qc.cegepoutaouais.tge.pige.client.ui.AppEvents; import ca.qc.cegepoutaouais.tge.pige.client.ui.AppWidgets; import ca.qc.cegepoutaouais.tge.pige.client.services.ManagementServiceAsync; import ca.qc.cegepoutaouais.tge.pige.client.PIGE; import ca.qc.cegepoutaouais.tge.pige.client.ui.widget.StatusBar; import ca.qc.cegepoutaouais.tge.pige.dao.pojos.Role; import com.extjs.gxt.ui.client.Registry; import com.extjs.gxt.ui.client.event.ComponentEvent; import com.extjs.gxt.ui.client.event.KeyListener; import com.extjs.gxt.ui.client.mvc.Dispatcher; import com.extjs.gxt.ui.client.widget.Html; import com.extjs.gxt.ui.client.widget.form.LabelField; import com.extjs.gxt.ui.client.widget.form.TextArea; import com.extjs.gxt.ui.client.widget.form.TextField; import com.extjs.gxt.ui.client.widget.layout.RowLayout; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.CheckBox; import com.google.gwt.user.client.ui.FlexTable; import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter; import com.google.gwt.user.client.ui.HasHorizontalAlignment; /** * * @author Renaud Brub */ public class RoleCEVDialog extends AbstractCEVDialog { private Html nameLbl = null; private LabelField descriptionLbl = null; private LabelField manageRoleLbl = null; private LabelField manageUserLbl = null; private LabelField manageLoanLbl = null; private LabelField appManagementLbl = null; private LabelField manageMessageLbl = null; private LabelField manageInventoryLbl = null; private LabelField canLoanLbl = null; private LabelField browseInventoryLbl = null; private LabelField adminInventoryLbl = null; private LabelField requireRoleManagerLbl = null; private Html requiredFieldLbl = null; private TextField<String> nameTF = null; private TextArea descriptionTA = null; private CheckBox manageRoleCBx = null; private CheckBox manageUserCBx = null; private CheckBox manageLoanCBx = null; private CheckBox appManagementCBx = null; private CheckBox manageMessageCBx = null; private CheckBox manageInventoryCBx = null; private CheckBox canLoanCBx = null; private CheckBox browseInventoryCBx = null; /*private CheckBox adminInventoryCBx = null;*/ private CheckBox requireRoleManagerCBx = null; private FlexTable table = null; public RoleCEVDialog() { buildUI(); } private void buildUI() { setLayout(new RowLayout()); setModal(true); setDraggable(true); setCollapsible(false); setClosable(true); setAutoHide(false); setBodyStyle("padding: 3px"); setWidth(465); table = new FlexTable(); nameLbl = new Html(messages.name()); descriptionLbl = new LabelField(messages.description()); manageRoleLbl = new LabelField(messages.manageRole()); manageUserLbl = new LabelField(messages.manageUser()); manageLoanLbl = new LabelField(messages.manageLoan()); manageMessageLbl = new LabelField(messages.manageMessage()); canLoanLbl = new LabelField(messages.canLoan()); browseInventoryLbl = new LabelField(messages.browseInventory()); manageInventoryLbl = new LabelField(messages.manageInventory()); adminInventoryLbl = new LabelField(messages.adminInventory()); appManagementLbl = new LabelField(messages.appManagement()); requireRoleManagerLbl = new LabelField("[Assignation] " + messages.requireRoleManagement()); requiredFieldLbl = new Html(messages.requiredField()); nameLbl.addStyleName("common-form-label"); descriptionLbl.addStyleName("common-form-label"); manageRoleLbl.addStyleName("common-form-label"); manageUserLbl.addStyleName("common-form-label"); manageLoanLbl.addStyleName("common-form-label"); appManagementLbl.addStyleName("common-form-label"); manageMessageLbl.addStyleName("common-form-label"); manageInventoryLbl.addStyleName("common-form-label"); canLoanLbl.addStyleName("common-form-label"); browseInventoryLbl.addStyleName("common-form-label"); adminInventoryLbl.addStyleName("common-form-label"); requireRoleManagerLbl.addStyleName("common-form-label"); requiredFieldLbl.addStyleName("common-label-required-field-note"); KeyListener keyListener = new KeyListener() { @Override public void componentKeyUp(ComponentEvent event) { validate(); } }; nameTF = new TextField(); nameTF.setWidth(235); nameTF.addKeyListener(keyListener); nameTF.setMaxLength(30); nameTF.setEmptyText(messages.requiredField()); nameTF.setAllowBlank(Boolean.FALSE); nameTF.setMaxLength(30); descriptionTA = new TextArea(); descriptionTA.setWidth(235); descriptionTA.setHeight(75); descriptionTA.setMaxLength(1000); manageRoleCBx = new CheckBox(); manageUserCBx = new CheckBox(); manageLoanCBx = new CheckBox(); appManagementCBx = new CheckBox(); manageMessageCBx = new CheckBox(); manageInventoryCBx = new CheckBox(); canLoanCBx = new CheckBox(); browseInventoryCBx = new CheckBox(); //adminInventoryCBx = new CheckBox(); requireRoleManagerCBx = new CheckBox(); FlexCellFormatter fcf = table.getFlexCellFormatter(); int r = 0; table.setWidget(r, 0, nameLbl); table.setWidget(r++, 1, nameTF); table.setWidget(r, 0, descriptionLbl); table.setWidget(r++, 1, descriptionTA); table.setWidget(r, 0, manageRoleLbl); table.setWidget(r++, 1, manageRoleCBx); table.setWidget(r, 0, manageUserLbl); table.setWidget(r++, 1, manageUserCBx); table.setWidget(r, 0, manageLoanLbl); table.setWidget(r++, 1, manageLoanCBx); table.setWidget(r, 0, manageMessageLbl); table.setWidget(r++, 1, manageMessageCBx); table.setWidget(r, 0, canLoanLbl); table.setWidget(r++, 1, canLoanCBx); table.setWidget(r, 0, browseInventoryLbl); table.setWidget(r++, 1, browseInventoryCBx); table.setWidget(r, 0, manageInventoryLbl); table.setWidget(r++, 1, manageInventoryCBx); table.setWidget(r, 0, adminInventoryLbl); //table.setWidget(r++, 1, adminInventoryCBx); table.setWidget(r, 0, appManagementLbl); table.setWidget(r++, 1, appManagementCBx); table.setWidget(r, 0, requireRoleManagerLbl); table.setWidget(r++, 1, requireRoleManagerCBx); fcf.setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT); fcf.setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_LEFT); fcf.setHorizontalAlignment(2, 1, HasHorizontalAlignment.ALIGN_LEFT); fcf.setHorizontalAlignment(3, 1, HasHorizontalAlignment.ALIGN_LEFT); fcf.setHorizontalAlignment(4, 1, HasHorizontalAlignment.ALIGN_LEFT); fcf.setHorizontalAlignment(5, 1, HasHorizontalAlignment.ALIGN_LEFT); fcf.setHorizontalAlignment(6, 1, HasHorizontalAlignment.ALIGN_LEFT); fcf.setHorizontalAlignment(7, 1, HasHorizontalAlignment.ALIGN_LEFT); fcf.setHorizontalAlignment(8, 1, HasHorizontalAlignment.ALIGN_LEFT); fcf.setHorizontalAlignment(9, 1, HasHorizontalAlignment.ALIGN_LEFT); fcf.setHorizontalAlignment(10, 1, HasHorizontalAlignment.ALIGN_LEFT); fcf.setHorizontalAlignment(11, 1, HasHorizontalAlignment.ALIGN_LEFT); add(table); } @Override protected void setReadOnly(Boolean b) { nameTF.setReadOnly(b); descriptionTA.setReadOnly(b); manageRoleCBx.setEnabled(!b); manageUserCBx.setEnabled(!b); manageLoanCBx.setEnabled(!b); appManagementCBx.setEnabled(!b); manageMessageCBx.setEnabled(!b); manageInventoryCBx.setEnabled(!b); canLoanCBx.setEnabled(!b); browseInventoryCBx.setEnabled(!b); //adminInventoryCBx.setEnabled(!b); requireRoleManagerCBx.setEnabled(!b); } @Override protected void setupWindow() { if (cevType.equals(CEV.VIEW)) { setHeadingText(messages.viewRole() + " (" + messages.readOnly() + ")"); } else if (cevType.equals(CEV.EDIT)) { setHeadingText(messages.editRole()); } else if (cevType.equals(CEV.CREATE)) { setHeadingText(messages.newRole()); } } @Override protected void fillFields() { Role role = getData("role"); if (role != null) { nameTF.setValue(role.getName()); descriptionTA.setValue(role.getDescription()); manageRoleCBx.setValue(role.getManageRole()); manageUserCBx.setValue(role.getManageUser()); manageLoanCBx.setValue(role.getManageLoan()); appManagementCBx.setValue(role.getAppManagement()); manageMessageCBx.setValue(role.getManageMessage()); manageInventoryCBx.setValue(role.getManageInventory()); canLoanCBx.setValue(role.getCanLoan()); browseInventoryCBx.setValue(role.getBrowseInventory()); /*adminInventoryCBx.setValue(role.getAdminInventory());*/ requireRoleManagerCBx.setValue(role.getRequireRoleManagement()); } } @Override protected void clearFields() { nameTF.clear(); descriptionTA.clear(); manageRoleCBx.setValue(false); manageUserCBx.setValue(false); manageLoanCBx.setValue(false); appManagementCBx.setValue(false); manageMessageCBx.setValue(false); manageInventoryCBx.setValue(false); canLoanCBx.setValue(false); browseInventoryCBx.setValue(false); /*adminInventoryCBx.setValue(false);*/ requireRoleManagerCBx.setValue(false); setFocusWidget(nameTF); } @Override protected void validate() { if (cevType != CEV.VIEW) { boolean isValid = hasValue(nameTF) && nameTF.validate(); if (cevType == CEV.CREATE) { createBtn.setEnabled(isValid); } else { updateBtn.setEnabled(isValid); } } } @Override protected void create() { Role role = new Role(); role.setName(nameTF.getValue()); role.setDescription(descriptionTA.getValue()); role.setManageRole(manageRoleCBx.getValue()); role.setManageUser(manageUserCBx.getValue()); role.setManageLoan(manageLoanCBx.getValue()); role.setManageMessage(manageMessageCBx.getValue()); role.setCanLoan(canLoanCBx.getValue()); role.setManageInventory(manageInventoryCBx.getValue()); role.setBrowseInventory(browseInventoryCBx.getValue()); //role.setAdminInventory(adminInventoryCBx.getValue()); role.setAppManagement(appManagementCBx.getValue()); role.setRequireRoleManagement(requireRoleManagerCBx.getValue()); /* * Envoyer la demande de cration du rle au serveur. */ ManagementServiceAsync rpcService = Registry.get(PIGE.MANAGEMENT_SERVICE); rpcService.createRole(role, new AsyncCallback() { @Override public void onFailure(Throwable caught) { PIGE.handleException(caught); } @Override public void onSuccess(Object o) { clearFields(); StatusBar statusBar = Registry.get(AppWidgets.APP_STATUS_BAR); statusBar.setTimedText(messages.roleCreatedSuccessfully()); Dispatcher.get().dispatch(AppEvents.ROLE_MANAGEMENT_UPDATE_EVENT); } }); } @Override protected void update() { Role role = getData("role"); if (role == null) { return; } Role newRole = new Role(); newRole.setId(role.getId()); newRole.setName(nameTF.getValue()); newRole.setDescription(descriptionTA.getValue()); newRole.setManageRole(manageRoleCBx.getValue()); newRole.setManageUser(manageUserCBx.getValue()); newRole.setManageLoan(manageLoanCBx.getValue()); newRole.setManageMessage(manageMessageCBx.getValue()); newRole.setCanLoan(canLoanCBx.getValue()); newRole.setManageInventory(manageInventoryCBx.getValue()); newRole.setBrowseInventory(browseInventoryCBx.getValue()); //newRole.setAdminInventory(adminInventoryCBx.getValue()); newRole.setAppManagement(appManagementCBx.getValue()); newRole.setRequireRoleManagement(requireRoleManagerCBx.getValue()); /* * Envoyer la demande de mise jour du rle au serveur. */ ManagementServiceAsync rpcService = Registry.get(PIGE.MANAGEMENT_SERVICE); rpcService.updateRole(newRole, new AsyncCallback() { @Override public void onFailure(Throwable caught) { PIGE.handleException(caught); } @Override public void onSuccess(Object o) { clearFields(); StatusBar statusBar = Registry.get(AppWidgets.APP_STATUS_BAR); statusBar.setTimedText(messages.roleUpdatedSuccesfully()); Dispatcher.get().dispatch(AppEvents.ROLE_MANAGEMENT_UPDATE_EVENT); } }); } }