KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > websphere6 > ui > InstancesModel


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 package org.netbeans.modules.j2ee.websphere6.ui;
20
21 import javax.swing.*;
22 import java.util.*;
23
24 /**
25  * A combobox model that represents the list of local instances. It
26  * contains a vector of objects of Instance class that contain all data
27  * for the instance
28  *
29  * @author Kirill Sorokin
30  * @author Dmitry Lipin
31  */

32 public class InstancesModel extends AbstractListModel
33         implements ComboBoxModel {
34     /**
35      * A vector with the instances
36      */

37     private Vector instances;
38
39     /**
40      * The index of the selected instance
41      */

42     private int selectedIndex = 0;
43     
44     /**
45      * Creates a new instance of InstancesModel
46      *
47      * @param instances a vector with the locally found instances
48      */

49     public InstancesModel(Vector instances) {
50         // save the instances
51
this.instances = instances;
52         
53         // set the selected index to zero
54
this.selectedIndex = 0;
55     }
56     
57     /**
58      * Sets the selected index to the index of the supplied item
59      *
60      * @param item the instance which should be selected
61      */

62     public void setSelectedItem(Object JavaDoc item) {
63         // set the index to the given item's index or to -1
64
// if the item does not exists
65
selectedIndex = instances.indexOf(item);
66     }
67     
68     /**
69      * Get the instance with the specified instance
70      *
71      * @param index the index of the desired instance
72      *
73      * @return the instance at the given index
74      */

75     public Object JavaDoc getElementAt(int index) {
76         return instances.elementAt(index);
77     }
78     
79     /**
80      * Returns the total number of instances
81      *
82      * @return the number of instances
83      */

84     public int getSize() {
85         return instances.size();
86     }
87     
88     /**
89      * Returns the instance at the selected index
90      *
91      * @return the instance at the selected index
92      */

93     public Object JavaDoc getSelectedItem() {
94         // if there are no instances return null
95
if (instances.size() == 0) {
96             return null;
97         }
98         
99         // return the element at the index
100
return instances.elementAt(selectedIndex);
101     }
102 }
103
Popular Tags