nigelht / macwidgets

Collection of Mac style widgets written in Java (by Ken Orr)
http://nigelht.github.io/macwidgets
3 stars 0 forks source link

Allow clear selection in SourceList #112

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hello!

I would need a way to deselect an item in SourceList. I tried with 
sourceList.setSelectedItem(null) but an exception occurred:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The 
given item is not part of this model.
    at com.explodingpixels.macwidgets.SourceListModel.validateItemIsInModel(SourceListModel.java:225)
    at com.explodingpixels.macwidgets.SourceList.setSelectedItem(SourceList.java:204)
        ....
    at jcodecollector.gui.MainFrame.clearSelection(MainFrame.java:245)
        .....

Suggestion (?):

public void setSelectedItem(SourceListItem item) {
  if (item == null) {
      fTree.clearSelection();
  }
   ...
   ...

Is it correct? I'm not able to compile the entire project manually...

Meanwhile I wrote this method to workaround the problem but I'm not satisfied 
because the user sees the item disappear for a 
moment:

    private void clearSelection() {
        SourceListItem selectedItem = sourceList.getSelectedItem();
        SourceListCategory parent = null;
        Iterator<SourceListCategory> iterator = sourceList.getModel().getCategories().iterator();

        int i = 0; // selected item position
        while (iterator.hasNext()) {
            SourceListCategory sourceListCategory = iterator.next();
            Iterator<SourceListItem> innerIterator = sourceListCategory.getItems().iterator();

            while (innerIterator.hasNext()) {
                SourceListItem item = innerIterator.next();
                if (item.getText().equals(selectedItem.getText())) {
                    parent = sourceListCategory;
                    break;
                }

                i++;
            }

            if (parent != null) {
                break;
            } else {
                i = 0;
            }
        }

        // remove and re-add in the previous position
        sourceList.getModel().removeItemFromCategory(selectedItem, parent);
        sourceList.getModel().addItemToCategory(selectedItem, parent, i);
    }

Thank you for your attention
Alessandro C.

Original issue reported on code.google.com by alex.co...@gmail.com on 21 Jul 2009 at 3:24

GoogleCodeExporter commented 9 years ago
I've added a clearSelection method to SourceList. If you'd like the updated 
jar, let me know ( 
kenneth.orr@gmail.com )

Original comment by kenneth....@gmail.com on 22 Jul 2009 at 10:35

GoogleCodeExporter commented 9 years ago
Great, thank you! *_*
Please send me the new jar, thank you again! 

Alessandro C.

Original comment by alex.co...@gmail.com on 22 Jul 2009 at 11:14