|
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.*; |
|
29 import java.util.HashMap; |
|
30 |
|
31 public abstract class ConstrainedLayout<C> extends AbstractLayout |
|
32 implements LayoutManager2 { |
|
33 |
|
34 // |
|
35 // Instance data |
|
36 // |
|
37 |
|
38 // Mapping from Component to constraint |
|
39 protected HashMap<Component, C> compToConst = new HashMap<Component, C>(); |
|
40 private C _constraint; |
|
41 |
|
42 // |
|
43 // Constructors |
|
44 // |
|
45 |
|
46 /** |
|
47 * Constructs a {@code ConstrainedLayout}. |
|
48 * |
|
49 * @param _constraint |
|
50 * the default constraint, if none is specified when a |
|
51 * {@code Component} is added |
|
52 */ |
|
53 public ConstrainedLayout(C _constraint) { |
|
54 setDefaultConstraint(_constraint); |
|
55 } |
|
56 |
|
57 // |
|
58 // LayoutManager methods |
|
59 // |
|
60 |
|
61 @Override |
|
62 public void addLayoutComponent(String name, Component comp) { |
|
63 addLayoutComponent(comp, getDefaultConstraint()); |
|
64 } |
|
65 |
|
66 @Override |
|
67 public void removeLayoutComponent(Component comp) { |
|
68 synchronized (comp.getTreeLock()) { |
|
69 compToConst.remove(comp); |
|
70 } |
|
71 } |
|
72 |
|
73 // |
|
74 // LayoutManager2 methods |
|
75 // |
|
76 |
|
77 @Override |
|
78 @SuppressWarnings({"unchecked"}) |
|
79 public void addLayoutComponent(Component comp, Object constraint) { |
|
80 synchronized (comp.getTreeLock()) { |
|
81 if (constraint == null) { |
|
82 constraint = getDefaultConstraint(); |
|
83 } |
|
84 |
|
85 Class<? extends Object> objClass = constraint.getClass(); |
|
86 Class<? extends C> reqClass = getConstraintClass(); |
|
87 |
|
88 if (reqClass.isAssignableFrom(objClass)) { |
|
89 C clone = cloneConstraint((C)constraint); |
|
90 compToConst.put(comp, clone); |
|
91 } else { |
|
92 throw new IllegalArgumentException(String.format( |
|
93 "invalid constraint (must be %s or null): %s", |
|
94 reqClass.getName(), objClass.getName())); |
|
95 } |
|
96 } |
|
97 } |
|
98 |
|
99 @Override |
|
100 public float getLayoutAlignmentX(Container parent) { |
|
101 return 0.5f; |
|
102 } |
|
103 |
|
104 @Override |
|
105 public float getLayoutAlignmentY(Container parent) { |
|
106 return 0.5f; |
|
107 } |
|
108 |
|
109 @Override |
|
110 public void invalidateLayout(Container container) { |
|
111 } |
|
112 |
|
113 @Override |
|
114 public Dimension maximumLayoutSize(Container container) { |
|
115 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); |
|
116 } |
|
117 |
|
118 // |
|
119 // ConstrainedLayout methods |
|
120 // |
|
121 |
|
122 public abstract C cloneConstraint(C constraint); |
|
123 |
|
124 public C getConstraint(Component c) { |
|
125 return compToConst.get(c); |
|
126 } |
|
127 |
|
128 public C getDefaultConstraint() { |
|
129 return _constraint; |
|
130 } |
|
131 |
|
132 public void setDefaultConstraint(C _constraint) { |
|
133 this._constraint = cloneConstraint(_constraint); |
|
134 } |
|
135 |
|
136 @SuppressWarnings({"unchecked"}) |
|
137 public Class<? extends C> getConstraintClass() { |
|
138 return (Class<? extends C>)getDefaultConstraint().getClass(); |
|
139 } |
|
140 } |