Bug 312572 - [sfs] Semantic Resources view should support content deletion via popup menu
diff --git a/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/Messages.java b/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/Messages.java
index b6e32ae..767b0e1 100644
--- a/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/Messages.java
+++ b/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/Messages.java
@@ -51,6 +51,9 @@
 	public static String SemanticResourcePropertyPage_Type_XFLD;
 	public static String SemanticResourcePropertyPage_Value_XGRP;
 	public static String SemanticResourcesView_AutoRefresh_XCKL;
+	public static String SemanticResourcesView_ConfirmDeleteQuestion;
+	public static String SemanticResourcesView_ConfirmDeleteTitle;
+	public static String SemanticResourcesView_DeleteMenu;
 	public static String SemanticResourcesView_Exists_XFLD;
 	public static String SemanticResourcesView_Path_XFLD;
 	public static String SemanticResourcesView_PropertiesOf_XGRP;
diff --git a/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/SemanticResourcesUIPlugin.java b/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/SemanticResourcesUIPlugin.java
index 33524c7..9dc9151 100644
--- a/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/SemanticResourcesUIPlugin.java
+++ b/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/SemanticResourcesUIPlugin.java
@@ -11,7 +11,10 @@
  *******************************************************************************/
 package org.eclipse.core.internal.resources.semantic.ui;
 
+import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Plugin;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.ui.statushandlers.StatusManager;
 
 /**
  * The activator class controls the plug-in life cycle
@@ -21,4 +24,21 @@
 	/** The plug-in ID */
 	public static final String PLUGIN_ID = "org.eclipse.ui.resources.semantic"; //$NON-NLS-1$
 
+	/**
+	 * Handle an error. The error is logged. If <code>show</code> is
+	 * <code>true</code> the error is shown to the user.
+	 * 
+	 * @param message
+	 *            a localized message
+	 * @param throwable
+	 * @param show
+	 */
+	public static void handleError(String message, Throwable throwable, boolean show) {
+		IStatus status = new Status(IStatus.ERROR, PLUGIN_ID, message, throwable);
+		int style = StatusManager.LOG;
+		if (show)
+			style |= StatusManager.SHOW;
+		StatusManager.getManager().handle(status, style);
+	}
+
 }
diff --git a/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/SemanticResourcesView.java b/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/SemanticResourcesView.java
index 463b3fa..b616e81 100644
--- a/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/SemanticResourcesView.java
+++ b/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/SemanticResourcesView.java
@@ -16,6 +16,7 @@
 import java.text.MessageFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Iterator;
 
 import org.eclipse.core.filesystem.EFS;
 import org.eclipse.core.filesystem.IFileSystem;
@@ -38,6 +39,7 @@
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.action.IToolBarManager;
 import org.eclipse.jface.action.ToolBarManager;
+import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.layout.GridDataFactory;
 import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.jface.viewers.ISelectionChangedListener;
@@ -45,8 +47,11 @@
 import org.eclipse.jface.viewers.SelectionChangedEvent;
 import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.jface.viewers.TreeViewer;
+import org.eclipse.osgi.util.NLS;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.SashForm;
+import org.eclipse.swt.events.MenuDetectEvent;
+import org.eclipse.swt.events.MenuDetectListener;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.layout.GridData;
@@ -56,12 +61,15 @@
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Group;
 import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Menu;
+import org.eclipse.swt.widgets.MenuItem;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.swt.widgets.ToolBar;
 import org.eclipse.swt.widgets.ToolItem;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.ide.fileSystem.FileSystemContributor;
 import org.eclipse.ui.part.ViewPart;
+import org.eclipse.ui.progress.IProgressService;
 import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
 
 /**
@@ -291,6 +299,55 @@
 			}
 		});
 
+		this.sfsTree.getTree().addMenuDetectListener(new MenuDetectListener() {
+
+			public void menuDetected(MenuDetectEvent e) {
+				Menu previous = sfsTree.getTree().getMenu();
+				if (previous != null)
+					previous.dispose();
+				Menu newMenu = new Menu(sfsTree.getTree());
+
+				MenuItem delete = new MenuItem(newMenu, SWT.PUSH);
+				delete.setText(Messages.SemanticResourcesView_DeleteMenu);
+				delete.addSelectionListener(new SelectionAdapter() {
+
+					@SuppressWarnings("rawtypes")
+					@Override
+					public void widgetSelected(SelectionEvent evt) {
+						IStructuredSelection sel = (IStructuredSelection) sfsTree.getSelection();
+
+						for (Iterator iter = sel.iterator(); iter.hasNext();) {
+							final SFSBrowserTreeObject selected = (SFSBrowserTreeObject) iter.next();
+							if (MessageDialog.openConfirm(getSite().getShell(), Messages.SemanticResourcesView_ConfirmDeleteTitle, NLS
+									.bind(Messages.SemanticResourcesView_ConfirmDeleteQuestion, selected.getPath().toString()))) {
+								IProgressService srv = (IProgressService) getSite().getService(IProgressService.class);
+
+								try {
+									srv.run(true, false, new IRunnableWithProgress() {
+
+										public void run(IProgressMonitor monitor) throws InvocationTargetException {
+											try {
+												selected.getStore().delete(0, monitor);
+												scheduleRefresh(0);
+											} catch (CoreException ce) {
+												throw new InvocationTargetException(ce);
+											}
+										}
+									});
+								} catch (InvocationTargetException e1) {
+									SemanticResourcesUIPlugin.handleError(e1.getCause().getMessage(), e1.getCause(), true);
+								} catch (InterruptedException e1) {
+									// ignore
+								}
+							}
+						}
+					}
+
+				});
+				sfsTree.getTree().setMenu(newMenu);
+			}
+		});
+
 	}
 
 	@Override
diff --git a/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/messages.properties b/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/messages.properties
index 64ea719..ee3832f 100644
--- a/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/messages.properties
+++ b/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/messages.properties
@@ -45,6 +45,9 @@
 SemanticResourcePropertyPage_Type_XFLD=Type
 SemanticResourcePropertyPage_Value_XGRP=Value
 SemanticResourcesView_AutoRefresh_XCKL=Auto-Refresh
+SemanticResourcesView_ConfirmDeleteQuestion=Do you want to delete Semantic File Store {0} with all children?
+SemanticResourcesView_ConfirmDeleteTitle=Confirm delete
+SemanticResourcesView_DeleteMenu=Delete
 SemanticResourcesView_Exists_XFLD=Exists
 SemanticResourcesView_Path_XFLD=Path
 SemanticResourcesView_PropertiesOf_XGRP=Properties of {0}
diff --git a/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/util/SFSBrowserTreeLabelProvider.java b/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/util/SFSBrowserTreeLabelProvider.java
index dd69e0a..4b1917a 100644
--- a/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/util/SFSBrowserTreeLabelProvider.java
+++ b/bundles/org.eclipse.ui.resources.semantic/src/org/eclipse/core/internal/resources/semantic/ui/util/SFSBrowserTreeLabelProvider.java
@@ -57,6 +57,9 @@
 			@Override
 			public void mouseDoubleClick(MouseEvent e) {
 				TreeItem item = tree.getItem(new Point(e.x, e.y));
+				if (item == null) {
+					return;
+				}
 				boolean expanded = tv.getExpandedState(item.getData());
 				if (expanded) {
 					tv.collapseToLevel(item.getData(), 1);