01-27-2010 12:58 AM
n00b here, so please be patient with me. I am experimenting with BrowserFieldDemo and I would like to get rid of the popup menu that appears when you are using the program to click on an html link (or anywhere on the page, for that matter). Specifically, I am talking about the popup menu that offers options like "Get Link."
I've searched around the forums and found some code that should accomplish this goal. (thanks Jasper!) However, I'm getting a "cannot find symbol" error when I insert the piece of code for onMenu (the menu removing piece). Can anyone help me troubleshoot what I'm doing wrong? I thought I had all of the necessary classes to run this (including the Menu and Screen classes), but perhaps not.
Here's the code:
import java.io.IOException; import javax.microedition.io.HttpConnection; import net.rim.device.api.browser.field.*; import net.rim.device.api.io.http.HttpHeaders; import net.rim.device.api.system.Application; import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.Status; import net.rim.device.api.ui.container.MainScreen; import net.rim.device.api.system.*; import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.Screen; final class BrowserFieldDemo extends UiApplication implements RenderingApplication { private static final String REFERER = "referer"; private RenderingSession _renderingSession; private HttpConnection _currentConnection; private MainScreen _mainScreen; private int instance; public boolean onMenu(int instance){ if( instance == Menu.INSTANCE_CONTEXT ){ //Retrieve the main menu (for some reason, the context menu cannot handle image hyperlinks) Menu mainMenu = this.getMenu( Menu.INSTANCE_DEFAULT ); int numItems = mainMenu.getSize(); for( int i = 0; i < numItems; i++ ){ MenuItem curItem = mainMenu.getItem(i); String name = curItem.toString(); if( name.equalsIgnoreCase( "Get Link" ) ){ curItem.run(); break; } } return false; }// else return super.onMenu(instance); } /*************************************************************************** * Main. ************************************************** ************************/ public static void main(String[] args) { BrowserFieldDemo app = new BrowserFieldDemo(); app.enterEventDispatcher(); } private BrowserFieldDemo() { _mainScreen = new MainScreen(); pushScreen(_mainScreen); _renderingSession = RenderingSession.getNewInstance(); // Enable javascript. //_renderingSession.getRenderingOptions().setPrope rty(RenderingOptions.CORE_OPTIONS_GUID, RenderingOptions.JAVASCRIPT_ENABLED, true); PrimaryResourceFetchThread thread = new PrimaryResourceFetchThread("http://www.google.com", null, null, null, this); thread.start(); } void processConnection(HttpConnection connection, Event e) { // Cancel previous request. if (_currentConnection != null) { try { _currentConnection.close(); } catch (IOException e1) { } } _currentConnection = connection; BrowserContent browserContent = null; try { browserContent = _renderingSession.getBrowserContent(connection, this, e); if (browserContent != null) { Field field = browserContent.getDisplayableContent(); if (field != null) { synchronized (Application.getEventLock()) { _mainScreen.deleteAll(); _mainScreen.add(field); } } browserContent.finishLoading(); } } catch (RenderingException re) { } finally { SecondaryResourceFetchThread.doneAddingImages(); } } /** * @see net.rim.device.api.browser.RenderingApplication#ev entOccurred(net.rim.device.api.browser.Event) */ public Object eventOccurred(Event event) { int eventId = event.getUID(); switch (eventId) { case Event.EVENT_URL_REQUESTED : { UrlRequestedEvent urlRequestedEvent = (UrlRequestedEvent) event; PrimaryResourceFetchThread thread = new PrimaryResourceFetchThread(urlRequestedEvent.getUR L(), urlRequestedEvent.getHeaders(), urlRequestedEvent.getPostData(), event, this); thread.start(); break; } case Event.EVENT_BROWSER_CONTENT_CHANGED: { // Browser field title might have changed update title. BrowserContentChangedEvent browserContentChangedEvent = (BrowserContentChangedEvent) event; if (browserContentChangedEvent.getSource() instanceof BrowserContent) { BrowserContent browserField = (BrowserContent) browserContentChangedEvent.getSource(); String newTitle = browserField.getTitle(); if (newTitle != null) { synchronized (getAppEventLock()) { _mainScreen.setTitle(newTitle); } } } break; } case Event.EVENT_REDIRECT : { RedirectEvent e = (RedirectEvent) event; String referrer = e.getSourceURL(); switch (e.getType()) { case RedirectEvent.TYPE_SINGLE_FRAME_REDIRECT : // Show redirect message. Application.getApplication().invokeAndWait(new Runnable() { public void run() { Status.show("You are being redirected to a different page..."); } }); break; case RedirectEvent.TYPE_JAVASCRIPT : break; case RedirectEvent.TYPE_META : // MSIE and Mozilla don't send a Referer for META Refresh. referrer = null; break; case RedirectEvent.TYPE_300_REDIRECT : // MSIE, Mozilla, and Opera all send the original // request's Referer as the Referer for the new // request. Object eventSource = e.getSource(); if (eventSource instanceof HttpConnection) { referrer = ((HttpConnection)eventSource).getRequestProperty(R EFERER); } break; } HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setProperty(REFERER, referrer); PrimaryResourceFetchThread thread = new PrimaryResourceFetchThread(e.getLocation(), requestHeaders,null, event, this); thread.start(); break; } case Event.EVENT_CLOSE : // TODO: close the appication break; case Event.EVENT_SET_HEADER : // No cache support. case Event.EVENT_SET_HTTP_COOKIE : // No cookie support. case Event.EVENT_HISTORY : // No history support. case Event.EVENT_EXECUTING_SCRIPT : // No progress bar is supported. case Event.EVENT_FULL_WINDOW : // No full window support. case Event.EVENT_STOP : // No stop loading support. default : } return null; } /** * @see net.rim.device.api.browser.RenderingApplication#ge tAvailableHeight(net.rim.device.api.browser.Browse rContent) */ public int getAvailableHeight(BrowserContent browserField) { // Field has full screen. return Display.getHeight(); } /** * @see net.rim.device.api.browser.RenderingApplication#ge tAvailableWidth(net.rim.device.api.browser.Browser Content) */ public int getAvailableWidth(BrowserContent browserField) { // Field has full screen. return Display.getWidth(); } /** * @see net.rim.device.api.browser.RenderingApplication#ge tHistoryPosition(net.rim.device.api.browser.Browse rContent) */ public int getHistoryPosition(BrowserContent browserField) { // No history support. return 0; } /** * @see net.rim.device.api.browser.RenderingApplication#ge tHTTPCookie(java.lang.String) */ public String getHTTPCookie(String url) { // No cookie support. return null; } /** * @see net.rim.device.api.browser.RenderingApplication#ge tResource(net.rim.device.api.browser.RequestedReso urce, * net.rim.device.api.browser.BrowserContent) */ public HttpConnection getResource( RequestedResource resource, BrowserContent referrer) { if (resource == null) { return null; } // Check if this is cache-only request. if (resource.isCacheOnly()) { // No cache support. return null; } String url = resource.getUrl(); if (url == null) { return null; } // If referrer is null we must return the connection. if (referrer == null) { HttpConnection connection = Utilities.makeConnection(resource.getUrl(), resource.getRequestHeaders(), null); return connection; } else { // If referrer is provided we can set up the connection on a separate thread. SecondaryResourceFetchThread.enqueue(resource, referrer); } return null; } /** * @see net.rim.device.api.browser.RenderingApplication#in vokeRunnable(java.lang.Runnable) */ public void invokeRunnable(Runnable runnable) { (new Thread(runnable)).start(); } } class PrimaryResourceFetchThread extends Thread { private BrowserFieldDemo _application; private Event _event; private byte[] _postData; private HttpHeaders _requestHeaders; private String _url; PrimaryResourceFetchThread(String url, HttpHeaders requestHeaders, byte[] postData, Event event, BrowserFieldDemo application) { _url = url; _requestHeaders = requestHeaders; _postData = postData; _application = application; _event = event; } public void run() { HttpConnection connection = Utilities.makeConnection(_url, _requestHeaders, _postData); _application.processConnection(connection, _event); } }
And here's the error I'm receiving:
C:\Program Files\Research In Motion\BlackBerry JDE 4.7.0\bin\BrowserConfirm\BrowserFieldDemo.java:74: cannot find symbol symbol : method getMenu(int) location: class com.rim.samples.device.blackberry.browser.BrowserFieldDemo Menu mainMenu = this.getMenu( Menu.INSTANCE_DEFAULT ); ^ C:\Program Files\Research In Motion\BlackBerry JDE 4.7.0\bin\BrowserConfirm\BrowserFieldDemo.java:87: cannot find symbol symbol : method onMenu(int) location: class net.rim.device.api.ui.UiApplication return super.onMenu(instance); ^ 2 errors
02-01-2010 07:55 PM - last edited on 02-01-2010 10:30 PM
I'm still trying to figure out what I'm doing wrong here. Anyone have any ideas?
Btw, I forgot to mention that I am using JDE 4.7.0.41 in Windows XP and a 9530 simulator.
02-09-2010 04:28 PM
Once you manage to overload the Browserfield, it's a pretty impressive tool.
I expect the BrowserField2 (that came with IDE 5.0, I believe) to improve on the GUI as well.
But, here is where your code sample goes wrong. The menu is managed in MainScreen-classes.
Your public boolean onMenu(int instance) needs to go in this class. The reason you're getting this error is because the menu-instance doesn't exist in the UIApplication, and your super-call will fail, because MainScreen isn't a parent of the UIApplication class.
In your code sample (I take it you found it in the Browserfield demo of the Blackberry JDE) you have a _mainScreen variable. That's where this function should go. The easiest way to do this starting from the sample above is to:
It's been a fight from hell to back for me to get that BrowserField to support both local and remote HTML and local and remote embedded media-files, but once you've got it implemented and you take a few steps back, the code looks **bleep** nice. Tricks like this left and right, stretching the BrowserField to it's max. Goodluck, mate.