1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
package com.a806278.MCO.client;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.a806278.MCO.Entity.AttributRegleUD;
import com.a806278.MCO.Entity.RegleUD;
import com.a806278.MCO.Resources.classes.WorkIcons;
import com.a806278.MCO.WebService.AttributRegleUDService;
import com.a806278.MCO.WebService.AttributRegleUDServiceAsync;
import com.a806278.MCO.WebService.RegleUDService;
import com.a806278.MCO.WebService.RegleUDServiceAsync;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.data.BeanModelReader;
import com.extjs.gxt.ui.client.data.ListLoader;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
@SuppressWarnings("unchecked")
public class AttributRegleUDPanel extends ContentPanel {
private FormBinding formBindings;
private AttributRegleUDServiceAsync AttributRegleUDService;
private Grid grid;
private FormPanel panel;
private SimpleComboBox comboTypeManuel;
//constructeur de la liste
public AttributRegleUDPanel(){
try{
//on parametre le panel
this.setFrame(true);
this.setHeaderVisible(false);
this.setSize(798,425);
this.setLayout(new RowLayout(Orientation.HORIZONTAL));
// Creation et initialisation du proxy client permettant lappel du service : TypeApp
// 1) Creation du stub
AttributRegleUDService = (AttributRegleUDServiceAsync) GWT.create(AttributRegleUDService.class);
// 2) Definir lURL : elle doit appartenir au meme domaine que la page
ServiceDefTarget endpoint = (ServiceDefTarget) AttributRegleUDService;
String moduleRelativeURL = GWT.getModuleBaseURL() + "AttributRegleUD";
endpoint.setServiceEntryPoint(moduleRelativeURL);
//on instancie le grid
grid = this.createGrid();
grid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
grid.getSelectionModel().addListener(Events.SelectionChange,
new Listener<SelectionChangedEvent>() {
public void handleEvent(SelectionChangedEvent be) {
if (be.getSelectedItem() != null) {
formBindings.bind(be.getSelectedItem());
BeanModel bm = (BeanModel) be.getSelectedItem().get("laRegleUD");
int j = comboTypeManuel.getStore().getModels().indexOf(bm);
comboTypeManuel.setValue(comboTypeManuel.getStore().getAt(j));
}
else {
formBindings.unbind();
}
}
});
//on ajoute le grid au panel
this.add(grid, new RowData(.6, 1));
//on instancie le formulaire
panel = createForm();
formBindings = new FormBinding(panel, true);
formBindings.setStore((Store) grid.getStore());
//on ajoute les boutons de comit et reset
panel.setButtonAlign(HorizontalAlignment.LEFT);
panel.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
grid.getStore().rejectChanges();
grid.getSelectionModel().deselectAll();
}
}));
panel.addButton(new Button("Commit", new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
//on recupere le contenu du textField
List<Field<?>> listFields = panel.getFields();
//on verifie que les champs nécessaires sont bien remplis
if(listFields.get(1).getRawValue().equals("") || listFields.get(2).getRawValue().equals("")){
grid.getStore().rejectChanges();
MessageBox.alert("Erreur", "Veuillez remplir tous les champs.", null);
}
else{
//on recupere le AttributRegleUD
AttributRegleUD newAttributRegleUD = new AttributRegleUD();
newAttributRegleUD.setNomAttribut(listFields.get(1).getRawValue());
newAttributRegleUD.setValeurAttribut(listFields.get(2).getRawValue());
//on recupere le TypeManuel
BeanModel bm = (BeanModel) listFields.get(2).getValue();
newAttributRegleUD.setLaRegleUD((RegleUD) bm.getBean());
String id = listFields.get(0).getRawValue();
// cas d'un ajout
if(id.equals("")){
//on verifie que le type n'existe pas déja
boolean b = exist(grid.getStore().getModels(),newAttributRegleUD);
if(b){
MessageBox.alert("Erreur", "Ce type existe deja.", null);
grid.getStore().rejectChanges();
}
else{
//on appelle la fonction d'ajout
insertLine(newAttributRegleUD);
}
}
//cas d'une modif
else{
//on recup l'id
newAttributRegleUD.setIdAttribut(Integer.valueOf(listFields.get(0).getRawValue()));
//on appelle la fonction de modif
updateLine(newAttributRegleUD);
}
}
}
}));
//on ajoute le formulaire
this.add(panel, new RowData(.4, 1));
}
catch(Exception ex){
System.out.println("Erreur.");
System.out.println(ex.getMessage());
}
}
private Grid createGrid(){
//on declare le grid
//on commence par les colonnes
List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
columns.add(new ColumnConfig("idAttribut", "Id de l'attribut", 100));
columns.add(new ColumnConfig("nomAttribut", "Nom de l'attribut", 100));
columns.add(new ColumnConfig("valeurAttribut", "Valeur de l'attribut", 100));
columns.add(new ColumnConfig("laRegleUD", "Regle UD", 150));
ColumnModel cm = new ColumnModel(columns);
// 2) on definit le proxy
// proxy and reader
RpcProxy proxy = new RpcProxy() {
@Override
public void load(Object loadConfig, AsyncCallback callback) {
AttributRegleUDService.getListOfAttributRegleUD(callback);
}
};
//on declare le reader et le loader
BeanModelReader reader = new BeanModelReader();
ListLoader loader = new BaseListLoader(proxy, reader);
final ListStore<BeanModel> store = new ListStore<BeanModel>(loader);
store.setMonitorChanges(true);
//on envoie la recuperation de la liste via RCP
loader.load();
//on declare le grid
grid = new Grid(store, cm);
grid.setWidth(306);
grid.setHeight(400);
grid.setBorders(true);
//on declare le menu
Menu contextMenu = new Menu();
//on declare les icones
WorkIcons workIcon = (WorkIcons) GWT.create(WorkIcons.class);
AbstractImagePrototype.create(workIcon.add());
//on ajoute le menu contextuel au grid
MenuItem insert = new MenuItem();
insert.setText("Insert Item");
insert.setIcon(AbstractImagePrototype.create(workIcon.add()));
insert.addSelectionListener(new SelectionListener<MenuEvent>() {
public void componentSelected(MenuEvent ce) {
//on enleve le binding
formBindings.unbind();
//on vide le formulaire
panel.clear();
//on sort du grid
grid.getSelectionModel().deselectAll();
}
});
contextMenu.add(insert);
MenuItem remove = new MenuItem();
remove.setText("Remove Selected");
remove.setIcon(AbstractImagePrototype.create(workIcon.delete()));
remove.addSelectionListener(new SelectionListener<MenuEvent>() {
public void componentSelected(MenuEvent ce) {
//on affiche le message de confirmation
MessageBox box = MessageBox.confirm("Confirmer la suppression", "Voulez vous supprimer cet enregistrement?",null);
box.addCallback(new Listener<MessageBoxEvent>() {
public void handleEvent(MessageBoxEvent be) {
//si la suppression est confirmé
if(be.getButtonClicked().getText().equals("Yes")){
//on demande la suppression
deleteLine();
}
}
});
}
});
contextMenu.add(remove);
//et on lie le menu au grid
grid.setContextMenu(contextMenu);
return grid;
}
/**
* Methode qui créé le formulaire
*
* @return
*/
private FormPanel createForm(){
//on declare le formulaire
FormPanel panel = new FormPanel();
panel.setHeaderVisible(false);
//champs id
TextField<String> idAttributRegleUD = new TextField<String>();
idAttributRegleUD.setName("idAttribut");
idAttributRegleUD.setFieldLabel("Id de l'attribut");
idAttributRegleUD.setEnabled(false);
panel.add(idAttributRegleUD);
// champs type
TextField<String> AttributRegleUD = new TextField<String>();
AttributRegleUD.setName("nomAttribut");
AttributRegleUD.setFieldLabel("Nom de l'attribut");
panel.add(AttributRegleUD);
// champs type
TextField<String> valeurAttribut = new TextField<String>();
valeurAttribut.setName("valeurAttribut");
valeurAttribut.setFieldLabel("Valeur de l'attribut");
panel.add(valeurAttribut);
//----------------------------------------------------------------------
// CREATION DU COMBOBOX TYPE_MANUEL
//---------------------------------------------------------------------
// Creation et initialisation du proxy client permettant lappel du service : TypeManuel
// 1) Creation du stub
final RegleUDServiceAsync RegleUDService = (RegleUDServiceAsync) GWT.create(RegleUDService.class);
// 2) Definir lURL : elle doit appartenir au meme domaine que la page
ServiceDefTarget endpoint2 = (ServiceDefTarget) RegleUDService;
String moduleRelativeURL2 = GWT.getModuleBaseURL() + "RegleUD";
endpoint2.setServiceEntryPoint(moduleRelativeURL2);
//on recupere la liste des RegleUD
// 2) on definit le proxy
// proxy and reader
RpcProxy proxy2 = new RpcProxy() {
@Override
public void load(Object loadConfig, AsyncCallback callback) {
RegleUDService.getListOfRegleUD(callback);
}
};
//on declare le reader et le loader
BeanModelReader reader2 = new BeanModelReader();
ListLoader loader2 = new BaseListLoader(proxy2, reader2);
final ListStore<BeanModel> store2 = new ListStore<BeanModel>(loader2);
store2.setMonitorChanges(true);
//on envoie la recuperation de la liste via RCP
loader2.load();
//on instancie le combobox
SimpleComboBox comboRegleUD = new SimpleComboBox();
comboRegleUD.setFieldLabel("Regle UD");
comboRegleUD.setStore(store2);
comboRegleUD.setDisplayField("text");
comboRegleUD.setEmptyText("Choose a Regle UD...");
comboRegleUD.setLazyRender(false);
//on l'ajoute au formulaire
panel.add(comboRegleUD);
//---------------------------------------------------------------
return panel;
}
/**
* Methode qui va supprimer la ligne sélectionnée
*/
private void deleteLine(){
//on appelle le service
AttributRegleUDService.deleteAttributRegleUD((Integer)grid.getSelectionModel().getSelectedItem().get("idAttribut"), new AsyncCallback<Boolean>() {
public void onFailure(Throwable caught) {
//on affiche un message d'alerte
MessageBox.alert("Erreur", "L'appel a echoue.", null);
}
public void onSuccess(Boolean result) {
if(result){
//on supprime la ligne du grid
grid.getStore().remove((BeanModel) grid.getSelectionModel().getSelectedItem());
panel.clear();
}
else{
//on affiche un message d'alerte
MessageBox.alert("Erreur", "La suppression a echoue.", null);
}
}
});
}
/**
* Methode qui va ajouter une ligne
*
* @param newTypeApp
*/
private void insertLine(AttributRegleUD newAttributRegleUD){
AttributRegleUDService.addAttributRegleUD(newAttributRegleUD, new AsyncCallback<AttributRegleUD>() {
public void onFailure(Throwable caught) {
MessageBox.alert("Erreur", "L'appel a echoue.", null);
}
public void onSuccess(AttributRegleUD result) {
//si l'ajout a fonctionné
if(result.getIdAttribut() != null){
//on ajoute la ligne du grid
grid.getStore().add(createModel(result));
//on selectionne la ligne insérée
int lastRow = grid.getStore().indexOf(createModel(result));
grid.getSelectionModel().select(lastRow, false);
grid.getView().focusRow(lastRow);
}
else{
//on affiche un message d'alerte
grid.getStore().rejectChanges();
MessageBox.alert("Erreur", "L'ajout a echoue.", null);
}
}
});
}
/**
* Methode qui va modifier une ligne
*
* @param leType
*/
private void updateLine(AttributRegleUD leType){
//on appelle le service
AttributRegleUDService.updateAttributRegleUD(leType, new AsyncCallback<Boolean>() {
public void onFailure(Throwable caught) {
//on affiche un message d'alerte
MessageBox.alert("Erreur", "L'appel a echoue.", null);
}
public void onSuccess(Boolean result) {
if(result){
//on effectue les changement
grid.getStore().commitChanges();
//on raffraichi le grid
grid.getStore().getLoader().load();
panel.clear();
}
else{
//on affiche un message d'alerte
grid.getStore().rejectChanges();
MessageBox.alert("Erreur", "La modification a echoue.", null);
}
}
});
}
private boolean exist(List listeModels, AttributRegleUD AttributRegleUD){
boolean exist = false;
//on boucle dans la liste
Iterator it = listeModels.iterator();
while(it.hasNext()){
//on le met dans un objet
BeanModel bm = (BeanModel) it.next();
AttributRegleUD leTypeToCompare = bm.getBean();
//on compare les labels
if(AttributRegleUD.equals(leTypeToCompare)){
exist = true;
}
}
return exist;
}
private BeanModel createModel(AttributRegleUD leType){
return BeanModelLookup.get().getFactory(AttributRegleUD.class).createModel(leType);
}
} |
Partager