KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > wizard > fromdb > DBSchemaUISupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.persistence.wizard.fromdb;
21
22 import java.awt.Component JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.AbstractListModel JavaDoc;
25 import javax.swing.ComboBoxModel JavaDoc;
26 import javax.swing.DefaultListCellRenderer JavaDoc;
27 import javax.swing.JComboBox JavaDoc;
28 import javax.swing.JList JavaDoc;
29 import org.openide.filesystems.FileObject;
30 import org.openide.util.NbBundle;
31
32 /**
33  *
34  * @author Andrei Badea
35  */

36 public class DBSchemaUISupport {
37
38     private DBSchemaUISupport() {
39     }
40
41     /**
42      * Connects a combo box with the list of dbschemas in a project, making
43      * the combo box display these dbschemas.
44      */

45     public static void connect(JComboBox JavaDoc comboBox, DBSchemaFileList dbschemaFileList) {
46         comboBox.setModel(new DBSchemaModel(dbschemaFileList));
47         comboBox.setRenderer(new DBSchemaRenderer(comboBox));
48     }
49
50     /**
51      * Model for database schemas. Contains either a list of schemas (FileObject's)
52      * or a single "no schemas" item.
53      */

54     private static final class DBSchemaModel extends AbstractListModel JavaDoc implements ComboBoxModel JavaDoc {
55
56         private final DBSchemaFileList dbschemaFileList;
57         private Object JavaDoc selectedItem;
58
59         public DBSchemaModel(DBSchemaFileList dbschemaFileList) {
60             this.dbschemaFileList = dbschemaFileList;
61         }
62
63         public void setSelectedItem(Object JavaDoc anItem) {
64             if (dbschemaFileList.getFileList().size() > 0) {
65                 selectedItem = anItem;
66             }
67         }
68
69         public Object JavaDoc getElementAt(int index) {
70             List JavaDoc<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 JavaDoc 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 JavaDoc {
89
90         private JComboBox JavaDoc comboBox;
91
92         public DBSchemaRenderer(JComboBox JavaDoc comboBox) {
93             this.comboBox = comboBox;
94         }
95
96         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
97             Object JavaDoc displayName = null;
98             ComboBoxModel JavaDoc 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