001: /*
002:
003: Derby - Class org.apache.derby.ui.actions.IJAction
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.ui.actions;
023:
024: import org.apache.derby.ui.DerbyPlugin;
025: import org.apache.derby.ui.common.CommonNames;
026: import org.apache.derby.ui.common.Messages;
027: import org.apache.derby.ui.util.DerbyUtils;
028: import org.apache.derby.ui.util.Logger;
029: import org.eclipse.core.resources.IFile;
030: import org.eclipse.core.resources.IProject;
031: import org.eclipse.core.runtime.IStatus;
032: import org.eclipse.jdt.core.IJavaProject;
033: import org.eclipse.jface.action.IAction;
034: import org.eclipse.jface.dialogs.MessageDialog;
035: import org.eclipse.jface.viewers.ISelection;
036: import org.eclipse.jface.viewers.IStructuredSelection;
037: import org.eclipse.swt.widgets.Shell;
038: import org.eclipse.ui.IActionDelegate;
039: import org.eclipse.ui.IObjectActionDelegate;
040: import org.eclipse.ui.IWorkbenchPart;
041:
042: public class IJAction implements IObjectActionDelegate {
043:
044: private IFile currentScript;
045: private IJavaProject currentJavaProject;
046: private IProject currentProject;
047:
048: /**
049: * Constructor for IJAction.
050: */
051: public IJAction() {
052: super ();
053: }
054:
055: /**
056: * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
057: */
058: public void setActivePart(IAction action, IWorkbenchPart targetPart) {
059: }
060:
061: /**
062: * @see IActionDelegate#run(IAction)
063: */
064: public void run(IAction action) {
065:
066: Shell shell = new Shell();
067: DerbyPlugin plugin = DerbyPlugin.getDefault();
068: if (plugin == null) {
069: MessageDialog.openInformation(shell,
070: CommonNames.PLUGIN_NAME, Messages.NO_ACTION);
071: } else {
072: try {
073: if (currentJavaProject != null) {
074: currentProject = currentJavaProject.getProject();
075: }
076: if (currentProject
077: .isNatureEnabled(CommonNames.DERBY_NATURE)) {
078: DerbyUtils.runIJ(currentScript, currentProject);
079: } else {
080: shell = new Shell();
081: MessageDialog.openInformation(shell,
082: CommonNames.PLUGIN_NAME,
083: Messages.NO_DERBY_NATURE + "\n"
084: + Messages.ADD_N_TRY);
085: }
086: } catch (Exception e) {
087: Logger.log("IAction.run() error " + e, IStatus.ERROR);
088: }
089: }
090: }
091:
092: /**
093: * @see IActionDelegate#selectionChanged(IAction, ISelection)
094: */
095: public void selectionChanged(IAction action, ISelection selection) {
096: currentJavaProject = org.apache.derby.ui.util.SelectionUtil
097: .findSelectedJavaProject(selection);
098: if (currentJavaProject == null) {
099: currentProject = org.apache.derby.ui.util.SelectionUtil
100: .findSelectedProject(selection);
101: }
102: currentScript = null;
103: if (selection != null) {
104: if (selection instanceof IStructuredSelection) {
105: IStructuredSelection ss = (IStructuredSelection) selection;
106: // get the first element, since selection is for single object
107: Object obj = ss.getFirstElement();
108: if (obj instanceof IFile) {
109: currentScript = (IFile) obj;
110: }
111: if (currentScript != null) {
112: currentProject = currentScript.getProject();
113: }
114: }
115: }
116: // To turn off the action item if the DERBY nature is not set
117: // We decided to go with the pop-up dialog way with the message to
118: // add the Derby nature and try.
119:
120: // try{
121: // if((currentScript!=null)&&(currentProject!=null)){
122: // if(currentScript.getName().toLowerCase().endsWith(".sql")&&(!currentProject.isNatureEnabled(CommonNames.DERBY_NATURE))){
123: // action.setEnabled(false);
124: // }
125: // }
126: // }catch(CoreException ce){
127: // Logger.log("IAction.selectionChanged() method error "+ce,IStatus.ERROR);
128: // }
129: }
130:
131: }
|