]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ArrayPropertyImagerFactory.java
Added Chart Preferences action to time series chart editor context menu
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / ArrayPropertyImagerFactory.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.swt;
13
14 import org.eclipse.jface.resource.ImageDescriptor;
15 import org.simantics.browsing.ui.BuiltinKeys;
16 import org.simantics.browsing.ui.BuiltinKeys.ImagerKey;
17 import org.simantics.browsing.ui.DataSource;
18 import org.simantics.browsing.ui.NodeContext;
19 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
20 import org.simantics.browsing.ui.common.ColumnKeys;
21 import org.simantics.browsing.ui.common.imagers.ContainerImager;
22 import org.simantics.browsing.ui.common.property.IArrayProperty;
23 import org.simantics.browsing.ui.content.Imager;
24 import org.simantics.browsing.ui.content.ImagerFactory;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.Resource;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.ui.icons.ImageUtil;
29 import org.simantics.utils.ui.BundleUtils;
30
31 /**
32  * A basic imager factory with support for both Resource and IArrayProperty.
33  * 
34  * @author Tuukka Lehtonen
35  */
36 public class ArrayPropertyImagerFactory implements ImagerFactory {
37
38     @SuppressWarnings("unchecked")
39     @Override
40     public Imager create(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) {
41         assert updater != null;
42         assert context != null;
43
44         @SuppressWarnings("rawtypes")
45         final ContainerImager result = new ContainerImager();
46         final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
47         if (source == null)
48             return result;
49
50         Object o = context.getConstant(BuiltinKeys.INPUT);
51         if (o instanceof IArrayProperty) {
52             IArrayProperty prop = (IArrayProperty) o;
53
54             if (prop.getRange().size() > 1) {
55                 // Only show the segment image for non-single-item slices of the
56                 // property root node. Otherwise just use the image resolved
57                 // through normal means.
58                 if (prop.isSlice()) {
59                     result.setImage(ColumnKeys.PROPERTY,
60                             BundleUtils.getImageDescriptorFromPlugin(
61                                     "org.simantics.browsing.ui.common", "/icons/segment_edit.gif"));
62                     return result;
63                 }
64             } else {
65                 // No image for single-item slices of the property.
66                 return result;
67             }
68         }
69
70         final Resource inputResource = (Resource) context.getAdapter(Resource.class);
71         if (inputResource == null)
72             return result;
73
74         source.schedule(graph -> {
75             ImageDescriptor descriptor;
76             try {
77                 descriptor = ImageUtil.adaptImageDescriptor(graph, inputResource);
78                 result.setImage(ColumnKeys.PROPERTY, descriptor);
79                 updater.scheduleReplace(context, key, result);
80             } catch (DatabaseException e) {
81                 e.printStackTrace();
82             }
83         });
84
85         return result;
86     }
87
88 }