]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui/src/org/simantics/browsing/ui/Column.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui / src / org / simantics / browsing / ui / Column.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui;\r
13 \r
14 \r
15 /**\r
16  * Immutable descriptor for an IGraphExplorer Column.\r
17  * \r
18  * Columns can be specified to be space-grabbing or non-space-grabbing. When a\r
19  * column is marked as grabbing, it will receive/lose its own weight's worth of\r
20  * screen space when the parenting control is resized in a way that allows\r
21  * columns to shrink or grow.\r
22  * \r
23  * Each column is identified by a string that must be unique among the set of\r
24  * columns used at a time. The key must be a valid string, <code>null</code> is\r
25  * not allowed.\r
26  * \r
27  * See <code>@see org.simantics.browsing.ui.common.ColumnKeys</code> for some\r
28  * commonly usable column keys.\r
29  * \r
30  * @see ColumnFactory\r
31  * \r
32  * @author Tuukka Lehtonen\r
33  */\r
34 public final class Column {\r
35 \r
36     public static enum Align {\r
37         LEFT,\r
38         CENTER,\r
39         RIGHT\r
40     }\r
41 \r
42     public static final int UNDEFINED_CONTROL_WIDTH = -1;\r
43 \r
44     public static final int DEFAULT_CONTROL_WIDTH   = UNDEFINED_CONTROL_WIDTH;\r
45 \r
46     /**\r
47      * Name of the column.\r
48      * @see ColumnKeys\r
49      */\r
50     private final String key;\r
51 \r
52     /**\r
53      * Text to be shown in the column.\r
54      * @see ColumnKeys\r
55      */\r
56     private final String label;\r
57 \r
58     /**\r
59      * Content alignment used by this column.\r
60      */\r
61     private final Align  alignment;\r
62 \r
63     /**\r
64      * -1 means not specified. If grab == false, this describes the default\r
65      * width, if grab == true, this describes the minimum width of the column.\r
66      */\r
67     private final int    width;\r
68 \r
69     /**\r
70      * A string to show as tooltip or <code>null</code>.\r
71      */\r
72     private final String tooltip;\r
73 \r
74     /**\r
75      * Indicates whether this column participates in grabbing extra widget space\r
76      * that is freed when columns are resized.\r
77      */\r
78     private final boolean grab;\r
79 \r
80     /**\r
81      * Weight describes how much a space-grabbing column will grab or release of\r
82      * screen space that is gained or lost when controls are resized. The\r
83      * percentage of space given to a column is defined by the formula:\r
84      * <code>100*weight / sum(all column weights)</code>\r
85      */\r
86     private final int weight;\r
87 \r
88     public Column(String key) {\r
89         this(key, key, Align.LEFT);\r
90     }\r
91 \r
92     public Column(String key, String label, Align alignment) {\r
93         this(key, label, alignment, DEFAULT_CONTROL_WIDTH);\r
94     }\r
95 \r
96     public Column(String key, String label, Align alignment, int width) {\r
97         this(key, label, alignment, width, null);\r
98     }\r
99 \r
100     public Column(String key, Align alignment, int width) {\r
101         this(key, key, alignment, width, null);\r
102     }\r
103 \r
104     public Column(String key, Align alignment, int width, String tooltip) {\r
105         this(key, key, alignment, width, tooltip);\r
106     }\r
107 \r
108     public Column(String key, Align alignment, int width, String tooltip, boolean grab) {\r
109         this(key, key, alignment, width, tooltip, grab);\r
110     }\r
111 \r
112     public Column(String key, Align alignment, int width, String tooltip, boolean grab, int weight) {\r
113         this(key, key, alignment, width, tooltip, grab, weight);\r
114     }\r
115 \r
116     public Column(String key, String label, Align alignment, int width, String tooltip) {\r
117         this(key, label, alignment, width, tooltip, false);\r
118     }\r
119 \r
120     public Column(String key, String label, Align alignment, int width, String tooltip, boolean grab) {\r
121         this(key, label, alignment, width, tooltip, grab, 1);\r
122     }\r
123 \r
124     public Column(String key, String label, Align alignment, int width, String tooltip, boolean grab, int weight) {\r
125         if (alignment == null)\r
126             throw new IllegalArgumentException("null alignment");\r
127         if (key == null)\r
128             throw new IllegalArgumentException("null key");\r
129 \r
130         this.key = key;\r
131         this.label = label;\r
132         this.alignment = alignment;\r
133         this.width = width;\r
134         this.tooltip = tooltip;\r
135         this.grab = grab;\r
136         this.weight = weight;\r
137     }\r
138 \r
139     public String getKey() {\r
140         return key;\r
141     }\r
142 \r
143     public String getLabel() {\r
144         return label;\r
145     }\r
146 \r
147     public Align getAlignment() {\r
148         return alignment;\r
149     }\r
150 \r
151     public int getWidth() {\r
152         return width;\r
153     }\r
154 \r
155     public String getTooltip() {\r
156         return tooltip;\r
157     }\r
158 \r
159     public boolean hasGrab() {\r
160         return grab;\r
161     }\r
162 \r
163     public int getWeight() {\r
164         return weight;\r
165     }\r
166 \r
167     public Column withKey(String key) {\r
168         return new Column(key, this.label, this.alignment, this.width, this.tooltip, this.grab, this.weight);\r
169     }\r
170 \r
171     public Column withLabel(String label) {\r
172         return new Column(this.key, label, this.alignment, this.width, this.tooltip, this.grab, this.weight);\r
173     }\r
174 \r
175     public Column withAlignment(Align alignment) {\r
176         return new Column(this.key, this.label, alignment, this.width, this.tooltip, this.grab, this.weight);\r
177     }\r
178 \r
179     public Column withWidth(int width) {\r
180         return new Column(this.key, this.label, this.alignment, width, this.tooltip, this.grab, this.weight);\r
181     }\r
182 \r
183     public Column withTooltip(String tooltip) {\r
184         return new Column(this.key, this.label, this.alignment, this.width, tooltip, this.grab, this.weight);\r
185     }\r
186 \r
187     public Column withGrab(boolean grab) {\r
188         return new Column(this.key, this.label, this.alignment, this.width, this.tooltip, grab, this.weight);\r
189     }\r
190 \r
191     public Column withWeight(int weight) {\r
192         return new Column(this.key, this.label, this.alignment, this.width, this.tooltip, this.grab, weight);\r
193     }\r
194     \r
195     @Override\r
196     public int hashCode() {\r
197         final int prime = 31;\r
198         int result = 1;\r
199         result = prime * result + alignment.hashCode();\r
200         result = prime * result + (grab ? 1231 : 1237);\r
201         result = prime * result + ((key == null) ? 0 : key.hashCode());\r
202         result = prime * result + ((label == null) ? 0 : label.hashCode());\r
203         result = prime * result + ((tooltip == null) ? 0 : tooltip.hashCode());\r
204         result = prime * result + width;\r
205         result = prime * result + weight;\r
206         return result;\r
207     }\r
208 \r
209     @Override\r
210     public boolean equals(Object obj) {\r
211         if (this == obj)\r
212             return true;\r
213         if (obj == null)\r
214             return false;\r
215         if (getClass() != obj.getClass())\r
216             return false;\r
217         Column other = (Column) obj;\r
218         if (alignment != other.alignment)\r
219             return false;\r
220         if (grab != other.grab)\r
221             return false;\r
222         if (key == null) {\r
223             if (other.key != null)\r
224                 return false;\r
225         } else if (!key.equals(other.key))\r
226             return false;\r
227         if (label == null) {\r
228             if (other.label != null)\r
229                 return false;\r
230         } else if (!label.equals(other.label))\r
231             return false;\r
232         if (tooltip == null) {\r
233             if (other.tooltip != null)\r
234                 return false;\r
235         } else if (!tooltip.equals(other.tooltip))\r
236             return false;\r
237         if (width != other.width)\r
238             return false;\r
239         if (weight != other.weight)\r
240             return false;\r
241         return true;\r
242     }\r
243 \r
244     @Override\r
245     public String toString() {\r
246         StringBuilder sb = new StringBuilder();\r
247         sb.append("Column[key=");\r
248         sb.append(key);\r
249         if (!key.equals(label))\r
250             sb.append(", label=");\r
251         sb.append(label);\r
252         sb.append(", align=");\r
253         sb.append(alignment);\r
254         sb.append(", width=");\r
255         sb.append(width);\r
256         if (!label.equals(tooltip)) {\r
257             sb.append(", tooltip=");\r
258             sb.append(tooltip);\r
259         }\r
260         sb.append(", grab=");\r
261         sb.append(grab);\r
262         sb.append(", weight=");\r
263         sb.append(weight);\r
264         sb.append("]");\r
265         return sb.toString();\r
266     }\r
267 \r
268 }\r