< prev index next >

src/java.desktop/share/classes/sun/applet/AppletViewer.java

Print this page




  31 import java.awt.event.*;
  32 import java.awt.print.*;
  33 import javax.print.attribute.*;
  34 import java.applet.*;
  35 import java.net.URL;
  36 import java.net.SocketPermission;
  37 import java.security.AccessController;
  38 import java.security.PrivilegedAction;
  39 import sun.awt.SunToolkit;
  40 import sun.awt.AppContext;
  41 
  42 /**
  43  * A frame to show the applet tag in.
  44  */
  45 @SuppressWarnings("serial") // JDK-implementation class
  46 final class TextFrame extends Frame {
  47 
  48     /**
  49      * Create the tag frame.
  50      */

  51     TextFrame(int x, int y, String title, String text) {
  52         setTitle(title);
  53         TextArea txt = new TextArea(20, 60);
  54         txt.setText(text);
  55         txt.setEditable(false);
  56 
  57         add("Center", txt);
  58 
  59         Panel p = new Panel();
  60         add("South", p);
  61         Button b = new Button(amh.getMessage("button.dismiss", "Dismiss"));
  62         p.add(b);
  63 
  64         class ActionEventListener implements ActionListener {
  65             @Override
  66             public void actionPerformed(ActionEvent evt) {
  67                 dispose();
  68             }
  69         }
  70         b.addActionListener(new ActionEventListener());


 139      */
 140 
 141     PrintStream statusMsgStream;
 142 
 143     /**
 144      * For cloning
 145      */
 146     AppletViewerFactory factory;
 147 
 148 
 149     private final class UserActionListener implements ActionListener {
 150         @Override
 151         public void actionPerformed(ActionEvent evt) {
 152             processUserAction(evt);
 153         }
 154     }
 155 
 156     /**
 157      * Create the applet viewer.
 158      */

 159     public AppletViewer(int x, int y, URL doc, Hashtable<String, String> atts,
 160                         PrintStream statusMsgStream, AppletViewerFactory factory) {
 161         this.factory = factory;
 162         this.statusMsgStream = statusMsgStream;
 163         setTitle(amh.getMessage("tool.title", atts.get("code")));
 164 
 165         MenuBar mb = factory.getBaseMenuBar();
 166 
 167         Menu m = new Menu(amh.getMessage("menu.applet"));
 168 
 169         addMenuItem(m, "menuitem.restart");
 170         addMenuItem(m, "menuitem.reload");
 171         addMenuItem(m, "menuitem.stop");
 172         addMenuItem(m, "menuitem.save");
 173         addMenuItem(m, "menuitem.start");
 174         addMenuItem(m, "menuitem.clone");
 175         m.add(new MenuItem("-"));
 176         addMenuItem(m, "menuitem.tag");
 177         addMenuItem(m, "menuitem.info");
 178         addMenuItem(m, "menuitem.edit").disable();


 211             public void windowIconified(WindowEvent evt) {
 212                 appletStop();
 213             }
 214 
 215             @Override
 216             public void windowDeiconified(WindowEvent evt) {
 217                 appletStart();
 218             }
 219         };
 220 
 221         class AppletEventListener implements AppletListener
 222         {
 223             final Frame frame;
 224 
 225             public AppletEventListener(Frame frame)
 226             {
 227                 this.frame = frame;
 228             }
 229 
 230             @Override

 231             public void appletStateChanged(AppletEvent evt)
 232             {
 233                 AppletPanel src = (AppletPanel)evt.getSource();
 234 
 235                 switch (evt.getID()) {
 236                     case AppletPanel.APPLET_RESIZE: {
 237                         if(src != null) {
 238                             resize(preferredSize());
 239                             validate();
 240                         }
 241                         break;
 242                     }
 243                     case AppletPanel.APPLET_LOADING_COMPLETED: {
 244                         Applet a = src.getApplet(); // sun.applet.AppletPanel
 245 
 246                         // Fixed #4754451: Applet can have methods running on main
 247                         // thread event queue.
 248                         //
 249                         // The cause of this bug is that the frame of the applet
 250                         // is created in main thread group. Thus, when certain


 577                 }
 578             }
 579             System.arraycopy(params, i, params, i + 1, len - i);
 580             params[i] = param;
 581             len++;
 582         }
 583 
 584         for (int i = 0 ; i < len ; i++) {
 585             String param = params[i];
 586             if (systemParam.get(param) == null) {
 587                 out.println("<param name=" + param +
 588                             " value=\"" + atts.get(param) + "\">");
 589             }
 590         }
 591         out.println("</applet>");
 592     }
 593 
 594     /**
 595      * Make sure the atrributes are uptodate.
 596      */

 597     public void updateAtts() {
 598         Dimension d = panel.size();
 599         Insets in = panel.insets();
 600         panel.atts.put("width",
 601                        Integer.toString(d.width - (in.left + in.right)));
 602         panel.atts.put("height",
 603                        Integer.toString(d.height - (in.top + in.bottom)));
 604     }
 605 
 606     /**
 607      * Restart the applet.
 608      */
 609     void appletRestart() {
 610         panel.sendEvent(AppletPanel.APPLET_STOP);
 611         panel.sendEvent(AppletPanel.APPLET_DESTROY);
 612         panel.sendEvent(AppletPanel.APPLET_INIT);
 613         panel.sendEvent(AppletPanel.APPLET_START);
 614     }
 615 
 616     /**


 631         /*
 632          * Make sure we don't have two threads running through the event queue
 633          * at the same time.
 634          */
 635         try {
 636             panel.joinAppletThread();
 637             panel.release();
 638         } catch (InterruptedException e) {
 639             return;   // abort the reload
 640         }
 641 
 642         panel.createAppletThread();
 643         panel.sendEvent(AppletPanel.APPLET_LOAD);
 644         panel.sendEvent(AppletPanel.APPLET_INIT);
 645         panel.sendEvent(AppletPanel.APPLET_START);
 646     }
 647 
 648     /**
 649      * Save the applet to a well known file (for now) as a serialized object
 650      */

 651     void appletSave() {
 652         AccessController.doPrivileged(new PrivilegedAction<Object>() {
 653 
 654             @Override
 655             public Object run() {
 656                 // XXX: this privileged block should be made smaller
 657                 // by initializing a private static variable with "user.dir"
 658 
 659                 // Applet needs to be stopped for serialization to succeed.
 660                 // Since panel.sendEvent only queues the event, there is a
 661                 // chance that the event will not be processed before
 662                 // serialization begins.  However, by sending the event before
 663                 // FileDialog is created, enough time is given such that this
 664                 // situation is unlikely to ever occur.
 665 
 666                 panel.sendEvent(AppletPanel.APPLET_STOP);
 667                 FileDialog fd = new FileDialog(AppletViewer.this,
 668                                                amh.getMessage("appletsave.filedialogtitle"),
 669                                                FileDialog.SAVE);
 670                 // needed for a bug under Solaris...


 682 
 683                 try (FileOutputStream fos = new FileOutputStream(file);
 684                      BufferedOutputStream bos = new BufferedOutputStream(fos);
 685                      ObjectOutputStream os = new ObjectOutputStream(bos)) {
 686 
 687                     showStatus(amh.getMessage("appletsave.err1", panel.applet.toString(), file.toString()));
 688                     os.writeObject(panel.applet);
 689                 } catch (IOException ex) {
 690                     System.err.println(amh.getMessage("appletsave.err2", ex));
 691                 } finally {
 692                     panel.sendEvent(AppletPanel.APPLET_START);
 693                 }
 694                 return null;
 695             }
 696         });
 697     }
 698 
 699     /**
 700      * Clone the viewer and the applet.
 701      */

 702     void appletClone() {
 703         Point p = location();
 704         updateAtts();
 705         @SuppressWarnings("unchecked")
 706         Hashtable<String, String> tmp = (Hashtable<String, String>) panel.atts.clone();
 707         factory.createAppletViewer(p.x + XDELTA, p.y + YDELTA,
 708                                    panel.documentURL, tmp);
 709     }
 710 
 711     /**
 712      * Show the applet tag.
 713      */

 714     void appletTag() {
 715         ByteArrayOutputStream out = new ByteArrayOutputStream();
 716         updateAtts();
 717         printTag(new PrintStream(out), panel.atts);
 718         showStatus(amh.getMessage("applettag"));
 719 
 720         Point p = location();
 721         new TextFrame(p.x + XDELTA, p.y + YDELTA, amh.getMessage("applettag.textframe"), out.toString());
 722     }
 723 
 724     /**
 725      * Show the applet info.
 726      */

 727     void appletInfo() {
 728         String str = panel.applet.getAppletInfo();
 729         if (str == null) {
 730             str = amh.getMessage("appletinfo.applet");
 731         }
 732         str += "\n\n";
 733 
 734         String atts[][] = panel.applet.getParameterInfo();
 735         if (atts != null) {
 736             for (int i = 0 ; i < atts.length ; i++) {
 737                 str += atts[i][0] + " -- " + atts[i][1] + " -- " + atts[i][2] + "\n";
 738             }
 739         } else {
 740             str += amh.getMessage("appletinfo.param");
 741         }
 742 
 743         Point p = location();
 744         new TextFrame(p.x + XDELTA, p.y + YDELTA, amh.getMessage("appletinfo.textframe"), str);
 745 
 746     }




  31 import java.awt.event.*;
  32 import java.awt.print.*;
  33 import javax.print.attribute.*;
  34 import java.applet.*;
  35 import java.net.URL;
  36 import java.net.SocketPermission;
  37 import java.security.AccessController;
  38 import java.security.PrivilegedAction;
  39 import sun.awt.SunToolkit;
  40 import sun.awt.AppContext;
  41 
  42 /**
  43  * A frame to show the applet tag in.
  44  */
  45 @SuppressWarnings("serial") // JDK-implementation class
  46 final class TextFrame extends Frame {
  47 
  48     /**
  49      * Create the tag frame.
  50      */
  51     @SuppressWarnings("deprecation")
  52     TextFrame(int x, int y, String title, String text) {
  53         setTitle(title);
  54         TextArea txt = new TextArea(20, 60);
  55         txt.setText(text);
  56         txt.setEditable(false);
  57 
  58         add("Center", txt);
  59 
  60         Panel p = new Panel();
  61         add("South", p);
  62         Button b = new Button(amh.getMessage("button.dismiss", "Dismiss"));
  63         p.add(b);
  64 
  65         class ActionEventListener implements ActionListener {
  66             @Override
  67             public void actionPerformed(ActionEvent evt) {
  68                 dispose();
  69             }
  70         }
  71         b.addActionListener(new ActionEventListener());


 140      */
 141 
 142     PrintStream statusMsgStream;
 143 
 144     /**
 145      * For cloning
 146      */
 147     AppletViewerFactory factory;
 148 
 149 
 150     private final class UserActionListener implements ActionListener {
 151         @Override
 152         public void actionPerformed(ActionEvent evt) {
 153             processUserAction(evt);
 154         }
 155     }
 156 
 157     /**
 158      * Create the applet viewer.
 159      */
 160     @SuppressWarnings("deprecation")
 161     public AppletViewer(int x, int y, URL doc, Hashtable<String, String> atts,
 162                         PrintStream statusMsgStream, AppletViewerFactory factory) {
 163         this.factory = factory;
 164         this.statusMsgStream = statusMsgStream;
 165         setTitle(amh.getMessage("tool.title", atts.get("code")));
 166 
 167         MenuBar mb = factory.getBaseMenuBar();
 168 
 169         Menu m = new Menu(amh.getMessage("menu.applet"));
 170 
 171         addMenuItem(m, "menuitem.restart");
 172         addMenuItem(m, "menuitem.reload");
 173         addMenuItem(m, "menuitem.stop");
 174         addMenuItem(m, "menuitem.save");
 175         addMenuItem(m, "menuitem.start");
 176         addMenuItem(m, "menuitem.clone");
 177         m.add(new MenuItem("-"));
 178         addMenuItem(m, "menuitem.tag");
 179         addMenuItem(m, "menuitem.info");
 180         addMenuItem(m, "menuitem.edit").disable();


 213             public void windowIconified(WindowEvent evt) {
 214                 appletStop();
 215             }
 216 
 217             @Override
 218             public void windowDeiconified(WindowEvent evt) {
 219                 appletStart();
 220             }
 221         };
 222 
 223         class AppletEventListener implements AppletListener
 224         {
 225             final Frame frame;
 226 
 227             public AppletEventListener(Frame frame)
 228             {
 229                 this.frame = frame;
 230             }
 231 
 232             @Override
 233             @SuppressWarnings("deprecation")
 234             public void appletStateChanged(AppletEvent evt)
 235             {
 236                 AppletPanel src = (AppletPanel)evt.getSource();
 237 
 238                 switch (evt.getID()) {
 239                     case AppletPanel.APPLET_RESIZE: {
 240                         if(src != null) {
 241                             resize(preferredSize());
 242                             validate();
 243                         }
 244                         break;
 245                     }
 246                     case AppletPanel.APPLET_LOADING_COMPLETED: {
 247                         Applet a = src.getApplet(); // sun.applet.AppletPanel
 248 
 249                         // Fixed #4754451: Applet can have methods running on main
 250                         // thread event queue.
 251                         //
 252                         // The cause of this bug is that the frame of the applet
 253                         // is created in main thread group. Thus, when certain


 580                 }
 581             }
 582             System.arraycopy(params, i, params, i + 1, len - i);
 583             params[i] = param;
 584             len++;
 585         }
 586 
 587         for (int i = 0 ; i < len ; i++) {
 588             String param = params[i];
 589             if (systemParam.get(param) == null) {
 590                 out.println("<param name=" + param +
 591                             " value=\"" + atts.get(param) + "\">");
 592             }
 593         }
 594         out.println("</applet>");
 595     }
 596 
 597     /**
 598      * Make sure the atrributes are uptodate.
 599      */
 600     @SuppressWarnings("deprecation")
 601     public void updateAtts() {
 602         Dimension d = panel.size();
 603         Insets in = panel.insets();
 604         panel.atts.put("width",
 605                        Integer.toString(d.width - (in.left + in.right)));
 606         panel.atts.put("height",
 607                        Integer.toString(d.height - (in.top + in.bottom)));
 608     }
 609 
 610     /**
 611      * Restart the applet.
 612      */
 613     void appletRestart() {
 614         panel.sendEvent(AppletPanel.APPLET_STOP);
 615         panel.sendEvent(AppletPanel.APPLET_DESTROY);
 616         panel.sendEvent(AppletPanel.APPLET_INIT);
 617         panel.sendEvent(AppletPanel.APPLET_START);
 618     }
 619 
 620     /**


 635         /*
 636          * Make sure we don't have two threads running through the event queue
 637          * at the same time.
 638          */
 639         try {
 640             panel.joinAppletThread();
 641             panel.release();
 642         } catch (InterruptedException e) {
 643             return;   // abort the reload
 644         }
 645 
 646         panel.createAppletThread();
 647         panel.sendEvent(AppletPanel.APPLET_LOAD);
 648         panel.sendEvent(AppletPanel.APPLET_INIT);
 649         panel.sendEvent(AppletPanel.APPLET_START);
 650     }
 651 
 652     /**
 653      * Save the applet to a well known file (for now) as a serialized object
 654      */
 655     @SuppressWarnings("deprecation")
 656     void appletSave() {
 657         AccessController.doPrivileged(new PrivilegedAction<Object>() {
 658 
 659             @Override
 660             public Object run() {
 661                 // XXX: this privileged block should be made smaller
 662                 // by initializing a private static variable with "user.dir"
 663 
 664                 // Applet needs to be stopped for serialization to succeed.
 665                 // Since panel.sendEvent only queues the event, there is a
 666                 // chance that the event will not be processed before
 667                 // serialization begins.  However, by sending the event before
 668                 // FileDialog is created, enough time is given such that this
 669                 // situation is unlikely to ever occur.
 670 
 671                 panel.sendEvent(AppletPanel.APPLET_STOP);
 672                 FileDialog fd = new FileDialog(AppletViewer.this,
 673                                                amh.getMessage("appletsave.filedialogtitle"),
 674                                                FileDialog.SAVE);
 675                 // needed for a bug under Solaris...


 687 
 688                 try (FileOutputStream fos = new FileOutputStream(file);
 689                      BufferedOutputStream bos = new BufferedOutputStream(fos);
 690                      ObjectOutputStream os = new ObjectOutputStream(bos)) {
 691 
 692                     showStatus(amh.getMessage("appletsave.err1", panel.applet.toString(), file.toString()));
 693                     os.writeObject(panel.applet);
 694                 } catch (IOException ex) {
 695                     System.err.println(amh.getMessage("appletsave.err2", ex));
 696                 } finally {
 697                     panel.sendEvent(AppletPanel.APPLET_START);
 698                 }
 699                 return null;
 700             }
 701         });
 702     }
 703 
 704     /**
 705      * Clone the viewer and the applet.
 706      */
 707     @SuppressWarnings("deprecation")
 708     void appletClone() {
 709         Point p = location();
 710         updateAtts();
 711         @SuppressWarnings("unchecked")
 712         Hashtable<String, String> tmp = (Hashtable<String, String>) panel.atts.clone();
 713         factory.createAppletViewer(p.x + XDELTA, p.y + YDELTA,
 714                                    panel.documentURL, tmp);
 715     }
 716 
 717     /**
 718      * Show the applet tag.
 719      */
 720     @SuppressWarnings("deprecation")
 721     void appletTag() {
 722         ByteArrayOutputStream out = new ByteArrayOutputStream();
 723         updateAtts();
 724         printTag(new PrintStream(out), panel.atts);
 725         showStatus(amh.getMessage("applettag"));
 726 
 727         Point p = location();
 728         new TextFrame(p.x + XDELTA, p.y + YDELTA, amh.getMessage("applettag.textframe"), out.toString());
 729     }
 730 
 731     /**
 732      * Show the applet info.
 733      */
 734     @SuppressWarnings("deprecation")
 735     void appletInfo() {
 736         String str = panel.applet.getAppletInfo();
 737         if (str == null) {
 738             str = amh.getMessage("appletinfo.applet");
 739         }
 740         str += "\n\n";
 741 
 742         String atts[][] = panel.applet.getParameterInfo();
 743         if (atts != null) {
 744             for (int i = 0 ; i < atts.length ; i++) {
 745                 str += atts[i][0] + " -- " + atts[i][1] + " -- " + atts[i][2] + "\n";
 746             }
 747         } else {
 748             str += amh.getMessage("appletinfo.param");
 749         }
 750 
 751         Point p = location();
 752         new TextFrame(p.x + XDELTA, p.y + YDELTA, amh.getMessage("appletinfo.textframe"), str);
 753 
 754     }


< prev index next >