Browser: addLocationListener(CloseWindowListener arg0) : Browser « org.eclipse.swt.browser « Java by API






Browser: addLocationListener(CloseWindowListener arg0)

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class MainClass {
  private static final String AT_REST = "Ready";

  public MainClass(){
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Advanced Browser");
    createContents(shell, "http://www.java2s.com");
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
    
  }
  public void createContents(Shell shell, String location) {
    shell.setLayout(new FormLayout());

    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);

    Label status = new Label(shell, SWT.NONE);
    data = new FormData();
    data.left = new FormAttachment(0, 0);
    data.right = new FormAttachment(100, 0);
    data.bottom = new FormAttachment(100, 0);
    status.setLayoutData(data);

    final Browser browser = new Browser(shell, SWT.BORDER);
    data = new FormData();
    data.top = new FormAttachment(controls);
    data.bottom = new FormAttachment(status);
    data.left = new FormAttachment(0, 0);
    data.right = new FormAttachment(100, 0);
    browser.setLayoutData(data);

    controls.setLayout(new GridLayout(7, false));

    Button button = new Button(controls, SWT.PUSH);
    button.setText("Back");
    button.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        browser.back();
      }
    });

    button = new Button(controls, SWT.PUSH);
    button.setText("Forward");
    button.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        browser.forward();
      }
    });

    button = new Button(controls, SWT.PUSH);
    button.setText("Refresh");
    button.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        browser.refresh();
      }
    });

    button = new Button(controls, SWT.PUSH);
    button.setText("Stop");
    button.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        browser.stop();
      }
    });

    final Text url = new Text(controls, SWT.BORDER);
    url.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    url.setFocus();

    button = new Button(controls, SWT.PUSH);
    button.setText("Go");
    button.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        browser.setUrl(url.getText());
      }
    });

    Label throbber = new Label(controls, SWT.NONE);
    throbber.setText(AT_REST);

    shell.setDefaultButton(button);

    browser.addCloseWindowListener(new AdvancedCloseWindowListener());
    browser.addLocationListener(new AdvancedLocationListener(url));
    browser.addProgressListener(new AdvancedProgressListener(throbber));
    browser.addStatusTextListener(new AdvancedStatusTextListener(status));

    if (location != null) {
      browser.setUrl(location);
    }
  }

  class AdvancedCloseWindowListener implements CloseWindowListener {
    public void close(WindowEvent event) {
      ((Browser) event.widget).getShell().close();
    }
  }

  class AdvancedLocationListener implements LocationListener {
    private Text location;

    public AdvancedLocationListener(Text text) {
      location = text;
    }

    public void changing(LocationEvent event) {
      location.setText("Loading " + event.location + "...");
    }
    public void changed(LocationEvent event) {
      location.setText(event.location);
    }
  }

  class AdvancedProgressListener implements ProgressListener {
    private Label progress;

    public AdvancedProgressListener(Label label) {
      progress = label;
    }
    public void changed(ProgressEvent event) {
      if (event.total != 0) {
        int percent = (int) (event.current / event.total);
        progress.setText(percent + "%");
      } else {
        progress.setText("");
      }
    }
    public void completed(ProgressEvent event) {
      progress.setText(AT_REST);
    }
  }
  class AdvancedStatusTextListener implements StatusTextListener {
    private Label status;
    public AdvancedStatusTextListener(Label label) {
      status = label;
    }
    public void changed(StatusTextEvent event) {
      status.setText(event.text);
    }
  }

  public static void main(String[] args) {
    new MainClass();
  }
}

           
       








Related examples in the same category

1.Browser: addCloseWindowListener(CloseWindowListener arg0)
2.Browser.addProgressListener(CloseWindowListener arg0)
3.Browser.addStatusTextListener(CloseWindowListener arg0)
4.Browser: back()
5.Browser: forward()
6.Browser: setUrl(String url)
7.Browser: stop()