2

I have a simple OutlineView in the NetBeans editor area that shows two columns. The content of the cells of the second column shall be settable with a custom property editor via the PropertySupport. The custom property editor contains a JList that allows multiple selection of items.

The PropertySupport class looks like

public class CityProperty extends PropertySupport.ReadWrite<String> { 

    Customer c; 

    public CityProperty(Customer c, HashMap<String, Boolean> optionalCities) { 
        super("city", String.class, "City", "Name of City"); 
        setValue("labelData", optionalCities); 
        this.c = c; 
    } 

    @Override 
    public String getValue() throws IllegalAccessException, InvocationTargetException { 
        return c.getCity(); 
    } 

    @Override 
    public PropertyEditor getPropertyEditor() { 
        return new CityPropertyEditor(c); 
    } 

    @Override 
    public void setValue(String newValue) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { 
        c.setCity(newValue); 
    } 
}

The PropertyEditor looks like

public class CityPropertyEditor extends PropertyEditorSupport implements ExPropertyEditor { 

    Customer c; 
    PropertyEnv env; 
    public CityPropertyEditorPanel editor = null; 

    public CityPropertyEditor(Customer c) { 
        this.editor = new CityPropertyEditorPanel(); 
        this.c = c; 
    } 

    @Override 
    public String getAsText() { 
        String s = (String) getValue(); 
        if (s == null) { 
            return "No City Set"; 
        } 
        return s; 
    } 

    @Override 
    public void setAsText(String s) { 
        setValue(s); 
    } 

    @Override 
    public void attachEnv(PropertyEnv env) { 
        this.env = env; 
    } 

    @Override 
    public Component getCustomEditor() { 
        HashMap<String, Boolean> cities = (HashMap<String, Boolean>) env.getFeatureDescriptor().getValue("labelData"); 
        DefaultListModel model = new DefaultListModel(); 


        /* selection in the gui */ 
        int[] selectedIdxs = new int[cities.size()]; 
        int idx = 0; 
        for (String str : cities.keySet()) { 
            model.addElement(str); 
            if (cities.get(str) == Boolean.FALSE) { 
                selectedIdxs[idx] = model.indexOf(str); 
                idx++; 
            } 
        } 
        if (selectedIdxs.length > 0){ 
            editor.jList.setSelectedIndices(selectedIdxs); 
        } 
        editor.jList.setModel(model); 

        return editor; 
    } 

    @Override 
    public boolean supportsCustomEditor() { 
        return true; 
    } 

    @Override 
    public Object getValue() { 
        System.out.println("getValue(): " + editor.jList.getSelectedValuesList());    
        System.out.println("getValue(): " + editor.jtf.getText()); 

        return super.getValue(); 
    } 
}

and the editor CityPropertyEditorPanel() itself is a simple JPanel with a JList and a JTextField.

My codes creates a nice custom editor with all the items listed, but it is not returning the new selected items from the list. My question is now, how do I get the selected items from the JList back to the CityProperty class? My try was to use

editor.jList.getSelectedValuesList()); 

in the getValue() method but the result is always empty. The same for the JTextField, where a new written value is also not transferred back.

What Am I doing wrong here?

skaffman
  • 398,947
  • 96
  • 818
  • 769
WeserLinux
  • 31
  • 2

1 Answers1

0

I think I found a solution/workaround.

The CityPropertyEditor recognized the content of the "editor" object when I activated the PropertyEnv.STATE_NEEDS_VALIDATION feature. The code then in CityPropertyEditor should have to override the attacheEnv method and include the VetoableChangeListener

@Override
public void attachEnv(PropertyEnv env) {
    this.env = env;
    env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);

    env.addVetoableChangeListener(new VetoableChangeListener() {

        @Override
        public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
            /* User has pushed OK */
            for (Entry entry : editor.isoValNew.entrySet()){
                isoVal.put((Double) entry.getKey(), (Boolean) entry.getValue());
            }
        }
    });
}

while the Jlist in the CityPropertyEditorPanel() itself has a ListSelectionListener who updates the Map variable isoValNew

    isoValueList.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            isoValNew.clear();

            for (Object obj : isoValueList.getSelectedValues()) {
                isoValNew.put((Double) obj, Boolean.TRUE);
            }
        }
    });

I'm sure this is not a perfect solution, but it works fine in my case.

Hope this helps someone.

WeserLinux
  • 31
  • 2