Web Brwser Code

Embed Size (px)

Citation preview

  • 7/29/2019 Web Brwser Code

    1/6

    importorg.eclipse.swt.SWT;002 importorg.eclipse.swt.browser.*;003importorg.eclipse.swt.events.*;004importorg.eclipse.swt.layout.*;005importorg.eclipse.swt.widgets.*;006007008publicclassBrowserExample {009privatestaticfinalString AT_REST = "Ready";010011publicvoidrun(String location) {012Display display = newDisplay();013Shell shell = newShell(display);014shell.setText("Browser display message");015createContents(shell, location);016shell.open();017while(!shell.isDisposed()) {018if(!display.readAndDispatch()) {019display.sleep();020}021}022display.dispose();023}024025//create main window content026// maintain param for location027028publicvoidcreateContents(Shell shell, String location) {029shell.setLayout(newFormLayout());030031// Create the composite to hold the buttons and text field032Composite controls = newComposite(shell, SWT.NONE);033FormData data = newFormData();034data.top = newFormAttachment(0, 0);035

    data.left = new

    FormAttachment(0, 0);

    036data.right = newFormAttachment(100, 0);037controls.setLayoutData(data);038039// Create the status bar040Label status = newLabel(shell, SWT.NONE);041data = newFormData();

  • 7/29/2019 Web Brwser Code

    2/6

    042data.left = newFormAttachment(0, 0);043data.right = newFormAttachment(100, 0);044data.bottom = newFormAttachment(100, 0);045status.setLayoutData(data);046047// Create the web browser048finalBrowser browser = newBrowser(shell, SWT.BORDER);049data = newFormData();050data.top = newFormAttachment(controls);051data.bottom = newFormAttachment(status);052data.left = newFormAttachment(0, 0);053data.right = newFormAttachment(100, 0);054browser.setLayoutData(data);055056// Create the controls and wire them to the browser057controls.setLayout(newGridLayout(7, false));058059// Create the back button060Button button = newButton(controls, SWT.PUSH);061button.setText("Back");062button.addSelectionListener(new SelectionAdapter() {063publicvoidwidgetSelected(SelectionEvent event) {064browser.back();065}066});067068// Create the forward button069button = newButton(controls, SWT.PUSH);070button.setText("Forward");071button.addSelectionListener(new SelectionAdapter() {072publicvoidwidgetSelected(SelectionEvent event) {073browser.forward();074}075});076077// Create the refresh button078button = newButton(controls, SWT.PUSH);079button.setText("Refresh");080button.addSelectionListener(new SelectionAdapter() {081publicvoidwidgetSelected(SelectionEvent event) {082browser.refresh();083}

  • 7/29/2019 Web Brwser Code

    3/6

    084});085086// Create the stop button087button = newButton(controls, SWT.PUSH);088button.setText("Stop");089button.addSelectionListener(new SelectionAdapter() {090publicvoidwidgetSelected(SelectionEvent event) {091browser.stop();092}093});094095// Create the address entry field and set focus to it096finalText url = newText(controls, SWT.BORDER);097url.setLayoutData(newGridData(GridData.FILL_HORIZONTAL)); 098url.setFocus();099100// Create the go button101button = newButton(controls, SWT.PUSH);102button.setText("Go");103button.addSelectionListener(new SelectionAdapter() {104publicvoidwidgetSelected(SelectionEvent event) {105browser.setUrl(url.getText()); 106}107});108109// Create the animated "throbber"110Label throbber = newLabel(controls, SWT.NONE);111throbber.setText(AT_REST);112113// Allow users to hit enter to go to the typed URL114shell.setDefaultButton(button); 115116// Add event handlers117browser.addCloseWindowListener(new AdvancedCloseWindowListener()); 118

    browser.addLocationListener(new

    AdvancedLocationListener(url));

    119browser.addProgressListener(new AdvancedProgressListener(throbber)); 120browser.addStatusTextListener(new AdvancedStatusTextListener(status)); 121122// Go to the initial URL123if(location != null) {124browser.setUrl(location);

  • 7/29/2019 Web Brwser Code

    4/6

    125}126}127128//This class implements a CloseWindowListener for BrowserDisplay129130classAdvancedCloseWindowListener implementsCloseWindowListener {131132// Called when the parent window should be closed133134publicvoidclose(WindowEvent event) {135// Close the parent window136((Browser) event.widget).getShell().close();137}138}139

    140//This class implements a LocationListener for BrowserDisplay141142classAdvancedLocationListener implementsLocationListener {143// The address text box to update144privateText location;145146//Constructs an AdvancedLocationListener147148publicAdvancedLocationListener(Text text) {149// Store the address box for updates150location = text;151}152153154publicvoidchanging(LocationEvent event) {155// Show the location that's loading156location.setText("Loading "+ event.location + "...");157}158159publicvoidchanged(LocationEvent event) {160// Show the loaded location161location.setText(event.location); 162}163}164

  • 7/29/2019 Web Brwser Code

    5/6

    165classAdvancedProgressListener implementsProgressListener {166// The label on which to report progress167privateLabel progress;168169publicAdvancedProgressListener(Label label) {170// Store the label on which to report updates171progress = label;172}173174publicvoidchanged(ProgressEvent event) {175if(event.total != 0) {176intpercent = (int) (event.current / event.total);177progress.setText(percent + "%");178} else{179progress.setText("???");180}181}182183184publicvoidcompleted(ProgressEvent event) {185progress.setText(AT_REST);186}187}188189

    class

    AdvancedStatusTextListener implements

    StatusTextListener {

    190// The label on which to report status191privateLabel status;192193publicAdvancedStatusTextListener(Label label) {194// Store the label on which to report status195status = label;196}197198publicvoidchanged(StatusTextEvent event) {199// Report the status200status.setText(event.text);201}202}203204publicstaticvoidmain(String[] args) {205newBrowserExample().run(args.length == 0? null: args[0]);

  • 7/29/2019 Web Brwser Code

    6/6

    206}207}