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.MaintenanceType; import ca.qc.cegepoutaouais.tge.pige.client.MaintenanceType.MaintenanceTypeModel; import ca.qc.cegepoutaouais.tge.pige.client.PIGE; import ca.qc.cegepoutaouais.tge.pige.client.services.ManagementServiceAsync; 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.ui.widget.StatusBar; import ca.qc.cegepoutaouais.tge.pige.dao.pojos.Item; import ca.qc.cegepoutaouais.tge.pige.dao.pojos.Maintenance; import com.extjs.gxt.ui.client.Registry; import com.extjs.gxt.ui.client.Style.SelectionMode; import com.extjs.gxt.ui.client.data.BeanModel; import com.extjs.gxt.ui.client.data.BeanModelFactory; import com.extjs.gxt.ui.client.data.BeanModelLookup; import com.extjs.gxt.ui.client.event.BaseEvent; import com.extjs.gxt.ui.client.event.ComponentEvent; import com.extjs.gxt.ui.client.event.Events; import com.extjs.gxt.ui.client.event.FieldEvent; import com.extjs.gxt.ui.client.event.KeyListener; import com.extjs.gxt.ui.client.event.Listener; import com.extjs.gxt.ui.client.event.SelectionListener; import com.extjs.gxt.ui.client.mvc.Dispatcher; import com.extjs.gxt.ui.client.store.ListStore; import com.extjs.gxt.ui.client.widget.Html; import com.extjs.gxt.ui.client.widget.LayoutContainer; import com.extjs.gxt.ui.client.widget.MessageBox; import com.extjs.gxt.ui.client.widget.TabItem; import com.extjs.gxt.ui.client.widget.TabPanel; import com.extjs.gxt.ui.client.widget.form.CheckBox; import com.extjs.gxt.ui.client.widget.form.ComboBox; import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction; import com.extjs.gxt.ui.client.widget.form.DateField; import com.extjs.gxt.ui.client.widget.form.FormPanel; import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign; import com.extjs.gxt.ui.client.widget.form.ListField; import com.extjs.gxt.ui.client.widget.form.NumberField; 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.ColumnData; import com.extjs.gxt.ui.client.widget.layout.ColumnLayout; import com.extjs.gxt.ui.client.widget.layout.FitLayout; import com.extjs.gxt.ui.client.widget.layout.FormData; import com.extjs.gxt.ui.client.widget.layout.FormLayout; import com.extjs.gxt.ui.client.widget.menu.HeaderMenuItem; import com.extjs.gxt.ui.client.widget.menu.Menu; import com.extjs.gxt.ui.client.widget.menu.MenuItem; import com.extjs.gxt.ui.client.widget.menu.SeparatorMenuItem; import com.google.gwt.i18n.client.DateTimeFormat; import com.google.gwt.i18n.client.NumberFormat; import com.google.gwt.user.client.rpc.AsyncCallback; import java.util.Date; import java.util.List; import java.util.Set; /** * * @author Renaud Brub */ public class MaintenanceCEVDialog extends AbstractCEVDialog { protected TextField<String> nameTF = null; protected ComboBox<MaintenanceTypeModel> typeCBx = null; protected TextArea descriptionTA = null; protected TextArea noteTA = null; protected DateField creationDateDF = null; protected DateField startDateDF = null; protected DateField endDateDF = null; protected NumberField frequencyNF = null; protected CheckBox useRecallCkBx = null; protected ListField<BeanModel> affectedItemsLF = null; protected ListStore itemStore = null; protected ListStore<MaintenanceTypeModel> typeListStore = null; public MaintenanceCEVDialog(CEV cevType) { super(cevType); setupUI(); } protected void setupUI() { // Configurations de la bote de dialogue. setLayout(new FitLayout()); setModal(true); setDraggable(true); setCollapsible(false); setClosable(true); setAutoHide(false); setResizable(false); setBodyStyle("padding: 3px"); setWidth(600); setHeight(500); TabPanel tabPanel = new TabPanel(); tabPanel.add(createGeneralTab()); tabPanel.add(createAdvancedTab()); tabPanel.add(createAffectedItemsTab()); add(tabPanel); } protected TabItem createGeneralTab() { TabItem tab = new TabItem(messages.general()); FormPanel formPanel = new FormPanel(); formPanel.setHeaderVisible(false); formPanel.setBodyBorder(false); FormData formData = new FormData("100%"); tab.add(formPanel); LayoutContainer topPanel = new LayoutContainer(); topPanel.setLayout(new ColumnLayout()); formPanel.add(topPanel, formData); LayoutContainer leftPanel = new LayoutContainer(); FormLayout formLayout = new FormLayout(); formLayout.setLabelAlign(LabelAlign.TOP); leftPanel.setLayout(formLayout); leftPanel.setStyleAttribute("padding-right", "18px"); LayoutContainer rightPanel = new LayoutContainer(); rightPanel.setStyleAttribute("padding-right", "18px"); formLayout = new FormLayout(); formLayout.setLabelAlign(LabelAlign.TOP); rightPanel.setLayout(formLayout); topPanel.add(leftPanel, new ColumnData(.5)); topPanel.add(rightPanel, new ColumnData(.5)); LayoutContainer bottomPanel = new LayoutContainer(); formLayout = new FormLayout(); formLayout.setLabelAlign(LabelAlign.TOP); bottomPanel.setLayout(formLayout); formPanel.add(bottomPanel, formData); KeyListener keyListener = new KeyListener() { @Override public void componentKeyUp(ComponentEvent event) { validate(); } }; nameTF = new TextField<String>(); nameTF.setFieldLabel(messages.name()); nameTF.addKeyListener(keyListener); nameTF.setAllowBlank(Boolean.FALSE); nameTF.setMaxLength(100); nameTF.setEmptyText(messages.requiredField()); typeListStore = new ListStore<MaintenanceTypeModel>(); typeCBx = new ComboBox<MaintenanceTypeModel>(); typeCBx.setStore(typeListStore); typeCBx.setFieldLabel(messages.type()); typeCBx.setEmptyText(messages.requiredField()); typeCBx.setAllowBlank(Boolean.FALSE); typeCBx.setForceSelection(true); typeCBx.setEditable(false); typeCBx.setDisplayField("label"); typeCBx.setTriggerAction(TriggerAction.ALL); typeCBx.addListener(Events.Select, new Listener<FieldEvent>() {//Test @Override public void handleEvent(FieldEvent event) { validate(); } }); descriptionTA = new TextArea(); descriptionTA.setFieldLabel(messages.description()); descriptionTA.setHeight(200); descriptionTA.addKeyListener(keyListener); noteTA = new TextArea(); noteTA.setFieldLabel(messages.note()); noteTA.setHeight(100); noteTA.addKeyListener(keyListener); leftPanel.add(nameTF, formData); rightPanel.add(typeCBx, formData); bottomPanel.add(descriptionTA, formData); bottomPanel.add(noteTA, formData); typeListStore.add(MaintenanceType.getInstance().getModelList()); return tab; } protected TabItem createAdvancedTab() { TabItem tab = new TabItem(messages.advanced()); FormPanel formPanel = new FormPanel(); formPanel.setHeaderVisible(false); formPanel.setBodyBorder(false); FormData formData = new FormData("100%"); tab.add(formPanel); LayoutContainer topPanel = new LayoutContainer(); topPanel.setLayout(new ColumnLayout()); formPanel.add(topPanel, formData); LayoutContainer leftPanel = new LayoutContainer(); FormLayout formLayout = new FormLayout(); formLayout.setLabelAlign(LabelAlign.LEFT); leftPanel.setLayout(formLayout); leftPanel.setStyleAttribute("padding-right", "18px"); topPanel.add(leftPanel, new ColumnData(.7)); KeyListener keyListener = new KeyListener() { @Override public void componentKeyUp(ComponentEvent event) { validate(); } }; DateTimeFormat dtf = DateTimeFormat.getFormat(PIGE.TERM_DATE_FORMAT); creationDateDF = new DateField(); creationDateDF.setFieldLabel(messages.creationDate()); creationDateDF.setReadOnly(Boolean.TRUE); creationDateDF.setFormatValue(Boolean.TRUE); creationDateDF.getPropertyEditor().setFormat(dtf); creationDateDF.setHideTrigger(Boolean.TRUE); useRecallCkBx = new CheckBox(); useRecallCkBx.setFieldLabel(messages.useRecall()); frequencyNF = new NumberField(); frequencyNF.setFormat(NumberFormat.getFormat(PIGE.QUANTITY_FORMAT)); frequencyNF.setPropertyEditorType(Integer.class); frequencyNF.setAllowBlank(Boolean.FALSE); frequencyNF.setAllowDecimals(Boolean.FALSE); frequencyNF.setAllowNegative(Boolean.FALSE); frequencyNF.setMaxLength(9); frequencyNF.setFieldLabel(messages.recallFrequency()); frequencyNF.addKeyListener(keyListener); startDateDF = new DateField(); startDateDF.setFieldLabel(messages.startDate()); startDateDF.setReadOnly(Boolean.TRUE); startDateDF.setFormatValue(Boolean.TRUE); startDateDF.getPropertyEditor().setFormat(dtf); endDateDF = new DateField(); endDateDF.setFieldLabel(messages.endDate()); endDateDF.setReadOnly(Boolean.TRUE); endDateDF.setFormatValue(Boolean.TRUE); endDateDF.getPropertyEditor().setFormat(dtf); leftPanel.add(creationDateDF, formData); leftPanel.add(startDateDF, formData); leftPanel.add(endDateDF, formData); leftPanel.add(useRecallCkBx, formData); leftPanel.add(frequencyNF, formData); return tab; } protected TabItem createAffectedItemsTab() { TabItem tab = new TabItem(messages.affectedItems()); affectedItemsLF = new ListField<BeanModel>(); affectedItemsLF.setFieldLabel(messages.affectedItems()); affectedItemsLF.getListView().getSelectionModel().setSelectionMode(SelectionMode.SINGLE); affectedItemsLF.setDisplayField("name"); itemStore = new ListStore(); affectedItemsLF.setStore(itemStore); affectedItemsLF.setHeight(340); FormPanel formPanel = new FormPanel(); formPanel.setHeaderVisible(false); formPanel.setBodyBorder(false); formPanel.setLabelAlign(LabelAlign.TOP); FormData formData = new FormData("100%"); formPanel.add(affectedItemsLF, formData); formPanel.add(new Html(messages.contextualMenuTip()), formData); tab.add(formPanel); affectedItemsLF.getListView().addListener(Events.OnDoubleClick, new Listener<BaseEvent>() { @Override public void handleEvent(BaseEvent be) { viewItem(); }; }); Menu menu = new Menu(); menu.add(new HeaderMenuItem(messages.itemMenu())); affectedItemsLF.setContextMenu(menu); MenuItem viewItemMenu = new MenuItem(messages.view() + PIGE.DIALOG_OPENER_INDICATOR); viewItemMenu.addSelectionListener(new SelectionListener() { @Override public void componentSelected(ComponentEvent ce) { viewItem(); } }); menu.add(viewItemMenu); if (!cevType.equals(CEV.VIEW)) { MenuItem addItemMenu = new MenuItem(messages.add() + PIGE.DIALOG_OPENER_INDICATOR); addItemMenu.addSelectionListener(new SelectionListener() { @Override public void componentSelected(ComponentEvent ce) { openInventoryExplorer(); } }); MenuItem deleteItemMenu = new MenuItem(messages.delete()); deleteItemMenu.addSelectionListener(new SelectionListener() { @Override public void componentSelected(ComponentEvent ce) { deleteItem(); } }); menu.add(new SeparatorMenuItem()); menu.add(addItemMenu); menu.add(deleteItemMenu); } return tab; } @Override protected void initialize() { if (cevType.equals(CEV.CREATE) || cevType.equals(CEV.COPY)) { creationDateDF.setValue(new Date()); } } @Override protected void clearFields() { nameTF.clear(); typeCBx.clear(); descriptionTA.clear(); noteTA.clear(); startDateDF.clear(); endDateDF.clear(); useRecallCkBx.setValue(Boolean.FALSE); frequencyNF.setValue(0); affectedItemsLF.clear(); } @Override protected void fillFields() { Maintenance maintenance = getData("maintenance"); if (maintenance != null) { nameTF.setValue(maintenance.getName()); descriptionTA.setValue(maintenance.getDescription()); noteTA.setValue(maintenance.getNote()); creationDateDF.setValue(maintenance.getCreationDate()); startDateDF.setValue(maintenance.getStartDate()); endDateDF.setValue(maintenance.getEndDate()); frequencyNF.setValue(maintenance.getFrequency()); useRecallCkBx.setValue(maintenance.getUseRecall()); /* * Slectionner le type de la liste droulante correspondant au type * de maintenance */ String maintenanceType = maintenance.getType(); ListStore<MaintenanceTypeModel> store = typeCBx.getStore(); int listSize = store.getCount(); MaintenanceTypeModel model; for (int i = 0; i < listSize; ++i) { model = store.getAt(i); if (model.getValue().equalsIgnoreCase(maintenanceType)) { typeCBx.setValue(model); break; } } // Remplir la liste des items affects. BeanModelFactory factory = BeanModelLookup.get().getFactory(Item.class); affectedItemsLF.getStore().removeAll(); affectedItemsLF.getStore().add(factory.createModel(maintenance.getItems())); } } @Override protected void setReadOnly(Boolean b) { nameTF.setReadOnly(b); typeCBx.setReadOnly(b); descriptionTA.setReadOnly(b); noteTA.setReadOnly(b); startDateDF.setReadOnly(b); endDateDF.setReadOnly(b); useRecallCkBx.setReadOnly(b); frequencyNF.setReadOnly(b); affectedItemsLF.setReadOnly(b); } @Override protected void setupWindow() { if (cevType.equals(CEV.VIEW)) { setHeadingText(messages.viewMaintenance() + " (" + messages.readOnly() + ")"); } else if (cevType.equals(CEV.EDIT)) { setHeadingText(messages.editMaintenance()); } else if (cevType.equals(CEV.CREATE)) { setHeadingText(messages.createMaintenance()); } } @Override protected void validate() { if (cevType.equals(CEV.VIEW)) { return; } boolean isValid = hasValue(nameTF) && typeCBx.getRawValue() != null && !typeCBx.getRawValue().isEmpty(); if (cevType.equals(CEV.CREATE) || cevType.equals(CEV.COPY)) { createBtn.setEnabled(isValid); } else { updateBtn.setEnabled(isValid); } } protected Maintenance buildMaintenance(Maintenance baseMaintenance) { baseMaintenance.setName(nameTF.getValue()); baseMaintenance.setType(typeCBx.getValue().getValue()); baseMaintenance.setDescription(descriptionTA.getValue()); baseMaintenance.setNote(noteTA.getValue()); baseMaintenance.setCreationDate(creationDateDF.getValue()); baseMaintenance.setStartDate(startDateDF.getValue()); baseMaintenance.setEndDate(endDateDF.getValue()); baseMaintenance.setFrequency(frequencyNF.getValue().intValue()); baseMaintenance.setUseRecall(useRecallCkBx.getValue()); Set<Item> set = baseMaintenance.getItems(); set.clear(); List<BeanModel> models = affectedItemsLF.getStore().getModels(); for (BeanModel bm : models) { set.add((Item) bm.getBean()); } return baseMaintenance; } @Override protected void create() { // Nouvelle maintenance sans id. Maintenance maintenance = buildMaintenance(new Maintenance()); // Envoyer la demande de cration de maintenance au serveur. ManagementServiceAsync rpcService = Registry.get(PIGE.MANAGEMENT_SERVICE); rpcService.createMaintenance(maintenance, new AsyncCallback() { @Override public void onSuccess(Object o) { clearFields(); validate(); StatusBar statusBar = Registry.get(AppWidgets.APP_STATUS_BAR); statusBar.setTimedText(messages.maintenanceCreatedSuccessfully()); setFocusWidget(nameTF); Dispatcher.get().dispatch(AppEvents.MAINTENANCE_MANAGEMENT_UPDATE_EVENT); } @Override public void onFailure(Throwable caught) { PIGE.handleException(caught); } }); } @Override protected void update() { // Rcuprer l'item avec un id valide. Maintenance maintenance = buildMaintenance((Maintenance) getData("maintenance")); // Envoyer la demande de cration de compte au serveur. ManagementServiceAsync rpcService = Registry.get(PIGE.MANAGEMENT_SERVICE); rpcService.updateMaintenance(maintenance, new AsyncCallback() { @Override public void onSuccess(Object o) { StatusBar statusBar = Registry.get(AppWidgets.APP_STATUS_BAR); statusBar.setTimedText(messages.updateSuccessful()); setFocusWidget(nameTF); Dispatcher.get().dispatch(AppEvents.MAINTENANCE_MANAGEMENT_UPDATE_EVENT); } @Override public void onFailure(Throwable caught) { PIGE.handleException(caught); } }); } protected void viewItem() { List<BeanModel> selection = affectedItemsLF.getSelection(); if (selection.size() > 0) { ItemCEVDialog dialog = new ItemCEVDialog(CEV.VIEW); dialog.setData("item", (Item) selection.get(0).getBean()); dialog.showDialog(); } else { MessageBox.info(messages.information(), messages.noItemSelected(), null); } } protected void deleteItem() { List<BeanModel> selection = affectedItemsLF.getSelection(); if (selection.size() > 0) { Maintenance maintenance = getData("maintenance"); maintenance.getItems().remove((Item) selection.get(0).getBean()); affectedItemsLF.getStore().remove(selection.get(0)); } else { MessageBox.info(messages.information(), messages.noItemSelected(), null); } } protected void openInventoryExplorer() { final InventoryExplorer explorer = new InventoryExplorer() { @Override protected void insertItem(Item item) { BeanModel bm = BeanModelLookup.get().getFactory(Item.class).createModel(item); if (!affectedItemsLF.getStore().contains(bm)) { affectedItemsLF.getStore().add(bm); } } }; explorer.show(); } }