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
9
* Red Hat - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.linuxtools.internal.rpm.rpmlint.actions;
13
import java.io.IOException;
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;
40
* Manually invoke rpmlint action, which prints the output of rpmlint execution to the console.
43
public class RunRpmlintAction extends AbstractHandler{
45
* @param event The execution event.
48
public Object execute(ExecutionEvent event) {
49
ISelection selection = HandlerUtil.getCurrentSelection(event);
50
if (selection instanceof IStructuredSelection) {
51
for (Object element : ((IStructuredSelection) selection).toList()) {
53
if (element instanceof IFile) {
54
rpmFile = (IFile) element;
55
} else if (element instanceof IAdaptable) {
56
rpmFile = (IFile) ((IAdaptable) element)
57
.getAdapter(IFile.class);
59
if (rpmFile != null) {
60
runRpmlint(rpmFile.getLocation().toString());
64
IEditorPart editor = HandlerUtil.getActiveEditor(event);
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());
78
private void runRpmlint(String location) {
80
if (Utils.fileExist(Activator.getRpmlintPath())) {
81
String output = Utils.runCommandToString(Activator
83
"-i", location); //$NON-NLS-1$
84
MessageConsole myConsole = findConsole(Messages.RunRpmlintAction_0);
85
MessageConsoleStream out = myConsole
87
myConsole.clearConsole();
91
IStatus warning = new Status(
95
Messages.RunRpmlintAction_1,
97
ErrorDialog.openError(PlatformUI.getWorkbench()
98
.getActiveWorkbenchWindow().getShell(),
99
Messages.RunRpmlintAction_2,
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);
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 });