View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.eclipse.common.util;
17  
18  import org.eclipse.core.resources.IResource;
19  import org.eclipse.jface.viewers.ISelection;
20  import org.eclipse.jface.viewers.IStructuredSelection;
21  import org.eclipse.ui.IEditorPart;
22  import org.eclipse.ui.IWorkbenchPage;
23  import org.eclipse.ui.IWorkbenchPart;
24  import org.eclipse.ui.IWorkbenchWindow;
25  
26  /**
27   * @author taichi
28   * 
29   */
30  public class ResouceUtil {
31  
32      public static IResource getCurrentSelectedResouce() {
33          IResource result = null;
34          IWorkbenchWindow window = WorkbenchUtil.getWorkbenchWindow();
35          if (window != null) {
36              IWorkbenchPage page = window.getActivePage();
37              if (page != null) {
38                  // getActiveEditorで取れる参照は、フォーカスがどこにあってもアクティブなエディタの参照が取れてしまう為。
39                  IWorkbenchPart part = page.getActivePart();
40                  if (part instanceof IEditorPart) {
41                      IEditorPart editor = (IEditorPart) part;
42                      result = AdaptableUtil.toResource(editor.getEditorInput());
43                  }
44              }
45              if (result == null) {
46                  ISelection selection = window.getSelectionService()
47                          .getSelection();
48                  if (selection instanceof IStructuredSelection) {
49                      IStructuredSelection ss = (IStructuredSelection) selection;
50                      Object o = ss.getFirstElement();
51                      result = AdaptableUtil.toResource(o);
52                  }
53              }
54          }
55          return result;
56      }
57  
58  }