Changeset 5895


Ignore:
Timestamp:
2003-11-19T14:15:23+13:00 (20 years ago)
Author:
kjdon
Message:

made a new BrowserApplication class to launch teh browser, added the configServer method from the preview pane into this class - uses calpane but should do http itself

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/Gatherer.java

    r5888 r5895  
    3434 **************************************************************************************/
    3535
     36import calpa.html.*;
    3637import com.l2fprod.gui.*;
    3738import com.l2fprod.gui.plaf.skin.*;
     
    123124    private GSDLSiteConfig gsdlsite_cfg = null;
    124125    private ExternalApplication server = null;
     126    private CalHTMLPane view   = null;
     127
    125128    /** The name of the Gatherers configuration file. */
    126129    static private String CONFIG_FILE_NAME = "gatherer.cfg";
     
    285288        g_man.display();
    286289
     290        // set up the calpane to use for config server
     291        CalHTMLPreferences prefs = new CalHTMLPreferences();
     292        DefaultCalHTMLObserver observer = new DefaultCalHTMLObserver();
     293        view = new CalHTMLPane(prefs, observer, "Default");
     294
    287295        // Place the window in the desired location on the screen, if this is do-able (not under most linux window managers apparently. In fact you're lucky if they listen to any of your screen size requests).
    288296        g_man.setLocation(bounds.x, bounds.y);
     
    371379    }
    372380
     381    // used to send new coll messages to the local library
     382    public void configServer(String command) {
     383
     384    try {
     385        String url = config.exec_address.toString() + command;
     386        ///ystem.err.println("Action: " + url);
     387        view.setLoadSynchronously(true);
     388        view.showHTMLDocument(new URL(url));
     389        view.setLoadSynchronously(false);
     390        ///ystem.err.println("Complete.");
     391        url = null;
     392    }
     393    catch(Exception error) {
     394        ///ystem.err.println("Bad URL.");
     395    }
     396
     397    }
     398
    373399    /** Retrieve the metadata directory, as required by any MSMCaller implementation.
    374400     * @return The currently active collection metadata directory as a <strong>String</strong>.
     
    417443    }
    418444    }
    419 
     445   
     446    public void spawnBrowser(String url) {
     447    String command = assoc_man.getBrowserCommand(url);
     448    if (command != null) {
     449        BrowserApplication app = new BrowserApplication(command, url);
     450        apps.add(app);
     451        app.start();
     452    }
     453    else {
     454        ///ystem.err.println("No browser command available.");
     455    }
     456    }
     457   
    420458    /** The entry point into the Gatherer. Parses arguments.
    421459     * @param args A collection of arguments that may include: initial screen size, dictionary, path to the GSDL etc.
     
    791829        if(gsdlsite_cfg.getURL() != null) {
    792830        // Send the command for it to exit.
    793         Gatherer.g_man.preview_pane.configServer(GSDLSiteConfig.QUIT_COMMAND);
     831        //Gatherer.g_man.preview_pane.configServer(GSDLSiteConfig.QUIT_COMMAND);
     832        configServer(GSDLSiteConfig.QUIT_COMMAND);
    794833        // Wait until it exits.
    795834        try {
     
    894933    }
    895934    }
     935    /** This private class contains an instance of an external application running within a JVM shell. It is important that this process sits in its own thread, but its more important that when we exit the Gatherer we don't actually System.exit(0) the Gatherer object until the user has volunteerily ended all of these child processes. Otherwise when we quit the Gatherer any changes the users may have made in external programs will be lost and the child processes are automatically deallocated. */
     936    private class BrowserApplication
     937    extends Thread {
     938    private Process process = null;
     939    /** The initial command string given to this sub-process. */
     940    private String command = null;
     941    private String url = null;
     942    private String[] commands = null;
     943    /** Constructor.
     944     * @param command The initial command <strong>String</strong>.
     945     */
     946//      public BrowserApplication(String command) {
     947//          this.command = command;
     948//      }
     949
     950    public BrowserApplication(String command, String url) {
     951        StringTokenizer st = new StringTokenizer(command);
     952        int num_tokens = st.countTokens();
     953        this.commands = new String [num_tokens];
     954        int i=0;
     955        while (st.hasMoreTokens()) {
     956        commands[i] = st.nextToken();
     957        i++;
     958        }
     959        //this.commands = commands;
     960        this.url = url;
     961    }
     962    /** We start the child process inside a new thread so it doesn't block the rest of Gatherer.
     963     * @see java.lang.Exception
     964     * @see java.lang.Process
     965     * @see java.lang.Runtime
     966     * @see java.lang.System
     967     * @see java.util.Vector
     968     */
     969    public void run() {
     970        // Call an external process using the args.
     971        if(commands == null) {
     972        apps.remove(this);
     973        return;
     974        }
     975        try {
     976        String prog_name = commands[0];
     977        String lower_name = prog_name.toLowerCase();
     978        if (lower_name.indexOf("mozilla") != -1 || lower_name.indexOf("netscape") != -1) {
     979            Gatherer.println("found mozilla or netscape, trying remote it");
     980            // mozilla and netscape, try using a remote command to get things in the same window
     981            String [] new_commands = new String[] {prog_name, "-raise", "-remote", "openURL("+url+")"};
     982            printArray(new_commands);
     983            Runtime rt = Runtime.getRuntime();
     984            process = rt.exec(new_commands);
     985            int exitCode = process.waitFor();
     986            if (exitCode != 0) { // if Netscape or mozilla was not open
     987            Gatherer.println("couldn't do remote, trying original command");
     988            printArray(commands);
     989            process = rt.exec(commands); // try the original command
     990            }
     991        } else {
     992            // just run what we have been given
     993            StringBuffer whole_command = new StringBuffer();
     994            for(int i = 0; i < commands.length; i++) {
     995            whole_command.append(commands[i]);
     996            whole_command.append(" ");
     997            }
     998            println("Running " + whole_command.toString());
     999            Runtime rt = Runtime.getRuntime();
     1000            process = rt.exec(commands);
     1001            process.waitFor();
     1002        }
     1003        }
     1004       
     1005        catch (Exception error) {
     1006        println("Error in BrowserApplication.run(): " + error);
     1007        printStackTrace(error);
     1008        }
     1009        // Remove ourself from Gatherer list of threads.
     1010        apps.remove(this);
     1011        // Call exit if we were the last outstanding child process thread.
     1012        if(apps.size() == 0 && exit == true) {
     1013        stopServerEXE();
     1014        System.exit(0);
     1015        }
     1016    }
     1017    public void printArray(String [] array) {
     1018        for(int i = 0; i < array.length; i++) {
     1019        System.err.println(array[i]+" ");
     1020        }
     1021    }
     1022    public void stopBrowserApplication() {
     1023        if(process != null) {
     1024        process.destroy();
     1025        }
     1026    }
     1027    }
    8961028
    8971029    /** This class is intented to detect a specific SIGNAL, in this case SIGINT, and exit properly, rather than letting the Gatherer be interrupted which has the potential to leave erroneous lock files. */
Note: See TracChangeset for help on using the changeset viewer.