]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.browsing.ui/src/org/simantics/browsing/ui/Column.java b/bundles/org.simantics.browsing.ui/src/org/simantics/browsing/ui/Column.java
new file mode 100644 (file)
index 0000000..9ef15f1
--- /dev/null
@@ -0,0 +1,268 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui;\r
+\r
+\r
+/**\r
+ * Immutable descriptor for an IGraphExplorer Column.\r
+ * \r
+ * Columns can be specified to be space-grabbing or non-space-grabbing. When a\r
+ * column is marked as grabbing, it will receive/lose its own weight's worth of\r
+ * screen space when the parenting control is resized in a way that allows\r
+ * columns to shrink or grow.\r
+ * \r
+ * Each column is identified by a string that must be unique among the set of\r
+ * columns used at a time. The key must be a valid string, <code>null</code> is\r
+ * not allowed.\r
+ * \r
+ * See <code>@see org.simantics.browsing.ui.common.ColumnKeys</code> for some\r
+ * commonly usable column keys.\r
+ * \r
+ * @see ColumnFactory\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public final class Column {\r
+\r
+    public static enum Align {\r
+        LEFT,\r
+        CENTER,\r
+        RIGHT\r
+    }\r
+\r
+    public static final int UNDEFINED_CONTROL_WIDTH = -1;\r
+\r
+    public static final int DEFAULT_CONTROL_WIDTH   = UNDEFINED_CONTROL_WIDTH;\r
+\r
+    /**\r
+     * Name of the column.\r
+     * @see ColumnKeys\r
+     */\r
+    private final String key;\r
+\r
+    /**\r
+     * Text to be shown in the column.\r
+     * @see ColumnKeys\r
+     */\r
+    private final String label;\r
+\r
+    /**\r
+     * Content alignment used by this column.\r
+     */\r
+    private final Align  alignment;\r
+\r
+    /**\r
+     * -1 means not specified. If grab == false, this describes the default\r
+     * width, if grab == true, this describes the minimum width of the column.\r
+     */\r
+    private final int    width;\r
+\r
+    /**\r
+     * A string to show as tooltip or <code>null</code>.\r
+     */\r
+    private final String tooltip;\r
+\r
+    /**\r
+     * Indicates whether this column participates in grabbing extra widget space\r
+     * that is freed when columns are resized.\r
+     */\r
+    private final boolean grab;\r
+\r
+    /**\r
+     * Weight describes how much a space-grabbing column will grab or release of\r
+     * screen space that is gained or lost when controls are resized. The\r
+     * percentage of space given to a column is defined by the formula:\r
+     * <code>100*weight / sum(all column weights)</code>\r
+     */\r
+    private final int weight;\r
+\r
+    public Column(String key) {\r
+        this(key, key, Align.LEFT);\r
+    }\r
+\r
+    public Column(String key, String label, Align alignment) {\r
+        this(key, label, alignment, DEFAULT_CONTROL_WIDTH);\r
+    }\r
+\r
+    public Column(String key, String label, Align alignment, int width) {\r
+        this(key, label, alignment, width, null);\r
+    }\r
+\r
+    public Column(String key, Align alignment, int width) {\r
+        this(key, key, alignment, width, null);\r
+    }\r
+\r
+    public Column(String key, Align alignment, int width, String tooltip) {\r
+        this(key, key, alignment, width, tooltip);\r
+    }\r
+\r
+    public Column(String key, Align alignment, int width, String tooltip, boolean grab) {\r
+        this(key, key, alignment, width, tooltip, grab);\r
+    }\r
+\r
+    public Column(String key, Align alignment, int width, String tooltip, boolean grab, int weight) {\r
+        this(key, key, alignment, width, tooltip, grab, weight);\r
+    }\r
+\r
+    public Column(String key, String label, Align alignment, int width, String tooltip) {\r
+        this(key, label, alignment, width, tooltip, false);\r
+    }\r
+\r
+    public Column(String key, String label, Align alignment, int width, String tooltip, boolean grab) {\r
+        this(key, label, alignment, width, tooltip, grab, 1);\r
+    }\r
+\r
+    public Column(String key, String label, Align alignment, int width, String tooltip, boolean grab, int weight) {\r
+        if (alignment == null)\r
+            throw new IllegalArgumentException("null alignment");\r
+        if (key == null)\r
+            throw new IllegalArgumentException("null key");\r
+\r
+        this.key = key;\r
+        this.label = label;\r
+        this.alignment = alignment;\r
+        this.width = width;\r
+        this.tooltip = tooltip;\r
+        this.grab = grab;\r
+        this.weight = weight;\r
+    }\r
+\r
+    public String getKey() {\r
+        return key;\r
+    }\r
+\r
+    public String getLabel() {\r
+        return label;\r
+    }\r
+\r
+    public Align getAlignment() {\r
+        return alignment;\r
+    }\r
+\r
+    public int getWidth() {\r
+        return width;\r
+    }\r
+\r
+    public String getTooltip() {\r
+        return tooltip;\r
+    }\r
+\r
+    public boolean hasGrab() {\r
+        return grab;\r
+    }\r
+\r
+    public int getWeight() {\r
+        return weight;\r
+    }\r
+\r
+    public Column withKey(String key) {\r
+        return new Column(key, this.label, this.alignment, this.width, this.tooltip, this.grab, this.weight);\r
+    }\r
+\r
+    public Column withLabel(String label) {\r
+        return new Column(this.key, label, this.alignment, this.width, this.tooltip, this.grab, this.weight);\r
+    }\r
+\r
+    public Column withAlignment(Align alignment) {\r
+        return new Column(this.key, this.label, alignment, this.width, this.tooltip, this.grab, this.weight);\r
+    }\r
+\r
+    public Column withWidth(int width) {\r
+        return new Column(this.key, this.label, this.alignment, width, this.tooltip, this.grab, this.weight);\r
+    }\r
+\r
+    public Column withTooltip(String tooltip) {\r
+        return new Column(this.key, this.label, this.alignment, this.width, tooltip, this.grab, this.weight);\r
+    }\r
+\r
+    public Column withGrab(boolean grab) {\r
+        return new Column(this.key, this.label, this.alignment, this.width, this.tooltip, grab, this.weight);\r
+    }\r
+\r
+    public Column withWeight(int weight) {\r
+        return new Column(this.key, this.label, this.alignment, this.width, this.tooltip, this.grab, weight);\r
+    }\r
+    \r
+    @Override\r
+    public int hashCode() {\r
+        final int prime = 31;\r
+        int result = 1;\r
+        result = prime * result + alignment.hashCode();\r
+        result = prime * result + (grab ? 1231 : 1237);\r
+        result = prime * result + ((key == null) ? 0 : key.hashCode());\r
+        result = prime * result + ((label == null) ? 0 : label.hashCode());\r
+        result = prime * result + ((tooltip == null) ? 0 : tooltip.hashCode());\r
+        result = prime * result + width;\r
+        result = prime * result + weight;\r
+        return result;\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object obj) {\r
+        if (this == obj)\r
+            return true;\r
+        if (obj == null)\r
+            return false;\r
+        if (getClass() != obj.getClass())\r
+            return false;\r
+        Column other = (Column) obj;\r
+        if (alignment != other.alignment)\r
+            return false;\r
+        if (grab != other.grab)\r
+            return false;\r
+        if (key == null) {\r
+            if (other.key != null)\r
+                return false;\r
+        } else if (!key.equals(other.key))\r
+            return false;\r
+        if (label == null) {\r
+            if (other.label != null)\r
+                return false;\r
+        } else if (!label.equals(other.label))\r
+            return false;\r
+        if (tooltip == null) {\r
+            if (other.tooltip != null)\r
+                return false;\r
+        } else if (!tooltip.equals(other.tooltip))\r
+            return false;\r
+        if (width != other.width)\r
+            return false;\r
+        if (weight != other.weight)\r
+            return false;\r
+        return true;\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        StringBuilder sb = new StringBuilder();\r
+        sb.append("Column[key=");\r
+        sb.append(key);\r
+        if (!key.equals(label))\r
+            sb.append(", label=");\r
+        sb.append(label);\r
+        sb.append(", align=");\r
+        sb.append(alignment);\r
+        sb.append(", width=");\r
+        sb.append(width);\r
+        if (!label.equals(tooltip)) {\r
+            sb.append(", tooltip=");\r
+            sb.append(tooltip);\r
+        }\r
+        sb.append(", grab=");\r
+        sb.append(grab);\r
+        sb.append(", weight=");\r
+        sb.append(weight);\r
+        sb.append("]");\r
+        return sb.toString();\r
+    }\r
+\r
+}\r