You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/07/23 15:20:10 UTC

svn commit: r967087 - in /pivot/trunk/eclipse: plugin.xml src/org/apache/pivot/eclipse/PivotApplicationLaunchShortcut.java src/org/apache/pivot/eclipse/PivotPlugin.java src/org/apache/pivot/eclipse/PivotScriptApplicationLaunchShortcut.java

Author: gbrown
Date: Fri Jul 23 13:20:10 2010
New Revision: 967087

URL: http://svn.apache.org/viewvc?rev=967087&view=rev
Log:
Resolve PIVOT-239.

Added:
    pivot/trunk/eclipse/src/org/apache/pivot/eclipse/PivotPlugin.java
Modified:
    pivot/trunk/eclipse/plugin.xml
    pivot/trunk/eclipse/src/org/apache/pivot/eclipse/PivotApplicationLaunchShortcut.java
    pivot/trunk/eclipse/src/org/apache/pivot/eclipse/PivotScriptApplicationLaunchShortcut.java

Modified: pivot/trunk/eclipse/plugin.xml
URL: http://svn.apache.org/viewvc/pivot/trunk/eclipse/plugin.xml?rev=967087&r1=967086&r2=967087&view=diff
==============================================================================
--- pivot/trunk/eclipse/plugin.xml (original)
+++ pivot/trunk/eclipse/plugin.xml Fri Jul 23 13:20:10 2010
@@ -29,7 +29,9 @@ limitations under the License.
                         <count value="1"/>
                         <iterate>
                             <and>
-                                <instanceof value="org.eclipse.jdt.core.IJavaElement"/>
+                                <adapt type="org.eclipse.jdt.core.IJavaElement">
+                                    <test property="org.eclipse.jdt.core.isInJavaProject"/>
+                                </adapt>
                                 <test property="org.eclipse.jdt.launching.extendsInterface"
                                     args="org.apache.pivot.wtk.Application"/>
                             </and>

Modified: pivot/trunk/eclipse/src/org/apache/pivot/eclipse/PivotApplicationLaunchShortcut.java
URL: http://svn.apache.org/viewvc/pivot/trunk/eclipse/src/org/apache/pivot/eclipse/PivotApplicationLaunchShortcut.java?rev=967087&r1=967086&r2=967087&view=diff
==============================================================================
--- pivot/trunk/eclipse/src/org/apache/pivot/eclipse/PivotApplicationLaunchShortcut.java (original)
+++ pivot/trunk/eclipse/src/org/apache/pivot/eclipse/PivotApplicationLaunchShortcut.java Fri Jul 23 13:20:10 2010
@@ -1,63 +1,135 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to you under the Apache License,
- * Version 2.0 (the "License"); you may not use this file except in
- * compliance with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
 package org.apache.pivot.eclipse;
 
+import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.debug.core.DebugPlugin;
 import org.eclipse.debug.core.ILaunchConfiguration;
 import org.eclipse.debug.core.ILaunchConfigurationType;
+import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
 import org.eclipse.debug.core.ILaunchManager;
+import org.eclipse.debug.ui.DebugUITools;
+import org.eclipse.debug.ui.ILaunchShortcut;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.core.IType;
-import org.eclipse.jdt.debug.ui.launchConfigurations.JavaLaunchShortcut;
 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
-import org.eclipse.jface.operation.IRunnableContext;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IEditorPart;
 
-public class PivotApplicationLaunchShortcut extends JavaLaunchShortcut {
+/**
+ * Pivot application launch shortcut.
+ */
+public class PivotApplicationLaunchShortcut implements ILaunchShortcut {
     @Override
-    protected ILaunchConfiguration createConfiguration(IType type) {
-        // TODO
-        return null;
-    }
+    public void launch(ISelection selection, String mode) {
+        if (selection instanceof IStructuredSelection) {
+            IStructuredSelection structuredSelection = (IStructuredSelection)selection;
+            Object[] elements = structuredSelection.toArray();
 
-    @Override
-    protected ILaunchConfigurationType getConfigurationType() {
-        ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
-        return launchManager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
+            if (elements.length == 1) {
+                launch(elements[0], mode);
+            }
+        }
     }
 
     @Override
-    protected IType[] findTypes(Object[] elements, IRunnableContext context)
-        throws InterruptedException, CoreException {
-        // TODO
-        return null;
-    }
+    public void launch(IEditorPart editor, String mode) {
+        IEditorInput editorInput = editor.getEditorInput();
+        IJavaElement javaElement = (IJavaElement)editorInput.getAdapter(IJavaElement.class);
 
-    @Override
-    protected String getTypeSelectionTitle() {
-        return "Select Type"; // TODO i18n
+        if (javaElement != null) {
+            launch(javaElement, mode);
+        }
     }
 
-    @Override
-    protected String getEditorEmptyMessage() {
-        return "Editor does not contain a launchable type."; // TODO i18n
+    private void launch(Object element, String mode) {
+        if (element instanceof IAdaptable) {
+            ICompilationUnit compilationUnit =
+                (ICompilationUnit)((IAdaptable)element).getAdapter(ICompilationUnit.class);
+
+            if (compilationUnit != null) {
+                IType type = compilationUnit.findPrimaryType();
+                ILaunchConfiguration launchConfiguration = getExistingLaunchConfiguration(type);
+
+                if (launchConfiguration == null) {
+                    launchConfiguration = createLaunchConfiguration(type);
+                }
+
+                if (launchConfiguration != null) {
+                    DebugUITools.launch(launchConfiguration, mode);
+                }
+            }
+        }
     }
 
-    @Override
-    protected String getSelectionEmptyMessage() {
-        return "Selection does not contain a launchable type."; // TODO i18n
+    private ILaunchConfiguration getExistingLaunchConfiguration(IType type) {
+        ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
+        ILaunchConfigurationType launchConfigurationType =
+            launchManager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
+
+        ILaunchConfiguration existingLaunchConfiguration = null;
+        try {
+            String applicationProjectName = type.getJavaProject().getElementName();
+            String applicationTypeName = type.getFullyQualifiedName();
+
+            ILaunchConfiguration[] launchConfigurations =
+                DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(launchConfigurationType);
+
+            for (int i = 0; i < launchConfigurations.length; i++) {
+                ILaunchConfiguration launchConfiguration = launchConfigurations[i];
+
+                String mainTypeName = launchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "");
+                String projectName = launchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, "");
+                String programArguments = launchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, "");
+
+                if (mainTypeName.equals(PivotPlugin.MAIN_TYPE_NAME)
+                    && projectName.equals(applicationProjectName)
+                    && programArguments.equals(applicationTypeName)) {
+                    existingLaunchConfiguration = launchConfiguration;
+                    break;
+                }
+            }
+        } catch (CoreException exception) {
+            MessageDialog.openError(PivotPlugin.getActiveWorkbenchShell(),
+                exception.getMessage(),
+                exception.getStatus().getMessage());
+        }
+
+        return existingLaunchConfiguration;
+    }
+
+    protected ILaunchConfiguration createLaunchConfiguration(IType type) {
+        ILaunchConfiguration launchConfiguration = null;
+
+        try {
+            String applicationProjectName = type.getJavaProject().getElementName();
+            String applicationTypeName = type.getFullyQualifiedName();
+
+            ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
+            ILaunchConfigurationType configurationType =
+                launchManager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
+            String name = launchManager.generateUniqueLaunchConfigurationNameFrom(type.getElementName());
+
+            ILaunchConfigurationWorkingCopy workingLaunchConfiguration = configurationType.newInstance(null, name);
+            workingLaunchConfiguration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
+                PivotPlugin.MAIN_TYPE_NAME);
+            workingLaunchConfiguration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
+                applicationProjectName);
+            workingLaunchConfiguration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
+                applicationTypeName);
+            workingLaunchConfiguration.setMappedResources(new IResource[] {type.getUnderlyingResource()});
+
+            launchConfiguration = workingLaunchConfiguration.doSave();
+        } catch (CoreException exception) {
+            MessageDialog.openError(PivotPlugin.getActiveWorkbenchShell(),
+                exception.getMessage(),
+                exception.getStatus().getMessage());
+        }
+
+        return launchConfiguration;
     }
 }

Added: pivot/trunk/eclipse/src/org/apache/pivot/eclipse/PivotPlugin.java
URL: http://svn.apache.org/viewvc/pivot/trunk/eclipse/src/org/apache/pivot/eclipse/PivotPlugin.java?rev=967087&view=auto
==============================================================================
--- pivot/trunk/eclipse/src/org/apache/pivot/eclipse/PivotPlugin.java (added)
+++ pivot/trunk/eclipse/src/org/apache/pivot/eclipse/PivotPlugin.java Fri Jul 23 13:20:10 2010
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.eclipse;
+
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.plugin.AbstractUIPlugin;
+
+/**
+ * Pivot plug-in.
+ */
+public class PivotPlugin extends AbstractUIPlugin {
+    public static final String MAIN_TYPE_NAME = "org.apache.pivot.wtk.DesktopApplicationContext";
+
+    public static IWorkbenchWindow getActiveWorkbenchWindow() {
+        return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+    }
+
+    public static Shell getActiveWorkbenchShell() {
+        IWorkbenchWindow window = getActiveWorkbenchWindow();
+
+        Shell activeWorkbenchShell;
+        if (window == null) {
+            activeWorkbenchShell = null;
+        } else {
+            activeWorkbenchShell = window.getShell();
+        }
+
+        return activeWorkbenchShell;
+    }
+}

Modified: pivot/trunk/eclipse/src/org/apache/pivot/eclipse/PivotScriptApplicationLaunchShortcut.java
URL: http://svn.apache.org/viewvc/pivot/trunk/eclipse/src/org/apache/pivot/eclipse/PivotScriptApplicationLaunchShortcut.java?rev=967087&r1=967086&r2=967087&view=diff
==============================================================================
--- pivot/trunk/eclipse/src/org/apache/pivot/eclipse/PivotScriptApplicationLaunchShortcut.java (original)
+++ pivot/trunk/eclipse/src/org/apache/pivot/eclipse/PivotScriptApplicationLaunchShortcut.java Fri Jul 23 13:20:10 2010
@@ -16,6 +16,138 @@
  */
 package org.apache.pivot.eclipse;
 
-public class PivotScriptApplicationLaunchShortcut extends PivotApplicationLaunchShortcut {
-    // TODO
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.debug.core.DebugPlugin;
+import org.eclipse.debug.core.ILaunchConfiguration;
+import org.eclipse.debug.core.ILaunchConfigurationType;
+import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
+import org.eclipse.debug.core.ILaunchManager;
+import org.eclipse.debug.ui.DebugUITools;
+import org.eclipse.debug.ui.ILaunchShortcut;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.IEditorPart;
+
+/**
+ * Pivot script application launch shortcut.
+ */
+public class PivotScriptApplicationLaunchShortcut implements ILaunchShortcut {
+    public static final String APPLICATION_TYPE_NAME = "org.apache.pivot.wtk.ScriptApplication";
+    public static final String SRC_ARGUMENT = "src";
+
+    @Override
+    public void launch(ISelection selection, String mode) {
+        if (selection instanceof IStructuredSelection) {
+            IStructuredSelection structuredSelection = (IStructuredSelection)selection;
+            Object[] elements = structuredSelection.toArray();
+
+            if (elements.length == 1) {
+                launch(elements[0], mode);
+            }
+        }
+    }
+
+    @Override
+    public void launch(IEditorPart editor, String mode) {
+        throw new UnsupportedOperationException();
+    }
+
+    private void launch(Object element, String mode) {
+        if (element instanceof IAdaptable) {
+            IFile file = (IFile)((IAdaptable)element).getAdapter(IFile.class);
+
+            if (file != null) {
+                ILaunchConfiguration launchConfiguration = getExistingLaunchConfiguration(file);
+
+                if (launchConfiguration == null) {
+                    launchConfiguration = createLaunchConfiguration(file);
+                }
+
+                if (launchConfiguration != null) {
+                    DebugUITools.launch(launchConfiguration, mode);
+                }
+            }
+        }
+    }
+
+    private ILaunchConfiguration getExistingLaunchConfiguration(IFile file) {
+        ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
+        ILaunchConfigurationType launchConfigurationType =
+            launchManager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
+
+        ILaunchConfiguration existingLaunchConfiguration = null;
+        try {
+            String fileProjectName = file.getProject().getName();
+
+            ILaunchConfiguration[] launchConfigurations =
+                DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(launchConfigurationType);
+
+            for (int i = 0; i < launchConfigurations.length; i++) {
+                ILaunchConfiguration launchConfiguration = launchConfigurations[i];
+
+                String mainTypeName = launchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, "");
+                String projectName = launchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, "");
+                String programArguments = launchConfiguration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, "");
+
+                if (mainTypeName.equals(PivotPlugin.MAIN_TYPE_NAME)
+                    && projectName.equals(fileProjectName)
+                    && programArguments.equals(getProgramArguments(file))) {
+                    existingLaunchConfiguration = launchConfiguration;
+                    break;
+                }
+            }
+        } catch (CoreException exception) {
+            MessageDialog.openError(PivotPlugin.getActiveWorkbenchShell(),
+                exception.getMessage(),
+                exception.getStatus().getMessage());
+        }
+
+        return existingLaunchConfiguration;
+    }
+
+    protected ILaunchConfiguration createLaunchConfiguration(IFile file) {
+        ILaunchConfiguration launchConfiguration = null;
+
+        try {
+            String fileProjectName = file.getProject().getName();
+            String fileName = file.getName();
+
+            ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
+            ILaunchConfigurationType configurationType =
+                launchManager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION);
+            String name = launchManager.generateUniqueLaunchConfigurationNameFrom(fileName);
+
+            ILaunchConfigurationWorkingCopy workingLaunchConfiguration = configurationType.newInstance(null, name);
+            workingLaunchConfiguration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
+                PivotPlugin.MAIN_TYPE_NAME);
+            workingLaunchConfiguration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
+                fileProjectName);
+            workingLaunchConfiguration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
+                getProgramArguments(file));
+            workingLaunchConfiguration.setMappedResources(new IResource[] {file});
+
+            launchConfiguration = workingLaunchConfiguration.doSave();
+        } catch (CoreException exception) {
+            MessageDialog.openError(PivotPlugin.getActiveWorkbenchShell(),
+                exception.getMessage(),
+                exception.getStatus().getMessage());
+        }
+
+        return launchConfiguration;
+    }
+
+    private static final String getProgramArguments(IFile file) {
+        IContainer parent = file.getParent();
+        IJavaElement javaElement = (IJavaElement)parent.getAdapter(IJavaElement.class);
+        String src = "/" + javaElement.getElementName().replace('.', '/') + "/" + file.getName();
+
+        return APPLICATION_TYPE_NAME + " " + "--" + SRC_ARGUMENT + "=" + src;
+    }
 }