]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/ui/iCheckBoxList/ICheckBoxList.java
1. Update ICheckBoxList to add one attribute "selected". Set the first item of ICheck...
[mirror_edk2.git] / Tools / Java / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / common / ui / iCheckBoxList / ICheckBoxList.java
1 /** @file
2
3 The file is used to override JList to create a List with CheckBox item
4
5 Copyright (c) 2006, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15 package org.tianocore.frameworkwizard.common.ui.iCheckBoxList;
16
17 import java.util.Vector;
18
19 import javax.swing.JList;
20
21 public class ICheckBoxList extends JList {
22 ///
23 /// Define class Serial Version UID
24 ///
25 private static final long serialVersionUID = -2843059688070447632L;
26
27 protected ICheckBoxListCellRenderer cellrenderer = new ICheckBoxListCellRenderer();
28
29 protected ICheckBoxListener listener = new ICheckBoxListener(this);
30
31 protected ICheckBoxListModel model = new ICheckBoxListModel();
32
33 /**
34 This the default Constructor
35
36 **/
37 public ICheckBoxList() {
38 this(null);
39 }
40
41 /**
42 This the override constructor to create checkbox item with input vector
43
44 @param options
45
46 **/
47 public ICheckBoxList(Vector<ICheckBoxListItem> items) {
48 if (items != null) {
49 for (int index = 0; index < items.size(); index++) {
50 model.addElement(items.elementAt(index));
51 }
52 }
53
54 //
55 // If there exists at least one item, set first item selected.
56 //
57 if (model.size() > 0) {
58 ICheckBoxListItem listItem = (ICheckBoxListItem) model.get(0);
59 listItem.setSelected(true);
60 }
61 this.setCellRenderer(cellrenderer);
62 this.setModel(model);
63 this.addMouseListener(listener);
64 this.addKeyListener(listener);
65 }
66
67 /**
68 Set all items of the CheckBoxList component.
69
70 @param items
71
72 **/
73 public void setAllItems(Vector<String> items) {
74 if (items != null) {
75 model.removeAllElements();
76 for (int index = 0; index < items.size(); index++) {
77 model.addElement(new ICheckBoxListItem(items.elementAt(index)));
78 }
79 }
80
81 //
82 // If there exists at least one item, set first item selected.
83 //
84 if (model.size() > 0) {
85 ICheckBoxListItem listItem = (ICheckBoxListItem) model.get(0);
86 listItem.setSelected(true);
87 }
88 }
89
90 /**
91 Get All Checked Items of the CheckBoxList component.
92
93 @return All Checked Items
94 **/
95 public Vector<ICheckBoxListItem> getAllCheckedItems() {
96 Vector<ICheckBoxListItem> result = new Vector<ICheckBoxListItem>();
97
98 for (int i = 0; i < this.getModel().getSize(); i++) {
99 if (((ICheckBoxListItem) this.getModel().getElementAt(i)).isChecked()) {
100 result.addElement((ICheckBoxListItem) this.getModel().getElementAt(i));
101 }
102 }
103 return result;
104 }
105
106 /**
107 Get All Checked Items index of the CheckBoxList component.
108
109 @return All Checked Items index
110 **/
111 public Vector<Integer> getAllCheckedItemsIndex() {
112 Vector<Integer> result = new Vector<Integer>();
113
114 for (int i = 0; i < this.getModel().getSize(); i++) {
115 if (((ICheckBoxListItem) this.getModel().getElementAt(i)).isChecked()) {
116 result.addElement(i);
117 }
118 }
119 return result;
120 }
121
122 /**
123 Get All Checked Items String of the CheckBoxList component.
124
125 @return Vector
126 **/
127 public Vector<String> getAllCheckedItemsString() {
128 Vector<String> result = new Vector<String>();
129
130 for (int i = 0; i < this.getModel().getSize(); i++) {
131 if (((ICheckBoxListItem) this.getModel().getElementAt(i)).isChecked()) {
132 result.addElement(((ICheckBoxListItem) this.getModel().getElementAt(i)).text);
133 }
134 }
135 return result;
136 }
137
138 /**
139 Get All Items String of the CheckBoxList component.
140
141 @return Vector
142 **/
143 public Vector<String> getAllItemsString() {
144 Vector<String> result = new Vector<String>();
145
146 for (int i = 0; i < this.getModel().getSize(); i++) {
147 result.addElement(((ICheckBoxListItem) this.getModel().getElementAt(i)).text);
148 }
149 return result;
150 }
151
152 /**
153 Set Checked status for all input items.
154
155 **/
156 public void initCheckedItem(boolean bool, Vector<String> items) {
157 if (items != null && items.size() != 0) {
158 for (int indexI = 0; indexI < items.size(); indexI++) {
159 for (int indexJ = 0; indexJ < model.size(); indexJ++) {
160 if (items.elementAt(indexI).equals(model.getAllElements().elementAt(indexJ).getText())) {
161 ICheckBoxListItem listItem = (ICheckBoxListItem) model.get(indexJ);
162 listItem.setChecked(bool);
163 break;
164 }
165 }
166 }
167 }
168
169 //
170 // If there exists at least one item, set first item selected.
171 //
172 if (model.size() > 0) {
173 ICheckBoxListItem listItem = (ICheckBoxListItem) model.get(0);
174 listItem.setSelected(true);
175 }
176
177 this.validate();
178 }
179
180 /**
181 Set all items of the compontent checked
182
183 **/
184 public void setAllItemsChecked() {
185 initCheckedItem(true, this.getAllItemsString());
186 }
187
188 /**
189 Set all items of the compontent unchecked
190
191 **/
192 public void setAllItemsUnchecked() {
193 initCheckedItem(false, this.getAllItemsString());
194 }
195
196 /**
197 Remove all items of list
198
199 **/
200 public void removeAllItem() {
201 model.removeAllElements();
202 }
203 }