Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / queries / IsNodeContextRemovable.java
1 /*******************************************************************************
2  * Copyright (c) 2015 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.model.queries;
13
14 import java.util.Collections;
15
16 import org.simantics.browsing.ui.BuiltinKeys;
17 import org.simantics.browsing.ui.NodeContext;
18 import org.simantics.browsing.ui.model.actions.ActionBrowseContext;
19 import org.simantics.browsing.ui.model.browsecontexts.ActionBrowseContextRequest;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
23 import org.simantics.db.common.request.Queries;
24 import org.simantics.db.common.request.UnaryRead;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.layer0.adapter.Remover;
27 import org.simantics.project.ontology.ProjectResource;
28
29 /**
30  * @author Tuukka Lehtonen
31  * @since 1.18.1
32  */
33 public class IsNodeContextRemovable extends UnaryRead<NodeContext, Boolean> {
34
35     public IsNodeContextRemovable(NodeContext context) {
36         super(context);
37     }
38
39     @Override
40     public Boolean perform(ReadGraph graph) throws DatabaseException {
41         boolean result = false;
42         try {
43             ActionBrowseContext defaultContext = graph.syncRequest(
44                     new ActionBrowseContextRequest(Collections.singleton(ProjectResource.getInstance(graph).ProjectActionContext)),
45                     TransientCacheAsyncListener.<ActionBrowseContext>instance());
46             ActionBrowseContext browseContext = ActionBrowseContext.get(graph, parameter, defaultContext);
47             if (browseContext != null && !browseContext.canRemove(graph, parameter))
48                 return false;
49             result = true;
50         } catch (DatabaseException e) {
51             return false;
52         }
53
54         Object input = parameter.getConstant(BuiltinKeys.INPUT);
55         if (result && input instanceof Resource) {
56             Remover remover = graph.syncRequest(Queries.adapt((Resource) input, Remover.class, true));
57             if (remover != null)
58                 return remover.canRemove(graph, null) == null;
59         }
60         return result;
61     }
62
63 }