]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/widget/ExplorerListener.java
Improvements to modelled SWT documents
[simantics/platform.git] / bundles / org.simantics.document.swt.core / src / org / simantics / document / swt / core / widget / ExplorerListener.java
1 /*******************************************************************************
2  * Copyright (c) 2019 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.document.swt.core.widget;
13
14 import java.util.List;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.Event;
18 import org.eclipse.swt.widgets.Listener;
19 import org.eclipse.swt.widgets.TreeItem;
20 import org.simantics.browsing.ui.BuiltinKeys;
21 import org.simantics.browsing.ui.NodeContext;
22 import org.simantics.document.server.client.WidgetData;
23 import org.simantics.document.server.io.CommandContext;
24 import org.simantics.document.server.io.CommandContextImpl;
25 import org.simantics.document.server.io.CommandContextMutable;
26 import org.simantics.document.server.io.ICommand;
27 import org.simantics.document.swt.core.SWTDocument;
28 import org.simantics.document.swt.core.base.PropertyWidgetManager;
29 import org.simantics.utils.datastructures.Pair;
30
31 public class ExplorerListener implements Listener {
32
33     private WidgetData wd;
34     private List<Pair<WidgetData, ICommand>> data;
35
36     public ExplorerListener(WidgetData wd, List<Pair<WidgetData, ICommand>> data) {
37         this.wd = wd;
38         this.data = data;
39     }
40
41     @Override
42     public void handleEvent(Event event) {
43         switch (event.type) {
44         case SWT.Selection:
45             if (event.detail == SWT.CHECK && event.item != null) {
46                 TreeItem item = (TreeItem) event.item;
47                 NodeContext ctx = (NodeContext)item.getData();
48                 Object value = ctx.getConstant(BuiltinKeys.INPUT);
49
50                 boolean checked = item.getChecked();
51
52                 CommandContextMutable context = new CommandContextImpl();
53                 context.putValue("event", "onCheck");
54                 context.putValue("checked", checked);
55                 context.putValue("item", value);
56
57                 if (!data.isEmpty()) {
58                     ((SWTDocument)wd.document).handleCommands(data, context, event.widget);
59                 }
60
61                 CommandContext ret = PropertyWidgetManager.sendEvent((SWTDocument)wd.document, wd, "onCheck", event.widget, context);
62             }
63             break;
64         }
65     }
66
67 }