1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
/*
@VaadinApache2LicenseForJavaFiles@
*/
package com.vaadin.ui;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import com.vaadin.ui.Layout.AlignmentHandler;
/**
* Helper class for setting alignments using a short notation.
*
* Supported notation is:
*
* t,top for top alignment
*
* m,middle for vertical center alignment
*
* b,bottom for bottom alignment
*
* l,left for left alignment
*
* c,center for horizontal center alignment
*
* r,right for right alignment
*
* @deprecated {@code AlignmentUtils} has been replaced by {@link Alignment}.
*/
@SuppressWarnings({ "serial" })
@Deprecated
public class AlignmentUtils implements Serializable {
private static int horizontalMask = AlignmentHandler.ALIGNMENT_LEFT
| AlignmentHandler.ALIGNMENT_HORIZONTAL_CENTER
| AlignmentHandler.ALIGNMENT_RIGHT;
private static int verticalMask = AlignmentHandler.ALIGNMENT_TOP
| AlignmentHandler.ALIGNMENT_VERTICAL_CENTER
| AlignmentHandler.ALIGNMENT_BOTTOM;
private static Map<String, Integer> alignmentStrings = new HashMap<String, Integer>();
private static void addMapping(int alignment, String... values) {
for (String s : values) {
alignmentStrings.put(s, alignment);
}
}
static {
addMapping(AlignmentHandler.ALIGNMENT_TOP, "t", "top");
addMapping(AlignmentHandler.ALIGNMENT_BOTTOM, "b", "bottom");
addMapping(AlignmentHandler.ALIGNMENT_VERTICAL_CENTER, "m", "middle");
addMapping(AlignmentHandler.ALIGNMENT_LEFT, "l", "left");
addMapping(AlignmentHandler.ALIGNMENT_RIGHT, "r", "right");
addMapping(AlignmentHandler.ALIGNMENT_HORIZONTAL_CENTER, "c", "center");
}
/**
* Set the alignment for the component using short notation
*
* @param parent
* @param component
* @param alignment
* String containing one or two alignment strings. If short
* notation "r","t",etc is used valid strings include
* "r","rt","tr","t". If the longer notation is used the
* alignments should be separated by a space e.g.
* "right","right top","top right","top". It is valid to mix
* short and long notation but they must be separated by a space
* e.g. "r top".
* @throws IllegalArgumentException
*/
public static void setComponentAlignment(AlignmentHandler parent,
Component component, String alignment)
throws IllegalArgumentException {
if (alignment == null || alignment.length() == 0) {
throw new IllegalArgumentException(
"alignment for setComponentAlignment() cannot be null or empty");
}
Integer currentAlignment = parent.getComponentAlignment(component)
.getBitMask();
if (alignment.length() == 1) {
// Use short form "t","l",...
currentAlignment = parseAlignment(alignment.substring(0, 1),
currentAlignment);
} else if (alignment.length() == 2) {
// Use short form "tr","lb",...
currentAlignment = parseAlignment(alignment.substring(0, 1),
currentAlignment);
currentAlignment = parseAlignment(alignment.substring(1, 2),
currentAlignment);
} else {
// Alignments are separated by space
String[] strings = alignment.split(" ");
if (strings.length > 2) {
throw new IllegalArgumentException(
"alignment for setComponentAlignment() should not contain more than 2 alignments");
}
for (String alignmentString : strings) {
currentAlignment = parseAlignment(alignmentString,
currentAlignment);
}
}
int horizontalAlignment = currentAlignment & horizontalMask;
int verticalAlignment = currentAlignment & verticalMask;
parent.setComponentAlignment(component, new Alignment(
horizontalAlignment + verticalAlignment));
}
/**
* Parse alignmentString which contains one alignment (horizontal or
* vertical) and return and updated version of the passed alignment where
* the alignment in one direction has been changed. If the passed
* alignmentString is unknown an exception is thrown
*
* @param alignmentString
* @param alignment
* @return
* @throws IllegalArgumentException
*/
private static int parseAlignment(String alignmentString, int alignment)
throws IllegalArgumentException {
Integer parsed = alignmentStrings.get(alignmentString.toLowerCase());
if (parsed == null) {
throw new IllegalArgumentException(
"Could not parse alignment string '" + alignmentString
+ "'");
}
if ((parsed & horizontalMask) != 0) {
// Get the vertical alignment from the current alignment
int vertical = (alignment & verticalMask);
// Add the parsed horizontal alignment
alignment = (vertical | parsed);
} else {
// Get the horizontal alignment from the current alignment
int horizontal = (alignment & horizontalMask);
// Add the parsed vertical alignment
alignment = (horizontal | parsed);
}
return alignment;
}
}
|