>>> Rogan Dawes wrote:
>>>> I had in mind a collection of ToggleCommands, which would be activated
>>>> or deactivated as the various options are selected.
>>>>
>>>> For example, create an exclusive command group ( I think this is the
>>>> right name for a Command Group that can only have a single selection? ).
>>>>
>>>> Then, exactly as one has the option to select one of the commands using
>>>> a group of radio buttons, and the commands would toggle their state as
>>>> they are selected and unselected, one could select a command using a
>>>> JComboBox. The interface is pretty intuitive, you can select only one
>>>> command at a time, exactly as one would with a group of radio buttons,
>>>> etc, etc
>>>>
>>>> I'm happy to create a patch. I think that CommandGroup is the correct
>>>> place, but I'm not sure. Some guidance would be appreciated.
>>>>
>>>> Rogan
>> Jan Hoskens wrote:
>>> I haven't looked at it yet, but I would start with examining the way
>>> ExclusiveCommandGroup works with toggleCommands. That one will probably
>>> handle a single selection. Then create a comboboxModel which handles the
>>> selection, a ListCellRenderer for commands and the createComboBox() method.
>>>
>>> This is just a quick guess, I haven't really looked into detail here.
>>> We'll refactor the patch if needed, so don't hesitate to do what you
>>> think should be done and place the code where it fits.
>>>
>>> Kind Regards,
>>> Jan
>>>
> Rogan Dawes wrote:
>> Sounds good. Thanks for the pointer - I had looked at CommandGroup, but
>> hadn't noticed ExclusiveCommandGroup* yet.
>>
>> I'll see what I come up with.
>>
>> Rogan
>>
Rogan Dawes wrote:
> Ok, so I thought I'd actually give this a shot, but I have no clue how
> this code actually works.
>
> I started at CommandGroup.createPopupMenu(), saw how a new popupmenu was
> constructed via the factory, and then bindMembers(Object owner,
> Container memberContainer, Object controlFactory,
> CommandButtonConfigurer configurer) was called.
>
> But from there, I couldn't figure out how new menu items could possibly
> be added to the Jmenu already created. I also didn't see how any
> listeners were configured to automatically update components when a
> selection was made.
>
> Help?
>
> Rogan
Ok, so I have a working implementation, which is not entirely in keeping
with the style of Spring Rich client, but serves to illustrate the concept.
The following function is added to ExclusiveCommandGroup:
public JComboBox createComboBox() {
final DefaultComboBoxModel model = new DefaultComboBoxModel();
ItemListener il = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED)
model.setSelectedItem(e.getSource());
}
};
// we create an array of buttons to serve as a proxy for the model
AbstractButton[] items = new AbstractButton[size()];
int i=0;
Iterator it = memberIterator();
while (it.hasNext()) {
items[i] = ((GroupMember) it.next())
.getCommand().createButton();
items[i].addItemListener(il);
model.insertElementAt(items[i], i);
i++;
}
model.addListDataListener(new ListDataListener() {
public void contentsChanged(ListDataEvent e) {
// Can the group change dynamically?
if (e.getIndex0() == -1) { // selection changed
ComboBoxModel model = (ComboBoxModel) e.getSource();
AbstractButton button = (AbstractButton)
model.getSelectedItem();
if (!button.isSelected())
button.doClick(0);
}
}
public void intervalAdded(ListDataEvent e) {
// Can the group change dynamically?
}
public void intervalRemoved(ListDataEvent e) {
// Can the group change dynamically?
}
});
JComboBox combobox = new JComboBox(model);
combobox.setRenderer(new ListCellRenderer() {
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = new JLabel();
if (value != null) {
AbstractButton button = (AbstractButton) value;
label.setText(button.getText());
label.setIcon(button.getIcon());
}
return label;
}
});
return combobox;
}
I'm not entirely sure how best to map commands which expect to all have
their own representation (such as a Button) to a grouped model like a
ComboBoxModel, while still maintaining the directions of references for
clean up purposes. Cleanup has not been addressed in this code yet.
Anyway, the easiest way I found was to create a Button for each command,
and use that as the list of items in the combobox. An ItemListener on
the Button is used to update the JComboBox's selectedItem, and a
ListSelectionListener is used to respond to JComboBox events by
executing Button.doClick() for the selected item.
A ListCellRenderer simply takes the Text and Icon from the Button, and
presents it in a JLabel.
Obviously, there should be use of control factories, etc to make it
configurable, but that is the basic approach I found.
Comments?
Rogan
|