Permalink
Browse files

fixed UI threading bug with image preview

added loading and "no preview" images to preview window
fixed bug in the "open file with system editor" command
  • Loading branch information...
1 parent ad1b287 commit b96b6043a6639a68f0268353537705878c065b76 @gregjan gregjan committed Sep 13, 2012
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -17,7 +17,9 @@
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.ide.IDE;
@@ -58,7 +60,7 @@ private void openTempCopy(IFileStore file, ExecutionEvent event) throws Executio
origPath.removeFileExtension().lastSegment() +
" (Temporary Copy)." +
origPath.getFileExtension());
- IFileStore tempStore = EFS.getLocalFileSystem().getStore(temppath);
+ final IFileStore tempStore = EFS.getLocalFileSystem().getStore(temppath);
if(!tempStore.fetchInfo().exists()) {
try {
tempStore.getParent().mkdir(EFS.NONE, new NullProgressMonitor());
@@ -68,14 +70,17 @@ private void openTempCopy(IFileStore file, ExecutionEvent event) throws Executio
throw new ExecutionException("Cannot copy file to temporary location: "+e.getMessage());
}
}
- IWorkbenchPage page = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
- System.out.print(page);
- try {
- IDE.openEditorOnFileStore(page, tempStore);
- } catch (PartInitException e) {
- e.printStackTrace();
- throw new ExecutionException("Cannot open editor for temporary copy: "+e.getMessage());
- }
+ PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
+
+ @Override
+ public void run() {
+ IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+ try {
+ IDE.openEditorOnFileStore(page, tempStore);
+ } catch (PartInitException e) {
+ e.printStackTrace();
+ }
+ }});
}
}
@@ -33,6 +33,7 @@
import org.osgi.framework.BundleContext;
import unc.lib.cdr.workbench.stage.StagingJob;
+import unc.lib.cdr.workbench.xwalk.CrosswalkJob;
/**
* The activator class controls the plug-in life cycle
@@ -96,10 +97,11 @@ public IStatus runInUIThread(@SuppressWarnings("unused") IProgressMonitor monito
*/
@Override
public void stop(BundleContext context) throws Exception {
- plugin = null;
IJobManager manager = Platform.getJobManager();
- manager.cancel(StagingJob.stagingFamily);
+ manager.cancel(StagingJob.stagingJobFamilyObject);
+ manager.cancel(CrosswalkJob.crosswalkJobFamilyObject);
super.stop(context);
+ plugin = null;
}
/**
@@ -40,13 +40,13 @@
@Override
public boolean belongsTo(Object family) {
- if(stagingFamily == family) {
+ if(stagingJobFamilyObject == family) {
return true;
}
return super.belongsTo(family);
}
- public static final String stagingFamily = "stagingFamily";
+ public static final String stagingJobFamilyObject = "stagingFamily";
/**
* @param name
@@ -135,6 +135,10 @@ public void handleEvent(Event event) {
*/
public void setImage(Image image) {
this.image = image;
- redraw();
+ this.getDisplay().asyncExec(new Runnable() {
+ @Override
+ public void run() {
+ redraw();
+ }});
}
}
@@ -89,7 +89,9 @@
ACLDecor("key_sm.gif", Size.ORIGINAL),
CrosswalkedRecord("property_obj.gif", Size.ORIGINAL),
LinkedObject("link_obj.gif", Size.ORIGINAL),
- EjectedDecore("ejected1.gif", Size.ORIGINAL);
+ EjectedDecore("ejected1.gif", Size.ORIGINAL),
+ Loading("loading.png", Size.ORIGINAL),
+ NoPreview("no_preview.png", Size.ORIGINAL);
Size size = Size.ORIGINAL;
String imageFile = null;
@@ -1,7 +1,11 @@
package unc.lib.cdr.workbench.views;
import org.eclipse.core.runtime.IAdaptable;
+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.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
@@ -24,19 +28,29 @@ public ViewPartPreviewFile() {
ISelectionListener selectionListener = new ISelectionListener() {
@Override
public void selectionChanged(IWorkbenchPart part, final ISelection selection) {
- // part.getSite().getShell().getDisplay().asyncExec(new Runnable() {
-
- // @Override
- // public void run() {
+ Job load = new Job("Loading image preview") {
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
handleSelection(selection);
- // }});
+ return Status.OK_STATUS;
+ }
+ };
+ load.setPriority(Job.DECORATE);
+ load.schedule();
}
};
private void handleSelection(ISelection selection) {
if (selection instanceof IStructuredSelection) {
IStructuredSelection sel = (IStructuredSelection) selection;
Object o = sel.getFirstElement();
+ Image first = LabelImageFactory.getImageForObject(o);
+ if (first == null) {
+ LabelImageFactory.getImage(LabelImageFactory.Icon.Loading);
+ }
+ if (viewer != null) {
+ viewer.setImage(first);
+ }
setImageProvider(getImageProvider(o));
}
}
@@ -49,12 +63,13 @@ private ImageProvider getImageProvider(Object object) {
ImageProvider provider = null;
if (object instanceof IAdaptable)
provider = (ImageProvider) ((IAdaptable) object).getAdapter(ImageProvider.class);
-
+
// If we haven't found an adapter yet, try asking the AdapterManager.
try {
if (provider == null)
provider = (ImageProvider) Platform.getAdapterManager().loadAdapter(object, ImageProvider.class.getName());
- } catch(NullPointerException ignored) {}
+ } catch (NullPointerException ignored) {
+ }
return provider;
}
@@ -71,6 +86,8 @@ protected void setImageProvider(final ImageProvider newprovider) {
Image newimage = newprovider.getImage(viewer.getDisplay());
if (newimage != null) {
viewer.setImage(newimage);
+ } else {
+ viewer.setImage(LabelImageFactory.getImage(LabelImageFactory.Icon.NoPreview));
}
disposeImage();
provider = newprovider;
@@ -72,6 +72,13 @@
import crosswalk.WalkWidget;
public class CrosswalkJob extends Job {
+ public static final String crosswalkJobFamilyObject = "crosswalkJobFamily";
+ @Override
+ public boolean belongsTo(Object family) {
+ if(family == crosswalkJobFamilyObject) return true;
+ return super.belongsTo(family);
+ }
+
private static final Logger LOG = LoggerFactory.getLogger(CrosswalkJob.class);
IFile file = null;
@@ -150,6 +157,7 @@ public MetsType getMets() {
// create new records, update status if old dmd was user linked
try {
while (true) {
+ if (monitor.isCanceled()) return Status.CANCEL_STATUS;
MdSecType md = processRecord(cw, m, nature, file, dataFileName);
newCwDmds.put(md.getID(), md);
if (oldCwDmds.containsKey(md.getID())) {

0 comments on commit b96b604

Please sign in to comment.