Search in sources :

Example 1 with MutableComboBoxModel

use of javax.swing.MutableComboBoxModel in project cayenne by apache.

the class PKDBGeneratorPanel method updateView.

void updateView(final DbEntity entity) {
    for (ItemListener listener : attributes.getItemListeners()) {
        attributes.removeItemListener(listener);
    }
    Collection<DbAttribute> pkAttributes = entity.getPrimaryKeys();
    if (pkAttributes.isEmpty()) {
        attributes.removeAllItems();
        attributes.addItem("<Entity has no PK columns>");
        attributes.setSelectedIndex(0);
        attributes.setEnabled(false);
    } else {
        attributes.setEnabled(true);
        MutableComboBoxModel model = new DefaultComboBoxModel(pkAttributes.toArray());
        String noSelection = "<Select Generated Column>";
        model.insertElementAt(noSelection, 0);
        model.setSelectedItem(noSelection);
        attributes.setModel(model);
        for (DbAttribute a : pkAttributes) {
            if (a.isGenerated()) {
                model.setSelectedItem(a);
                break;
            }
        }
        // listen for selection changes of the new entity
        attributes.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent e) {
                Object item = e.getItem();
                if (item instanceof DbAttribute) {
                    boolean generated = e.getStateChange() == ItemEvent.SELECTED;
                    DbAttribute a = (DbAttribute) item;
                    if (a.isGenerated() != generated) {
                        a.setGenerated(generated);
                        mediator.fireDbEntityEvent(new EntityEvent(this, entity));
                    }
                }
            }
        });
    }
    // revalidate as children layout has changed...
    revalidate();
}
Also used : ItemEvent(java.awt.event.ItemEvent) EntityEvent(org.apache.cayenne.map.event.EntityEvent) DbAttribute(org.apache.cayenne.map.DbAttribute) MutableComboBoxModel(javax.swing.MutableComboBoxModel) ItemListener(java.awt.event.ItemListener) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel)

Aggregations

ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 DefaultComboBoxModel (javax.swing.DefaultComboBoxModel)1 MutableComboBoxModel (javax.swing.MutableComboBoxModel)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 EntityEvent (org.apache.cayenne.map.event.EntityEvent)1