--- /dev/null
+/*****************************************************************************
+ * Copyright Igor Barma, Eric Brun, Alexandre Desoubeaux, Christian Martel,
+ * (2 décembre 2008)
+ *
+ * Ce logiciel est un programme informatique servant à l'évaluation des
+ * compétences.
+ *
+ * Ce logiciel est régi par la licence CeCILL soumise au droit français et
+ * respectant les principes de diffusion des logiciels libres. Vous pouvez
+ * utiliser, modifier et/ou redistribuer ce programme sous les conditions
+ * de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
+ * sur le site "http://www.cecill.info".
+ *
+ * En contrepartie de l'accessibilité au code source et des droits de copie,
+ * de modification et de redistribution accordés par cette licence, il n'est
+ * offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
+ * seule une responsabilité restreinte pèse sur l'auteur du programme, le
+ * titulaire des droits patrimoniaux et les concédants successifs.
+ *
+ * A cet égard l'attention de l'utilisateur est attirée sur les risques
+ * associés au chargement, à l'utilisation, à la modification et/ou au
+ * développement et à la reproduction du logiciel par l'utilisateur étant
+ * donné sa spécificité de logiciel libre, qui peut le rendre complexe à
+ * manipuler et qui le réserve donc à des développeurs et des professionnels
+ * avertis possédant des connaissances informatiques approfondies. Les
+ * utilisateurs sont donc invités à charger et tester l'adéquation du
+ * logiciel à leurs besoins dans des conditions permettant d'assurer la
+ * sécurité de leurs systèmes et ou de leurs données et, plus généralement,
+ * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
+ *
+ * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
+ * pris connaissance de la licence CeCILL, et que vous en avez accepté les
+ * termes.
+ *******************************************************************************/
+package com.pentila.evalcomp.client.widget;
+import java.util.Map;
+
+import com.smartgwt.client.data.Record;
+
+import com.smartgwt.client.types.ListGridEditEvent;
+import com.smartgwt.client.types.Overflow;
+import com.smartgwt.client.types.SelectionStyle;
+
+import com.smartgwt.client.widgets.form.fields.FormItem;
+import com.smartgwt.client.widgets.grid.ListGrid;
+import com.smartgwt.client.widgets.grid.ListGridField;
+import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
+import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
+
+/**
+ * A PropertyGrid based on a {@link ListGrid} that automatically creates two
+ * fields (one for name column and other for values column) and can display a
+ * custom editor for every row
+ *
+ * @author Dan Mihai Ile
+ *
+ */
+public class PropertyGrid extends ListGrid {
+
+ /**
+ * Map to hold the editors
+ */
+ private Map<String, FormItem> editorsMap;
+
+ /**
+ * Hold the name of the attribute on a {@link Record} which contains the
+ * editorsMap key
+ */
+ private String valueFieldAttribute;
+
+ /**
+ * addSelectionChangedHandler fires several times for the same record and we
+ * don't want to set the editor repeatedly for the same record, this tracks
+ * the last record that has the editor set up
+ */
+ protected Record lastEditorChangedRecord;
+
+ private String fieldNameAttribute;
+
+ private Boolean readOnly = false;
+
+
+
+ /**
+ * Create a new {@link PropertyGrid} with two column fields (first for
+ * names, second for values)
+ */
+ public PropertyGrid() {
+ fieldNameAttribute = null;
+ // some property grid default values
+ setShowAllRecords(true);
+ setBodyOverflow(Overflow.VISIBLE);
+ setOverflow(Overflow.VISIBLE);
+ setLeaveScrollbarGap(false);
+ setSelectionType(SelectionStyle.SINGLE);
+ setEditEvent(ListGridEditEvent.CLICK);
+ setShowHeaderContextMenu(false);
+ setWrapCells(true);
+ setFixedRecordHeights(false);
+ // create the 2 fields, one for names, other for values
+ ListGridField nameField = new ListGridField();
+ nameField.setCanEdit(false);
+ ListGridField valueField = new ListGridField();
+ valueField.setCanEdit(true);
+
+
+ setFields(nameField, valueField);
+
+ // listener to set the right editor
+
+ this.addRecordClickHandler(new RecordClickHandler(){
+
+ public void onRecordClick(RecordClickEvent event) {
+ // TODO Auto-generated method stub
+ if (!readOnly){
+ Record record = event.getRecord();
+ //if (event.getState() && !record.equals(lastEditorChangedRecord)) {
+ lastEditorChangedRecord = record;
+ enableSpecificEditor(record);
+
+ //}
+ }
+ }
+
+ });
+
+ /*addSelectionChangedHandler(new SelectionChangedHandler() {
+ public void onSelectionChanged(SelectionEvent event) {
+ Record record = event.getRecord();
+ //if (event.getState() && !record.equals(lastEditorChangedRecord)) {
+ lastEditorChangedRecord = record;
+ enableSpecificEditor(record);
+ //}
+ }
+ });*/
+
+ }
+
+ /**
+ * The name column field
+ *
+ * @return
+ */
+ public ListGridField getNameField() {
+ return getField(0);
+ }
+
+ /**
+ * The values column field
+ *
+ * @return
+ */
+ public ListGridField getValuesField() {
+ return getField(1);
+ }
+
+
+
+ public void setEditorsMap(String attribute, Map<String, FormItem> map){
+ this.valueFieldAttribute = attribute;
+ this.editorsMap = map;
+ }
+
+ /**
+ * Set the name of the attribute from where the row will get the editor name
+ * to be used on the {@link Map} from where the rows will get it's editors
+ * <p>
+ * When an edit starts the {@link PropertyGrid} will retrieve the key of the
+ * editor form the row's attributes and use that key on the map to get the
+ * editor to be used
+ *
+ * @param attribute
+ * the name of the attribute on the rows that contains the key of
+ * the editor
+ * @param map
+ * a map that contains the key of the editor and the editor
+ */
+ public void setEditorsMap(String attribute, String fieldName, Map<String, FormItem> map) {
+ this.fieldNameAttribute = fieldName;
+ setEditorsMap(attribute, map);
+ }
+
+ /**
+ * Enable the specific editor for the given record
+ *
+ * @param record
+ * the record
+ */
+ private void enableSpecificEditor(Record record) {
+ String fieldType = null;
+ String attribute = null;
+
+ if (fieldNameAttribute != null){
+ attribute = record.getAttribute(fieldNameAttribute);
+ if (attribute != null && !editorsMap.containsKey(attribute)){
+ attribute = null;
+ }
+
+ }
+
+ if (attribute == null){
+ attribute = record.getAttribute(valueFieldAttribute);
+ }
+ if (attribute != null) {
+ // direct hit, user has set the attribute for the field type
+ fieldType = attribute;
+ } else {
+ // user didn't setup the attribute for the field type, try to use a
+ // default editor by discovering the value type
+
+ }
+
+ if (fieldType != null) {
+
+ FormItem formItem = editorsMap.get(fieldType);
+ if (formItem != null){
+ formItem.enable();
+
+ getField(1).setEditorType(formItem);
+ }
+
+ }
+
+
+ }
+
+ public Boolean getReadOnly() {
+ return readOnly;
+ }
+
+ public void setReadOnly(Boolean readOnly) {
+
+ getField(1).setCanEdit(!readOnly);
+
+ this.readOnly = readOnly;
+ }
+
+
+
+}
\ No newline at end of file