01: /*
02: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
03: *
04: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
05: *
06: * The contents of this file are subject to the terms of either the GNU
07: * General Public License Version 2 only ("GPL") or the Common
08: * Development and Distribution License("CDDL") (collectively, the
09: * "License"). You may not use this file except in compliance with the
10: * License. You can obtain a copy of the License at
11: * http://www.netbeans.org/cddl-gplv2.html
12: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13: * specific language governing permissions and limitations under the
14: * License. When distributing the software, include this License Header
15: * Notice in each file and include the License file at
16: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17: * particular file as subject to the "Classpath" exception as provided
18: * by Sun in the GPL Version 2 section of the License file that
19: * accompanied this code. If applicable, add the following below the
20: * License Header, with the fields enclosed by brackets [] replaced by
21: * your own identifying information:
22: * "Portions Copyrighted [year] [name of copyright owner]"
23: *
24: * Contributor(s):
25: *
26: * Portions Copyrighted 2007 Sun Microsystems, Inc.
27: */
28: package org.netbeans.modules.search;
29:
30: import java.util.List;
31: import javax.swing.ComboBoxModel;
32: import javax.swing.event.ListDataListener;
33:
34: /**
35: * Unmodifiable {@code ComboBoxModel} built on a {@link java.util.List}
36: * of elements.
37: * This implementation is very simple and assumes that the passed list
38: * of elements is not empty and is not modified during this model's lifetime.
39: *
40: * @author Marian Petras
41: */
42: final class ListComboBoxModel implements ComboBoxModel {
43:
44: private final List<? extends Object> elements;
45: private final int maxIndex;
46: private final boolean reverseOrder;
47: private Object selectedItem;
48:
49: public ListComboBoxModel(List<? extends Object> elements) {
50: this (elements, false);
51: }
52:
53: public ListComboBoxModel(List<? extends Object> elements,
54: final boolean reverseOrder) {
55: if (elements == null) {
56: throw new IllegalArgumentException(
57: "the list of elements must not be null"); //NOI18N
58: }
59: if (elements.isEmpty()) {
60: throw new IllegalArgumentException(
61: "empty list of elements is not allowed"); //NOI18N
62: }
63: this .elements = elements;
64: this .maxIndex = elements.size() - 1;
65: this .reverseOrder = reverseOrder;
66: }
67:
68: public void setSelectedItem(Object item) {
69: this .selectedItem = item;
70: }
71:
72: public Object getSelectedItem() {
73: return selectedItem;
74: }
75:
76: public int getSize() {
77: return maxIndex + 1;
78: }
79:
80: public Object getElementAt(int index) {
81: return elements.get(reverseOrder ? maxIndex - index : index);
82: }
83:
84: public void addListDataListener(ListDataListener l) {
85: /*
86: * Does nothing as the data listeners would never be notified
87: * of any change.
88: */
89: }
90:
91: public void removeListDataListener(ListDataListener l) {
92: /*
93: * Does nothing as the data listeners would never be notified
94: * of any change.
95: */
96: }
97:
98: }
|