org.simantics.browsing.ui.common.node,
org.simantics.browsing.ui.common.processors,
org.simantics.browsing.ui.common.property,
+ org.simantics.browsing.ui.common.state,
org.simantics.browsing.ui.common.viewpoints,
org.simantics.browsing.ui.common.views
Bundle-Vendor: VTT Technical Research Center of Finland
--- /dev/null
+package org.simantics.browsing.ui.common.state;
+
+import java.io.File;
+import java.util.concurrent.CompletableFuture;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.Status;
+import org.simantics.DatabaseJob;
+import org.simantics.browsing.ui.ExplorerState;
+import org.simantics.browsing.ui.NodeContext;
+import org.simantics.browsing.ui.StatePersistor;
+import org.simantics.browsing.ui.common.Activator;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 1.36.0
+ */
+public class ExplorerStates {
+
+ public static File explorerStateLocation() {
+ return Platform.getStateLocation(Activator.getDefault().getBundle()).toFile();
+ }
+
+ public static CompletableFuture<ExplorerState> scheduleRead(NodeContext root, StatePersistor persistor) {
+ CompletableFuture<ExplorerState> result = new CompletableFuture<>();
+ new ReaderJob(root, persistor, result).schedule();
+ return result;
+ }
+
+ private static class ReaderJob extends DatabaseJob {
+
+ private NodeContext root;
+ private StatePersistor persistor;
+ private CompletableFuture<ExplorerState> consumer;
+
+ public ReaderJob(NodeContext root, StatePersistor persistor, CompletableFuture<ExplorerState> consumer) {
+ super("Reading previous graph explorer state");
+ this.root = root;
+ this.persistor = persistor;
+ this.consumer = consumer;
+ }
+
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ try {
+ monitor.beginTask("Loading persisted data", 1);
+ ExplorerState state = persistor.deserialize(explorerStateLocation(), root);
+ monitor.worked(1);
+ consumer.complete(state);
+ return Status.OK_STATUS;
+ } finally {
+ monitor.done();
+ }
+ }
+
+ }
+
+}
* Contributors:
* VTT Technical Research Centre of Finland - initial API and implementation
*******************************************************************************/
-package org.simantics.browsing.ui.swt;
+package org.simantics.browsing.ui.common.state;
import java.util.Map;
* Contributors:
* VTT Technical Research Centre of Finland - initial API and implementation
*******************************************************************************/
-package org.simantics.browsing.ui.swt;
+package org.simantics.browsing.ui.common.state;
import java.util.TreeMap;
-package org.simantics.browsing.ui.swt;
+package org.simantics.browsing.ui.common.state;
import java.io.File;
import java.nio.file.Files;
import org.simantics.browsing.ui.NodeContext;
import org.simantics.browsing.ui.StatePersistor;
import org.simantics.databoard.util.StringUtil;
-import org.simantics.db.common.utils.Logger;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* @author Tuukka Lehtonen
*/
public class IdentifiedStatePersistor implements StatePersistor {
+ private static final Logger LOGGER = LoggerFactory.getLogger(IdentifiedStatePersistor.class);
+
protected final String id;
public IdentifiedStatePersistor(String id) {
if (bean != null && memento != null)
Files.write(memento, bean.serialize());
} catch (Throwable t) {
- Logger.defaultLogError(t);
+ LOGGER.error("Failed to serialize ExplorerState " + state, t);
}
}
stateBean.deserialize( Files.readAllBytes(path) );
return toState(stateBean);
} catch (Throwable t) {
- Logger.defaultLogError(t);
+ LOGGER.error("Failed to deserialize ExplorerState from " + path, t);
return ExplorerState.EMPTY;
}
}
* Contributors:
* Semantum Oy - initial API and implementation
*******************************************************************************/
-package org.simantics.browsing.ui.swt;
+package org.simantics.browsing.ui.common.state;
import org.simantics.databoard.annotations.Optional;
import org.simantics.databoard.util.Bean;
* Contributors:
* Semantum Oy - initial API and implementation
*******************************************************************************/
-package org.simantics.browsing.ui.swt;
+package org.simantics.browsing.ui.common.state;
import org.simantics.databoard.annotations.Optional;
import org.simantics.databoard.util.Bean;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
-import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.resource.ColorDescriptor;
import org.simantics.browsing.ui.common.processors.IsExpandedProcessor;
import org.simantics.browsing.ui.common.processors.NoSelectionRequestProcessor;
import org.simantics.browsing.ui.common.processors.ProcessorLifecycle;
+import org.simantics.browsing.ui.common.state.ExplorerStates;
import org.simantics.browsing.ui.content.Labeler;
import org.simantics.browsing.ui.content.Labeler.CustomModifier;
import org.simantics.browsing.ui.content.Labeler.DialogModifier;
import org.simantics.utils.threads.ThreadUtils;
import org.simantics.utils.ui.AdaptionUtils;
import org.simantics.utils.ui.ISelectionUtils;
+import org.simantics.utils.ui.SWTUtils;
import org.simantics.utils.ui.jface.BasePostSelectionProvider;
import gnu.trove.map.hash.THashMap;
private void initializeState() {
if (persistor == null)
return;
+ ExplorerStates.scheduleRead(getRoot(), persistor)
+ .thenAccept(state -> SWTUtils.asyncExec(natTable, () -> restoreState(state)));
+ }
- ExplorerState state = persistor.deserialize(
- Platform.getStateLocation(Activator.getDefault().getBundle()).toFile(),
- getRoot());
-
-
+ private void restoreState(ExplorerState state) {
Object processor = getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED);
if (processor instanceof DefaultIsExpandedProcessor) {
DefaultIsExpandedProcessor isExpandedProcessor = (DefaultIsExpandedProcessor)processor;
for(NodeContext expanded : state.expandedNodes) {
- isExpandedProcessor.setExpanded(expanded, true);
+ isExpandedProcessor.replaceExpanded(expanded, true);
}
}
}
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
-import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.IStatusLineManager;
import org.simantics.browsing.ui.common.processors.IsExpandedProcessor;
import org.simantics.browsing.ui.common.processors.NoSelectionRequestProcessor;
import org.simantics.browsing.ui.common.processors.ProcessorLifecycle;
+import org.simantics.browsing.ui.common.state.ExplorerStates;
import org.simantics.browsing.ui.content.ImageDecorator;
import org.simantics.browsing.ui.content.Imager;
import org.simantics.browsing.ui.content.LabelDecorator;
import org.simantics.utils.threads.SWTThread;
import org.simantics.utils.threads.ThreadUtils;
import org.simantics.utils.ui.ISelectionUtils;
+import org.simantics.utils.ui.SWTUtils;
import org.simantics.utils.ui.jface.BasePostSelectionProvider;
import org.simantics.utils.ui.widgets.VetoingEventHandler;
import org.simantics.utils.ui.workbench.WorkbenchUtils;
});
}
}
-
+
private void initializeState() {
if (persistor == null)
return;
+ ExplorerStates.scheduleRead(getRoot(), persistor)
+ .thenAccept(state -> SWTUtils.asyncExec(tree, () -> restoreState(state)));
+ }
- ExplorerState state = persistor.deserialize(
- Platform.getStateLocation(Activator.getDefault().getBundle()).toFile(),
- getRoot());
-
+ private void restoreState(ExplorerState state) {
// topNodeToSet will be processed by #setData when it encounters a
// NodeContext that matches this one.
// topNodePath = state.topNodePath;
if (processor instanceof DefaultIsExpandedProcessor) {
DefaultIsExpandedProcessor isExpandedProcessor = (DefaultIsExpandedProcessor)processor;
for(NodeContext expanded : state.expandedNodes) {
- isExpandedProcessor.setExpanded(expanded, true);
+ isExpandedProcessor.replaceExpanded(expanded, true);
}
}
}
}
persistor.serialize(
- Platform.getStateLocation(Activator.getDefault().getBundle()).toFile(),
+ ExplorerStates.explorerStateLocation(),
getRoot(),
new ExplorerState(topNodePath, topNodePathChildIndex, expandedNodes, columnWidths));
}
*/
private void doSetColumns(Column[] cols, Consumer<Map<Column, Object>> callback) {
// Attempt to keep previous column widths.
- Map<String, Integer> prevWidths = new HashMap<String, Integer>();
+ Map<String, Integer> prevWidths = new HashMap<>();
for (TreeColumn column : tree.getColumns()) {
- prevWidths.put(column.getText(), column.getWidth());
- column.dispose();
+ Column c = (Column) column.getData();
+ if (c != null) {
+ prevWidths.put(c.getKey(), column.getWidth());
+ column.dispose();
+ }
}
- HashMap<String, Integer> keyToIndex = new HashMap<String, Integer>();
+ HashMap<String, Integer> keyToIndex = new HashMap<>();
for (int i = 0; i < cols.length; ++i) {
keyToIndex.put(cols[i].getKey(), i);
}
this.columnImageArray = new Image[cols.length];
this.columnDescOrImageArray = new Object[cols.length];
- Map<Column, Object> map = new HashMap<Column, Object>();
+ Map<Column, Object> map = new HashMap<>();
tree.setHeaderVisible(columnsAreVisible);
for (Column column : columns) {
int cw = column.getWidth();
// Try to keep previous widths
- Integer w = prevWidths.get(column);
+ Integer w = prevWidths.get(column.getKey());
if (w != null)
c.setWidth(w);
else if (cw != Column.DEFAULT_CONTROL_WIDTH)
if(callback != null) callback.accept(map);
// Make sure the explorer fits the columns properly after initialization.
- tree.getDisplay().asyncExec(new Runnable() {
- @Override
- public void run() {
- if (tree.isDisposed())
- return;
+ SWTUtils.asyncExec(tree, () -> {
+ if (!tree.isDisposed())
refreshColumnSizes();
- }
});
}
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
-import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.layout.GridDataFactory;
import org.simantics.browsing.ui.common.processors.IsExpandedProcessor;
import org.simantics.browsing.ui.common.processors.NoSelectionRequestProcessor;
import org.simantics.browsing.ui.common.processors.ProcessorLifecycle;
+import org.simantics.browsing.ui.common.state.ExplorerStates;
import org.simantics.browsing.ui.content.ImageDecorator;
import org.simantics.browsing.ui.content.Imager;
import org.simantics.browsing.ui.content.LabelDecorator;
import org.simantics.utils.threads.ThreadUtils;
import org.simantics.utils.ui.AdaptionUtils;
import org.simantics.utils.ui.ISelectionUtils;
+import org.simantics.utils.ui.SWTUtils;
import org.simantics.utils.ui.jface.BasePostSelectionProvider;
import gnu.trove.map.hash.THashMap;
private void initializeState() {
if (persistor == null)
return;
+ ExplorerStates.scheduleRead(getRoot(), persistor)
+ .thenAccept(state -> SWTUtils.asyncExec(viewer.getTree(), () -> restoreState(state)));
+ }
- ExplorerState state = persistor.deserialize(
- Platform.getStateLocation(Activator.getDefault().getBundle()).toFile(),
- getRoot());
-
-
+ private void restoreState(ExplorerState state) {
Object processor = getPrimitiveProcessor(BuiltinKeys.IS_EXPANDED);
if (processor instanceof DefaultIsExpandedProcessor) {
DefaultIsExpandedProcessor isExpandedProcessor = (DefaultIsExpandedProcessor)processor;
for(NodeContext expanded : state.expandedNodes) {
- isExpandedProcessor.setExpanded(expanded, true);
+ isExpandedProcessor.replaceExpanded(expanded, true);
}
}
}
private void doSetColumns(Column[] cols, Consumer<Map<Column, Object>> callback) {
// Attempt to keep previous column widths.
- Map<String, Integer> prevWidths = new HashMap<String, Integer>();
-
+ Map<String, Integer> prevWidths = new HashMap<>();
for (TreeViewerColumn c : treeViewerColumns) {
- prevWidths.put(c.getColumn().getText(), c.getColumn().getWidth());
- c.getColumn().dispose();
+ Column col = (Column) (c.getColumn().getData());
+ if (col != null)
+ prevWidths.put(col.getKey(), c.getColumn().getWidth());
+ c.getColumn().dispose();
}
-
+
treeViewerColumns.clear();
HashMap<String, Integer> keyToIndex = new HashMap<String, Integer>();
int cw = column.getWidth();
// Try to keep previous widths
- Integer w = prevWidths.get(column);
+ Integer w = prevWidths.get(column.getKey());
if (w != null)
c.setWidth(w);
else if (cw != Column.DEFAULT_CONTROL_WIDTH)
return context == null;
}
- @SuppressWarnings("rawtypes")
+ @SuppressWarnings("unchecked")
@Override
- public Object getAdapter(Class adapter) {
+ public <T> T getAdapter(Class<T> adapter) {
if (adapter == NodeContext.class)
- return context;
+ return (T) context;
return context.getAdapter(adapter);
}
* store the key, this is not used.
*/
NodeContext getNC = new NodeContext() {
- @SuppressWarnings("rawtypes")
- @Override
- public Object getAdapter(Class adapter) {
+ @Override
+ public <T> T getAdapter(Class<T> adapter) {
return null;
}
/*******************************************************************************
- * Copyright (c) 2007, 2012 Association for Decentralized Information Management
+ * Copyright (c) 2007, 2018 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
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
-import java.util.function.Consumer;
import org.eclipse.core.runtime.IAdaptable;
-import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Tree;
-import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchSite;
import org.simantics.browsing.ui.common.processors.UserSelectedComparableFactoryQueryProcessor;
import org.simantics.browsing.ui.common.processors.UserSelectedViewpointFactoryQueryProcessor;
import org.simantics.browsing.ui.common.processors.ViewpointFactoryResolver;
+import org.simantics.browsing.ui.common.state.ExplorerStates;
import org.simantics.browsing.ui.common.views.FilterAreaSource;
import org.simantics.browsing.ui.common.views.IFilterArea;
import org.simantics.browsing.ui.common.views.IFilterAreaProvider;
import org.simantics.browsing.ui.graph.impl.SessionContextInputSource;
import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;
import org.simantics.browsing.ui.model.nodetypes.NodeType;
-import org.simantics.browsing.ui.swt.Activator;
import org.simantics.browsing.ui.swt.ComparatorSelector;
import org.simantics.browsing.ui.swt.ContextMenuInitializer;
import org.simantics.browsing.ui.swt.DefaultExplorerSelectionListener;
import org.simantics.utils.datastructures.hints.IHintListener;
import org.simantics.utils.datastructures.hints.IHintObservable;
import org.simantics.utils.datastructures.hints.IHintTracker;
+import org.simantics.utils.ui.SWTUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
}
private int getColumnWidth(Column column, ExplorerState state) {
- // Get saved width from the persistor if there is one.
+ // Get saved width from the persistor if there is one.
if (state != null && state.columnWidths != null) {
- Integer width = state.columnWidths.get(column.getLabel());
- if (width != null)
- return width;
+ Integer width = state.columnWidths.get(column.getLabel());
+ if (width != null)
+ return width;
}
return column.getWidth();
}
-
- public void setColumns(Column[] columns) {
-
- explorer.setColumns(columns, new Consumer<Map<Column, Object>>() {
-
- @Override
- public void accept(Map<Column, Object> objects) {
- ExplorerState state = null;
- if (persistor != null) {
- state = persistor.deserialize(
- Platform.getStateLocation(Activator.getDefault().getBundle()).toFile(),
- explorer.getRoot());
- }
-
- for(Map.Entry<Column, Object> entry : objects.entrySet()) {
- Column column = entry.getKey();
- TreeColumn treeColumn = (TreeColumn)entry.getValue();
-
- if (column.getWidth() < 0) {
- throw new IllegalArgumentException("Column minimum width cannot be < 0, got " + column.getWidth());
- }
-
- int width = getColumnWidth(column, state);
- if(column.hasGrab()) {
-
- ad.setColumnData(treeColumn, new ColumnWeightData(column.getWeight(), width));
-
- } else {
-
- ad.setColumnData(treeColumn, new ColumnWeightData(0, width));
- }
-
- }
- }
+ protected void restoreColumnSizes(Map<Column, Object> columns) {
+ if (persistor != null) {
+ setColumnData(columns, null);
+ ExplorerStates.scheduleRead(explorer.getRoot(), persistor).thenAccept(state -> {
+ SWTUtils.asyncExec(GraphExplorerComposite.this, () -> {
+ if (explorerComposite.isDisposed())
+ setColumnData(columns, state);
+ });
+ });
+ } else {
+ setColumnData(columns, null);
+ }
+ }
+ protected void setColumnData(Map<Column, Object> columns, ExplorerState state) {
+ columns.forEach((column, widget) -> {
+ org.eclipse.swt.widgets.Widget columnWidget = (org.eclipse.swt.widgets.Widget) widget;
+ ad.setColumnData(columnWidget,
+ new ColumnWeightData(
+ column.hasGrab() ? column.getWeight() : 0,
+ getColumnWidth(column, state)));
});
+ }
+ public void setColumns(Column[] columns) {
+ // ColumnWeightData does not support column weight/width < 0
+ for (Column column : columns) {
+ if (column.getWeight() < 0)
+ throw new IllegalArgumentException("Column weight must be >= 0, got " + column.getWeight() + " for " + column);
+ if (column.getWidth() < 0)
+ throw new IllegalArgumentException("Column minimum width must be >= 0, got " + column.getWidth() + " for " + column);
+ }
+ explorer.setColumns(columns, this::restoreColumnSizes);
}
@Override
import org.simantics.browsing.ui.NodeContext.ConstantKey;
import org.simantics.browsing.ui.common.NodeContextBuilder;
import org.simantics.browsing.ui.common.NodeContextBuilder.MapNodeContext;
+import org.simantics.browsing.ui.common.state.GraphExplorerStateBean;
+import org.simantics.browsing.ui.common.state.GraphExplorerStateNodeBean;
+import org.simantics.browsing.ui.common.state.IdentifiedStatePersistor;
+import org.simantics.browsing.ui.common.state.StringArrayBean;
+import org.simantics.browsing.ui.common.state.StringBean;
import org.simantics.browsing.ui.model.actions.ActionBrowseContext;
import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;
import org.simantics.browsing.ui.model.browsecontexts.BrowseContexts;
import org.simantics.browsing.ui.model.nodetypes.EntityNodeType;
import org.simantics.browsing.ui.model.nodetypes.NodeType;
import org.simantics.browsing.ui.model.nodetypes.SpecialNodeType;
-import org.simantics.browsing.ui.swt.GraphExplorerStateBean;
-import org.simantics.browsing.ui.swt.GraphExplorerStateNodeBean;
-import org.simantics.browsing.ui.swt.IdentifiedStatePersistor;
import org.simantics.browsing.ui.swt.NodeContextValueBean;
-import org.simantics.browsing.ui.swt.StringArrayBean;
-import org.simantics.browsing.ui.swt.StringBean;
import org.simantics.databoard.Bindings;
import org.simantics.databoard.binding.impl.ArrayListBinding;
import org.simantics.databoard.util.Bean;