~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to rpm/org.eclipse.linuxtools.rpm.rpmlint/src/org/eclipse/linuxtools/internal/rpm/rpmlint/actions/RunRpmlintAction.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2009 Red Hat, Inc.
 
3
 * All rights reserved. This program and the accompanying materials
 
4
 * are made available under the terms of the Eclipse Public License v1.0
 
5
 * which accompanies this distribution, and is available at
 
6
 * http://www.eclipse.org/legal/epl-v10.html
 
7
 * 
 
8
 * Contributors:
 
9
 *     Red Hat - initial API and implementation
 
10
 *******************************************************************************/
 
11
package org.eclipse.linuxtools.internal.rpm.rpmlint.actions;
 
12
 
 
13
import java.io.IOException;
 
14
 
 
15
import org.eclipse.core.commands.AbstractHandler;
 
16
import org.eclipse.core.commands.ExecutionEvent;
 
17
import org.eclipse.core.resources.IFile;
 
18
import org.eclipse.core.runtime.IAdaptable;
 
19
import org.eclipse.core.runtime.IStatus;
 
20
import org.eclipse.core.runtime.Status;
 
21
import org.eclipse.jface.dialogs.ErrorDialog;
 
22
import org.eclipse.jface.viewers.ISelection;
 
23
import org.eclipse.jface.viewers.IStructuredSelection;
 
24
import org.eclipse.linuxtools.internal.rpm.rpmlint.Activator;
 
25
import org.eclipse.linuxtools.internal.rpm.rpmlint.RpmlintLog;
 
26
import org.eclipse.linuxtools.rpm.core.utils.Utils;
 
27
import org.eclipse.ui.IEditorInput;
 
28
import org.eclipse.ui.IEditorPart;
 
29
import org.eclipse.ui.IFileEditorInput;
 
30
import org.eclipse.ui.IURIEditorInput;
 
31
import org.eclipse.ui.PlatformUI;
 
32
import org.eclipse.ui.console.ConsolePlugin;
 
33
import org.eclipse.ui.console.IConsole;
 
34
import org.eclipse.ui.console.IConsoleManager;
 
35
import org.eclipse.ui.console.MessageConsole;
 
36
import org.eclipse.ui.console.MessageConsoleStream;
 
37
import org.eclipse.ui.handlers.HandlerUtil;
 
38
 
 
39
/**
 
40
 * Manually invoke rpmlint action, which prints the output of rpmlint execution to the console.
 
41
 *
 
42
 */
 
43
public class RunRpmlintAction extends AbstractHandler{
 
44
        /**
 
45
         * @param event The execution event.
 
46
         * @return Nothing.
 
47
         */
 
48
        public Object execute(ExecutionEvent event)  {
 
49
                ISelection selection = HandlerUtil.getCurrentSelection(event);
 
50
                if (selection instanceof IStructuredSelection) {
 
51
                        for (Object element : ((IStructuredSelection) selection).toList()) {
 
52
                                IFile rpmFile = null;
 
53
                                if (element instanceof IFile) {
 
54
                                        rpmFile = (IFile) element;
 
55
                                } else if (element instanceof IAdaptable) {
 
56
                                        rpmFile = (IFile) ((IAdaptable) element)
 
57
                                                        .getAdapter(IFile.class);
 
58
                                }
 
59
                                if (rpmFile != null) {
 
60
                                        runRpmlint(rpmFile.getLocation().toString());
 
61
                                }
 
62
                        }
 
63
                } else {
 
64
                        IEditorPart editor = HandlerUtil.getActiveEditor(event);
 
65
                        if (editor != null) {
 
66
                                IEditorInput editorInput = editor.getEditorInput();
 
67
                                if (editorInput != null && editorInput instanceof IFileEditorInput) {
 
68
                                        runRpmlint(((IFileEditorInput) editorInput).getFile().getLocation().toString());
 
69
                                } else if (editorInput != null && editorInput instanceof IURIEditorInput) {
 
70
                                        runRpmlint(((IURIEditorInput) editorInput).getURI().getPath().toString());
 
71
                                }
 
72
                        }
 
73
                }
 
74
                return null;
 
75
 
 
76
        }
 
77
 
 
78
        private void runRpmlint(String location) {
 
79
                try {
 
80
                        if (Utils.fileExist(Activator.getRpmlintPath())) {
 
81
                                String output = Utils.runCommandToString(Activator
 
82
                                                .getRpmlintPath(),
 
83
                                                "-i", location); //$NON-NLS-1$
 
84
                                MessageConsole myConsole = findConsole(Messages.RunRpmlintAction_0);
 
85
                                MessageConsoleStream out = myConsole
 
86
                                                .newMessageStream();
 
87
                                myConsole.clearConsole();
 
88
                                myConsole.activate();
 
89
                                out.println(output);
 
90
                        } else {
 
91
                                IStatus warning = new Status(
 
92
                                                IStatus.WARNING,
 
93
                                                Activator.PLUGIN_ID,
 
94
                                                1,
 
95
                                                Messages.RunRpmlintAction_1,
 
96
                                                null);
 
97
                                ErrorDialog.openError(PlatformUI.getWorkbench()
 
98
                                                .getActiveWorkbenchWindow().getShell(),
 
99
                                                Messages.RunRpmlintAction_2,
 
100
                                                null, warning);
 
101
                        }
 
102
                } catch (IOException e) {
 
103
                        // FIXME: rpmlint is not installed in the default place
 
104
                        // -> ask user to open the prefs page.
 
105
                        RpmlintLog.logError(e);
 
106
                }
 
107
        }
 
108
 
 
109
        private MessageConsole findConsole(String name) {
 
110
                ConsolePlugin plugin = ConsolePlugin.getDefault();
 
111
                IConsoleManager conMan = plugin.getConsoleManager();
 
112
                IConsole[] existing = conMan.getConsoles();
 
113
                for (int i = 0; i < existing.length; i++)
 
114
                        if (name.equals(existing[i].getName()))
 
115
                                return (MessageConsole) existing[i];
 
116
                // no console found, so create a new one
 
117
                MessageConsole myConsole = new MessageConsole(name, null);
 
118
                conMan.addConsoles(new IConsole[] { myConsole });
 
119
                return myConsole;
 
120
        }
 
121
}