Changeset 1831


Ignore:
Timestamp:
2001-01-11T16:17:07+13:00 (23 years ago)
Author:
paynter
Message:

Tidying code, rearranging initialisation code.

Location:
trunk/gsdl/src/phind/client
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/src/phind/client/Phind.java

    r1830 r1831  
    3232
    3333<APPLET CODE="Phind.class" WIDTH=500 HEIGHT=500>
     34
    3435  <PARAM NAME=phindcgi    VALUE="http://kowhai/cgi-bin/phindcgi">
    3536  <PARAM NAME=library     VALUE="http://kowhai/cgi-bin/library">
     37
    3638  <PARAM NAME=backdrop    VALUE="http://kowhai/~paynter/transfer/phindtest/green1.jpg">
     39
    3740  <PARAM NAME=collection  VALUE="fao.org">
     41
    3842  <PARAM NAME=orientation VALUE="vertical">
    3943  <PARAM NAME=depth       VALUE="2">
     44  <PARAM NAME=blocksize   VALUE="10">
     45
    4046  <PARAM NAME=fontsize    VALUE="12">
    41   <PARAM NAME=blocksize   VALUE="10">
    4247  The Phind java applet.
    4348</APPLET>
     
    96101public class Phind extends java.applet.Applet {
    97102   
    98     // elements of the page
    99     Label titleLabel;
    100     TextField wordField;
    101     Button searchButton, prevButton, nextButton;
     103    // What is the collection called?
     104    public String collection;
     105
     106    // Internet address of phind resources
     107    public String library_address, phindcgi_address;
     108
     109    // Number of phrases to retrieve at any one time
     110    public int phraseBlockSize;
    102111
    103112    // Appearance parameters
     
    108117
    109118    // Do we want a background image in the applet?
    110     String backdrop_address;
    111119    public boolean showImage;
     120    public String backdrop_address;
    112121    public Image backgroundImage;
    113 
    114     // Holders for the downloaded data
    115     Panel resultPanel;
    116     ResultDisplay firstDisplay, lastDisplay;
    117122
    118123    // the mode of operation
     
    122127    final int searchMode = 2;
    123128
    124     // what to search for
    125     String collection;
    126     int phraseBlockSize;
    127 
    128     //int stopwordLevel = 1;
    129     //int lowFrequencyCutoff = 0;
    130     //int stemming = 0;
    131 
    132     // where to search
    133     Socket connection;
    134     String library_address, phindcgi_address;
    135     URL library, phindcgi;
    136 
    137     String search_url;
    138 
    139     // browser details
    140     String localHost;
     129    // elements of the page
     130    Label titleLabel;
     131    TextField wordField;
     132    Button searchButton, prevButton, nextButton;
     133
     134    // Holders for the downloaded data
     135    Panel resultPanel;
     136    ResultDisplay firstDisplay, lastDisplay;
    141137
    142138    // The time at which the last query finished
     
    166162 
    167163    public String getAppletInfo() {
    168     return "Phind by Gordon Paynter ([email protected]). Copyright 1997-2000.";
    169     }
     164    return "Phind by Gordon Paynter ([email protected]). Copyright 1997-2000.";
     165    }
     166
    170167
    171168    public void init() {
    172169
    173     // default search variable values
    174170    mode = initMode;
     171   
     172    // Default applet parameters
     173    collection = "";
     174    phindcgi_address = "";
     175    library_address = "";
     176   
     177    showImage = false;
     178    backdrop_address = "";
     179 
     180    vertical = true;
     181    depth = 3;
     182
     183    phraseBlockSize = 10;
     184    fontSize = 12;
     185
     186    // Read applet parameters
     187    getParameters();
     188
     189   
     190    // Initialise the user interface
     191    setBackground(Color.white);
    175192    lastQueryEndTime = new Date();
     193
     194    // fonts used to output text
     195    plainFont = new Font("Helvetica", Font.PLAIN, fontSize);
     196    boldFont  = new Font("Helvetica", Font.BOLD, fontSize);
     197
     198    // The phind applet layout manager
     199    setLayout(new BorderLayout());
     200
     201    // the Panel containing the displays is in the center of the display
     202    resultPanel = new Panel();
     203    if (vertical) {
     204        resultPanel.setLayout(new GridLayout(depth,1,0,2));
     205    } else {
     206        System.out.println("horizontal");
     207        resultPanel.setLayout(new GridLayout(1,depth,2,0));
     208    }
     209    add("Center", resultPanel);
     210
     211    // Create ResultDisplays and place in the interface
     212    ResultDisplay d1, d2 = null;
     213    firstDisplay = new ResultDisplay(this, null);
     214    resultPanel.add(firstDisplay);
     215
     216    d1 = firstDisplay;
     217    for (int i = 2; i <= depth; i++) {
     218        d2 = new ResultDisplay(this, d1);
     219        resultPanel.add(d2);
     220        d1 = d2;
     221    }
     222        lastDisplay = d2;
     223
     224
     225    // The "control panel"
     226    Panel p1 = new Panel();
     227    add("North", p1);
     228    p1.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
     229
     230    searchButton = new Button("Search");
     231    searchButton.setFont(boldFont);
     232    p1.add(searchButton);
     233
     234    Label tempLabel = new Label("  for");
     235    tempLabel.setFont(boldFont);
     236    p1.add(tempLabel);
     237
     238    wordField = new TextField(12);
     239    wordField.setFont(boldFont);
     240    p1.add(wordField);
     241
     242    Label temp2 = new Label("          ");
     243    p1.add(temp2);
     244
     245    prevButton = new Button("Previous");
     246    prevButton.setFont(boldFont);
     247    prevButton.disable();
     248    p1.add(prevButton);
     249
     250    nextButton = new Button("  Next  ");
     251    nextButton.setFont(boldFont);
     252    nextButton.disable();
     253    p1.add(nextButton);
     254
     255    // lets get started then
     256    setStatus("Welcome to Phind.");
     257    mode = idleMode;
     258    }
     259
     260
     261    // Get the applet parameters
     262    void getParameters() {
     263
    176264
    177265    // What is this collection called?
    178266    try {
    179267        collection = new String(getParameter("collection"));
    180     } catch (Exception e) {
    181         System.err.println("Phind: You must specify a collection.");
     268        System.out.println("Phind collection: " + collection);
     269    } catch (Exception e) {
     270        System.err.println("Phind could not read collection name: " + e.toString());
    182271        stop();
    183     }
    184     System.out.println("Phind collection: " + collection);
     272    }
    185273
    186274    // Where do we get the phind data
    187275    try {
    188276        phindcgi_address = new String(getParameter("phindcgi"));
     277        phindcgi_address = tidy_URL(phindcgi_address, true);
     278        System.out.println("Phind phindcgi: " + phindcgi_address);
    189279    } catch (Exception e) {
    190         System.err.println("Phind: You must specify a URL for phindcgi.");
     280        System.err.println("Phind could not process phindcgi URL: " + e.toString());
    191281        stop();
    192282    }
    193     phindcgi_address = tidy_URL(phindcgi_address, true);
    194     System.out.println("Phind phindcgi: " + phindcgi_address);
    195283
    196284    // Where is the collection served from
    197285    try {
    198286        library_address = new String(getParameter("library"));
     287        library_address = tidy_URL(library_address, true);
     288        System.out.println("Phind library: " + library_address);
    199289    } catch (Exception e) {
    200         System.err.println("Phind: You must specify a URL for library.");
    201         stop();
    202     }
    203     library_address = tidy_URL(library_address, true);
    204     System.out.println("Phind library: " + library_address);
     290        System.err.println("Phind library: not specified");
     291        library_address = "";
     292    }
    205293
    206294    // Should we display a background image?
    207     showImage = false;
    208     try {
     295    try {
    209296        backdrop_address = new String(getParameter("backdrop"));
    210297        backdrop_address = tidy_URL(backdrop_address, false);
     
    214301        System.out.println("Phind backdrop: " + backdrop_address);
    215302    } catch (Exception e) {
    216         System.out.println("Phind backdrop not set");
    217     }
    218 
    219     // Is there a search URL for this collection?
    220     try {
    221         search_url = new String(getParameter("search_url"));
    222         System.out.println("Phind search URL: " + search_url);
    223     } catch (Exception e) {
    224         System.out.println("Phind search_url not set.");
     303        System.out.println("Phind backdrop not set: " + e.toString());
    225304    }
    226305
    227306    // Are the windows arranged vertically or horizontally
    228     vertical = true;
    229307    try {
    230308        if (getParameter("orientation").toLowerCase().startsWith("h")) {
     
    237315
    238316    // How many phind windows are there?
    239     Integer temp;
    240317    try {
    241         temp = Integer.valueOf(getParameter("depth"));
    242         depth = temp.intValue();
    243     } catch (Exception e) {
    244         temp = null;
     318        depth = Integer.valueOf(getParameter("depth")).intValue();
     319    } catch (Exception e) {
    245320        depth = 3;
    246321    }
     
    249324    // How many phrases should we fetch at any given time?
    250325    try {
    251         temp = Integer.valueOf(getParameter("blocksize"));
    252         phraseBlockSize = temp.intValue();
     326        phraseBlockSize = Integer.valueOf(getParameter("blocksize")).intValue();
    253327    } catch (Exception e) {
    254         temp = null;
    255328        phraseBlockSize = 10;
    256329    }
    257     System.out.println("Phind phrase block size: " + phraseBlockSize);
     330    System.out.println("Phind retrieval block size: " + phraseBlockSize);
    258331
    259332    // How large should the font be?
    260333    try {
    261         temp = Integer.valueOf(getParameter("fontsize"));
    262         fontSize = temp.intValue();
     334        fontSize = Integer.valueOf(getParameter("fontsize")).intValue();
    263335    } catch (Exception e) {
    264         temp = null;
    265336        fontSize = 12;
    266337    }
    267338    System.out.println("Phind font size: " + fontSize);
    268 
    269     // browser details
    270     try {
    271         localHost = InetAddress.getLocalHost().toString();
    272     } catch (Exception e) {
    273         localHost = "Can't get client host.";
    274     }
    275     System.out.println("Phind client: " + localHost);
    276     System.out.println("");
    277 
    278     // The phind applet layout manager
    279     setLayout(new BorderLayout());
    280 
    281     // initialise the user interface
    282     setBackground(Color.white);
    283     plainFont = new Font("Helvetica", Font.PLAIN, fontSize);
    284     boldFont  = new Font("Helvetica", Font.BOLD, fontSize);
    285 
    286     // the main Result area
    287 
    288     // the Panel containing the displays is in the center of the display
    289     resultPanel = new Panel();
    290     if (vertical) {
    291         resultPanel.setLayout(new GridLayout(depth,1,0,2));
    292     } else {
    293         System.out.println("horizontal");
    294         resultPanel.setLayout(new GridLayout(1,depth,2,0));
    295     }
    296     add("Center", resultPanel);
    297 
    298     // Create ResultDisplays and place in the interface
    299     ResultDisplay d1, d2 = null;
    300     firstDisplay = new ResultDisplay(this, null);
    301     resultPanel.add(firstDisplay);
    302 
    303     d1 = firstDisplay;
    304     for (int i = 2; i <= depth; i++) {
    305         d2 = new ResultDisplay(this, d1);
    306         resultPanel.add(d2);
    307         d1 = d2;
    308     }
    309      
    310     lastDisplay = d2;
    311 
    312 
    313     // The "control panel"
    314     Panel p1 = new Panel();
    315     add("North", p1);
    316     p1.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
    317 
    318     searchButton = new Button("Search");
    319     searchButton.setFont(boldFont);
    320     p1.add(searchButton);
    321 
    322     Label tempLabel = new Label("  for");
    323     tempLabel.setFont(boldFont);
    324     p1.add(tempLabel);
    325 
    326     wordField = new TextField(12);
    327     wordField.setFont(boldFont);
    328     p1.add(wordField);
    329 
    330     Label temp2 = new Label("          ");
    331     p1.add(temp2);
    332 
    333     prevButton = new Button("Previous");
    334     prevButton.setFont(boldFont);
    335     prevButton.disable();
    336     p1.add(prevButton);
    337 
    338     nextButton = new Button("  Next  ");
    339     nextButton.setFont(boldFont);
    340     nextButton.disable();
    341     p1.add(nextButton);
    342 
    343     // lets get started then
    344     setStatus("Welcome to Phind.");
    345     mode = idleMode;
    346     }
    347 
     339    }
    348340
    349341    void setStatus(String status) {
     
    551543    System.out.println("sending query: " + query);
    552544    try {
    553         phindcgi = new URL(query);
     545        URL phindcgi = new URL(query);
    554546        DataInputStream in = new DataInputStream(phindcgi.openStream());
    555547        byte[] buffer;
     
    702694    }
    703695
     696
     697
     698    // Open an arbitrary web page
     699    void displayWebPage(String address, String window) {
     700    try {
     701        URL url= new URL(address);
     702        if (window.length() > 0) {
     703        getAppletContext().showDocument(url, window);
     704        } else {
     705        getAppletContext().showDocument(url);
     706        }
     707    } catch (Exception e) {
     708        System.out.println("Cannot open web page: " + e.toString());
     709    }
     710    }
     711
    704712}
  • trunk/gsdl/src/phind/client/ResultCanvas.java

    r1830 r1831  
    477477        //User clicks on a phrase
    478478        if (item.isPhrase()) {
    479 
    480479            itemSelected = itemSelected;
    481480            update(getGraphics());
    482      
    483481            parent.lookupPhrase(item.hiddenText(), item.toString(), 2);
    484482
    485483            // If meta key is held down, send query to search engine
    486             if (event.metaDown()) {
    487             if (phind.search_url.equals("none") || phind.search_url.equals("")) {
    488                 System.out.println("No searching in this collection");
    489             } else {
    490                 URL url;
    491                 try {
    492                 url = new URL(phind.search_url + "%22" + item.toString() + "%22&");
    493                 phind.getAppletContext().showDocument(url, "phindsearch");
    494                 System.out.println("The query is: " + url.toString());
    495                 } catch (Exception e) {
    496                 System.out.println("URL error: " + e.toString());
    497                 }
    498             }
     484            if (event.metaDown() && !phind.library_address.equals("")) {
     485            String address = phind.library_address 
     486                + "a=q&c=" + phind.collection
     487                + "&q=%22" + item.toString().replace(' ', '+') + "%22";
     488            phind.displayWebPage(address, "phindsearch");
    499489            }
    500 
    501490        }
    502491
     
    506495            update(getGraphics());
    507496            parent.lookupPhrase(item.hiddenText(), item.toString(), 2);
     497
     498            // If meta key is held down, send query to search engine
     499            if (event.metaDown() && !phind.library_address.equals("")) {
     500            String address = phind.library_address 
     501                + "a=q&c=" + phind.collection
     502                + "&q=%22" + item.toString().replace(' ', '+') + "%22";
     503            phind.displayWebPage(address, "phindsearch");
     504            }
    508505        }
    509506
     
    513510            update(getGraphics());
    514511
    515             URL url;
    516             try {
    517             String address = phind.library_address
    518                 + "a=d&c=" + phind.collection
    519                 + "&d=" + item.hiddenText()
    520                 + "&q=" + parent.searchPhrase.replace(' ', '+');
    521             url = new URL(address);
    522             System.out.println("URL selected: " + url.toString());
    523             phind.getAppletContext().showDocument(url, "phinddoc");
    524             } catch (Exception e) {
    525             System.out.println("URL error: " + e.toString());
    526             }
     512            String address = phind.library_address
     513            + "a=d&c=" + phind.collection
     514            + "&d=" + item.hiddenText()
     515            + "&q=" + parent.searchPhrase.replace(' ', '+');
     516            phind.displayWebPage(address, "phinddoc");
    527517        }
    528518         
Note: See TracChangeset for help on using the changeset viewer.