1 19 20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb; 21 22 import java.awt.Component ; 23 import java.util.List ; 24 import javax.swing.AbstractListModel ; 25 import javax.swing.ComboBoxModel ; 26 import javax.swing.DefaultListCellRenderer ; 27 import javax.swing.JComboBox ; 28 import javax.swing.JList ; 29 import org.openide.filesystems.FileObject; 30 import org.openide.util.NbBundle; 31 32 36 public class DBSchemaUISupport { 37 38 private DBSchemaUISupport() { 39 } 40 41 45 public static void connect(JComboBox comboBox, DBSchemaFileList dbschemaFileList) { 46 comboBox.setModel(new DBSchemaModel(dbschemaFileList)); 47 comboBox.setRenderer(new DBSchemaRenderer(comboBox)); 48 } 49 50 54 private static final class DBSchemaModel extends AbstractListModel implements ComboBoxModel { 55 56 private final DBSchemaFileList dbschemaFileList; 57 private Object selectedItem; 58 59 public DBSchemaModel(DBSchemaFileList dbschemaFileList) { 60 this.dbschemaFileList = dbschemaFileList; 61 } 62 63 public void setSelectedItem(Object anItem) { 64 if (dbschemaFileList.getFileList().size() > 0) { 65 selectedItem = anItem; 66 } 67 } 68 69 public Object getElementAt(int index) { 70 List <FileObject> dbschemaFiles = dbschemaFileList.getFileList(); 71 if (dbschemaFiles.size() > 0) { 72 return dbschemaFiles.get(index); 73 } else { 74 return NbBundle.getMessage(DBSchemaUISupport.class, "LBL_NoSchemas"); 75 } 76 } 77 78 public int getSize() { 79 int dbschemaCount = dbschemaFileList.getFileList().size(); 80 return dbschemaCount > 0 ? dbschemaCount : 1; 81 } 82 83 public Object getSelectedItem() { 84 return dbschemaFileList.getFileList().size() > 0 ? selectedItem : NbBundle.getMessage(DBSchemaUISupport.class, "LBL_NoSchemas"); 85 } 86 } 87 88 private static final class DBSchemaRenderer extends DefaultListCellRenderer { 89 90 private JComboBox comboBox; 91 92 public DBSchemaRenderer(JComboBox comboBox) { 93 this.comboBox = comboBox; 94 } 95 96 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 97 Object displayName = null; 98 ComboBoxModel model = comboBox.getModel(); 99 100 if (model instanceof DBSchemaModel && value instanceof FileObject) { 101 displayName = ((DBSchemaModel)model).dbschemaFileList.getDisplayName((FileObject)value); 102 } else { 103 displayName = value; 104 } 105 106 return super.getListCellRendererComponent(list, displayName, index, isSelected, cellHasFocus); 107 } 108 } 109 } 110 | Popular Tags |