



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A programming project where students are required to create a web browser using java and swt. The browser should allow users to surf the web, go forward and back through browsing history, bookmark popular urls, and hold up to six tabs. The application window includes a toolbar with navigation buttons, a textbox for entering urls, and a horizontal tab control for browser tabs.
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!
import java.util.ArrayList; import org.eclipse.swt.browser.Browser; import org.eclipse.swt.browser.ProgressEvent; import org.eclipse.swt.browser.ProgressListener; import org.eclipse.swt.browser.TitleEvent; import org.eclipse.swt.browser.TitleListener; import org.eclipse.swt.widgets.Composite;
public class BrowserEx extends Browser {
// Parallel arrays storing browser history Title and URL private ArrayList
// Set to false to prevent the progress listener from adding the loaded page // to the browser history arrays private boolean visitNewUrl = true ;
// Title of currently loaded web page private String title;
// Index into history arrays indicating which page is currently being viewed int currentLoc = -1;
// Index into history arrays indicating which page is the last valid // page the user can forward to int forwardLoc = -1;
public BrowserEx(Composite parent, int style) { super (parent, style);
historyTitle = new ArrayList
super .addProgressListener( new ProgressListener() { public void changed(ProgressEvent event) { }
public void completed(ProgressEvent event) { // Only add this URL if user didn't use back and forward methods if (visitNewUrl) addLocationToHistory(); else visitNewUrl = true ; } });
// Set tab title to browser's web page title super .addTitleListener( new TitleListener() { public void changed(TitleEvent event) { title = event.title; } }); }
protected void checkSubclass() { // Disable the check that prevents subclassing of SWT components }
public String getTitle() { if (title == null ) return ""; return title; }
public boolean back() { visitNewUrl = false ; // Prevent adding this URL to history currentLoc--; return super .back(); }
public boolean back( int times) { for ( int i = 0; i < times; i++) { if (!back()) return false ;
return true ; }
public boolean forward() { visitNewUrl = false ; // Prevent adding this URL to history currentLoc++; return super .forward(); }
public boolean forward( int times) { for ( int i = 0; i < times; i++) { if (!forward()) return false ; } return true ; }
public String[] getBackList() { // Return a list of items that are in the back area }
public String[] getForwardList() { // Return a list of items that are in the forward area }
private void addLocationToHistory(String title, String url) { currentLoc++;
// See if there are any items in forward if (currentLoc == historyTitle.size()) {
if (currentLoc > 0) { if (historyUrl.get(currentLoc - 1).equalsIgnoreCase(url)) { // Ignore duplicate URLs that are next to each other currentLoc--; return ; } }
// Nothing in forward, so add to end of list historyTitle.add(title); historyUrl.add(url); } else { // There are items in forward, so replace them historyTitle.add(currentLoc, title); historyUrl.add(currentLoc, url); }
forwardLoc = currentLoc;
printHistory(); }
private void addLocationToHistory() { addLocationToHistory(title, super .getUrl()); }
}
tabFolder = new CTabFolder(parent, SWT. BORDER );
final CTabItem tabItem = new CTabItem(tabFolder, SWT. CLOSE ); final BrowserEx browser = new BrowserEx(tabFolder, SWT. NONE ); tabItem.setControl(browser);