[jsword-devel] BibleDesktop in SWT
DM Smith
dmsmith555 at yahoo.com
Sat Jun 25 16:53:17 MST 2005
Success for using an embedded browser with bible://gen.1-5 (any passage
reference works but use an OSIS format and ) and clicking on embedded
bible:// urls.
The code is some sample code I snagged from the Internet and modified a bit.
This is fast. It took less than 5 seconds to load all of Gen into the
single window. With that kind of performance, we can probably get rid of
the verses per tab notion or replace it with a notion of chapters/tab or
do a book/tab.....
To run the example, get Eclipse 3.1RC4 (just came out) and put this
class in a default package in either the BibleDesktop package or in its
own package. Make sure that the project contains BibleDesktop (for the
converter), JSword and Common.
Right click on the file and choose Run As... -> SWT Application
(This menu choice was recently added, then it was removed, now it is
back in. Who knows if it is going to stay!)
When the app opens you will see typical navigation buttons. The nav
buttons don't work.
Because we intercept the url and load the browser with setText(),
history is not valid. So forward and back don't work.
I did not look into why refresh does not work.
Stop does not work because I did not thread the getting of the bible text.
Also if you right click you will notice that there is a context menu. I
did not try it, but I saw example code on how to disable it. I don't
know if it is possible to provide a different popup menu. It was not
possible earlier.
Print button works! by using a scriptlet.
DM Smith wrote:
> I finished the book. As I see it there are only two
> challenges/problems in using SWT.
> 1) We need to determine whether we can get the Browser component to
> work for our custom protocols. If we can't do it directly, we could
> probably change the urls to be to a port on localhost that the
> application listens to and speaks HTTP over it. I've done it before
> but I'd rather not go there.
import javax.xml.transform.TransformerException;
import org.crosswire.common.xml.*;
import org.crosswire.jsword.book.*;
import org.crosswire.jsword.passage.*;
import org.crosswire.jsword.util.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
mport org.eclipse.swt.widgets.*;
import org.xml.sax.*;
/**
* This class implements a web browser
*/
public class SimpleBrowser {
/**
* Runs the application
*/
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Simple Browser");
createContents(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* Creates the main window's contents
*
* @param shell the main window
*/
private void createContents(Shell shell) {
shell.setLayout(new FormLayout());
// Create the composite to hold the buttons and text field
Composite controls = new Composite(shell, SWT.NONE);
FormData data = new FormData();
data.top = new FormAttachment(0, 0);
data.left = new FormAttachment(0, 0);
data.right = new FormAttachment(100, 0);
controls.setLayoutData(data);
// Create the web browser
final Browser browser = new Browser(shell, SWT.NONE);
browser.addLocationListener(new LocationAdapter()
{
/* (non-Javadoc)
* @see
org.eclipse.swt.browser.LocationAdapter#changing(org.eclipse.swt.browser.LocationEvent)
*/
public void changing(LocationEvent ev)
{
String url = ev.location;
System.out.println("Location = " + ev.location);
if (url.startsWith("bible://"))
{
ev.doit = false;
Browser b = (Browser) ev.getSource();
try
{
// Rip off the protocol and the trailing /
// Note the protocol is converted to lower case
// and a / is appended.
String ref = url.substring(8, url.length() - 1);
b.setText(readStyledText(ref));
}
catch (NoSuchKeyException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (BookException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (TransformerException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SAXException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
data = new FormData();
data.top = new FormAttachment(controls);
data.bottom = new FormAttachment(100, 0);
data.left = new FormAttachment(0, 0);
data.right = new FormAttachment(100, 0);
browser.setLayoutData(data);
// Create the controls and wire them to the browser
controls.setLayout(new GridLayout(7, false));
// Create the back button
Button button = new Button(controls, SWT.PUSH);
button.setText("Back");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
browser.back();
}
});
// Create the forward button
button = new Button(controls, SWT.PUSH);
button.setText("Forward");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
browser.forward();
}
});
// Create the refresh button
button = new Button(controls, SWT.PUSH);
button.setText("Refresh");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
browser.refresh();
}
});
// Create the stop button
button = new Button(controls, SWT.PUSH);
button.setText("Stop");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
browser.stop();
}
});
// Create the back button
button = new Button(controls, SWT.PUSH);
button.setText("Print");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
browser.setUrl("javascript:print();");
}
});
// Create the address entry field and set focus to it
final Text url = new Text(controls, SWT.BORDER);
url.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
url.setFocus();
// Create the go button
button = new Button(controls, SWT.PUSH);
button.setText("Go");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
browser.setUrl(url.getText());
}
});
// Allow users to hit enter to go to the typed URL
shell.setDefaultButton(button);
}
public String readStyledText(String ref) throws NoSuchKeyException,
BookException, TransformerException, SAXException
{
Book bible = Books.installed().getBook("NASB");;
Key key = bible.getKey(ref); //$NON-NLS-1$
BookData data = bible.getData(key);
SAXEventProvider osissep = data.getSAXEventProvider();
Converter styler = ConverterFactory.getConverter();
SAXEventProvider htmlsep = styler.convert(osissep);
String text = XMLUtil.writeToString(htmlsep);
return text;
}
/**
* The application entry point
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new SimpleBrowser().run();
}
}
More information about the jsword-devel
mailing list