Wednesday, March 11, 2009

Launching a url using a browser component

If you need to launch a url in Lotus Expeditor based clients, you have several options depending on how you want to present it.

Here's a few ways on how to do it:
  • Using a Browser Widget ( added to a Composite)
  • Using the EmbeddedBrowser View

Using a Browser Widget ( added to a Composite)
Eclipse and Lotus Expeditor offers each a simple browser widget that can be added to a composite. They a very much alike.

Here's a sample using the core Eclipse SWT browser widget:

Browser browser;
try {

browser = new Browser(composite, SWT.NONE);
browser.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, true));
browser.setUrl("http://www.ibm.com");

} catch (SWTError e) {
// Handle any errors.
}

And here's a sample with the Lotus Expeditor equivalent:

WebBrowser browser;
try {

browser = new WebBrowser(composite, SWT.NONE);
browser.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, true));
browser.setUrl("http://www.ibm.com");

} catch (SWTError e) {
// Handle any errors.
}


You can use the browser.setText("...") method to add custom HTML content into the browser, if needed.
You also have the ability to listen to events and traverse the DOM (Document Object Model).

Using the EmbeddedBrowser View
The Lotus Expeditor platform also gives you a viewpart based browser component.

// Every instance needs it's own unique id.
String id = "com.rcpcompany.lotus.notes.spy.browserview.sample";

// Create a map with all the needed properties.
Map configMap = new HashMap();
configMap.put(BrowserPreference.ID, id);
configMap.put(BrowserPreference.INITIAL_URL, url);

// Get the active page
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();

// Create the browser instance.
EmbeddedBrowser browser = BrowserFactory.launch(page, configMap, id);

// Maximize the new browser view if needed.
page.setPartState(page.getActivePartReference(), IWorkbenchPage.STATE_MAXIMIZED);

Tuesday, March 3, 2009

Getting access to files in Eclipse plug-ins

There are several ways to access files in Eclipse. Here are some samples
  • Getting an ImageDescriptor from a plug-in
  • Getting a resolved URL to a resource in another plug-in
  • Opening a resource in an editor

Getting an ImageDescriptor from a plug-in

You can get an image descriptor to an image resource via the
  • UIPlugin.imageDescriptorFromPlugin("pluginId", "icons/image.png")

Getting a URL to a resource inside a plug-in
First you have to have a plug-in (Bundle). Your can get it via
  • Activator.getDefault().getBundle() or
  • Platform.getBundle("plug-in id")

//If you want to access your own plug-in use Activator.getDefault().getBundle()

Bundle bundle = Platform.getBundle("org.eclipse.ui");

// This line return a url looking like this, bundleentry://921/plugin.xml
URL url = bundle.getEntry("/plugin.xml");

try {
// This line resolves the bundle resource url to an explicit file url into the bundle
// The url will get to look like below,
// jar:file:C:\...\org.eclipse.ui_3.4.0.I20080610-1200.jar!/plugin.xml
URL realUrl = FileLocator.resolve(url);

} catch (IOException e) {
e.printStackTrace();
}


Opening a resource in an editor
Here is a sample on how to open the org.eclipse.ui plugin.xml in an editor.

// Start by getting an IPath to a bundle resource.
IPath path = null;
URL url = null;
try {
url = FileLocator.toFileURL( Platform.getBundle("org.eclipse.ui").getEntry("/plugin.xml"));
path = new Path(url.getPath());
} catch (IOException e) {

e.printStackTrace();
}

// Next, use the Eclipse file store to validate and pass to an editor
IFileStore fileStore = EFS.getLocalFileSystem().getStore(path);

if (!fileStore.fetchInfo().isDirectory() && fileStore.fetchInfo().exists()) {

IWorkbenchPage page = getSite().getPage();
try {
IDE.openEditorOnFileStore(page, fileStore);
} catch (PartInitException e) {
e.printStackTrace();
}
}

Listening for selected Lotus Notes documents

If you want to react when documents are selected in the Notes client, you will use the Eclipse Selection Service.

When listening you can reference the some of the document summary classes
  • DocumentSummary (base class for selections)
  • NotesDocumentSummary (document selection in view or when opened)
  • NotesOpenDocumentSummary (only when opened)
  • NotesFieldSelection (field activation in open document)
  • NotesLiveNameSelection (live name field activation in open document)
  • And many others
The classes are located in the following plug-ins
  • com.ibm.csi
  • com.ibm.notes.client

Creating the listener


// Prepare the selection listening. Start by getting the selection service.
ISelectionService ss = getSite().getWorkbenchWindow().getSelectionService();

// Create a selection listener
listener = new ISelectionListener() {

public void selectionChanged(IWorkbenchPart part, ISelection selection) {

// Is the selection a structured selection
if (selection instanceof IStructuredSelection) {

// Get the selection (could contain multiple selected objects)
IStructuredSelection ss = (IStructuredSelection) selection;

// Are the any objects in the selection?
if (ss.isEmpty()) return;

// Let's test the first selected object
Object e = ss.getFirstElement();

// Is the object a NotesOpenDocumentSummary?
if (e instanceof NotesOpenDocumentSummary) {
NotesOpenDocumentSummary ds = (NotesOpenDocumentSummary)e;
String title = ds.getTitle();
String url = ds.getUrl();

//...

}
}
}
};

// Add a post selection listener, so we don't have to listen to
// insignificant UP and DOWN keystroke selection triggering,

// we just need the final selection.

ss.addPostSelectionListener( listener);

Removing the listener
Remember to remove you listener when done.

// Do the following in the dispose() method of the view part or alike.

public void dispose() {
if (listener != null) {
ISelectionService ss = getSite().getWorkbenchWindow().getSelectionService();
ss.removePostSelectionListener(listener);
}
super.dispose();
}

Search This Blog

Eclipse Live: Equinox Ganymede

The creator of an Eclipse Bible, introduces Equinox and its building blocks for creating applications. We overview the architecture and technology, and demonstrate many of the building blocks.
by Jeff McAffer (Code 9)