]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/tester/CollectionResourcePropertyTester.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / tester / CollectionResourcePropertyTester.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.ui.tester;
13
14 import java.util.Arrays;
15
16 import org.eclipse.core.expressions.PropertyTester;
17 import org.simantics.DatabaseJob;
18 import org.simantics.Simantics;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.common.request.UniqueRead;
22 import org.simantics.db.common.utils.NameUtils;
23 import org.simantics.db.common.utils.RequestUtil;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.exception.ResourceNotFoundException;
26 import org.simantics.db.management.ISessionContext;
27 import org.simantics.scl.reflection.OntologyVersions;
28 import org.simantics.ui.SimanticsUI;
29 import org.simantics.ui.utils.ResourceAdaptionUtils;
30
31 /**
32  * @author Tuukka Lehtonen
33  */
34 public class CollectionResourcePropertyTester extends PropertyTester {
35
36     private static final boolean DEBUG = false;
37
38     /**
39      * Tests if the received resource is an instance of any of the URIs
40      * listed in the arguments.
41      */
42     protected static final String RESOURCE_TYPES = "resourceTypes";
43
44     private static final OntologyVersions VERSIONS = OntologyVersions.getInstance();
45     
46     @Override
47     public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
48         if (DEBUG)
49             System.out.println("TEST: " + receiver + ", " + property + ", " + Arrays.toString(args) + ", " + expectedValue);
50
51         final Resource[] resources = ResourceAdaptionUtils.toResources(receiver);
52         if (resources == null)
53             return false;
54
55         ISessionContext ctx = Simantics.getSessionContext();
56         if (ctx == null)
57             return false;
58
59         if (DatabaseJob.inProgress())
60             return false;
61
62         try {
63             return RequestUtil.trySyncRequest(
64                     ctx.getSession(),
65                     SimanticsUI.UI_THREAD_REQUEST_START_TIMEOUT,
66                     SimanticsUI.UI_THREAD_REQUEST_EXECUTION_TIMEOUT,
67                     false,
68                     new UniqueRead<Boolean>() {
69                 @Override
70                 public Boolean perform(ReadGraph g) throws DatabaseException {
71                     return Boolean.valueOf(doTest(g, resources, property, args, expectedValue));
72                 }
73             });
74         } catch (DatabaseException | InterruptedException e) {
75             // Purposefully not logging these exceptions, there might be way too
76             // many even under normal circumstances.
77             // TODO: add debug tracing options controlling the printing of these exceptions
78             return false;
79         }
80     }
81
82     protected boolean doTest(ReadGraph g, Resource[] resources, String property, Object[] args, Object expectedValue) throws DatabaseException {
83         if (RESOURCE_TYPES.equals(property)) {
84                 loop: for(Resource r : resources) {
85                         if (DEBUG)
86                                 System.out.println("** " + NameUtils.getSafeName(g, r));
87                         try {
88                                 for (int i = 0; i < args.length; i++) {
89                                         if (g.isInstanceOf(r, g.getResource(VERSIONS.currentVersion((String) args[i])))) {
90                                                 if (DEBUG)
91                                                         System.out.println("IS INSTANCE OF " + args[i]);
92                                                 continue loop;
93                                         }
94                                 }
95                         } catch (ResourceNotFoundException e) {
96                                 /* This is a natural situation (database does not contain all resources
97                    referred in plugins). No error reporting.
98                                  */
99                                 return false;
100                         }
101                         if (DEBUG)
102                                 System.out.println("NOT AN INSTANCE OF ANY OF: " + Arrays.toString(args));
103                         return false;
104                 }
105             return true;
106         }
107         return false;
108     }
109
110 }