Sunteți pe pagina 1din 6

import org.eclipse.swt.SWT; 002 import org.eclipse.swt.browser.*; 003 import org.eclipse.swt.events.*; 004 import org.eclipse.swt.layout.*; 005 import org.eclipse.swt.widgets.

*; 006 007 008 public class BrowserExample { 009 private static final String AT_REST = "Ready"; 010 011 public void run(String location) { 012 Display display = new Display(); 013 Shell shell = new Shell(display); 014 shell.setText("Browser display message"); 015 createContents(shell, location); 016 shell.open(); 017 while (!shell.isDisposed()) { 018 if (!display.readAndDispatch()) { 019 display.sleep(); 020 } 021 } 022 display.dispose(); 023 } 024 025 //create main window content 026 // maintain param for location 027 028 public void createContents(Shell shell, String location) { 029 shell.setLayout(new FormLayout()); 030 031 // Create the composite to hold the buttons and text field 032 Composite controls = new Composite(shell, SWT.NONE); 033 FormData data = new FormData(); 034 data.top = new FormAttachment(0, 0); 035 data.left = new FormAttachment(0, 0); 036 data.right = new FormAttachment(100, 0); 037 controls.setLayoutData(data); 038 039 // Create the status bar 040 Label status = new Label(shell, SWT.NONE); 041 data = new FormData();

042 data.left = new FormAttachment(0, 0); 043 data.right = new FormAttachment(100, 0); 044 data.bottom = new FormAttachment(100, 0); 045 status.setLayoutData(data); 046 047 // Create the web browser 048 final Browser browser = new Browser(shell, SWT.BORDER); 049 data = new FormData(); 050 data.top = new FormAttachment(controls); 051 data.bottom = new FormAttachment(status); 052 data.left = new FormAttachment(0, 0); 053 data.right = new FormAttachment(100, 0); 054 browser.setLayoutData(data); 055 056 // Create the controls and wire them to the browser 057 controls.setLayout(new GridLayout(7, false)); 058 059 // Create the back button 060 Button button = new Button(controls, SWT.PUSH); 061 button.setText("Back"); 062 button.addSelectionListener(new SelectionAdapter() { 063 public void widgetSelected(SelectionEvent event) { 064 browser.back(); 065 } 066 }); 067 068 // Create the forward button 069 button = new Button(controls, SWT.PUSH); 070 button.setText("Forward"); 071 button.addSelectionListener(new SelectionAdapter() { 072 public void widgetSelected(SelectionEvent event) { 073 browser.forward(); 074 } 075 }); 076 077 // Create the refresh button 078 button = new Button(controls, SWT.PUSH); 079 button.setText("Refresh"); 080 button.addSelectionListener(new SelectionAdapter() { 081 public void widgetSelected(SelectionEvent event) { 082 browser.refresh(); 083 }

084 }); 085 086 // Create the stop button 087 button = new Button(controls, SWT.PUSH); 088 button.setText("Stop"); 089 button.addSelectionListener(new SelectionAdapter() { 090 public void widgetSelected(SelectionEvent event) { 091 browser.stop(); 092 } 093 }); 094 095 // Create the address entry field and set focus to it 096 final Text url = new Text(controls, SWT.BORDER); 097 url.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 098 url.setFocus(); 099 100 // Create the go button 101 button = new Button(controls, SWT.PUSH); 102 button.setText("Go"); 103 button.addSelectionListener(new SelectionAdapter() { 104 public void widgetSelected(SelectionEvent event) { 105 browser.setUrl(url.getText()); 106 } 107 }); 108 109 // Create the animated "throbber" 110 Label throbber = new Label(controls, SWT.NONE); 111 throbber.setText(AT_REST); 112 113 // Allow users to hit enter to go to the typed URL 114 shell.setDefaultButton(button); 115 116 // Add event handlers 117 browser.addCloseWindowListener(new AdvancedCloseWindowListener()); 118 browser.addLocationListener(new AdvancedLocationListener(url)); 119 browser.addProgressListener(new AdvancedProgressListener(throbber)); 120 browser.addStatusTextListener(new AdvancedStatusTextListener(status)); 121 122 // Go to the initial URL 123 if (location != null) { 124 browser.setUrl(location);

125 } 126 } 127 128 //This class implements a CloseWindowListener for BrowserDisplay 129 130 class AdvancedCloseWindowListener implements CloseWindowListener { 131 132 // Called when the parent window should be closed 133 134 public void close(WindowEvent event) { 135 // Close the parent window 136 ((Browser) event.widget).getShell().close(); 137 } 138 } 139 140 //This class implements a LocationListener for BrowserDisplay 141 142 class AdvancedLocationListener implements LocationListener { 143 // The address text box to update 144 private Text location; 145 146 //Constructs an AdvancedLocationListener 147 148 public AdvancedLocationListener(Text text) { 149 // Store the address box for updates 150 location = text; 151 } 152 153 154 public void changing(LocationEvent event) { 155 // Show the location that's loading 156 location.setText("Loading " + event.location + "..."); 157 } 158 159 public void changed(LocationEvent event) { 160 // Show the loaded location 161 location.setText(event.location); 162 } 163 } 164

165 class AdvancedProgressListener implements ProgressListener { 166 // The label on which to report progress 167 private Label progress; 168 169 public AdvancedProgressListener(Label label) { 170 // Store the label on which to report updates 171 progress = label; 172 } 173 174 public void changed(ProgressEvent event) { 175 if (event.total != 0) { 176 int percent = (int) (event.current / event.total); 177 progress.setText(percent + "%"); 178 } else { 179 progress.setText("???"); 180 } 181 } 182 183 184 public void completed(ProgressEvent event) { 185 progress.setText(AT_REST); 186 } 187 } 188 189 class AdvancedStatusTextListener implements StatusTextListener { 190 // The label on which to report status 191 private Label status; 192 193 public AdvancedStatusTextListener(Label label) { 194 // Store the label on which to report status 195 status = label; 196 } 197 198 public void changed(StatusTextEvent event) { 199 // Report the status 200 status.setText(event.text); 201 } 202 } 203 204 public static void main(String[] args) { 205 new BrowserExample().run(args.length == 0 ? null : args[0]);

206 } 207 }

S-ar putea să vă placă și