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