KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > ui > actions > IJAction


1 /*
2
3     Derby - Class org.apache.derby.ui.actions.IJAction
4     
5     Licensed to the Apache Software Foundation (ASF) under one or more
6     contributor license agreements. See the NOTICE file distributed with
7     this work for additional information regarding copyright ownership.
8     The ASF licenses this file to you under the Apache License, Version 2.0
9     (the "License"); you may not use this file except in compliance with
10     the License. You may obtain a copy of the License at
11     
12        http://www.apache.org/licenses/LICENSE-2.0
13     
14     Unless required by applicable law or agreed to in writing, software
15     distributed under the License is distributed on an "AS IS" BASIS,
16     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17     See the License for the specific language governing permissions and
18     limitations under the License.
19
20 */

21
22 package org.apache.derby.ui.actions;
23
24
25 import org.apache.derby.ui.DerbyPlugin;
26 import org.apache.derby.ui.common.CommonNames;
27 import org.apache.derby.ui.common.Messages;
28 import org.apache.derby.ui.util.DerbyUtils;
29 import org.apache.derby.ui.util.Logger;
30 import org.eclipse.core.resources.IFile;
31 import org.eclipse.core.resources.IProject;
32 import org.eclipse.core.runtime.IStatus;
33 import org.eclipse.jdt.core.IJavaProject;
34 import org.eclipse.jface.action.IAction;
35 import org.eclipse.jface.dialogs.MessageDialog;
36 import org.eclipse.jface.viewers.ISelection;
37 import org.eclipse.jface.viewers.IStructuredSelection;
38 import org.eclipse.swt.widgets.Shell;
39 import org.eclipse.ui.IActionDelegate;
40 import org.eclipse.ui.IObjectActionDelegate;
41 import org.eclipse.ui.IWorkbenchPart;
42
43
44 public class IJAction implements IObjectActionDelegate {
45
46     private IFile currentScript;
47     private IJavaProject currentJavaProject;
48     private IProject currentProject;
49     /**
50      * Constructor for IJAction.
51      */

52     public IJAction() {
53         super();
54     }
55
56     /**
57      * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
58      */

59     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
60     }
61
62     /**
63      * @see IActionDelegate#run(IAction)
64      */

65     public void run(IAction action) {
66         
67         Shell shell = new Shell();
68         DerbyPlugin plugin = DerbyPlugin.getDefault();
69         if (plugin== null) {
70             MessageDialog.openInformation(shell,
71                 CommonNames.PLUGIN_NAME,
72                 Messages.NO_ACTION);
73         }
74         else {
75             try {
76                 if(currentJavaProject!=null){
77                     currentProject=currentJavaProject.getProject();
78                 }
79                 if(currentProject.isNatureEnabled(CommonNames.DERBY_NATURE)){
80                     DerbyUtils.runIJ(currentScript,currentProject);
81                 }else{
82                     shell = new Shell();
83                     MessageDialog.openInformation(
84                         shell,
85                         CommonNames.PLUGIN_NAME,
86                         Messages.NO_DERBY_NATURE+"\n"+
87                         Messages.ADD_N_TRY);
88                 }
89             }catch(Exception JavaDoc e){
90                 Logger.log("IAction.run() error "+e,IStatus.ERROR);
91             }
92         }
93     }
94
95     /**
96      * @see IActionDelegate#selectionChanged(IAction, ISelection)
97      */

98     public void selectionChanged(IAction action, ISelection selection) {
99         currentJavaProject = org.apache.derby.ui.util.SelectionUtil.findSelectedJavaProject(selection);
100         if(currentJavaProject==null){
101             currentProject=org.apache.derby.ui.util.SelectionUtil.findSelectedProject(selection);
102         }
103         currentScript = null;
104         if (selection != null) {
105             if (selection instanceof IStructuredSelection) {
106                 IStructuredSelection ss = (IStructuredSelection)selection;
107                 // get the first element, since selection is for single object
108
Object JavaDoc obj = ss.getFirstElement();
109                 if (obj instanceof IFile) {
110                     currentScript = (IFile)obj;
111                 }
112                 if(currentScript!=null){
113                     currentProject=currentScript.getProject();
114                 }
115             }
116         }
117         // To turn off the action item if the DERBY nature is not set
118
// We decided to go with the pop-up dialog way with the message to
119
// add the Derby nature and try.
120

121 // try{
122
// if((currentScript!=null)&&(currentProject!=null)){
123
// if(currentScript.getName().toLowerCase().endsWith(".sql")&&(!currentProject.isNatureEnabled(CommonNames.DERBY_NATURE))){
124
// action.setEnabled(false);
125
// }
126
// }
127
// }catch(CoreException ce){
128
// Logger.log("IAction.selectionChanged() method error "+ce,IStatus.ERROR);
129
// }
130
}
131
132 }
133
Popular Tags