]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/StringRepresentationLabelerFactory.java
Replace System.err and System.out with SLF4J Logging
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / StringRepresentationLabelerFactory.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.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 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class StringRepresentationLabelerFactory implements LabelerFactory {
35
36     String column = ColumnKeys.SINGLE;
37
38     //static int counter = 0;
39
40     public StringRepresentationLabelerFactory() {
41     }
42
43     public StringRepresentationLabelerFactory(String column) {
44         this.column = column;
45     }
46
47     @Override
48     public Labeler create(PrimitiveQueryUpdater updater, final NodeContext context, LabelerKey key) {
49         assert(updater != null);
50         assert(context != null);
51
52         return new LazyGraphLabeler(updater, context, key) {
53             @Override
54             public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
55 //              if((counter++ % 5000) == 0) System.out.println("L" + counter);
56                 final Resource r = (Resource) context.getConstant(BuiltinKeys.INPUT);
57                 String representation = LabelerUtil.safeStringRepresentation(graph, r);
58                 return Collections.singletonMap(column, representation);
59             }
60             @Override
61             public Modifier getModifier(ModificationContext modificationContext, String key) {
62                 ISessionContext session = Simantics.getSessionContext();
63                 if (session == null)
64                     return null;
65
66                 if (column.equals(key)) {
67                     return new GraphStringModifier(context, session.getSession()) {
68                         @Override
69                         public void doModify(WriteGraph graph, String label) throws DatabaseException {
70                             getModifier().modify(graph, label);
71                         }
72                         @Override
73                         public String createModifierInput(String fromLabel) {
74                             return fromLabel;
75                         }
76                     };
77                 }
78                 return null;
79             }
80
81             @Override
82             public Logger getLogger() {
83                 return LoggerFactory.getLogger(StringRepresentationLabelerFactory.class);
84             }
85         };
86     }
87
88 }