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.util.swing.layout; |
|
27 |
|
28 import java.awt.Component; |
|
29 import java.util.HashMap; |
|
30 |
|
31 public abstract class UniqueConstrainedLayout<C> extends ConstrainedLayout<C> { |
|
32 // |
|
33 // Instance data |
|
34 // |
|
35 |
|
36 /* |
|
37 * Mapping from constraint to Component |
|
38 */ |
|
39 protected HashMap<C, Component> constToComp = new HashMap<C, Component>(); |
|
40 |
|
41 // |
|
42 // Constructors |
|
43 // |
|
44 |
|
45 public UniqueConstrainedLayout(C _constraint) { |
|
46 super(_constraint); |
|
47 } |
|
48 |
|
49 // |
|
50 // LayoutManager methods |
|
51 // |
|
52 |
|
53 @Override |
|
54 public void removeLayoutComponent(Component comp) { |
|
55 synchronized (comp.getTreeLock()) { |
|
56 C constraint = compToConst.get(comp); |
|
57 if (constraint != null) { |
|
58 Component c = constToComp.get(constraint); |
|
59 if (c == comp) { |
|
60 constToComp.remove(constraint); |
|
61 } |
|
62 } |
|
63 |
|
64 super.removeLayoutComponent(comp); |
|
65 } |
|
66 } |
|
67 |
|
68 // |
|
69 // LayoutManager2 methods |
|
70 // |
|
71 |
|
72 @Override |
|
73 public void addLayoutComponent(Component comp, Object constraint) { |
|
74 synchronized (comp.getTreeLock()) { |
|
75 super.addLayoutComponent(comp, constraint); |
|
76 |
|
77 @SuppressWarnings({"unchecked"}) |
|
78 C c = constraint == null ? getDefaultConstraint() : (C)constraint; |
|
79 Component oldComp = constToComp.get(c); |
|
80 |
|
81 if (oldComp != comp) { |
|
82 if (oldComp != null) { |
|
83 removeLayoutComponent(oldComp); |
|
84 } |
|
85 constToComp.put(c, comp); |
|
86 } |
|
87 } |
|
88 } |
|
89 |
|
90 // |
|
91 // ConstrainedLayout methods |
|
92 // |
|
93 |
|
94 public C cloneConstraint(C constraint) { |
|
95 return constraint; |
|
96 } |
|
97 |
|
98 // |
|
99 // UniqueConstrainedLayout methods |
|
100 // |
|
101 |
|
102 public Component getComponent(C c) { |
|
103 return constToComp.get(c); |
|
104 } |
|
105 } |
|