/////// Bookmark.java /////// import java.awt.*; import java.io.*; import java.net.*; import java.awt.event.*; import java.util.StringTokenizer; /** Program that searches a file of favorite web sites for a location and automatically displays the web page a separate browser window */ public class Bookmark extends Frame { public Bookmark(String file) { super(file); bmFile=file; init(); } protected Bookmark() { } public void init() { try { FileInputStream in = new FileInputStream(bmFile); lines = new TextLines(); lines.input(in); setLayout(card = new CardLayout()); setSize(600, 180); if ( sp == null ) { searchPanel(); add(sp, SEARCH); } if ( op == null ) { openPanel(); add(op, OPEN); } if ( ep == null ) { errPanel(); add(ep, ERROR); } card.show(this, SEARCH); } catch(FileNotFoundException e) { System.err.println(bmFile + "not found!"); e.printStackTrace(); return; } } protected String bmFile; // the bookmark file protected void searchPanel() { sp = new Panel(); sp.setLayout(new BorderLayout()); Panel tp = new Panel(); tp.setLayout(new BorderLayout()); Panel entp = new Panel(); Label enter = new Label("String to search: "); entp.add(enter); entp.add(st = new TextField(20)); Panel bp = new Panel(); bp.setLayout(new FlowLayout(FlowLayout.CENTER, 100, 5)); bp.add(sb = new Button("Search")); bp.add(gb = new Button(" Go ")); bp.add(nb = new Button("Cancel")); tp.add(entp, "Center"); tp.add(bp, "South"); sp.add(tp, "Center"); sb.addActionListener(new SearchHandler()); gb.addActionListener(new GoHandler()); nb.addActionListener(new CancelHandler()); } protected void openPanel() { op = new Panel(); op.setLayout(new BorderLayout()); Label fd = new Label("Matches found: "); op.add(fd, "West"); lis = new List(5); lis.setBackground(Color.cyan); op.add(lis, "Center"); Panel bp = new Panel(); bp.setLayout(new FlowLayout(FlowLayout.CENTER, 100, 5)); bp.add(ob = new Button(" Open ")); bp.add(cb = new Button(" Cancel ")); op.add(bp, "South"); ob.addActionListener(new OpenHandler()); cb.addActionListener(new OkHandler()); } protected void errPanel() { ep = new Panel(); ep.setLayout(new BorderLayout()); errlab = new Label(NOMATCH); ep.add(errlab, "Center"); ep.add(kb = new Button(" OK "), "South"); kb.addActionListener(new OkHandler()); } // url is 1st token protected String firstToken(String s) { StringTokenizer tk = new StringTokenizer(s); return tk.nextToken(); } protected String browser = "netscape"; protected String[] command = {browser, ""}; protected Button sb, gb, ob, nb, cb, kb; protected Panel op, sp, ep; protected CardLayout card = null; protected List lis; protected Label errlab; protected TextField st; protected TextLines lines; protected MatchingLines sites; final static String NOMATCH = "No Match Found"; final static String ERROR = "Error Panel"; final static String SEARCH = "Search Panel"; final static String OPEN = "Open Panel"; // back to search panel private final class CancelHandler implements ActionListener { public void actionPerformed(ActionEvent e) { setVisible(false); System.exit(0); } } // back to search panel private final class OkHandler implements ActionListener { public void actionPerformed(ActionEvent e) { card.show(Bookmark.this,SEARCH); } } // Opens user selection private final class OpenHandler implements ActionListener { public void actionPerformed(ActionEvent e) { openSite(firstToken(lis.getSelectedItem())); } } protected void openSite(String site) { command[1] = site; try { Runtime.getRuntime().exec(command); } catch (IOException e) { errlab.setText("Trouble: " + command[0] + " " + command[1]); card.show(this,ERROR); } } // If search produces unique match, open it directly private final class GoHandler implements ActionListener { public void actionPerformed(ActionEvent e) { sites = new MatchingLines(lines, st.getText()); if ( sites.length() == 1 ) openSite(firstToken(sites.getLine(0).toString())); else // ask user showSites(); } } // displays matched sites for user selection protected void showSites() { // single-selection list if ( lis.getItemCount() > 0 ) lis.removeAll(); // clear any previous entries int items = sites.length(); if ( items > 0 ) { for (int i=0; i < items; i++) lis.add(sites.getLine(i).toString()); lis.select(0); card.show(this,OPEN); } else // error panel { errlab.setText(NOMATCH); card.show(this,ERROR); } } // Displays search result and let user select one to open private final class SearchHandler implements ActionListener { public void actionPerformed(ActionEvent e) { sites = new MatchingLines(lines, st.getText()); showSites(); } } public static void main(String[] args) { if ( args.length > 0 && args.length <= 2 ) { Bookmark bk = new Bookmark(args[0]); if ( args.length == 2 ) bk.command[0] = bk.browser = args[1]; bk.setVisible(true); } else { System.err.println( "Usage: java Bookmark bookmark-file browser"); System.exit(1); } } }