Ignore:
Timestamp:
2024-04-22T16:15:36+12:00 (4 weeks ago)
Author:
anupama
Message:

Cleaning up the code somewhat.

Location:
main/trunk/greenstone3/src/java/org/greenstone/applet
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/applet/GsdlCollageApplet/DisplayImages.java

    r38908 r38937  
    123123    if(screen_buffer_ == null) {
    124124        System.err.println("### why is screen_buffer_ null?");
    125     }
     125        if(java.awt.GraphicsEnvironment.isHeadless()) {
     126        System.err.println("### It's running headless so screen_buffer_ is null");
     127        }
     128        if(!app_.isDisplayable()) {
     129        System.err.println("### Collage app is not displayable, that's why screen_buffer_ is null");
     130        }
     131    }
     132   
    126133    screen_graphic_ = screen_buffer_.getGraphics();
    127134    screen_graphic_.setColor(bgcolor_);
     
    438445            Thread.sleep(1000);         
    439446            continue;
    440         }   
     447        }
     448        //System.err.println("#### DisplayImages.run(): moving to next image");
    441449        CollageImage collage_image = download_images_.getCollageImage();
    442450   
  • main/trunk/greenstone3/src/java/org/greenstone/applet/GsdlCollageApplet/GsdlCollageApplet.java

    r38936 r38937  
    150150    super();
    151151   
    152     // status bar that applet has, but we want one also if we run JPhind as application
    153     // And in fact, when webswing runs our applet, we never get a status bar. So we
    154     // create a custom status bar now even if we're running as an applet.
    155     // When this applet is run through the appletviewer we might end up with 2 status bars.
     152    // status bar if run as application or as webswing applet,
     153    // mimicing running as regular applet which gives us a status bar by default
     154    // Useful for Phind, not for Collage where there are layout issues even if hidden
    156155    //setLayout(new BorderLayout());
    157156    //if(!isRunAsApplet) {
     
    176175    }
    177176
     177    /**
     178     * This method is called (by main()) only when this class is run
     179     * as an application, not as applet. It prepares the variables and
     180     * commandline parameters to be set up, so the rest of the
     181     * initialisation can be done in the usual way (with the
     182     * Applet.getParameters() method) in the init() method called
     183     * hereafter for both application and applet mode.
     184     */
    178185    public void applicationPreInit(String[] args) {
    179186    // I copied the contents of this contructor for my code for the JPhind.java constructor
     
    204211        if(key.equals("xtraParams")) {
    205212            value = value.replace("&", "&"); // just in case we have html entities
    206             parseXtraParams(value, "=", "&", appParams);
    207             /*
    208             String[] param_list = value.split("&", 0); // 0 for all occurrences
    209             for(String key_val : param_list) {         
    210             String[] paramPair = key_val.split("=", 2); // get first 2 strings, key and val
    211             //System.err.println("key_val: " + key_val);
    212             if(paramPair.length == 2) {
    213                 String xtraParamsKey = paramPair[0];
    214                 String xtraParamsVal = paramPair[1];
    215                 //System.err.println("key - val: " + xtraParamsKey + " - " + xtraParamsVal);
    216                 appParams.put(xtraParamsKey, xtraParamsVal);
    217             }
    218             }
    219             */
     213            parseXtraParams(value, "=", "&", appParams);           
    220214        }
    221215       
     
    223217        }
    224218    }
    225 
    226     // TODO: we can possibly remove this as it's done in init() now before we need it
    227     isWebswing = appParams.getOrDefault("webswing", "0").equals("1") ? true : false;
    228219   
    229220    // Need these parameters set before init(), so that the display works
     
    318309    for(String key_val : param_list) {
    319310        String[] paramPair = key_val.split(kvSeparator, 2); // get first 2 strings, key and val
    320         System.err.println("key_val: " + key_val);
     311        //System.err.println("key_val: " + key_val);
    321312        if(paramPair.length == 2) {
    322313        String xtraParamsKey = paramPair[0];
     
    328319        }
    329320
    330         if (verbosity_ >= 3) {
     321        if (verbosity_ >= 4) {
    331322            System.err.println("*** xtraParams key - val: " + xtraParamsKey + " - " + xtraParamsVal);
    332323        }
     
    530521    }
    531522
    532     // This block was a nice idea if it worked, but it doesn't work. It's here for syntax.
    533     // Handle getting called by webswing JavaScript (but only if run in webswing mode):
    534     // when the user navigates away from the webswing app's web page, do cleanup
     523    // This block was a nice idea had it worked, but it doesn't
     524    // work. It's here as sample syntax.  Handle getting called by
     525    // webswing JavaScript (but only if run in webswing mode):
     526    // when the user navigates away from the webswing app's web
     527    // page, do cleanup
    535528    // https://www.webswing.org/docs/23.2/integrate/jslink.html#invoking-java-from-javascript
    536529    // https://vaadin.com/directory/component/webswing-vaadin1
    537530    if(isWebswing) {
    538531
    539         System.err.println("#### isWebswing, about to add listeners");
     532        //System.err.println("#### isWebswing, about to add listeners");
    540533       
    541534        WebswingUtil.getWebswingApi().addBrowserActionListener(new WebActionListener() {
     
    543536            public void actionPerformed(WebActionEvent actionEvent) {
    544537
    545             System.err.println("******** JavaScript to Java: actionPerformed called.");
     538            System.err.println("***JavaScript to Java comms: actionPerformed called");
    546539            //System.err.println("actionEvent = ");
    547540           
     
    566559        // shutdown the way we'd like on the javascript kill()
    567560        // command when the user navigates away from the collage
    568         // page as that only generates a Java WindowClosingEvent
    569         // by default if not shutdown handlers registered. And a
     561        // page, as that only generates a Java WindowClosingEvent
     562        // by default if no shutdown handlers registered. And a
    570563        // WindowClosingEvent only has any effect on applications,
    571564        // not applets. But we can write our own custom handler to
    572         // mimic the applet shutdown sequence: refer to
     565        // mimic the applet shutdown sequence of stop() then
     566        // destroy() see
    573567        // https://www.webswing.org/docs/20.2/integrate/api.html
    574         // This code also written with the help of the source code
    575         // to know the methods in interface WebswingShutdownListener
    576         // and the import statements required for the methods.
    577         // and then call System.exit(0) on top of usual applet
    578         // behaviour to make sure we get rid of the applet
    579         // "application" that webswing launched for us (it took
    580         // the applet code and ran it, presumably like a java
    581         // program, so a System.exit call may be warranted).
     568        // This code also written with the help of webswing's
     569        // source code to know the methods in interface
     570        // WebswingShutdownListener and the import statements
     571        // required for these methods.
     572        // After stop() and destroy(), we call System.exit(0) on
     573        // top of usual applet behaviour to make sure we get rid
     574        // of the applet "application" that webswing launched for
     575        // us (it took the applet code and ran it, presumably like
     576        // a java program, so a System.exit call may be warranted,
     577        // whereas System.exit() isn't called on a regular applet).
    582578        WebswingUtil.getWebswingApi().addShutdownListener(new WebswingShutdownListener() {
    583             // These are official API comments for info
     579            // These are official webswing API comments for info
    584580            /**
    585581             * Invoked before Webswing requests application to exit.
     
    614610            // We're like the browser now and so we should
    615611            // honour the above contract.
    616 
    617             System.err.println("------- WebswingShutdownListener.onShutdown() - our custom behaviour");
     612            if (verbosity_ >= 3) {
     613                System.err.println("------- GsdlCollageApplet WebswingShutdownListener.onShutdown() - our custom behaviour");
     614               
     615                // This message is sent to JavaScript too late: webpage has already
     616                // finished unloading and the JS webswingInstance can't receive msgs.
     617                // if(isWebswing && isRunAsApplet) {
     618                //    WebswingUtil.getWebswingApi().sendActionEvent("javaToWebswingJSConsoleLog", "GsdlCollageApplet running as webswing applet - quitting now", null);
     619                // }
     620            }
    618621            stop();
    619622            destroy();
     
    623626            // webswing.  And this handler/webswing stands
    624627            // in place of a browser and controls what
    625             // Ought to happen if a browser doesn't take
     628            // *ought* to happen if a browser doesn't take
    626629            // care of the applet life cycle and we ought
    627             // to do it.
     630            // to do it (here in the webswing handler).
    628631            System.exit(0);
    629632            }
     
    631634    }
    632635   
    633     /*
    634     System.err.println("XXX App width: " + this.getWidth());
    635     System.err.println("XXX App height: " + this.getHeight());
    636 
    637    
    638     // webswing applet doesn't have dimensions set because there's no applet element setting it
    639     // so we need to set the size on the Collage Applet if launched with webswing and run in applet mode
    640     if(isWebswing && isRunAsApplet) {       
    641         //this.setPreferredSize(new Dimension(X_DIM(), Y_DIM()));
    642         this.setSize(X_DIM(), Y_DIM());
    643         this.resize(X_DIM(), Y_DIM());
    644 
    645         System.err.println("@@@@ App width: " + this.getWidth());
    646         System.err.println("@@@@ App height: " + this.getHeight());
    647        
    648         if(!this.isShowing()) {
    649         System.err.println("@@@@ The webswing Applet isn't showing!");
    650         this.setVisible(true);
    651         }
    652         if(!this.isShowing()) {
    653         System.err.println("@@@@ The webswing Applet is STILL not showing!");
    654         }
    655     }
    656     */
    657636       
    658637    MediaTracker trk = new  MediaTracker(this);
     
    665644                        image_url, href_musthave, image_mustnothave,
    666645                        image_ignore, imageType_,document_root,verbosity_,trk);
    667 
    668     if(isShowing() && isDisplayable()) { //The case if(!(isRunAsApplet && isWebswing)) {
    669         System.err.println("**** Creating display thread");
     646    // If GsdlCollageApplet is launched as a *webswing applet*, it
     647    // doesn't have dimensions, isn't showing nor is it even
     648    // displayable at this point in the code. Only when webswing
     649    // resizes the GsdlCollageApplet Component, does it have
     650    // dimensions and do we know for sure it is showing.
     651    if(isShowing() && isDisplayable()) { // case of: if(!(isRunAsApplet && isWebswing)) {
     652        if (verbosity_ >= 4) {
     653        System.err.println("**** Creating display thread");
     654        }
    670655        // starts the display image thread with the currently downloaded images
    671656        display_thread_ = new DisplayImages(this, download_images_,isJava2_, bgcolor_);
    672657    } else {
    673         System.err.println("**** GsdlCollageApplet.init() - display_thread_ not yet showing.");
    674         System.err.println("**** Will create and start display_thread_ later, in ComponentListener.resize()");
     658        if (verbosity_ >= 4) {
     659        System.err.println("**** GsdlCollageApplet.init() - display_thread_ not yet showing.");
     660        System.err.println("**** Will create and start display_thread_ later, in ComponentListener.resize()");
     661        }
    675662    }
    676663    // The display_thread_ needs the component showing before it can be started up.
     
    688675
    689676    public void componentHidden(ComponentEvent e) {
    690         System.err.println(e.getComponent().getClass().getName() + " --- Hidden");
     677    if (verbosity_ >= 4) {
     678        System.err.println(e.getComponent().getClass().getName() + " --- Hidden");
     679    }
    691680    }
    692681
    693682    public void componentMoved(ComponentEvent e) {
    694     System.err.println(e.getComponent().getClass().getName() + " --- Moved");
     683    if (verbosity_ >= 4) {
     684        System.err.println(e.getComponent().getClass().getName() + " --- Moved");
     685    }
    695686    }
    696687
    697688    public void componentResized(ComponentEvent e) {
    698     System.err.println(e.getComponent().getClass().getName() + " --- Resized");
    699    
    700689    if(display_thread_ == null) {
     690        if (verbosity_ >= 4) {
     691        System.err.println(e.getComponent().getClass().getName() + " --- Resized");
     692        System.err.println("Ready to instantiate display thread");
     693        }
    701694        display_thread_ = new DisplayImages(this, download_images_,isJava2_, bgcolor_);
    702695        display_thread_.start();
     
    704697    }
    705698
    706     public void componentShown(ComponentEvent e) {
    707     System.err.println("XXXXX" + e.getComponent().getClass().getName() + " --- Shown");
     699    public void componentShown(ComponentEvent e) { 
    708700    if(display_thread_ == null) {
     701        if (verbosity_ >= 4) {
     702        System.err.println("@@@ " + e.getComponent().getClass().getName() + " --- Shown");
     703        System.err.println("GsdlCollageApplet ready to instantiate display thread");
     704        }
    709705        display_thread_ = new DisplayImages(this, download_images_,isJava2_, bgcolor_);
    710706        display_thread_.start();
     
    718714    // TODO: Do I need to follow
    719715    // https://stackoverflow.com/questions/5861894/how-to-synchronize-or-lock-upon-variables-in-java
     716    // I just followed the Java API's recommendations about what to do
     717    // as replacement around Thread.stop() and Thread.destroy() being
     718    // deprecated (note: not Applet.stop() and
     719    // Applet.destroy(). https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#stop--
    720720    public void stopRunning() {
    721721    if(verbosity_ >= 3) {
     
    741741    }
    742742
     743    // Applet's mouseDown() is deprecated, so we implement mousePressed
    743744    protected class CollageMouseAdapter extends MouseAdapter {
    744745 
     
    748749    public void mousePressed(MouseEvent e)
    749750    {
    750 
    751     System.err.println("Mouse pressed");
    752751    int x = e.getX();
    753752    int y = e.getY();
     
    782781            System.err.println("@@@GsdlCollageApplet.CollageMouseAdapter.mousePressed()"
    783782               + "\n\topening url " + from_url + "\n\t"
    784                + "for non-applet and non-webswing application is not yet implemented.");
     783               + "for non-applets and non-webswing applications is not yet implemented.");
    785784        }
    786785        }
     
    790789
    791790    }
    792         //return true;
    793791    }
    794792    }
     
    797795    public void start()
    798796    {
    799     // starts the display image thread with the currently downloaded images
    800     //display_thread_ = new DisplayImages(this, download_images_,isJava2_, bgcolor_);
    801     //this.setVisible(true);   
    802797    download_thread_.start();   
    803798
     
    838833    public void stop()
    839834    {
    840     System.err.println("\n\n*** Stopping collage Applet: stopping threads...");
     835    if(verbosity_ >= 3) {
     836        System.err.println("\n\n@@@ GsdlCollageApplet.stop() called: stopping threads...");
     837    }
    841838    //download_thread_.stop();
    842839    //display_thread_.stop();
    843840    stopRunning();
    844841    // TODO: maybe we want to unset the interrupt variable on the threads to restart
    845     // on start() getting called? No: we system.exit() in the webswing shutdown handler
    846    
    847     //download_thread_ = null;
    848     //display_thread_ = null;
    849    
    850     }
    851 
    852     /** Destroys threads for downloading and displaying images */
     842    // on start() getting called? No: because we system.exit() in the webswing
     843    // shutdown handler we implemented.
     844    }
     845
     846    /** Destroys threads for downloading and displaying images Java
     847     * API method specification for destroy(): "Called by the browser
     848     * or applet viewer to inform this applet that it is being
     849     * reclaimed and that it should destroy any resources that it has
     850     * allocated. The stop method will always be called before
     851     * destroy."
     852     */
    853853    public void destroyed()
    854854    {
    855 
    856     System.err.println("@@@@@@@@@ GsdlCollageApplet.destroy() called");
    857 
    858     // Let's be sure to have stopped running
    859     //stopRunning();
    860    
     855    if(verbosity_ >= 3) {
     856        System.err.println("@@@@@@@@@ GsdlCollageApplet.destroy() called");
     857    }
    861858    download_thread_ = null;
    862859   
     
    887884        System.err.println("Applet still trying to paint!!");
    888885        }
    889 
    890    
    891    
    892     }
    893 
     886   
     887    }
    894888 
    895889   
  • main/trunk/greenstone3/src/java/org/greenstone/applet/phind/JPhind.java

    r38936 r38937  
    350350    add(resultPanel, BorderLayout.CENTER);
    351351
    352     // status bar that applet has, but we want one also if we run JPhind as application
    353     // And in fact, when webswing runs our applet, we never get a status bar. So we
    354     // create a custom status bar now even if we're running as an applet.
    355     // When this applet is run through the appletviewer we might end up with 2 status bars.
     352    // Manual status bar to mimic applet's default one, in case we
     353    // want one if we run Collage as application And in fact, when
     354    // webswing runs our applet, we never get a status bar. So we
     355    // can create a custom status bar now even if we're running as
     356    // an applet.  When this applet is run through the
     357    // appletviewer we might end up with 2 status bars.
     358   
    356359    //if(!isRunAsApplet) {
    357360        statusBar = new JLabel();
Note: See TracChangeset for help on using the changeset viewer.