1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui;
16 * Immutable descriptor for an IGraphExplorer Column.
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.
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
27 * See <code>@see org.simantics.browsing.ui.common.ColumnKeys</code> for some
28 * commonly usable column keys.
32 * @author Tuukka Lehtonen
34 public final class Column {
36 public static enum Align {
42 public static final int UNDEFINED_CONTROL_WIDTH = -1;
44 public static final int DEFAULT_CONTROL_WIDTH = UNDEFINED_CONTROL_WIDTH;
50 private final String key;
53 * Text to be shown in the column.
56 private final String label;
59 * Content alignment used by this column.
61 private final Align alignment;
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.
67 private final int width;
70 * A string to show as tooltip or <code>null</code>.
72 private final String tooltip;
75 * Indicates whether this column participates in grabbing extra widget space
76 * that is freed when columns are resized.
78 private final boolean grab;
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>
86 private final int weight;
88 public Column(String key) {
89 this(key, key, Align.LEFT);
92 public Column(String key, String label, Align alignment) {
93 this(key, label, alignment, DEFAULT_CONTROL_WIDTH);
96 public Column(String key, String label, Align alignment, int width) {
97 this(key, label, alignment, width, null);
100 public Column(String key, Align alignment, int width) {
101 this(key, key, alignment, width, null);
104 public Column(String key, Align alignment, int width, String tooltip) {
105 this(key, key, alignment, width, tooltip);
108 public Column(String key, Align alignment, int width, String tooltip, boolean grab) {
109 this(key, key, alignment, width, tooltip, grab);
112 public Column(String key, Align alignment, int width, String tooltip, boolean grab, int weight) {
113 this(key, key, alignment, width, tooltip, grab, weight);
116 public Column(String key, String label, Align alignment, int width, String tooltip) {
117 this(key, label, alignment, width, tooltip, false);
120 public Column(String key, String label, Align alignment, int width, String tooltip, boolean grab) {
121 this(key, label, alignment, width, tooltip, grab, 1);
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"); //$NON-NLS-1$
128 throw new IllegalArgumentException("null key"); //$NON-NLS-1$
132 this.alignment = alignment;
134 this.tooltip = tooltip;
136 this.weight = weight;
139 public String getKey() {
143 public String getLabel() {
147 public Align getAlignment() {
151 public int getWidth() {
155 public String getTooltip() {
159 public boolean hasGrab() {
163 public int getWeight() {
167 public Column withKey(String key) {
168 return new Column(key, this.label, this.alignment, this.width, this.tooltip, this.grab, this.weight);
171 public Column withLabel(String label) {
172 return new Column(this.key, label, this.alignment, this.width, this.tooltip, this.grab, this.weight);
175 public Column withAlignment(Align alignment) {
176 return new Column(this.key, this.label, alignment, this.width, this.tooltip, this.grab, this.weight);
179 public Column withWidth(int width) {
180 return new Column(this.key, this.label, this.alignment, width, this.tooltip, this.grab, this.weight);
183 public Column withTooltip(String tooltip) {
184 return new Column(this.key, this.label, this.alignment, this.width, tooltip, this.grab, this.weight);
187 public Column withGrab(boolean grab) {
188 return new Column(this.key, this.label, this.alignment, this.width, this.tooltip, grab, this.weight);
191 public Column withWeight(int weight) {
192 return new Column(this.key, this.label, this.alignment, this.width, this.tooltip, this.grab, weight);
196 public int hashCode() {
197 final int prime = 31;
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;
210 public boolean equals(Object obj) {
215 if (getClass() != obj.getClass())
217 Column other = (Column) obj;
218 if (alignment != other.alignment)
220 if (grab != other.grab)
223 if (other.key != null)
225 } else if (!key.equals(other.key))
228 if (other.label != null)
230 } else if (!label.equals(other.label))
232 if (tooltip == null) {
233 if (other.tooltip != null)
235 } else if (!tooltip.equals(other.tooltip))
237 if (width != other.width)
239 if (weight != other.weight)
245 public String toString() {
246 StringBuilder sb = new StringBuilder();
247 sb.append("Column[key="); //$NON-NLS-1$
249 if (!key.equals(label))
250 sb.append(", label="); //$NON-NLS-1$
252 sb.append(", align="); //$NON-NLS-1$
253 sb.append(alignment);
254 sb.append(", width="); //$NON-NLS-1$
256 if (!label.equals(tooltip)) {
257 sb.append(", tooltip="); //$NON-NLS-1$
260 sb.append(", grab="); //$NON-NLS-1$
262 sb.append(", weight="); //$NON-NLS-1$
264 sb.append("]"); //$NON-NLS-1$
265 return sb.toString();