Search in sources :

Example 1 with PerformRefactoringOperation

use of org.eclipse.ltk.core.refactoring.PerformRefactoringOperation in project tdi-studio-se by Talend.

the class EditPropertiesAction method processRename.

protected void processRename(IRepositoryNode node, String originalName) {
    try {
        IRunProcessService runProcessService = CorePlugin.getDefault().getRunProcessService();
        ITalendProcessJavaProject talendProcessJavaProject = runProcessService.getTalendProcessJavaProject();
        if (talendProcessJavaProject == null) {
            return;
        }
        IFolder srcFolder = talendProcessJavaProject.getSrcFolder();
        IPackageFragmentRoot root = talendProcessJavaProject.getJavaProject().getPackageFragmentRoot(srcFolder);
        // add for bug TDI-24379 on August 23, 2013.
        IFolder srcInterFolder = srcFolder.getFolder(JavaUtils.JAVA_INTERNAL_DIRECTORY);
        if (srcInterFolder.exists()) {
            File file = new File(srcInterFolder.getLocationURI());
            for (File f : file.listFiles()) {
                if (f.isFile()) {
                    f.delete();
                }
            }
        }
        // qli modified to fix the bug 5400 and 6185.
        // update for fix [TESB-6784]
        IPackageFragment routinesPkg = getPackageFragment(root, node);
        // ICompilationUnit unit = routinesPkg.getCompilationUnit(originalName +
        // SuffixConstants.SUFFIX_STRING_java);
        //$NON-NLS-1$
        ICompilationUnit unit = routinesPkg.getCompilationUnit(originalName + ".java");
        if (unit == null) {
            return;
        }
        String newName = node.getObject().getProperty().getLabel();
        JavaRenameProcessor processor = new RenameCompilationUnitProcessor(unit);
        // processor.setNewElementName(newName + SuffixConstants.SUFFIX_STRING_java);
        //$NON-NLS-1$
        processor.setNewElementName(newName + ".java");
        RenameRefactoring ref = new RenameRefactoring(processor);
        final PerformRefactoringOperation operation = new PerformRefactoringOperation(ref, CheckConditionsOperation.ALL_CONDITIONS);
        IRunnableWithProgress r = new IRunnableWithProgress() {

            @Override
            public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            operation.run(monitor);
                        } catch (CoreException e) {
                            ExceptionHandler.process(e);
                        }
                    }
                });
            }
        };
        PlatformUI.getWorkbench().getProgressService().run(true, true, r);
        RefactoringStatus conditionStatus = operation.getConditionStatus();
        if (conditionStatus.hasError()) {
            //$NON-NLS-1$
            String errorMessage = Messages.getString("EditPropertiesAction.renameError", unit.getElementName(), newName);
            RefactoringStatusEntry[] entries = conditionStatus.getEntries();
            for (RefactoringStatusEntry entry : entries) {
                //$NON-NLS-1$
                errorMessage += "\n>>>" + entry.getMessage();
            }
            Shell shell = null;
            IRepositoryView viewPart = getViewPart();
            if (viewPart != null) {
                shell = viewPart.getViewSite().getShell();
            } else {
                shell = Display.getCurrent().getActiveShell();
            }
            //$NON-NLS-1$
            MessageDialog.openError(shell, Messages.getString("EditPropertiesAction.warning"), errorMessage);
            return;
        }
        // ICompilationUnit newUnit = routinesPkg.getCompilationUnit(newName + SuffixConstants.SUFFIX_STRING_java);
        //$NON-NLS-1$
        ICompilationUnit newUnit = routinesPkg.getCompilationUnit(newName + ".java");
        if (newUnit == null) {
            return;
        }
        RoutineItem item = (RoutineItem) node.getObject().getProperty().getItem();
        IFile javaFile = (IFile) newUnit.getAdapter(IResource.class);
        try {
            ByteArray byteArray = item.getContent();
            byteArray.setInnerContentFromFile(javaFile);
            IRepositoryService service = CorePlugin.getDefault().getRepositoryService();
            IProxyRepositoryFactory repFactory = service.getProxyRepositoryFactory();
            repFactory.save(item);
        } catch (Exception e) {
            // e.printStackTrace();
            ExceptionHandler.process(e);
        }
    } catch (Exception e) {
        // e.printStackTrace();
        ExceptionHandler.process(e);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IRunProcessService(org.talend.designer.runprocess.IRunProcessService) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) PerformRefactoringOperation(org.eclipse.ltk.core.refactoring.PerformRefactoringOperation) IRepositoryView(org.talend.repository.ui.views.IRepositoryView) ITalendProcessJavaProject(org.talend.core.runtime.process.ITalendProcessJavaProject) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IRepositoryService(org.talend.repository.model.IRepositoryService) Shell(org.eclipse.swt.widgets.Shell) ByteArray(org.talend.core.model.properties.ByteArray) JavaRenameProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) RefactoringStatusEntry(org.eclipse.ltk.core.refactoring.RefactoringStatusEntry) RoutineItem(org.talend.core.model.properties.RoutineItem) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) RenameCompilationUnitProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenameCompilationUnitProcessor) CoreException(org.eclipse.core.runtime.CoreException) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 2 with PerformRefactoringOperation

use of org.eclipse.ltk.core.refactoring.PerformRefactoringOperation in project tdi-studio-se by Talend.

the class StandAloneTalendJavaEditor method refreshName.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.core.ui.IUIRefresher#refreshName()
     */
@Override
public void refreshName() {
    ICompilationUnit unit = (ICompilationUnit) this.getInputJavaElement();
    String newName = item.getProperty().getLabel();
    try {
        boolean noError = true;
        // String newName2 = newName + SuffixConstants.SUFFIX_STRING_java;
        //$NON-NLS-1$
        String newName2 = newName + ".java";
        if (item instanceof RoutineItem && !unit.getElementName().equals(newName2)) {
            JavaRenameProcessor processor = new RenameCompilationUnitProcessor(unit);
            processor.setNewElementName(newName2);
            RenameRefactoring ref = new RenameRefactoring(processor);
            final PerformRefactoringOperation operation = new PerformRefactoringOperation(ref, CheckConditionsOperation.ALL_CONDITIONS);
            IRunnableWithProgress r = new IRunnableWithProgress() {

                @Override
                public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    Display.getDefault().asyncExec(new Runnable() {

                        @Override
                        public void run() {
                            try {
                                operation.run(monitor);
                            } catch (CoreException e) {
                                ExceptionHandler.process(e);
                            }
                        }
                    });
                }
            };
            PlatformUI.getWorkbench().getProgressService().run(true, true, r);
            RefactoringStatus conditionStatus = operation.getConditionStatus();
            if (conditionStatus != null && conditionStatus.hasError()) {
                //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                String errorMessage = "Rename " + unit.getElementName() + " to " + newName + " has errors!";
                RefactoringStatusEntry[] entries = conditionStatus.getEntries();
                for (RefactoringStatusEntry entry : entries) {
                    //$NON-NLS-1$
                    errorMessage += "\n>>>" + entry.getMessage();
                }
                //$NON-NLS-1$
                MessageDialog.openError(this.getSite().getShell(), "Warning", errorMessage);
                noError = false;
            }
        }
        if (noError) {
            doSave(null);
        }
        setName();
    } catch (Exception e) {
        ExceptionHandler.process(e);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) RefactoringStatusEntry(org.eclipse.ltk.core.refactoring.RefactoringStatusEntry) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) RoutineItem(org.talend.core.model.properties.RoutineItem) PerformRefactoringOperation(org.eclipse.ltk.core.refactoring.PerformRefactoringOperation) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) LoginException(org.talend.commons.exception.LoginException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.talend.commons.exception.PersistenceException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) RenameCompilationUnitProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.RenameCompilationUnitProcessor) CoreException(org.eclipse.core.runtime.CoreException) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) JavaRenameProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor)

Example 3 with PerformRefactoringOperation

use of org.eclipse.ltk.core.refactoring.PerformRefactoringOperation in project jbosstools-hibernate by jbosstools.

the class HibernateConsolePlugin method listenForConfigurations.

private void listenForConfigurations() {
    final ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
    kcl = new KnownConfigurationsAdapter() {

        /**
         * @param root
         * @param forUpdate - shows whether physical removal necessary
         */
        public void configurationRemoved(ConsoleConfiguration root, boolean forUpdate) {
            if (!forUpdate) {
                try {
                    removeConfiguration(root.getName());
                } catch (CoreException e) {
                    logErrorMessage(HibernateConsoleMessages.HibernateConsolePlugin_could_not_delete_launch_config_for + root.getName(), e);
                }
            }
        }
    };
    KnownConfigurations.getInstance().addConsoleConfigurationListener(kcl);
    icl = new ILaunchConfigurationListener() {

        boolean isConsoleConfiguration(ILaunchConfiguration configuration) {
            try {
                return configuration.getType().getIdentifier().equals(ICodeGenerationLaunchConstants.CONSOLE_CONFIGURATION_LAUNCH_TYPE_ID);
            } catch (CoreException e) {
            // HibernateConsolePlugin.getDefault().log( e );
            // ignore since it occurs on delete
            }
            return false;
        }

        public void launchConfigurationRemoved(ILaunchConfiguration configuration) {
            ConsoleConfiguration cfg = KnownConfigurations.getInstance().find(configuration.getName());
            if (cfg != null) {
                // file system removal have been made already.
                KnownConfigurations.getInstance().removeConfiguration(cfg, true);
            }
        }

        public void launchConfigurationChanged(ILaunchConfiguration configuration) {
            if (configuration.isWorkingCopy() || isTemporary(configuration)) {
                return;
            }
            if (isConsoleConfiguration(configuration)) {
                KnownConfigurations instance = KnownConfigurations.getInstance();
                ConsoleConfiguration oldcfg = instance.find(configuration.getName());
                if (oldcfg != null) {
                    // reset it no matter what.
                    oldcfg.reset();
                } else {
                    // A new one!
                    ConsoleConfigurationPreferences adapter = buildConfigurationPreferences(configuration);
                    instance.addConfiguration(new ConsoleConfiguration(adapter), true);
                }
            }
        }

        private ConsoleConfigurationPreferences buildConfigurationPreferences(ILaunchConfiguration configuration) {
            return new EclipseLaunchConsoleConfigurationPreferences(configuration);
        }

        public void launchConfigurationAdded(ILaunchConfiguration configuration) {
            if (isConsoleConfiguration(configuration)) {
                ILaunchConfiguration movedFrom = launchManager.getMovedFrom(configuration);
                if (movedFrom != null && isConsoleConfiguration(movedFrom)) {
                    KnownConfigurations instance = KnownConfigurations.getInstance();
                    ConsoleConfiguration oldcfg = instance.find(movedFrom.getName());
                    if (oldcfg != null) {
                        // call this before we remove old configuration
                        refactor(movedFrom, configuration);
                        // reset it no matter what.
                        oldcfg.reset();
                        instance.removeConfiguration(oldcfg, false);
                    }
                }
                KnownConfigurations instance = KnownConfigurations.getInstance();
                ConsoleConfigurationPreferences adapter = buildConfigurationPreferences(configuration);
                boolean temporary = isTemporary(configuration);
                if (!temporary) {
                    instance.addConfiguration(new ConsoleConfiguration(adapter), true);
                }
            }
        }

        private void refactor(ILaunchConfiguration oldConfiguration, ILaunchConfiguration newConfiguration) {
            if (!oldConfiguration.getName().equals(newConfiguration.getName())) {
                // only rename of console configuration refactoring is supported.
                ConsoleConfigurationRenameProcessor proc = new ConsoleConfigurationRenameProcessor(oldConfiguration, newConfiguration.getName());
                // Refactor for rename
                PerformRefactoringOperation refOperation = new PerformRefactoringOperation(new ProcessorBasedRefactoring(proc), CheckConditionsOperation.ALL_CONDITIONS);
                try {
                    ResourcesPlugin.getWorkspace().run(refOperation, null);
                } catch (OperationCanceledException oce) {
                    throw new OperationCanceledException();
                } catch (CoreException ce) {
                    HibernateConsolePlugin.openError(new Shell(), HibernateConsoleMessages.EditConsoleConfiguration_rename_refactoring_error_totle, ce.getLocalizedMessage(), ce, HibernateConsolePlugin.PERFORM_SYNC_EXEC);
                }
            }
        }

        private boolean isTemporary(ILaunchConfiguration configuration) {
            boolean temporary = true;
            try {
                temporary = configuration.getAttribute(AddConfigurationAction.TEMPORARY_CONFIG_FLAG, false);
            } catch (CoreException e) {
                HibernateConsolePlugin.getDefault().showError(getShell(), HibernateConsoleMessages.HibernateConsolePlugin_problem_to_get_flag, e);
            }
            return temporary;
        }
    };
    launchManager.addLaunchConfigurationListener(icl);
}
Also used : KnownConfigurationsAdapter(org.hibernate.console.KnownConfigurationsAdapter) ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) ConsoleConfiguration(org.hibernate.console.ConsoleConfiguration) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ConsoleConfigurationRenameProcessor(org.hibernate.eclipse.launch.core.refactoring.ConsoleConfigurationRenameProcessor) ILaunchManager(org.eclipse.debug.core.ILaunchManager) PerformRefactoringOperation(org.eclipse.ltk.core.refactoring.PerformRefactoringOperation) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) ILaunchConfigurationListener(org.eclipse.debug.core.ILaunchConfigurationListener) Shell(org.eclipse.swt.widgets.Shell) CoreException(org.eclipse.core.runtime.CoreException) ConsoleConfigurationPreferences(org.hibernate.console.preferences.ConsoleConfigurationPreferences) KnownConfigurations(org.hibernate.console.KnownConfigurations)

Example 4 with PerformRefactoringOperation

use of org.eclipse.ltk.core.refactoring.PerformRefactoringOperation in project eclipse.platform.runtime by eclipse.

the class ConvertMessageBundleAction method run.

@Override
public void run(IAction action) {
    if (fAccessorUnit == null)
        return;
    try {
        final GotoResourceAction pAction = new GotoResourceAction(fPart);
        IRunnableWithProgress runnable = new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) {
                pAction.run();
            }
        };
        PlatformUI.getWorkbench().getProgressService().run(false, false, runnable);
        IFile propertiesFile = (IFile) pAction.getResource();
        if (propertiesFile == null)
            return;
        MessageBundleRefactoring refactoring = new MessageBundleRefactoring(fAccessorUnit.getTypes()[0], propertiesFile);
        PerformRefactoringOperation op = new PerformRefactoringOperation(refactoring, CheckConditionsOperation.ALL_CONDITIONS);
        PlatformUI.getWorkbench().getProgressService().run(false, false, new WorkbenchRunnableAdapter(op));
    } catch (CoreException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) WorkbenchRunnableAdapter(org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter) PerformRefactoringOperation(org.eclipse.ltk.core.refactoring.PerformRefactoringOperation) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Example 5 with PerformRefactoringOperation

use of org.eclipse.ltk.core.refactoring.PerformRefactoringOperation in project eclipse.platform.runtime by eclipse.

the class RemoveUnusedMessagesAction method run.

@Override
public void run(IAction action) {
    if (fAccessorUnit == null)
        return;
    try {
        final GotoResourceAction pAction = new GotoResourceAction(fPart);
        IRunnableWithProgress runnable = new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) {
                pAction.run();
            }
        };
        PlatformUI.getWorkbench().getProgressService().run(false, false, runnable);
        IFile propertiesFile = (IFile) pAction.getResource();
        if (propertiesFile == null)
            return;
        RemoveUnusedMessages refactoring = new RemoveUnusedMessages(fAccessorUnit.getTypes()[0], propertiesFile);
        PerformRefactoringOperation op = new PerformRefactoringOperation(refactoring, CheckConditionsOperation.ALL_CONDITIONS);
        PlatformUI.getWorkbench().getProgressService().run(false, true, new WorkbenchRunnableAdapter(op));
    } catch (CoreException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) WorkbenchRunnableAdapter(org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter) PerformRefactoringOperation(org.eclipse.ltk.core.refactoring.PerformRefactoringOperation) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Aggregations

CoreException (org.eclipse.core.runtime.CoreException)6 PerformRefactoringOperation (org.eclipse.ltk.core.refactoring.PerformRefactoringOperation)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)5 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)5 IFile (org.eclipse.core.resources.IFile)3 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)3 RefactoringStatusEntry (org.eclipse.ltk.core.refactoring.RefactoringStatusEntry)3 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 JavaRenameProcessor (org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor)2 RenameCompilationUnitProcessor (org.eclipse.jdt.internal.corext.refactoring.rename.RenameCompilationUnitProcessor)2 WorkbenchRunnableAdapter (org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter)2 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)2 Shell (org.eclipse.swt.widgets.Shell)2 PartInitException (org.eclipse.ui.PartInitException)2 RoutineItem (org.talend.core.model.properties.RoutineItem)2 File (java.io.File)1 IContainer (org.eclipse.core.resources.IContainer)1