/******************************************************************************* * Copyright (c) 2019 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Semantum Oy - initial API and implementation *******************************************************************************/ package org.simantics.document.swt.core.widget; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeItem; import org.eclipse.ui.IWorkbenchSite; import org.simantics.Simantics; import org.simantics.browsing.ui.Column; import org.simantics.browsing.ui.Column.Align; import org.simantics.browsing.ui.StatePersistor; import org.simantics.browsing.ui.swt.InputSourceImpl; import org.simantics.browsing.ui.swt.widgets.DragSourceListenerFactory; import org.simantics.browsing.ui.swt.widgets.ModelBrowser; import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl; import org.simantics.db.layer0.variable.VariableBean; import org.simantics.db.management.ISessionContext; import org.simantics.document.server.IEventCommand; import org.simantics.document.server.JSONObject; import org.simantics.document.server.bean.Command; import org.simantics.document.server.client.CommandManager; import org.simantics.document.server.client.WidgetData; import org.simantics.document.server.handler.AbstractEventHandler; import org.simantics.document.server.io.CommandContext; import org.simantics.document.server.io.CommandContextImpl; import org.simantics.document.server.io.CommandContextMutable; import org.simantics.document.server.io.ICommand; import org.simantics.document.server.io.JSONObjectUtils; import org.simantics.document.swt.core.SWTDocument; import org.simantics.document.swt.core.SWTViews; import org.simantics.document.swt.core.base.LeafWidgetManager; import org.simantics.document.swt.core.base.PostEventCommand; import org.simantics.document.swt.core.base.WidgetContainer; import org.simantics.document.swt.core.bean.ColumnBean; import org.simantics.document.swt.core.bean.ColumnsBean; import org.simantics.scl.runtime.function.Function1; import org.simantics.scl.runtime.function.Function2; import org.simantics.ui.selection.WorkbenchSelectionUtils; import org.simantics.utils.datastructures.Pair; public class Explorer extends LeafWidgetManager { @Override protected void doUpdateProperties(SWTDocument document, ModelBrowser control, JSONObject object) { // String text = object.getField("text"); // control.setText(text); } protected Column[] getColumns(ColumnsBean bean) { Column[] result = new Column[bean.columns.length]; for(int i=0;i selectionListener = object.getJSONField("selectionListener"); final Function2 checkStateListener = object.getJSONField("checkStateListener"); DragSourceListenerFactory dragSourceListenerFactory = object.getJSONField("dragSourceListenerFactory"); final IWorkbenchSite site = document.getSite(); final ISelectionProvider selectionProvider = document.getSelectionProvider(); Map args = new HashMap(); args.put("displaySelectors", Boolean.FALSE); args.put("displayFilter", displayFilter != null ? displayFilter : Boolean.FALSE); // GridLayoutFactory.fillDefaults().applyTo(parent); ColumnsBean columns = object.getBeanJSONFieldDefault("columns", ColumnsBean.BINDING, null); String editingColumn = object.getJSONFieldDefault("editingColumn", null); // Column[] COLUMNS = new Column[] { // new Column("HasDisplayProperty", "Parameter description", Align.LEFT, 80, "Input parameter name", true), // new Column("HasDisplayValue", "Value", Align.RIGHT, 65, "Value"), // new Column("HasDisplayUnit", "Unit", Align.RIGHT, 100, "Unit") // }; // parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_RED)); final ModelBrowser control = new ModelBrowser(Collections.singleton(browseContext), args, site, parent, new WidgetSupportImpl(), style); Tree tree = control.getExplorerControl(); tree.addListener(SWT.SetData, new Listener() { @Override public void handleEvent(Event event) { SWTViews.notifyScrolledComposite(control); } }); if(columns != null) control.setColumns(getColumns(columns)); if(editingColumn != null) control.setEditingColumn(editingColumn); if(contextMenuId != null) control.setContextMenuId(contextMenuId); if(dragSourceListenerFactory != null) control.setDragSourceListenerFactory(dragSourceListenerFactory); if(displayHeader != null) tree.setHeaderVisible(displayHeader); control.setStatePersistor(persistor); control.finish(); control.setInputSource(new InputSourceImpl() { @Override public Object get(ISessionContext ctx, Object selection) { return selection; } }); // TODO: fixme! Object i = object.getJSONField("explorerInput"); if(i instanceof VariableBean) { control.setInput(Simantics.getSessionContext(), ((VariableBean)i).getVariable()); } else { control.setInput(Simantics.getSessionContext(), i); } control.addListenerToControl(SWT.Selection, new Listener() { @Override public void handleEvent(Event event) { switch (event.type) { case SWT.Selection: if (event.detail == SWT.CHECK && event.item != null) { TreeItem item = (TreeItem) event.item; boolean checked = item.getChecked(); if(checkStateListener != null) selectionListener.apply(event); } break; } if(selectionListener != null) selectionListener.apply(event); ISelection selection = (ISelection)control.getExplorer().getWidgetSelection(); // TODO: refactor this! if(selectionProvider != null) { selectionProvider.setSelection(selection); } // if (site != null) { // ISelectionProvider sp = site.getSelectionProvider(); // if (sp != null) { // sp.setSelection(selection); // } // } } }); return control; } @Override public String getProperty(SWTDocument document, JSONObject object, WidgetContainer widget, String property) { if("selection".equals(property)) { ISelection selection = (ISelection)widget.getControl().getExplorer().getWidgetSelection(); return SWTViews.encode(object, property, WorkbenchSelectionUtils.getWorkbenchSelectionElements(selection)); } return null; } public static class ExplorerCommandManager implements CommandManager> { @Override public Collection updateCommandListeners(final SWTDocument document, final JSONObject object, WidgetContainer container) { WidgetData wd = document.getWidget(JSONObjectUtils.getId(object)); List commands = object.getJSONField("commands"); HashSet listeners = new HashSet(); List> data = new ArrayList<>(); data.addAll(SWTViews.getTriggeredCommands(document, commands, "eventOut")); data.add(new Pair(wd, new Command("onCheck"))); Listener listener = new ExplorerListener(wd, data); ModelBrowser browser = container.getControl(); if(!browser.isDisposed()) { browser.addListenerToControl(SWT.Selection, listener); listeners.add(listener); } return listeners; } @Override public void removeListener(WidgetContainer container, Object listener) { if(container.getControl().isDisposed()) return; if(listener instanceof ExplorerListener) container.getControl().removeListenerFromControl(SWT.Selection, (Listener)listener); } } @Override public IEventCommand eventCommand(SWTDocument document, JSONObject object, WidgetContainer widget, ICommand command, CommandContext p) { if("onCheck".equals(command.getCommand())) { CommandContextMutable context = new CommandContextImpl().merge(p); AbstractEventHandler onCheck = object.getJSONField("onCheck"); return new PostEventCommand(document, onCheck, context); } return null; } }