|
|
|
|
Re: Specifying file to open on command line of RCP editor [message #534989 is a reply to message #534659] |
Thu, 20 May 2010 22:31 |
Jeff Johnston Messages: 215 Registered: July 2009 |
Senior Member |
|
|
Ok, I finally figured out what was meant by the advise given. The --launcher.openFile won't work for an RCP application without fiddling with the WorkbenchAdviser.
The following are my WorkbenchAdviser and special classes in case someone else is trying to get this to work too. Note that my RCP app has the title: "RPM Specfile Editor". To run this on Linux, I have to issue:
[path_to_my_rcp_folder]/eclipse/eclipse -name "RPM Specfile Editor" --launcher.openFile FILE1 FILE2 FILE3 ....
I will eventually wrapper this so the end-user would just specify the editor name and whatever files (if any) to open on startup.
package org.eclipse.linuxtools.rpm.speceditor.rcp;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.application.WorkbenchAdvisor;
public class RPMEditorApplication implements IApplication {
private Display display;
public Object start(IApplicationContext context) throws Exception {
display = PlatformUI.createDisplay();
DelayedEventsProcessor processor = new DelayedEventsProcessor(display);
WorkbenchAdvisor workbenchAdvisor = new RPMEditorWorkbenchAdvisor(processor);
try {
int returnCode = PlatformUI.createAndRunWorkbench(display, workbenchAdvisor);
if (returnCode == PlatformUI.RETURN_RESTART)
return IApplication.EXIT_RESTART;
return IApplication.EXIT_OK;
} finally {
display.dispose();
}
}
public void stop() {
// FIXME: is this ok?
display.close();
display.dispose();
}
}
package org.eclipse.linuxtools.rpm.speceditor.rcp;
import org.eclipse.linuxtools.rpm.speceditor.rcp.actions.RPMEditorActionBarAdvisor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
public class RPMEditorWorkbenchAdvisor extends WorkbenchAdvisor {
private DelayedEventsProcessor processor;
public RPMEditorWorkbenchAdvisor(DelayedEventsProcessor processor) {
this.processor = processor;
}
public String getInitialWindowPerspectiveId() {
return "org.eclipse.linuxtools.rpm.speceditor.rcp.perspective"; //$NON-NLS-1$
}
public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
return new WorkbenchWindowAdvisor(configurer) {
public void preWindowOpen() {
super.preWindowOpen();
getWindowConfigurer().setInitialSize(new Point(600, 450));
getWindowConfigurer().setShowCoolBar(true);
getWindowConfigurer().setShowStatusLine(true);
getWindowConfigurer().setTitle("RPM Specfile Editor"); //$NON-NLS-1$
}
public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer abConfigurer) {
return new RPMEditorActionBarAdvisor(abConfigurer);
}
};
}
/* (non-Javadoc)
* @see org.eclipse.ui.application.WorkbenchAdvisor#eventLoopIdle(org.eclipse.swt.widgets.Display)
*/
public void eventLoopIdle(Display display) {
if (processor != null)
processor.catchUp(display);
super.eventLoopIdle(display);
}
}
package org.eclipse.linuxtools.rpm.speceditor.rcp;
/*******************************************************************************
* Copyright (c) 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
******************************************************************************/
import java.util.ArrayList;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileInfo;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
/**
* Helper class used to process delayed events.
* Events currently supported:
* <ul>
* <li>SWT.OpenDoc</li>
* </ul>
* @since 3.3
*/
public class DelayedEventsProcessor implements Listener {
private ArrayList<String> filesToOpen = new ArrayList<String>(1);
/**
* Constructor.
* @param display display used as a source of event
*/
public DelayedEventsProcessor(Display display) {
display.addListener(SWT.OpenDocument, this);
}
/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
*/
public void handleEvent(Event event) {
final String path = event.text;
if (path == null)
return;
// If we start supporting events that can arrive on a non-UI thread, the following
// line will need to be in a "synchronized" block:
System.out.println("handleEvent adding " + path.toString());
filesToOpen.add(path);
}
/**
* Process delayed events.
* @param display display associated with the workbench
*/
public void catchUp(Display display) {
if (filesToOpen.isEmpty())
return;
// If we start supporting events that can arrive on a non-UI thread, the following
// lines will need to be in a "synchronized" block:
String[] filePaths = new String[filesToOpen.size()];
filesToOpen.toArray(filePaths);
filesToOpen.clear();
for(int i = 0; i < filePaths.length; i++) {
openFile(display, filePaths[i]);
}
}
private void openFile(Display display, final String path) {
display.asyncExec(new Runnable() {
public void run() {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window == null)
return;
IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(path));
IFileInfo fetchInfo = fileStore.fetchInfo();
if (!fetchInfo.isDirectory() && fetchInfo.exists()) {
IWorkbenchPage page = window.getActivePage();
try {
IDE.openEditorOnFileStore(page, fileStore);
} catch (PartInitException e) {
String msg = "Error on open of: " + fileStore.getName();
MessageDialog.open(MessageDialog.ERROR, window.getShell(),
"Initial Open",
msg, SWT.SHEET);
}
} else {
String msg = "File not found: " + path.toString();
MessageDialog.open(MessageDialog.ERROR, window.getShell(),
"Initial Open",
msg, SWT.SHEET);
}
}
});
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03155 seconds