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.action;
17  
18  import org.eclipse.core.resources.IProject;
19  import org.eclipse.core.resources.IResource;
20  import org.eclipse.core.runtime.CoreException;
21  import org.eclipse.jface.action.IAction;
22  import org.eclipse.jface.viewers.ISelection;
23  import org.eclipse.jface.viewers.IStructuredSelection;
24  import org.eclipse.ui.IActionDelegate;
25  import org.seasar.eclipse.common.CommonPlugin;
26  import org.seasar.eclipse.common.util.AdaptableUtil;
27  import org.seasar.eclipse.common.util.ProjectUtil;
28  
29  /**
30   * @author taichi
31   * 
32   */
33  public abstract class AbstractProjectAction implements IActionDelegate {
34  
35      private IProject project = null;
36  
37      /**
38       * 
39       */
40      public AbstractProjectAction() {
41          super();
42      }
43  
44      /*
45       * (non-Javadoc)
46       * 
47       * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
48       *      org.eclipse.jface.viewers.ISelection)
49       */
50      public void selectionChanged(IAction action, ISelection selection) {
51          if (selection instanceof IStructuredSelection) {
52              IStructuredSelection ss = (IStructuredSelection) selection;
53              Object o = ss.getFirstElement();
54              this.project = AdaptableUtil.toProject(o);
55              if (this.project == null) {
56                  IResource r = AdaptableUtil.toResource(o);
57                  if (r != null) {
58                      this.project = r.getProject();
59                  }
60              }
61          }
62      }
63  
64      /*
65       * (non-Javadoc)
66       * 
67       * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
68       */
69      public void run(IAction action) {
70          try {
71              if (this.project == null) {
72                  this.project = ProjectUtil.getCurrentSelectedProject();
73              }
74              if (this.project != null) {
75                  run(action, this.project);
76              }
77          } catch (CoreException e) {
78              CommonPlugin.log(e);
79          }
80      }
81  
82      public abstract void run(IAction action, IProject project)
83              throws CoreException;
84  }