Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Specifying file to open on command line of RCP editor
Specifying file to open on command line of RCP editor [message #534356] Tue, 18 May 2010 15:46 Go to next message
Jeff Johnston is currently offline Jeff Johnston
Messages: 173
Registered: July 2009
Senior Member
I have an editor app and want to give the user the option to pass the name of a file to open at startup.

I started by adding the following code in my start() routine:

  	String[] args = (String[])  context.getArguments().get(IApplicationContext.APPLICATION_ARGS);
    	System.out.println("I am here...args length is "+ args.length);
    	if (args.length > 0) {
    		for (int i = 0; i < args.length; ++i) {
    			System.out.println("arg " + i + " is " + args[i]);
    		}
    	}


When I run in it from Eclipse and pass arguments in the run configuration like: "-file my.file", I see those arguments fine. When I build it to run from the command line by exporting, I don't see any output. How should I print debugging statements?

To open the file at start(), do I need to create a thread and then use similar logic to what is used for the OpenFileAction of the sample RCP TextEditor or can I just directly open the editor?

Any examples of priming an RCP editor out there I can look at?
Re: Specifying file to open on command line of RCP editor [message #534377 is a reply to message #534356] Tue, 18 May 2010 17:52 Go to previous messageGo to next message
Jeff Johnston is currently offline Jeff Johnston
Messages: 173
Registered: July 2009
Senior Member
Ok, after some brief examination I realize now that I cannot open the file directly in the start() method and that was a foolish proposal.

So, what is the proper way to open a file automatically when the RCP editor is started?
Re: Specifying file to open on command line of RCP editor [message #534531 is a reply to message #534377] Wed, 19 May 2010 08:50 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

<3.6: you have to write a server of some kind that can listen on a
socket, and an little app that can connect to the server and tell it to
"open <file>"

In 3.6 support for this was added to the IDE and the eclipse launcher.
See https://bugs.eclipse.org/bugs/show_bug.cgi?id=4922

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: Specifying file to open on command line of RCP editor [message #534659 is a reply to message #534531] Wed, 19 May 2010 14:28 Go to previous messageGo to next message
Jeff Johnston is currently offline Jeff Johnston
Messages: 173
Registered: July 2009
Senior Member
I found the info for --launcher.openFile and --launcher.defaultAction but I am ran into a problem trying to get it to work using 3.6RC1.

If I already have another Eclipse session running and specify --launcher.openFile, then it opens the file in the existing eclipse session and my RCP app does not start. I tried both putting the specification on the command line and manually editing the eclipse.ini for the RCP eclipse. Both cases open the specified file in the existing Eclipse.

Shouldn't an RCP instance of Eclipse always open a new instance and never share activities with an open Eclipse session?
Re: Specifying file to open on command line of RCP editor [message #534989 is a reply to message #534659] Thu, 20 May 2010 18:31 Go to previous message
Jeff Johnston is currently offline Jeff Johnston
Messages: 173
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);
				}
			}
		});
	}

}

Previous Topic:RCP e4: Activities
Next Topic:Force loading of a stacked view
Goto Forum:
  


Current Time: Mon Jul 28 06:20:21 EDT 2014

Powered by FUDForum. Page generated in 0.02785 seconds