X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.ui%2Fsrc%2Forg%2Fsimantics%2Fui%2Fworkbench%2Fdialogs%2FResourceLabelProvider.java;fp=bundles%2Forg.simantics.ui%2Fsrc%2Forg%2Fsimantics%2Fui%2Fworkbench%2Fdialogs%2FResourceLabelProvider.java;h=704138d2af6e7627eae9a0ba6d8f7c878c17be1f;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/dialogs/ResourceLabelProvider.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/dialogs/ResourceLabelProvider.java new file mode 100644 index 000000000..704138d2a --- /dev/null +++ b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/dialogs/ResourceLabelProvider.java @@ -0,0 +1,366 @@ +/******************************************************************************* + * Copyright (c) 2007, 2013 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: + * VTT Technical Research Centre of Finland - initial API and implementation + * Semantum Oy - index based searching (#4255) + *******************************************************************************/ +package org.simantics.ui.workbench.dialogs; + +import gnu.trove.map.hash.THashMap; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.MultiStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.jface.resource.DeviceResourceManager; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.resource.ResourceManager; +import org.eclipse.jface.viewers.ILabelProvider; +import org.eclipse.jface.viewers.ILabelProviderListener; +import org.eclipse.jface.viewers.LabelProviderChangedEvent; +import org.eclipse.swt.graphics.Device; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.progress.IProgressConstants; +import org.simantics.DatabaseJob; +import org.simantics.Simantics; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Session; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.ui.icons.ImageDescriptorProvider; +import org.simantics.ui.icons.ImageUtil; +import org.simantics.ui.internal.Activator; +import org.simantics.utils.threads.IThreadWorkQueue; +import org.simantics.utils.threads.SWTThread; +import org.simantics.utils.ui.gfx.CompositionImageDescriptor; + +/** + * ResourceLabelProvider is a label provider for Simantics database + * {@link Resource}s. + * + *
+ * This label provider utilizes {@link ImageDescriptorProvider} adapters for
+ * determining the icon to use.
+ *
+ * @author Toni Kalajainen
+ * @author Tuukka Lehtonen
+ *
+ * @see Resource
+ * @see ILabelProvider
+ * @see ILabelProviderListener
+ */
+public class ResourceLabelProvider implements ILabelProvider {
+
+ protected Listtrue
if scheduled, false
otherwise
+ */
+ public boolean scheduleIfNecessary(long delay) {
+ if (isScheduled.compareAndSet(false, true)) {
+ schedule(delay);
+ return true;
+ }
+ return false;
+ }
+
+ void dispose() {
+ tasks = null;
+ labelProvider = null;
+ }
+
+ @Override
+ public boolean shouldSchedule() {
+ return tasks != null;
+ }
+
+ @Override
+ public boolean shouldRun() {
+ return tasks != null;
+ }
+
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ try {
+ isScheduled.set(false);
+
+ TaskRepository tr = tasks;
+ if (tr == null)
+ return Status.CANCEL_STATUS;
+
+ Task[] taskList = tr.pruneTasks();
+ return runTasks(tr, taskList);
+ } finally {
+ monitor.done();
+ }
+ }
+
+ private IStatus runTasks(final TaskRepository tr, final Task[] taskList) {
+ Session session = Simantics.peekSession();
+ if (session == null)
+ return Status.CANCEL_STATUS;
+ final ResourceManager rm = tr.resourceManager;
+ if (rm == null)
+ return Status.CANCEL_STATUS;
+
+ final MultiStatus result = new MultiStatus(Activator.PLUGIN_ID, 0, "Problems encountered while invoking resource label provider:", null);
+ try {
+ Simantics.getSession().syncRequest(new ReadRequest() {
+ @Override
+ public void run(ReadGraph graph) throws DatabaseException {
+ for (Task task : taskList) {
+ try {
+ if (tr.isDisposed())
+ return;
+
+ runTask(rm, graph, task);
+ tasks.complete(task);
+ } catch (DatabaseException e) {
+ result.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
+ }
+ }
+ }
+ });
+ return result;
+ } catch (DatabaseException e) {
+ return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Resource label provider failed unexpectedly.", e);
+ } finally {
+ labelProvider.asyncFireChangedEvent(new LabelProviderChangedEvent(labelProvider));
+ }
+ }
+
+ protected void runTask(ResourceManager rm, ReadGraph graph, Task task) throws DatabaseException {
+ task.label = getText(graph, task.resource);
+ task.image = getImage(rm, graph, task.resource);
+ }
+
+ protected String getText(ReadGraph graph, Resource resource) {
+ try {
+ return graph.adapt(resource, String.class);
+ } catch (DatabaseException e) {
+ try {
+ return NameUtils.getSafeName(graph, resource);
+ } catch (DatabaseException e2) {
+ return "";
+ }
+ }
+ }
+
+ public Image getImage(ResourceManager rm, ReadGraph graph, Resource resource) throws DatabaseException {
+ ImageDescriptor i = getImageDescriptor(graph, resource);
+ return i == null ? null : (Image) rm.get(i);
+ }
+
+ public ImageDescriptor getImageDescriptor(ReadGraph graph, Resource resource, ImageDescriptor... decorations)
+ throws DatabaseException {
+ ImageDescriptor baseImage = ImageUtil.adaptImageDescriptor(graph, resource);
+ if (baseImage == null)
+ return null;
+ if (decorations == null || decorations.length == 0)
+ return baseImage;
+ List