]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartDoubleClickHandler.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / ChartDoubleClickHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * 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.charts.ui;
13
14 import org.eclipse.swt.widgets.Display;
15 import org.eclipse.swt.widgets.Shell;
16 import org.simantics.Simantics;
17 import org.simantics.charts.ontology.ChartResource;
18 import org.simantics.charts.query.ChartAndSubscriptionItemData;
19 import org.simantics.charts.query.ChartAndSubscriptionItemReadQuery;
20 import org.simantics.charts.query.ChartAndSubscriptionItemWriteAction;
21 import org.simantics.charts.query.ChartDataQuery;
22 import org.simantics.charts.query.ChartDataWrite;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.Session;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.ui.DoubleClickEvent;
28 import org.simantics.ui.IDoubleClickAction;
29 import org.simantics.ui.utils.ResourceAdaptionUtils;
30 import org.simantics.utils.ui.dialogs.ShowError;
31
32 public class ChartDoubleClickHandler implements IDoubleClickAction {
33
34     @Override
35     public void doubleClickEvent(DoubleClickEvent e) throws DatabaseException {
36         ReadGraph g = e.getGraph();
37         final Resource resource = ResourceAdaptionUtils.toSingleResource(e.getResource());
38         if (resource == null)
39             return;
40
41         ChartResource CHART = ChartResource.getInstance(g);
42         
43         // Handle double-click on Chart Item.
44         if (g.isInstanceOf(resource, CHART.Chart_Item)) {
45             Display display = Display.getDefault();
46                 openChartItemPropertiesDialog(g, resource, display);
47                 e.consume();
48         }
49         /*
50         if (g.isInstanceOf(resource, CHART.Chart)) {
51             Display display = Display.getDefault();
52                 openChartPropertiesDialog(g, resource, display);
53                 e.consume();
54         }
55         */
56     }
57
58     public static void openChartItemPropertiesDialog(ReadGraph g, final Resource chartItem, final Display display) throws DatabaseException
59     {
60         final ChartAndSubscriptionItemData data = g.sync( new ChartAndSubscriptionItemReadQuery(chartItem) );
61         display.asyncExec(new Runnable() {
62             @Override
63             public void run() {
64                 openChartItemPropertiesDialog(display.getActiveShell(), chartItem, data, true);
65             }
66         });
67     }
68
69     public static ChartAndSubscriptionItemDialog openChartItemPropertiesDialog(
70             Shell parentShell,
71             final Resource chartItem,
72             final ChartAndSubscriptionItemData data,
73             boolean shouldBlock)
74     {
75         Runnable applyAction = new Runnable() {
76             @Override
77             public void run() {
78                 try {
79                     Session s = Simantics.getSession();
80                     s.markUndoPoint();
81                     s.sync( new ChartAndSubscriptionItemWriteAction(chartItem, data) );
82                 } catch (DatabaseException e) {
83                     ShowError.showError(e.getClass().getName(), e.getLocalizedMessage(), e);
84                 }
85             }
86         };
87         ChartAndSubscriptionItemDialog d =  new ChartAndSubscriptionItemDialog( parentShell, data, true, applyAction );
88         d.setBlockOnOpen(shouldBlock);
89         if (shouldBlock) {
90             d.open();
91             return null;
92         } else {
93             d.open();
94             return d;
95         }
96     }
97
98     public static void openChartPropertiesDialog(ReadGraph g, final Resource chart, final Display display) throws DatabaseException
99     {
100         final ChartData data = g.sync( new ChartDataQuery(chart) );
101
102         display.asyncExec( new Runnable() {
103             @Override
104             public void run() {
105                 Display display = Display.getCurrent();
106                 Runnable applyAction = new Runnable() {
107                     @Override
108                     public void run() {
109                         try {
110                             Session s = Simantics.getSession();
111                             s.markUndoPoint();
112                             s.sync( new ChartDataWrite(chart, data) );
113                         } catch (DatabaseException e) {
114                             ShowError.showError(e.getClass().getName(), e.getLocalizedMessage(), e);
115                         }
116                     }
117                 };
118                 ChartDialog d =  new ChartDialog( display.getActiveShell(), data, applyAction );
119                 d.open();
120             }
121         });
122     }
123
124 }