Skip to main content
summaryrefslogtreecommitdiffstats
blob: b2ec30464a0c99c15ea071bb8bf5ab52d073a709 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.ui.ws;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.ui.IPackagesViewPart;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.osee.framework.ui.plugin.util.AWorkbench;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.views.navigator.IResourceNavigator;
import org.eclipse.ui.views.navigator.ResourceNavigator;

/**
 * @author Donald G. Dunne
 */
public final class AWorkspace {

   private AWorkspace() {
   }

   public static String getWorkspacePath() {
      return ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString();
   }

   public static File iFileToFile(IFile iFile) {
      return new File(iFile.getLocation().toString());
   }

   public static IFile fileToIFile(File file) {
      String p = file.getAbsolutePath();
      IPath path = new Path(p);
      IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
      if (iFile.exists()) {
         return iFile;
      }
      p = p.replace('\\', '/');
      // System.err.println("p *" + p + "*");
      // Run through projects to see if any contain this file
      IProject projs[] = getProjects();
      for (int i = 0; i < projs.length; i++) {
         IProject proj = projs[i];
         String projLoc = proj.getLocation().toString();
         // System.err.println("proj *" + projLoc + "*");
         if (p.equals(projLoc)) {
            return null;
         } else if (p.startsWith(projLoc)) {
            // System.out.println("found it");
            p = p.replaceFirst(projLoc, "");
            p = "/" + proj.getName() + p;
            // System.err.println("new pLoc*" + p + "*");
            path = new Path(p);
            iFile = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
            if (iFile.exists()) {
               return iFile;
            } else {
               // System.err.println("iFile DOESN'T exist *" +
               // iFile.getRawLocation()+ "*");
            }
         }
         // System.out.println("proj");
      }
      return null;
   }

   public static IFile getIFile(String filename) {
      return fileToIFile(new File(filename));
   }

   public static IProject[] getProjects() {
      IWorkspace workspace = ResourcesPlugin.getWorkspace();
      return workspace.getRoot().getProjects();
   }

   public static boolean showInResourceNavigator(IFile file) {
      if (file == null) {
         return false;
      }
      IWorkbenchPage page = AWorkbench.getActivePage();
      try {
         IViewPart viewPart =
            page.showView("org.eclipse.ui.views.ResourceNavigator", null, IWorkbenchPage.VIEW_ACTIVATE);

         if (viewPart != null && viewPart instanceof ResourceNavigator) {
            ResourceNavigator resourceNavigator = (ResourceNavigator) viewPart;
            StructuredSelection ss = new StructuredSelection(file);
            resourceNavigator.selectReveal(ss);
            return true;
         }
      } catch (PartInitException ex) {
         ex.printStackTrace();
      }

      return false;
   }

   public static boolean openEditor(String filename) {
      IFile iFile = AWorkspace.getIFile(filename);
      return openEditor(iFile);
   }

   public static boolean openEditor(IFile iFile) {
      IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
      try {
         IDE.openEditor(page, iFile, true);
      } catch (PartInitException e) {
         e.printStackTrace();
         return false;
      }
      return true;
   }

   public static boolean showInPackageExplorer(IFile file) {
      if (file == null) {
         return false;
      }
      IViewPart p =
         PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(
            "org.eclipse.jdt.ui.PackageExplorer");
      if (p != null && p instanceof IPackagesViewPart) {
         StructuredSelection ss = new StructuredSelection(file);
         IPackagesViewPart rn = (IPackagesViewPart) p;
         rn.selectAndReveal(ss);
      }
      return true;
   }

   public static void refreshResource(IResource resource) {
      IResource parentResource = resource.getParent();
      try {
         parentResource.refreshLocal(IResource.DEPTH_INFINITE, null);
      } catch (org.eclipse.core.runtime.CoreException ex) {
         MessageDialog.openInformation(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
            "Refresh Navigator", "Can't refresh \"" + resource.getName() + "\"\n\nYou must refresh Manually");
         return;
      }

   }

   public static StructuredSelection getSelection() {
      IViewReference[] parts = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
      for (int i = 0; i < parts.length; i++) {
         StructuredSelection sel = getSelection(parts[i].getPart(false));
         if (sel != null) {
            return sel;
         }
      }
      return null;
   }

   public static StructuredSelection getSelection(IWorkbenchPart targetPart) {
      if (targetPart instanceof IResourceNavigator) {
         IResourceNavigator navigator = (IResourceNavigator) targetPart;
         return (StructuredSelection) navigator.getViewer().getSelection();
      } else if (targetPart instanceof IPackagesViewPart) {
         IPackagesViewPart navigator = (IPackagesViewPart) targetPart;
         return (StructuredSelection) navigator.getTreeViewer().getSelection();
      }
      return null;
   }

   public static IResource findWorkspaceFile(String fileName) {
      IContainer ws = ResourcesPlugin.getWorkspace().getRoot();
      List<IResource> resources = new ArrayList<IResource>();
      recursiveFileFind(fileName, ws, resources);
      return !resources.isEmpty() ? resources.iterator().next() : null;
   }

   public static void recursiveFileFind(String fileName, IResource resource, List<IResource> matches) {
      if (resource.getName().equalsIgnoreCase(fileName)) {
         matches.add(resource);
      }
      if (resource instanceof IContainer) {
         try {
            for (IResource res : ((IContainer) resource).members()) {
               recursiveFileFind(fileName, res, matches);
            }
         } catch (CoreException ex) {
            // do nothing
         }
      }
   }

   public static List<IResource> findWorkspaceFileMatch(String regex) {
      IContainer ws = ResourcesPlugin.getWorkspace().getRoot();
      List<IResource> resources = new ArrayList<IResource>();
      recursiveFileFindMatch(regex, ws, resources);
      return resources;
   }

   private static void recursiveFileFindMatch(String regex, IResource resource, List<IResource> matches) {
      if (IResource.FILE == resource.getType() && resource.getName().length() > 0) {
         if (resource.getName().matches(regex)) {
            matches.add(resource);
         }
      }
      if (resource instanceof IContainer) {
         try {
            for (IResource res : ((IContainer) resource).members()) {
               recursiveFileFindMatch(regex, res, matches);
            }
         } catch (CoreException ex) {
            // do nothing
         }
      }
   }

}

Back to the top