]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/Cells.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / Cells.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.spreadsheet.graph;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Set;
19
20 import org.simantics.databoard.Bindings;
21 import org.simantics.databoard.binding.mutable.Variant;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.common.request.UnaryRead;
25 import org.simantics.db.common.utils.Logger;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.variable.ConstantChildVariable;
28 import org.simantics.db.layer0.variable.ConstantPropertyVariableBuilder;
29 import org.simantics.db.layer0.variable.Variable;
30 import org.simantics.db.layer0.variable.Variables;
31 import org.simantics.spreadsheet.ClientModel;
32 import org.simantics.spreadsheet.resource.SpreadsheetResource;
33 import org.simantics.utils.datastructures.Pair;
34
35 public class Cells extends UnaryRead<Variable, Collection<Pair<String, Variable>>> {
36
37     public Cells(Variable variable) {
38         super(variable);
39     }
40
41         private static Set<String> CLASSIFICATIONS = new HashSet<String>();
42         
43         static {
44                 CLASSIFICATIONS.add(SpreadsheetResource.URIs.Attribute);
45         }
46     
47     @Override
48     public Collection<Pair<String, Variable>> perform(ReadGraph graph) throws DatabaseException {
49         SpreadsheetResource SHEET = SpreadsheetResource.getInstance(graph);
50         ArrayList<Pair<String, Variable>> result = new ArrayList<Pair<String, Variable>>(); 
51         
52         if (GraphUI.DEBUG)
53             System.out.println("Executing Cells for parameter " + parameter.getURI(graph));
54         
55         for(Variable child : parameter.getChildren(graph)) {
56                 Resource type = child.getPossibleType(graph);
57                 if(type != null) {
58                         if(graph.isInheritedFrom(type, SHEET.Range)) {
59                                 
60                                 try {
61                                         
62                                         Collection<Pair<String, Variable>> rangeCells = graph.sync(new Cells(child));
63                                         result.addAll(rangeCells);
64                                         
65                                 } catch (DatabaseException e) {
66                                         
67                                 String location = child.getPossiblePropertyValue(graph, SHEET.Range_location, Bindings.STRING);
68                                 if(location == null) location = "A1";
69                                 
70                                         result.add(error(graph, child, location, e));
71                                 
72                                         Logger.defaultLogError(e);
73                                 }
74                         } else if(graph.isInheritedFrom(type, SHEET.Cell)) {
75                                 
76                                 try {
77                                         
78                                         String name = child.getName(graph);
79                                         result.add(Pair.make(name, child));
80
81                                 } catch (DatabaseException e) {
82                                     if (GraphUI.DEBUG)
83                                         e.printStackTrace();
84                                         Logger.defaultLogError(e);
85                                         
86                                 }
87                         }
88                 }
89         }
90         for (Variable child : parameter.getParent(graph).getChildren(graph)) {
91             
92             Resource type = child.getPossibleType(graph);
93             if (type != null)
94             if (type != null && type.equals(SHEET.Style)) {
95                 String name = child.getName(graph);
96                 result.add(Pair.make(name, child));
97             }
98         }
99         return result;
100     }
101     
102     private static Pair<String, Variable> error(ReadGraph graph, Variable child, String location, DatabaseException e) throws DatabaseException {
103         
104         SpreadsheetResource SHEET = SpreadsheetResource.getInstance(graph);
105
106                 ArrayList<ConstantPropertyVariableBuilder> builders = new ArrayList<ConstantPropertyVariableBuilder>();
107                 
108                 builders.add(new ConstantPropertyVariableBuilder(ClientModel.CONTENT, Variant.ofInstance(e.getExplanation(graph)), Bindings.VARIANT, Collections.<ConstantPropertyVariableBuilder>emptyList(), CLASSIFICATIONS));
109                 builders.add(new ConstantPropertyVariableBuilder(Variables.TYPE, SHEET.Cell, null, Collections.<ConstantPropertyVariableBuilder>emptyList(), Collections.<String>emptySet()));
110                 
111                 Variable var = new ConstantChildVariable(child, location, builders);
112                 return new Pair<String, Variable>(location, var);
113
114     }
115
116 }