|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.panel.swing.control; |
|
27 |
|
28 import java.beans.*; |
|
29 import java.util.*; |
|
30 import javax.swing.*; |
|
31 import com.oracle.solaris.vp.panel.common.control.*; |
|
32 import com.oracle.solaris.vp.panel.common.model.PanelDescriptor; |
|
33 import com.oracle.solaris.vp.panel.swing.view.ListSelectorPanel; |
|
34 |
|
35 @SuppressWarnings({"serial"}) |
|
36 public class ChildControlListSelectorControl<P extends PanelDescriptor, |
|
37 C extends ListSelectorPanel, O extends ControlManagedObject> |
|
38 extends ListSelectorControl<P, C, O> { |
|
39 |
|
40 // |
|
41 // Inner classes |
|
42 // |
|
43 |
|
44 private static class ManagedObjectListModel extends DefaultListModel |
|
45 implements PropertyChangeListener { |
|
46 |
|
47 @Override |
|
48 public void propertyChange(PropertyChangeEvent evt) { |
|
49 int index = indexOf(evt.getSource()); |
|
50 fireContentsChanged(this, index, index); |
|
51 } |
|
52 } |
|
53 |
|
54 // |
|
55 // Instance data |
|
56 // |
|
57 |
|
58 /* |
|
59 * This list should be kept consistent with the parent's children list. |
|
60 */ |
|
61 protected List<O> objects_ = new ArrayList<O>(); |
|
62 |
|
63 private ListModel model; |
|
64 |
|
65 // |
|
66 // Constructors |
|
67 // |
|
68 |
|
69 public ChildControlListSelectorControl(String id, String name, P descriptor) |
|
70 { |
|
71 super(id, name, descriptor); |
|
72 } |
|
73 |
|
74 // |
|
75 // SwingControl methods |
|
76 // |
|
77 |
|
78 @Override |
|
79 protected void configComponent(C comp) { |
|
80 super.configComponent(comp); |
|
81 |
|
82 // List of Controls is not likely to benefit from multiple selection |
|
83 getComponent().getSelectionComponent().setSelectionMode( |
|
84 ListSelectionModel.SINGLE_SELECTION); |
|
85 } |
|
86 |
|
87 // |
|
88 // ListSelectorControl methods |
|
89 // |
|
90 |
|
91 @Override |
|
92 protected int getListIndexOf(Control child) { |
|
93 return children.indexOf(child); |
|
94 } |
|
95 |
|
96 @Override |
|
97 protected ListModel getListModel() { |
|
98 if (model == null) { |
|
99 ensureChildrenCreated(); |
|
100 ManagedObjectListModel dModel = new ManagedObjectListModel(); |
|
101 for (O child : objects_) { |
|
102 dModel.addElement(child); |
|
103 child.addPropertyChangeListener(dModel); |
|
104 } |
|
105 model = dModel; |
|
106 } |
|
107 |
|
108 return model; |
|
109 } |
|
110 |
|
111 @Override |
|
112 protected Navigable[] getPathForSelection(List<O> selection) { |
|
113 if (selection.size() != 1) { |
|
114 return null; |
|
115 } |
|
116 |
|
117 Control selected = selection.get(0).getControl(); |
|
118 |
|
119 return new Navigable[] { |
|
120 new SimpleNavigable(selected.getId(), selected.getName()) |
|
121 }; |
|
122 } |
|
123 |
|
124 // |
|
125 // ChildControlListSelectorControl methods |
|
126 // |
|
127 |
|
128 public void addChild(O child) { |
|
129 objects_.add(child); |
|
130 super.addChildren(child.getControl()); |
|
131 addToLayout(child.getControl()); |
|
132 } |
|
133 } |