]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/actions/ActionCategory.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / actions / ActionCategory.java
1 /*******************************************************************************
2  * Copyright (c) 2010, 2011 Association for Decentralized Information Management in
3  * 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.model.actions;
13
14 import org.simantics.databoard.Bindings;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.common.utils.NameUtils;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.viewpoint.ontology.ViewpointResource;
20
21 public class ActionCategory implements IActionCategory {
22
23     Resource resource;
24     String label;
25     double priority;
26     boolean isSubmenu;
27     
28     public ActionCategory(Resource resource, String label, double priority, boolean isSubmenu) {
29         this.resource = resource;
30         this.label = label;
31         this.priority = priority;
32         this.isSubmenu = isSubmenu;
33     }
34
35     @Override
36     public String getLabel() {
37         return label;
38     }
39
40     @Override
41     public double getPriority() {
42         return priority;
43     }
44
45     @Override
46     public boolean isSubmenu() {
47         return isSubmenu;
48     }
49
50     public static ActionCategory create(ReadGraph g, Resource r) throws DatabaseException {
51         ViewpointResource vr = ViewpointResource.getInstance(g);       
52         
53         String label = NameUtils.getSafeLabel(g, r);
54         
55         Resource priorityResource = g.getPossibleObject(r, vr.ActionCategory_HasPriority);
56         double priority = priorityResource == null ? 0.0 : (Double)g.getValue(priorityResource, Bindings.DOUBLE);
57
58         Resource isSubmenuResource = g.getPossibleObject(r, vr.ActionCategory_IsSubmenu);
59         boolean isSubmenu = isSubmenuResource == null ? false : (Boolean)g.getValue(isSubmenuResource, Bindings.BOOLEAN);
60         
61         return new ActionCategory(r, label, priority, isSubmenu);
62     }
63     
64     @Override
65     public int hashCode() {
66         return resource.hashCode();
67     }
68     
69     @Override
70     public boolean equals(Object obj) {
71         if(this == obj)
72             return true;
73         if(obj == null || obj.getClass() != ActionCategory.class)
74             return false;
75         return resource.equals(((ActionCategory)obj).resource);
76     }
77 }