/* * Copyright 2012-2014 Aerospike, Inc. * * Portions may be licensed to Aerospike, Inc. under one or more contributor * license agreements. * * Licensed 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 com.aerospike.udf.actions; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.jface.action.IAction; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.TreeSelection; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IObjectActionDelegate; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.IWorkbenchWindowActionDelegate; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.ide.IDE; import com.aerospike.core.CoreActivator; import com.aerospike.core.model.Module; /** * Our sample action implements workbench action delegate. * The action proxy will be created by the workbench and * shown in the UI. When the user tries to use the action, * this delegate will be created and execution will be * delegated to it. * @see IWorkbenchWindowActionDelegate */ public class EditRegisteredUDF implements IWorkbenchWindowActionDelegate { private ISelection selection; private Shell shell; /** * The constructor. */ public EditRegisteredUDF() { } /** * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart) */ public void setActivePart(IAction action, IWorkbenchPart targetPart) { this.shell = targetPart.getSite().getShell(); } /** * The action has been activated. The argument of the * method represents the 'real' action sitting * in the workbench UI. * @see IWorkbenchWindowActionDelegate#run */ public void run(IAction action) { if (selection != null && selection instanceof TreeSelection){ TreeSelection ts = (TreeSelection)selection; Object element = ts.getFirstElement(); if (element instanceof Module){ Module udfModule = (Module)element; IProject project = udfModule.getPackage(); /* * export it to the local project */ IFolder udfFolder = CoreActivator.getUdfFolder(project); IFile udfFile = udfFolder.getFile(udfModule.getName()); try { String contense = udfModule.getSource(); InputStream stream = new ByteArrayInputStream(contense.getBytes()); if (udfFile.exists()) { udfFile.setContents(stream, true, true, null); } else { udfFile.create(stream, true, null); } stream.close(); udfFile.setPersistentProperty(CoreActivator.EDITED_FROM_NODE, udfModule.getCluster().getRandomNodeID()); /* * Edit it */ IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); IDE.openEditor( page, udfFile ); } catch (IOException e) { } catch ( PartInitException e ) { CoreActivator.showError("Could not open UDF module: " + udfModule.getName()); } catch (CoreException e) { CoreActivator.showError("Could not download UDF module: " + udfModule.getName()); } } } } /** * Selection in the workbench has been changed. We * can change the state of the 'real' action here * if we want, but this can only happen after * the delegate has been created. * @see IWorkbenchWindowActionDelegate#selectionChanged */ public void selectionChanged(IAction action, ISelection selection) { this.selection = selection; } /** * We can use this method to dispose of any system * resources we previously allocated. * @see IWorkbenchWindowActionDelegate#dispose */ public void dispose() { } /** * We will cache window object in order to * be able to provide parent shell for the message dialog. * @see IWorkbenchWindowActionDelegate#init */ public void init(IWorkbenchWindow window) { } }