]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.selectionview/src/org/simantics/selectionview/ComparableTabContributor.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / ComparableTabContributor.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.selectionview;
13
14 import org.eclipse.swt.graphics.Image;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.ui.IWorkbenchSite;
17 import org.simantics.db.management.ISessionContext;
18
19 /**
20  * A wrapper for {@link PropertyTabContributor} to label/image it and make it
21  * comparable by assigning a number to it. Used in {@link SelectionProcessor}
22  * implementations to contribute tabs to tabbed property pages.
23  * 
24  * @author Tuukka Lehtonen
25  * @see PropertyTabContributor
26  */
27 public class ComparableTabContributor implements PropertyTabContributor, Comparable<ComparableTabContributor> {
28
29     private final PropertyTabContributor orig;
30     private final double                 priority;
31     private final Object                 input;
32     private final String                 label;
33     private final String                 id;
34     private final Image                  image;
35
36     public ComparableTabContributor(PropertyTabContributor orig, double priority, Object input, String label) {
37         this(orig, priority, input, label, null);
38     }
39
40     public ComparableTabContributor(PropertyTabContributor orig, double priority, Object input, String label, Image image) {
41         this(orig, priority, input, label, label, image);
42     }
43
44     public ComparableTabContributor(PropertyTabContributor orig, double priority, Object input, String label, String id, Image image) {
45         if (orig == null)
46             throw new NullPointerException("null property tab contributor");
47         if (input == null)
48             throw new NullPointerException("null input");
49         if (label == null)
50             throw new NullPointerException("null label");
51         this.orig = orig;
52         this.priority = priority;
53         this.input = input;
54         this.label = label;
55         this.id = id;
56         this.image = image;
57     }
58
59     @Override
60     public IPropertyTab create(Composite parent, IWorkbenchSite site, ISessionContext context, Object input) {
61         return orig.create(parent, site, context, input);
62     }
63
64     public String getLabel() {
65         return label;
66     }
67
68     public Image getImage() {
69         return image;
70     }
71
72     public Object getInput() {
73         return input;
74     }
75     
76     public String getId() {
77         return id;
78     }
79
80     @Override
81     public int compareTo(ComparableTabContributor o) {
82         int comp = Double.compare(o.priority, priority);
83         if(comp != 0) return comp;
84         return id.compareTo(o.id);
85     }
86
87     @Override
88     public int hashCode() {
89         return orig.hashCode() ^ Double.valueOf(priority).hashCode() ^ input.hashCode() ^ label.hashCode();
90     }
91
92     @Override
93     public boolean equals(Object object) {
94         if (this == object)
95             return true;
96         else if (object == null)
97             return false;
98         else if (!(object instanceof ComparableTabContributor))
99             return false;
100
101         ComparableTabContributor r = (ComparableTabContributor)object;
102         return orig.equals(r.orig) && priority == r.priority && input.equals(r.input) && label.equals(r.label);
103     }
104
105     @Override
106     public String toString() {
107         return orig + "[" + label + "] " + priority + " " + input;
108     }
109
110 }