How do you disable user selection on a CellList?

1 290 просмотров
Перейти к первому непрочитанному сообщению

Jim Douglas

не прочитано,
16 июн. 2011 г., 17:44:5616.06.2011
– Google Web Toolkit
I'm getting slightly overwhelmed by the infrastructure required to
manage a CellList, so I might be missing something really simple. How
do I set a CellList so that users can't change the selection with the
keyboard or mouse, while leaving programmatic selection in place
(which might be either MultiSelectionModel or SingleSelectionModel)?

And a marginally related subject -- is there no default UI for
managing multi-selection? If I define an HTML select widget (GWT
ListBox) for multiple selection, there's a standard UI (hold down a
modifier key while clicking the mouse).

http://www.w3schools.com/tags/att_select_multiple.asp

Thomas Broyer

не прочитано,
16 июн. 2011 г., 18:01:5816.06.2011
– google-we...@googlegroups.com


On Thursday, June 16, 2011 11:44:56 PM UTC+2, Jim Douglas wrote:
I'm getting slightly overwhelmed by the infrastructure required to
manage a CellList, so I might be missing something really simple.  How
do I set a CellList so that users can't change the selection with the
keyboard or mouse, while leaving programmatic selection in place
(which might be either MultiSelectionModel or SingleSelectionModel)?

You can use setCellEventPreviewHandler to control how selection is handled; notably by passing a DefaultSelectionEventManager, for instance a custom one (createCustomManager) whose EventTranslator always returns false from clearSelection and IGNORE from translateSelectionEvent.
(not tested, but I guess it should work)
 
And a marginally related subject -- is there no default UI for
managing multi-selection?  If I define an HTML select widget (GWT
ListBox) for multiple selection, there's a standard UI (hold down a
modifier key while clicking the mouse).

The default behavior should allow it (provided you use a MultiSelectionModel, of course): Ctrl+Click to toggle an item to add/remove from the current selection, Shift+Click for the whole range between the current "anchor" (last place clicked without "shift" pressed) and the clicked row. It should also work with the keyboard: up/down to move, space to change selection (with Ctrl or Shift to add/remove a single item or range of items)
This is the behavior of DefaultSelectionEventManager.createDefaultManager(), which is the default behavior of any AbstractHasData.

Jim Douglas

не прочитано,
16 июн. 2011 г., 18:31:3916.06.2011
– Google Web Toolkit
Wow, thanks for the quick response, Thomas!

I found the problem with multi-selection -- it doesn't work with this
setting:

cellList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.BOUND_TO_SELECTION);

I added that because I don't like the way SingleSelectionModel works
without it (i.e. it's different from the way the native HTML select
widget works). I didn't realize that it also disables the ability of
users to manage multi-selection. Should be easily fixed; just set the
KeyboardSelectionPolicy as needed along with the SelectionModel.

I'll have to look into the event preview handler; it looks like it'll
take some fiddling to sort out.

On Jun 16, 3:01 pm, Thomas Broyer <t.bro...@gmail.com> wrote:
> On Thursday, June 16, 2011 11:44:56 PM UTC+2, Jim Douglas wrote:
>
> > I'm getting slightly overwhelmed by the infrastructure required to
> > manage a CellList, so I might be missing something really simple.  How
> > do I set a CellList so that users can't change the selection with the
> > keyboard or mouse, while leaving programmatic selection in place
> > (which might be either MultiSelectionModel or SingleSelectionModel)?
>
> You can use setCellEventPreviewHandler to control how selection is handled;
> notably by passing a DefaultSelectionEventManager, for instance a custom one
> (createCustomManager) whose EventTranslator<http://google-web-toolkit.googlecode.com/svn/javadoc/2.3/com/google/g...> always

Jim Douglas

не прочитано,
17 июн. 2011 г., 14:35:0017.06.2011
– Google Web Toolkit
Thomas,

Thanks again for pointing me in the right direction. It ended up
being a bit more involved than I expected, but it seems to be working
ok. Here's the code for anyone else who might be interested:

private final CellList<String> m_cellList =
new CellList<String>(new TextCell());

private DefaultSelectionEventManager<String>
m_selectionEventManager;

private HandlerRegistration m_selectionChangeHandler;

private boolean m_multiple;

private void setEnabled(boolean p_enabled)
{
if (p_enabled)
m_selectionEventManager =
DefaultSelectionEventManager.createDefaultManager();
else
m_selectionEventManager =

DefaultSelectionEventManager.createBlacklistManager(0);
setMultipleSelect(m_multiple);
}

public void setMultipleSelect(boolean p_multiple)
{
Set<String> selectedItems = getSelectedItems();
AbstractSelectionModel<String> selectionModel;
if (p_multiple)
{
selectionModel = new MultiSelectionModel<String>();
m_cellList.setKeyboardSelectionPolicy(ENABLED);
}
else
{
selectionModel = new SingleSelectionModel<String>();
m_cellList.setKeyboardSelectionPolicy(BOUND_TO_SELECTION);
}
m_cellList.setSelectionModel(selectionModel,
m_selectionEventManager);
if (selectedItems != null)
{
Iterator<String> iter = selectedItems.iterator();
while (iter.hasNext())
selectionModel.setSelected(iter.next(), true);
}
if (m_selectionChangeHandler != null)
m_selectionChangeHandler.removeHandler();
m_selectionChangeHandler =
selectionModel.addSelectionChangeHandler(this);
m_multiple = p_multiple;
}

@SuppressWarnings("unchecked")
public Set<String> getSelectedItems()
{
Set<String> set = new HashSet<String>();
if (m_cellList.getSelectionModel() == null)
{
return set;
}
else if (m_multiple)
{
MultiSelectionModel<String> model =

(MultiSelectionModel<String>)m_cellList.getSelectionModel();
set = model.getSelectedSet();
}
else
{
SingleSelectionModel<String> model =

(SingleSelectionModel<String>)m_cellList.getSelectionModel();
String item = model.getSelectedObject();
if (item != null)
set.add(item);
}
return set;
}

On Jun 16, 3:01 pm, Thomas Broyer <t.bro...@gmail.com> wrote:
> On Thursday, June 16, 2011 11:44:56 PM UTC+2, Jim Douglas wrote:
>
> > I'm getting slightly overwhelmed by the infrastructure required to
> > manage a CellList, so I might be missing something really simple.  How
> > do I set a CellList so that users can't change the selection with the
> > keyboard or mouse, while leaving programmatic selection in place
> > (which might be either MultiSelectionModel or SingleSelectionModel)?
>
> You can use setCellEventPreviewHandler to control how selection is handled;
> notably by passing a DefaultSelectionEventManager, for instance a custom one
> (createCustomManager) whose EventTranslator<http://google-web-toolkit.googlecode.com/svn/javadoc/2.3/com/google/g...> always
Ответить всем
Отправить сообщение автору
Переслать
0 новых сообщений