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.IProject;
19  import org.eclipse.core.resources.IResource;
20  import org.eclipse.core.runtime.IAdaptable;
21  import org.eclipse.ui.texteditor.ITextEditor;
22  
23  /**
24   * @author taichi
25   * 
26   */
27  public class AdaptableUtil {
28  
29      public static ITextEditor toTextEditor(Object adaptable) {
30          ITextEditor result = null;
31          if (adaptable instanceof ITextEditor) {
32              result = (ITextEditor) adaptable;
33          } else if (adaptable instanceof IAdaptable) {
34              IAdaptable a = (IAdaptable) adaptable;
35              result = (ITextEditor) a.getAdapter(ITextEditor.class);
36          }
37          return result;
38      }
39  
40      public static IResource toResource(Object adaptable) {
41          IResource result = null;
42          if (adaptable instanceof IResource) {
43              result = (IResource) adaptable;
44          } else if (adaptable instanceof IAdaptable) {
45              IAdaptable a = (IAdaptable) adaptable;
46              result = (IResource) a.getAdapter(IResource.class);
47          }
48          return result;
49      }
50  
51      public static IProject toProject(Object adaptable) {
52          IProject result = null;
53          if (adaptable instanceof IProject) {
54              result = (IProject) adaptable;
55          } else if (adaptable instanceof IAdaptable) {
56              IAdaptable a = (IAdaptable) adaptable;
57              result = (IProject) a.getAdapter(IProject.class);
58          }
59          if (result == null) {
60              IResource r = toResource(adaptable);
61              if (r != null) {
62                  result = r.getProject();
63              }
64          }
65          return result;
66      }
67  }