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