]> gerrit.simantics Code Review - simantics/platform.git/blob
506e737d54c319ce170c6266391c7ef2edc1ca1e
[simantics/platform.git] /
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.browsing.ui.graph.impl;
13
14 import java.util.Collections;
15 import java.util.Map;
16
17 import org.simantics.Simantics;
18 import org.simantics.browsing.ui.BuiltinKeys;
19 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
20 import org.simantics.browsing.ui.GraphExplorer.ModificationContext;
21 import org.simantics.browsing.ui.NodeContext;
22 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
23 import org.simantics.browsing.ui.common.ColumnKeys;
24 import org.simantics.browsing.ui.content.Labeler;
25 import org.simantics.browsing.ui.content.LabelerFactory;
26 import org.simantics.db.ReadGraph;
27 import org.simantics.db.Resource;
28 import org.simantics.db.WriteGraph;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.db.management.ISessionContext;
31
32 public class StringRepresentationLabelerFactory implements LabelerFactory {
33
34     String column = ColumnKeys.SINGLE;
35
36     //static int counter = 0;
37
38     public StringRepresentationLabelerFactory() {
39     }
40
41     public StringRepresentationLabelerFactory(String column) {
42         this.column = column;
43     }
44
45     @Override
46     public Labeler create(PrimitiveQueryUpdater updater, final NodeContext context, LabelerKey key) {
47         assert(updater != null);
48         assert(context != null);
49
50         return new LazyGraphLabeler(updater, context, key) {
51             @Override
52             public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
53 //              if((counter++ % 5000) == 0) System.out.println("L" + counter);
54                 final Resource r = (Resource) context.getConstant(BuiltinKeys.INPUT);
55                 String representation = LabelerUtil.safeStringRepresentation(graph, r);
56                 return Collections.singletonMap(column, representation);
57             }
58             @Override
59             public Modifier getModifier(ModificationContext modificationContext, String key) {
60                 ISessionContext session = Simantics.getSessionContext();
61                 if (session == null)
62                     return null;
63
64                 if (column.equals(key)) {
65                     return new GraphStringModifier(context, session.getSession()) {
66                         @Override
67                         public void doModify(WriteGraph graph, String label) throws DatabaseException {
68                             getModifier().modify(graph, label);
69                         }
70                         @Override
71                         public String createModifierInput(String fromLabel) {
72                             return fromLabel;
73                         }
74                     };
75                 }
76                 return null;
77             }
78         };
79     }
80
81 }