PDA

View Full Version : Different editors for different cells in the same grid columns. Is it possible?



atrubka
16 Dec 2010, 10:54 AM
Hi, guys.

I have an ultimate requirement to allow for different editors in different cells of the same column. Those editors may not even use Field component, so I can't really use CellEditor, which only works with a Field instance.

I found that I can trick the system by using custom renderer, but it only works well with modal windows. Otherwise it doesn't really know when to update the model.

Is it at all possible to use custom editors like I described?


At the very minimum I would need to be able to tell whether a cell can be edited or not. Is it possible?

Thank you all in advance!

sven
16 Dec 2010, 10:59 AM
What kind of editor are you looking for if you dont want to use fields? EditorGrid at the moment only works with a CellEditor which only supports fields. You can maybe use an AdapterField to trick it.

atrubka
16 Dec 2010, 11:14 AM
One of the editors is a container of label+button. The button starts dialog.

Other editors are combo boxes with different sets of values (in the same column), so I have to somehow reinitialize the underlying store in the same ComboBox field.

There might be more in the future, but for the immediate use case I need those 2.

atrubka
16 Dec 2010, 11:42 AM
Another difficulty with combo box is that I need to use raw value rather than value from the list.
So I can't rely on selection change listener.
I was playing with some focus events, etc., but no luck so far. It's still buggy.

sven
16 Dec 2010, 11:42 AM
For point 2) the answer is just two threads under yours at the moment: http://www.sencha.com/forum/showthread.php?118872-Grid-combobox-values-should-not-be-same-across-all-the-rows-of-the-same-column

atrubka
16 Dec 2010, 12:35 PM
I've read that. Originally it was designed for 2 different editors working together, but I see how I can use it for my use case.
I also need to know how to switch to different editors and even more importantly to no editors.

I found BaseEvent.setCancelled() and I'm trying to utilize it to make a cell non-editable.
Let me see if I can make it work this way.

In any case this would be a rather ugly solution, where some editors will be implemented though renderer...

sven
16 Dec 2010, 12:37 PM
I found BaseEvent.setCancelled() and I'm trying to utilize it to make a cell non-editable.
Let me see if I can make it work this way.


Yes you can cancel the BeforeEdit event and editing wont start.

atrubka
16 Dec 2010, 3:35 PM
I've managed to make it working, but the solution is rather ugly.
The editor below switches underlying combo box between different list stores depending on what type of value needs to be edited.
It also can control whether the value should be edited or not (but only if listener is installed to a grid, which is not the best solution either).
There's also a renderer that handles another type of editor.
Since in my use case combo box should not restrict user from typing an arbitrary string (works more like suggestion) it makes it even uglier as standard way to convert values doesn't work. My creature throws


java.lang.ClassCastException: java.lang.String cannot be cast to com.extjs.gxt.ui.client.data.ModelData
at com.extjs.gxt.ui.client.widget.form.ComboBox.setValue(ComboBox.java:1)
at com.extjs.gxt.ui.client.widget.Editor.startEdit(Editor.java:425)

sometimes, so it will have to be improved.
Please take a look at it. I'm begging for a more elegant solution. :(



public class StandardFieldFormatEditor extends CellEditor {
private static class StringComboValue extends SimpleComboValue<String> {
public StringComboValue(String s) {
super(s);
}
}

private static final Map<StandardFieldType<?>, ListStore<SimpleComboValue<String>>> _suggestedFormats = new HashMap<StandardFieldType<?>, ListStore<SimpleComboValue<String>>>();

{
_suggestedFormats.put(StandardFieldType.DATE,
createListStore("mmddyyyy", "mm/dd/yyyy", "mm.dd.yyyy", "mm-dd-yyyy"));
_suggestedFormats.put(StandardFieldType.BUYSELL, createListStore("B/S", "Buy/Sell", "1/-1"));
_suggestedFormats.put(StandardFieldType.PUTCALL, createListStore("P/C", "Put/Call", "1/-1"));
}

private ListStore<SimpleComboValue<String>> createListStore(String... values) {
ListStore<SimpleComboValue<String>> result = new ListStore<SimpleComboValue<String>>();

for (String value : values) {
result.add(new StringComboValue(value));
}
return result;
}

private static SimpleComboBox<String> createComboBox() {
return new SimpleComboBox<String>();
}

public StandardFieldFormatEditor() {
super(createComboBox());
}

@SuppressWarnings("unchecked")
private SimpleComboBox<String> getComboBox() {
return (SimpleComboBox<String>) getField();
}

@Override
public Object preProcessValue(Object value) {
return (value != null) ? getComboBox().findModel(value.toString()) : null;
}

@Override
public Object postProcessValue(Object value) {
return getComboBox().getRawValue();
// return (value instanceof StringComboValue) ? ((StringComboValue)
// value).getValue() : null;
}

private boolean setType(StandardFieldType<?> type) {
ListStore<SimpleComboValue<String>> store = _suggestedFormats.get(type);
if (store != null) {
getComboBox().setStore(store);
return true;
} else {
return false;
}
}

@Override
public void startEdit(Element el, Object value) {
super.startEdit(el, value);
if (value != null) {
getComboBox().setRawValue(value.toString());
}
}

public void installGridListeners(Grid<StandardFieldMappingModel> grid) {
grid.addListener(Events.BeforeEdit, new Listener<GridEvent<StandardFieldMappingModel>>() {
@SuppressWarnings("rawtypes")
public void handleEvent(GridEvent<StandardFieldMappingModel> e) {
StandardFieldMappingModel model = e.getModel();
StandardField field = model.getField();
StandardFieldType fieldType = field.getType();

boolean typeFound = setType(fieldType);

if (!typeFound) {
e.setCancelled(true);
}
}
});
}
}