|
1 | 1 | package net.servicestack.eclipse.startup; |
2 | 2 |
|
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.io.InputStreamReader; |
| 7 | + |
| 8 | +import net.servicestack.eclipse.handlers.UpdateCommandState; |
| 9 | + |
| 10 | +import org.eclipse.core.resources.IFile; |
| 11 | +import org.eclipse.core.resources.IFolder; |
| 12 | +import org.eclipse.core.resources.IProject; |
| 13 | +import org.eclipse.core.resources.IResource; |
| 14 | +import org.eclipse.core.runtime.CoreException; |
| 15 | +import org.eclipse.core.runtime.IAdaptable; |
| 16 | +import org.eclipse.core.runtime.IPath; |
| 17 | +import org.eclipse.core.runtime.Path; |
| 18 | +import org.eclipse.core.runtime.jobs.ISchedulingRule; |
| 19 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 20 | +import org.eclipse.jdt.core.IImportDeclaration; |
| 21 | +import org.eclipse.jdt.core.IPackageFragment; |
| 22 | +import org.eclipse.jface.action.MenuManager; |
3 | 23 | import org.eclipse.jface.viewers.ISelection; |
| 24 | +import org.eclipse.jface.viewers.IStructuredSelection; |
| 25 | +import org.eclipse.jface.viewers.ITreeSelection; |
| 26 | +import org.eclipse.jface.viewers.TreePath; |
| 27 | +import org.eclipse.jface.viewers.TreeSelection; |
| 28 | +import org.eclipse.swt.widgets.Menu; |
4 | 29 | import org.eclipse.ui.ISelectionListener; |
5 | 30 | import org.eclipse.ui.ISelectionService; |
6 | 31 | import org.eclipse.ui.IStartup; |
|
9 | 34 | import org.eclipse.ui.IWorkbenchPart; |
10 | 35 | import org.eclipse.ui.IWorkbenchWindow; |
11 | 36 | import org.eclipse.ui.PlatformUI; |
| 37 | +import org.eclipse.ui.handlers.HandlerUtil; |
| 38 | +import org.eclipse.ui.services.ISourceProviderService; |
12 | 39 |
|
13 | 40 | public class PluginStartup implements IStartup, ISelectionListener { |
14 | 41 |
|
@@ -63,7 +90,93 @@ private void removeSelectionListener(IWorkbenchWindow window) { |
63 | 90 |
|
64 | 91 | @Override |
65 | 92 | public void selectionChanged(IWorkbenchPart part, ISelection selection) { |
| 93 | + |
| 94 | + IWorkbench workbench = PlatformUI.getWorkbench(); |
| 95 | + ISourceProviderService sourceProviderService = |
| 96 | + (ISourceProviderService) workbench.getService(ISourceProviderService.class); |
| 97 | + // now get my service |
| 98 | + UpdateCommandState commandStateService = (UpdateCommandState) sourceProviderService |
| 99 | + .getSourceProvider(UpdateCommandState.SHOW_UPDATE_CONTEXT_STATE); |
| 100 | + |
66 | 101 | // TODO handle selection changes |
67 | | - System.out.println("selection changed"); |
| 102 | + if(!(selection instanceof ITreeSelection)) { |
| 103 | + commandStateService.setUpdateDisabled(); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + TreeSelection treeSelection = (TreeSelection)selection; |
| 108 | + if(!(treeSelection.getFirstElement() instanceof ICompilationUnit)) { |
| 109 | + commandStateService.setUpdateDisabled(); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + ICompilationUnit compilationUnit = (ICompilationUnit)treeSelection.getFirstElement(); |
| 114 | + IResource resource = extractSelection(selection); |
| 115 | + IProject project = resource.getProject(); |
| 116 | + IImportDeclaration importDeclaration = compilationUnit.getImport("net.servicestack.client"); |
| 117 | + if(importDeclaration == null) { |
| 118 | + commandStateService.setUpdateDisabled(); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + IFile file = project.getFile(stripFirstDirectoryFromPath(compilationUnit.getPath())); |
| 123 | + InputStream contents = null; |
| 124 | + try { |
| 125 | + contents = file.getContents(); |
| 126 | + InputStreamReader is = new InputStreamReader(contents); |
| 127 | + BufferedReader br = new BufferedReader(is); |
| 128 | + String read; |
| 129 | + try { |
| 130 | + for(int i = 0; i < 10; i++) { |
| 131 | + String line = br.readLine(); |
| 132 | + if(line.startsWith("/* Options:")) { |
| 133 | + |
| 134 | + commandStateService.setUpdateEnabled(); |
| 135 | + return; |
| 136 | + } |
| 137 | + } |
| 138 | + } catch (IOException e) { |
| 139 | + // TODO Auto-generated catch block |
| 140 | + e.printStackTrace(); |
| 141 | + } |
| 142 | + |
| 143 | + } catch (CoreException e) { |
| 144 | + // TODO Auto-generated catch block |
| 145 | + e.printStackTrace(); |
| 146 | + } |
| 147 | + if(contents == null) { |
| 148 | + commandStateService.setUpdateDisabled(); |
| 149 | + return; |
| 150 | + } |
| 151 | + commandStateService.setUpdateDisabled(); |
| 152 | + System.out.println("File selected"); |
68 | 153 | } |
| 154 | + |
| 155 | + IResource extractSelection(ISelection sel) { |
| 156 | + if (!(sel instanceof IStructuredSelection)) |
| 157 | + return null; |
| 158 | + IStructuredSelection ss = (IStructuredSelection) sel; |
| 159 | + Object element = ss.getFirstElement(); |
| 160 | + if (element instanceof IResource) |
| 161 | + return (IResource) element; |
| 162 | + if (!(element instanceof IAdaptable)) |
| 163 | + return null; |
| 164 | + IAdaptable adaptable = (IAdaptable) element; |
| 165 | + Object adapter = adaptable.getAdapter(IResource.class); |
| 166 | + return (IResource) adapter; |
| 167 | + } |
| 168 | + |
| 169 | + private IPath stripFirstDirectoryFromPath(IPath path) { |
| 170 | + String constructedPath = ""; |
| 171 | + // Skip first segment of project path as we want path relative to |
| 172 | + // project. |
| 173 | + for (int i = 1; i < path.segmentCount(); i++) { |
| 174 | + constructedPath += "/" + path.segment(i); |
| 175 | + } |
| 176 | + if (constructedPath.equals("")) { |
| 177 | + constructedPath = "/"; |
| 178 | + } |
| 179 | + Path result = new Path(constructedPath); |
| 180 | + return result; |
| 181 | + } |
69 | 182 | } |
0 commit comments