Permalink
Browse files

create api in thread and defensive

  • Loading branch information...
1 parent 3bc0fdf commit 8274e5839fa50283eb40c336bba495e4d6bdadad @JPMoresmau committed Jan 21, 2013
View
15 ...sf.eclipsefp.haskell.buildwrapper/src/net/sf/eclipsefp/haskell/buildwrapper/BWFacade.java
@@ -364,12 +364,15 @@ public void synchronize(boolean force){
}
JSONArray dels=allPaths.optJSONArray(1);
if (dels!=null){
- for (int a=0;a<dels.length();a++){
- try {
- String p=dels.getString(a);
- BuildWrapperPlugin.getDefault().getUsageAPI().removeFile(getProject(), p);
- } catch (JSONException je){
- BuildWrapperPlugin.logError(BWText.process_parse_component_error, je);
+ UsageAPI api=BuildWrapperPlugin.getDefault().getUsageAPI();
+ if (api!=null){
+ for (int a=0;a<dels.length();a++){
+ try {
+ String p=dels.getString(a);
+ api.removeFile(getProject(), p);
+ } catch (JSONException je){
+ BuildWrapperPlugin.logError(BWText.process_parse_component_error, je);
+ }
}
}
}
View
8 ...fp.haskell.buildwrapper/src/net/sf/eclipsefp/haskell/buildwrapper/BuildWrapperPlugin.java
@@ -66,10 +66,16 @@ public BuildWrapperPlugin() {
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
- usageAPI=new UsageAPI();
usageThread.start();
}
+ /**
+ * @param usageAPI the usageAPI to set
+ */
+ public void setUsageAPI(UsageAPI usageAPI) {
+ this.usageAPI = usageAPI;
+ }
+
/*
* (non-Javadoc)
* @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
View
9 ...efp.haskell.buildwrapper/src/net/sf/eclipsefp/haskell/buildwrapper/usage/UsageThread.java
@@ -32,6 +32,13 @@ public UsageThread() {
}
public void run(){
+ /**
+ * create the API in a thread, since new UsageAPI does a new UsageDB, which loads the SQLite library, etc
+ * This can hang on MacOS, so let's no hand the UI thread
+ */
+ final UsageAPI api=new UsageAPI();
+
+ BuildWrapperPlugin.getDefault().setUsageAPI(api);
while (!shouldStop){
synchronized (ps) {
try{
@@ -44,7 +51,7 @@ public void run(){
IProject p=getNext();
while (p!=null && !shouldStop){
- boolean retAll=!BuildWrapperPlugin.getDefault().getUsageAPI().knowsProject(p);
+ boolean retAll=!api.knowsProject(p);
BWFacade f=BuildWrapperPlugin.getFacade(p);
if (f!=null){
List<Component> cs=f.getComponents();
View
450 net.sf.eclipsefp.haskell.ui/src/net/sf/eclipsefp/haskell/ui/actions/OpenModuleAction.java
@@ -1,223 +1,227 @@
-/**
- * Copyright (c) 2012 by JP Moresmau
- * This code is made available under the terms of the Eclipse Public License,
- * version 1.0 (EPL). See http://www.eclipse.org/legal/epl-v10.html
- */
-package net.sf.eclipsefp.haskell.ui.actions;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import net.sf.eclipsefp.haskell.buildwrapper.BuildWrapperPlugin;
-import net.sf.eclipsefp.haskell.buildwrapper.types.Module;
-import net.sf.eclipsefp.haskell.ui.HaskellUIPlugin;
-import net.sf.eclipsefp.haskell.ui.internal.util.UITexts;
-import net.sf.eclipsefp.haskell.ui.util.HaskellUIImages;
-import net.sf.eclipsefp.haskell.ui.util.IImageNames;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.runtime.Platform;
-import org.eclipse.jface.action.Action;
-import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.LabelProvider;
-import org.eclipse.jface.window.Window;
-import org.eclipse.swt.events.SelectionAdapter;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.graphics.Image;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.swt.widgets.Table;
-import org.eclipse.swt.widgets.TableItem;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.IWorkbenchWindowActionDelegate;
-import org.eclipse.ui.PartInitException;
-import org.eclipse.ui.dialogs.ElementListSelectionDialog;
-import org.eclipse.ui.dialogs.FilteredList;
-import org.eclipse.ui.ide.IDE;
-
-
-/**
- * @author JP Moresmau
- *
- */
-public class OpenModuleAction extends Action implements
- IWorkbenchWindowActionDelegate {
-
- private Shell shell;
- private IWorkbenchPage page;
- /**
- *
- */
- public OpenModuleAction() {
- super(Platform.getResourceBundle( HaskellUIPlugin.getDefault().getBundle()).getString( "OpenModuleAction.label" ));
- }
-
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
- */
- @Override
- public void run( final IAction arg0 ) {
-
- ModuleListSelectionDialog elsd=new ModuleListSelectionDialog( shell);
-
- //elsd.setFilter( filter )
- List<Module> mods=BuildWrapperPlugin.getDefault().getUsageAPI().listLocalModules();
-
- elsd.setElements( mods.toArray( new Module[mods.size()] ) );
- int code=elsd.open();
- if (Window.OK==code){
- Object[] res=elsd.getResult();
- for (Object o:res){
- if (o instanceof Module){
- Module mod=(Module)o;
- Long fileid=mod.getFileID();
- if (fileid!=null){
- IFile efile=BuildWrapperPlugin.getDefault().getUsageAPI().getFile( fileid );
- if (efile!=null){
- try {
- IDE.openEditor( page, efile);
- } catch (PartInitException pie){
- HaskellUIPlugin.log( pie );
- }
- }
- }
- }
- }
- }
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
- */
- @Override
- public void selectionChanged( final IAction arg0, final ISelection arg1 ) {
- //NOOP
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
- */
- @Override
- public void dispose() {
- shell=null;
- page=null;
-
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
- */
- @Override
- public void init( final IWorkbenchWindow arg0 ) {
- shell=arg0.getShell();
- page=arg0.getActivePage();
- }
-
- private static class ModuleListSelectionDialog extends ElementListSelectionDialog {
- /**
- * table items that have the selection focus (show package name)
- */
- private final Map<Integer,TableItem> lastTis=new HashMap<Integer,TableItem>();
-
- public ModuleListSelectionDialog( final Shell parent ) {
- super( parent, new ModuleLabelProvider() );
- setTitle( UITexts.OpenModuleAction_title );
- setMessage( UITexts.OpenModuleAction_text );
- setEmptyListMessage( UITexts.OpenModuleAction_nomodule );
- setEmptySelectionMessage( UITexts.OpenModuleAction_nomodulesel );
- setMultipleSelection( true );
- setStatusLineAboveButtons( true );
- setAllowDuplicates( true );
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.dialogs.SelectionStatusDialog#configureShell(org.eclipse.swt.widgets.Shell)
- */
- @Override
- protected void configureShell( final Shell shell ) {
- super.configureShell( shell );
- shell.setImage( HaskellUIImages.getImage( IImageNames.HASKELL_MISC ));
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.dialogs.AbstractElementListSelectionDialog#createFilteredList(org.eclipse.swt.widgets.Composite)
- */
- @Override
- protected FilteredList createFilteredList( final Composite parent ) {
- FilteredList fl=super.createFilteredList( parent );
- fl.addSelectionListener( new SelectionAdapter() {
- @Override
- public void widgetSelected(final SelectionEvent e) {
- TableItem[] tis=((Table)e.widget).getItems();
- Object[] obj=getSelectedElements();
- int[] sels=getSelectionIndices();
- /**
- * new focused table items
- */
- Map<Integer,TableItem> newTis=new HashMap<Integer, TableItem>();
- for (int a=0;a<sels.length;a++){
- int idx=sels[a];
- Module mod=(Module)obj[a];
-
- TableItem ti=lastTis.remove( idx );
- // change text
- if (ti==null){
- tis[idx].setText( mod.getModuleName() +" - "+ mod.getPackageName() );
- }
- newTis.put( idx, tis[idx] );
- }
- // reset text to unselected
- for (Integer i:lastTis.keySet()){
- TableItem ti=lastTis.get(i);
- if (ti!=null && !ti.isDisposed()){
- String s=ti.getText();
- int ix=s.indexOf( " - " );
- if (ix>-1){
- ti.setText( s.substring( 0,ix ) );
- }
- }
- }
- lastTis.clear();
- // new map
- lastTis.putAll( newTis );
-
- }
- } );
- return fl;
- }
-
- }
-
- private static class ModuleLabelProvider extends LabelProvider{
- private final Image img;
- /**
- *
- */
- public ModuleLabelProvider() {
- img=HaskellUIImages.getImage( IImageNames.MODULE );
- }
-
- @Override
- public String getText(final Object element) {
- if (element instanceof Module){
- Module mod=(Module)element;
- return mod.getModuleName();
- }
- return super.getText( element );
- }
-
- @Override
- public org.eclipse.swt.graphics.Image getImage(final Object element) {
- if (element instanceof Module){
- return img;
- }
- return super.getImage( element );
-
- }
-
-
-
- }
-}
+/**
+ * Copyright (c) 2012 by JP Moresmau
+ * This code is made available under the terms of the Eclipse Public License,
+ * version 1.0 (EPL). See http://www.eclipse.org/legal/epl-v10.html
+ */
+package net.sf.eclipsefp.haskell.ui.actions;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import net.sf.eclipsefp.haskell.buildwrapper.BuildWrapperPlugin;
+import net.sf.eclipsefp.haskell.buildwrapper.types.Module;
+import net.sf.eclipsefp.haskell.buildwrapper.usage.UsageAPI;
+import net.sf.eclipsefp.haskell.ui.HaskellUIPlugin;
+import net.sf.eclipsefp.haskell.ui.internal.util.UITexts;
+import net.sf.eclipsefp.haskell.ui.util.HaskellUIImages;
+import net.sf.eclipsefp.haskell.ui.util.IImageNames;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.window.Window;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Table;
+import org.eclipse.swt.widgets.TableItem;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.IWorkbenchWindowActionDelegate;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.dialogs.ElementListSelectionDialog;
+import org.eclipse.ui.dialogs.FilteredList;
+import org.eclipse.ui.ide.IDE;
+
+
+/**
+ * @author JP Moresmau
+ *
+ */
+public class OpenModuleAction extends Action implements
+ IWorkbenchWindowActionDelegate {
+
+ private Shell shell;
+ private IWorkbenchPage page;
+ /**
+ *
+ */
+ public OpenModuleAction() {
+ super(Platform.getResourceBundle( HaskellUIPlugin.getDefault().getBundle()).getString( "OpenModuleAction.label" ));
+ }
+
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
+ */
+ @Override
+ public void run( final IAction arg0 ) {
+
+
+ //elsd.setFilter( filter )
+ UsageAPI api=BuildWrapperPlugin.getDefault().getUsageAPI();
+ if (api!=null){
+ ModuleListSelectionDialog elsd=new ModuleListSelectionDialog( shell);
+ List<Module> mods=api.listLocalModules();
+
+ elsd.setElements( mods.toArray( new Module[mods.size()] ) );
+ int code=elsd.open();
+ if (Window.OK==code){
+ Object[] res=elsd.getResult();
+ for (Object o:res){
+ if (o instanceof Module){
+ Module mod=(Module)o;
+ Long fileid=mod.getFileID();
+ if (fileid!=null){
+ IFile efile=api.getFile( fileid );
+ if (efile!=null){
+ try {
+ IDE.openEditor( page, efile);
+ } catch (PartInitException pie){
+ HaskellUIPlugin.log( pie );
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
+ */
+ @Override
+ public void selectionChanged( final IAction arg0, final ISelection arg1 ) {
+ //NOOP
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
+ */
+ @Override
+ public void dispose() {
+ shell=null;
+ page=null;
+
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
+ */
+ @Override
+ public void init( final IWorkbenchWindow arg0 ) {
+ shell=arg0.getShell();
+ page=arg0.getActivePage();
+ }
+
+ private static class ModuleListSelectionDialog extends ElementListSelectionDialog {
+ /**
+ * table items that have the selection focus (show package name)
+ */
+ private final Map<Integer,TableItem> lastTis=new HashMap<Integer,TableItem>();
+
+ public ModuleListSelectionDialog( final Shell parent ) {
+ super( parent, new ModuleLabelProvider() );
+ setTitle( UITexts.OpenModuleAction_title );
+ setMessage( UITexts.OpenModuleAction_text );
+ setEmptyListMessage( UITexts.OpenModuleAction_nomodule );
+ setEmptySelectionMessage( UITexts.OpenModuleAction_nomodulesel );
+ setMultipleSelection( true );
+ setStatusLineAboveButtons( true );
+ setAllowDuplicates( true );
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.dialogs.SelectionStatusDialog#configureShell(org.eclipse.swt.widgets.Shell)
+ */
+ @Override
+ protected void configureShell( final Shell shell ) {
+ super.configureShell( shell );
+ shell.setImage( HaskellUIImages.getImage( IImageNames.HASKELL_MISC ));
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.dialogs.AbstractElementListSelectionDialog#createFilteredList(org.eclipse.swt.widgets.Composite)
+ */
+ @Override
+ protected FilteredList createFilteredList( final Composite parent ) {
+ FilteredList fl=super.createFilteredList( parent );
+ fl.addSelectionListener( new SelectionAdapter() {
+ @Override
+ public void widgetSelected(final SelectionEvent e) {
+ TableItem[] tis=((Table)e.widget).getItems();
+ Object[] obj=getSelectedElements();
+ int[] sels=getSelectionIndices();
+ /**
+ * new focused table items
+ */
+ Map<Integer,TableItem> newTis=new HashMap<Integer, TableItem>();
+ for (int a=0;a<sels.length;a++){
+ int idx=sels[a];
+ Module mod=(Module)obj[a];
+
+ TableItem ti=lastTis.remove( idx );
+ // change text
+ if (ti==null){
+ tis[idx].setText( mod.getModuleName() +" - "+ mod.getPackageName() );
+ }
+ newTis.put( idx, tis[idx] );
+ }
+ // reset text to unselected
+ for (Integer i:lastTis.keySet()){
+ TableItem ti=lastTis.get(i);
+ if (ti!=null && !ti.isDisposed()){
+ String s=ti.getText();
+ int ix=s.indexOf( " - " );
+ if (ix>-1){
+ ti.setText( s.substring( 0,ix ) );
+ }
+ }
+ }
+ lastTis.clear();
+ // new map
+ lastTis.putAll( newTis );
+
+ }
+ } );
+ return fl;
+ }
+
+ }
+
+ private static class ModuleLabelProvider extends LabelProvider{
+ private final Image img;
+ /**
+ *
+ */
+ public ModuleLabelProvider() {
+ img=HaskellUIImages.getImage( IImageNames.MODULE );
+ }
+
+ @Override
+ public String getText(final Object element) {
+ if (element instanceof Module){
+ Module mod=(Module)element;
+ return mod.getModuleName();
+ }
+ return super.getText( element );
+ }
+
+ @Override
+ public org.eclipse.swt.graphics.Image getImage(final Object element) {
+ if (element instanceof Module){
+ return img;
+ }
+ return super.getImage( element );
+
+ }
+
+
+
+ }
+}
View
7 ...l.ui/src/net/sf/eclipsefp/haskell/ui/internal/refactoring/participants/ChangeCreator.java
@@ -12,6 +12,7 @@
import net.sf.eclipsefp.haskell.buildwrapper.types.Occurrence;
import net.sf.eclipsefp.haskell.buildwrapper.types.SearchResultLocation;
import net.sf.eclipsefp.haskell.buildwrapper.types.UsageResults;
+import net.sf.eclipsefp.haskell.buildwrapper.usage.UsageAPI;
import net.sf.eclipsefp.haskell.core.util.ResourceUtil;
import net.sf.eclipsefp.haskell.ui.HaskellUIPlugin;
import net.sf.eclipsefp.haskell.ui.internal.util.UITexts;
@@ -173,7 +174,11 @@ public static Change createRenameMoveChange(final IFile file, final IPath newPat
if( updateReferences ) {
// Update references
- UsageResults ur=BuildWrapperPlugin.getDefault().getUsageAPI().getModuleReferences( null, oldModule, null, true );
+ UsageAPI api=BuildWrapperPlugin.getDefault().getUsageAPI();
+ if (api==null){
+ return change;
+ }
+ UsageResults ur=api.getModuleReferences( null, oldModule, null, true );
boolean someChange = ur.getSize()>0;
CompositeChange referencesChange=getReferencesChange( ur, oldModule,newModule);
View
24 ...fp.haskell.ui/src/net/sf/eclipsefp/haskell/ui/internal/refactoring/participants/Util.java
@@ -13,6 +13,7 @@
import net.sf.eclipsefp.haskell.buildwrapper.BuildWrapperPlugin;
import net.sf.eclipsefp.haskell.buildwrapper.types.SearchResultLocation;
import net.sf.eclipsefp.haskell.buildwrapper.types.UsageResults;
+import net.sf.eclipsefp.haskell.buildwrapper.usage.UsageAPI;
import net.sf.eclipsefp.haskell.core.cabalmodel.CabalSyntax;
import net.sf.eclipsefp.haskell.core.cabalmodel.PackageDescription;
import net.sf.eclipsefp.haskell.core.cabalmodel.PackageDescriptionLoader;
@@ -183,16 +184,19 @@ public static int getModuleNameOffset( final IResource resource,final String mod
break;
}
}*/
- UsageResults ur=BuildWrapperPlugin.getDefault().getUsageAPI().getModuleDefinitions( null, module, resource.getProject(), true );
- Map<IFile,Map<String,Collection<SearchResultLocation>>> m1=ur.getUsageInProject( resource.getProject() );
- if (m1!=null){
- Map<String,Collection<SearchResultLocation>> m=m1.get( resource );
- if (m!=null){
- Collection<SearchResultLocation> srls=m.get( "module "+module );
- if (srls!=null){
- for (SearchResultLocation srl:srls){
- offset=srl.getStartOffset( doc );
- return offset;
+ UsageAPI api=BuildWrapperPlugin.getDefault().getUsageAPI();
+ if (api!=null){
+ UsageResults ur=api.getModuleDefinitions( null, module, resource.getProject(), true );
+ Map<IFile,Map<String,Collection<SearchResultLocation>>> m1=ur.getUsageInProject( resource.getProject() );
+ if (m1!=null){
+ Map<String,Collection<SearchResultLocation>> m=m1.get( resource );
+ if (m!=null){
+ Collection<SearchResultLocation> srls=m.get( "module "+module );
+ if (srls!=null){
+ for (SearchResultLocation srl:srls){
+ offset=srl.getStartOffset( doc );
+ return offset;
+ }
}
}
}
View
22 net.sf.eclipsefp.haskell.ui/src/net/sf/eclipsefp/haskell/ui/internal/search/UsageQuery.java
@@ -89,17 +89,19 @@ public IStatus run( final IProgressMonitor paramIProgressMonitor )
resultsRefs.add(resultsDefs);*/
UsageAPI api=BuildWrapperPlugin.getDefault().getUsageAPI();
UsageResults results=new UsageResults();
- for (IProject p:projects){
- // search for each project
- UsageResults res=exact?
- api.exactSearch( null, term, p, typeFlags, scopeFlags )
- :api.likeSearch( null, term, p, typeFlags, scopeFlags );
- // restrict if needed
- if (restrictedResources!=null){
- res.filter( restrictedResources );
+ if (api!=null){
+ for (IProject p:projects){
+ // search for each project
+ UsageResults res=exact?
+ api.exactSearch( null, term, p, typeFlags, scopeFlags )
+ :api.likeSearch( null, term, p, typeFlags, scopeFlags );
+ // restrict if needed
+ if (restrictedResources!=null){
+ res.filter( restrictedResources );
+ }
+ // add to global
+ results.add(res);
}
- // add to global
- results.add(res);
}
sr.setResults( results);

0 comments on commit 8274e58

Please sign in to comment.