home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 139 / dpcs0999.iso / Web / CFserver / data1.cab / Java / netscape / application / ExternalWindow.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-04-12  |  12.8 KB  |  586 lines

  1. package netscape.application;
  2.  
  3. import java.applet.Applet;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.Dialog;
  7. import java.awt.Dimension;
  8. import java.awt.Frame;
  9. import java.awt.Insets;
  10. import java.awt.MenuBar;
  11. import java.awt.Point;
  12. import java.awt.Rectangle;
  13. import java.awt.Toolkit;
  14. import netscape.util.InconsistencyException;
  15.  
  16. public class ExternalWindow implements Window, ApplicationObserver {
  17.    java.awt.Window awtWindow;
  18.    private FoundationPanel panel;
  19.    private WindowOwner owner;
  20.    private int type;
  21.    private Size minimumSize;
  22.    private String title;
  23.    private Rect bounds;
  24.    private boolean resizable;
  25.    private boolean visible;
  26.    private boolean hideOnPause;
  27.    private boolean showOnResume;
  28.    private boolean containsDocument;
  29.    private boolean waitingForInvalidation;
  30.    Menu menu;
  31.    MenuView menuView;
  32.    static final String AWTWINDOW_KEY = "awtWindow";
  33.    static final String PANEL_KEY = "panel";
  34.    static final String OWNER_KEY = "owner";
  35.    static final String TYPE_KEY = "type";
  36.    static final String MINSIZE_KEY = "minimumSize";
  37.    static final String MENU_KEY = "menu";
  38.    static final String HIDEONPAUSE_KEY = "hideOnPause";
  39.    static final String CONTAINS_DOCUMENT_KEY = "containsDocument";
  40.  
  41.    public ExternalWindow() {
  42.       this(1);
  43.    }
  44.  
  45.    private Frame firstRootViewParentFrame() {
  46.       Application var1 = Application.application();
  47.       if (var1 != null) {
  48.          RootView var2 = var1.firstRootView();
  49.          if (var2 != null) {
  50.             FoundationPanel var3 = var2.panel();
  51.  
  52.             Container var4;
  53.             for(var4 = ((Component)var3).getParent(); var4 != null && !(var4 instanceof Frame); var4 = ((Component)var4).getParent()) {
  54.             }
  55.  
  56.             if (var4 != null) {
  57.                return (Frame)var4;
  58.             }
  59.          }
  60.       }
  61.  
  62.       return this.appletParentFrame();
  63.    }
  64.  
  65.    private Frame appletParentFrame() {
  66.       Applet var1 = AWTCompatibility.awtApplet();
  67.       if (var1 == null) {
  68.          return null;
  69.       } else {
  70.          Container var2;
  71.          for(var2 = ((Component)var1).getParent(); var2 != null && !(var2 instanceof Frame); var2 = ((Component)var2).getParent()) {
  72.          }
  73.  
  74.          return (Frame)var2;
  75.       }
  76.    }
  77.  
  78.    private synchronized void validateAWTWindow(int var1, boolean var2) {
  79.       if (this.waitingForInvalidation) {
  80.          this.waitingForInvalidation = false;
  81.       }
  82.  
  83.       if (this.awtWindow == null) {
  84.          Application.application();
  85.          this.panel.rootView();
  86.          if (var2) {
  87.             FoundationDialog var3 = this.createDialog();
  88.             var3.setExternalWindow(this);
  89.             this.awtWindow = var3;
  90.          } else if (var1 == 1) {
  91.             FoundationFrame var4 = this.createFrame();
  92.             var4.setExternalWindow(this);
  93.             this.awtWindow = var4;
  94.          } else {
  95.             FoundationWindow var5 = this.createWindow();
  96.             var5.setExternalWindow(this);
  97.             this.awtWindow = var5;
  98.          }
  99.  
  100.          if (this.awtWindow instanceof Dialog) {
  101.             ((Dialog)this.awtWindow).setResizable(this.resizable);
  102.          } else if (this.awtWindow instanceof FoundationFrame) {
  103.             ((FoundationFrame)this.awtWindow).setResizable(this.resizable);
  104.          }
  105.  
  106.          this.awtWindow.addNotify();
  107.          this.awtWindow.add(this.panel);
  108.          this.awtWindow.reshape(this.bounds.x, this.bounds.y, this.bounds.width, this.bounds.height);
  109.          this.awtWindow.layout();
  110.          if (var1 == 1) {
  111.             if (this.awtWindow instanceof Dialog) {
  112.                ((Dialog)this.awtWindow).setTitle(this.title);
  113.             } else {
  114.                ((FoundationFrame)this.awtWindow).setTitle(this.title);
  115.             }
  116.          }
  117.  
  118.          if (this.menu != null) {
  119.             ((FoundationFrame)this.awtWindow).setMenuBar(this.menu.awtMenuBar());
  120.          }
  121.       }
  122.  
  123.    }
  124.  
  125.    synchronized void invalidateAWTWindow() {
  126.       if (this.waitingForInvalidation) {
  127.          this._invalidateAWTWindow();
  128.       }
  129.  
  130.       this.waitingForInvalidation = false;
  131.    }
  132.  
  133.    void _invalidateAWTWindow() {
  134.       if (this.awtWindow != null) {
  135.          this.bounds = this.bounds();
  136.          this.awtWindow.remove(this.panel);
  137.          this.awtWindow.dispose();
  138.          this.awtWindow = null;
  139.       }
  140.  
  141.    }
  142.  
  143.    public ExternalWindow(int var1) {
  144.       this.resizable = true;
  145.       this.visible = false;
  146.       this.hideOnPause = true;
  147.       this.showOnResume = false;
  148.       this.containsDocument = false;
  149.       this.waitingForInvalidation = false;
  150.       Application.application();
  151.       this.title = "";
  152.       this.type = var1;
  153.       this.panel = this.createPanel();
  154.       this.bounds = new Rect(0, 0, 0, 0);
  155.       this.setBounds(0, 0, 150, 150);
  156.       Application.application().addObserver(this);
  157.    }
  158.  
  159.    public void setTitle(String var1) {
  160.       if (var1 == null) {
  161.          var1 = "";
  162.       }
  163.  
  164.       this.title = var1;
  165.       if (this.awtWindow != null && this.type == 1) {
  166.          if (this.awtWindow instanceof Dialog) {
  167.             ((Dialog)this.awtWindow).setTitle(this.title);
  168.             return;
  169.          }
  170.  
  171.          ((FoundationFrame)this.awtWindow).setTitle(this.title);
  172.       }
  173.  
  174.    }
  175.  
  176.    public String title() {
  177.       return this.title;
  178.    }
  179.  
  180.    public void show() {
  181.       this.validateAWTWindow(this.type, false);
  182.       if (this.owner == null || this.owner.windowWillShow(this)) {
  183.          this.awtWindow.show();
  184.          this.panel.rootView.setVisible(true);
  185.          this.visible = true;
  186.          this.showOnResume = false;
  187.          this.awtWindow.toFront();
  188.          if (this.owner != null) {
  189.             this.owner.windowDidShow(this);
  190.          }
  191.       }
  192.  
  193.    }
  194.  
  195.    public void showModally() {
  196.       Application var1 = Application.application();
  197.       EventLoop var3 = Application.application().eventLoop();
  198.       if (this.type == 0) {
  199.          throw new InconsistencyException("Cannot run blank windows modally");
  200.       } else {
  201.          if (this.owner == null || this.owner.windowWillShow(this)) {
  202.             this.validateAWTWindow(this.type, true);
  203.             ModalDialogManager var4 = new ModalDialogManager((Dialog)this.awtWindow);
  204.             var4.show();
  205.             this.showOnResume = false;
  206.             this.panel.rootView.setVisible(true);
  207.             this.visible = true;
  208.             if (this.owner != null) {
  209.                this.owner.windowDidShow(this);
  210.             }
  211.  
  212.             var1.beginModalSessionForView(this.rootView());
  213.             var1.drawAllDirtyViews();
  214.  
  215.             while(this.isVisible()) {
  216.                Event var2 = var3.getNextEvent();
  217.  
  218.                try {
  219.                   var3.processEvent(var2);
  220.                } catch (Exception var6) {
  221.                   System.err.println("Uncaught Exception.");
  222.                   ((Throwable)var6).printStackTrace(System.err);
  223.                   System.err.println("Restarting modal EventLoop.");
  224.                }
  225.             }
  226.  
  227.             var1.endModalSessionForView(this.rootView());
  228.          }
  229.  
  230.       }
  231.    }
  232.  
  233.    public void hide() {
  234.       if (this.awtWindow != null) {
  235.          if (this.owner == null || this.owner.windowWillHide(this)) {
  236.             if (this.containsDocument() && this.isCurrentDocument()) {
  237.                Application.application().chooseNextCurrentDocumentWindow(this);
  238.             }
  239.  
  240.             this.awtWindow.hide();
  241.             this.visible = false;
  242.             this.panel.rootView.setVisible(false);
  243.             this.showOnResume = false;
  244.             if (this.owner != null) {
  245.                this.owner.windowDidHide(this);
  246.             }
  247.  
  248.             WindowInvalidationAgent var1 = new WindowInvalidationAgent(this);
  249.             this.waitingForInvalidation = true;
  250.             var1.run();
  251.          }
  252.  
  253.       }
  254.    }
  255.  
  256.    public boolean isVisible() {
  257.       return this.visible;
  258.    }
  259.  
  260.    public void dispose() {
  261.       RootView var1 = this.rootView();
  262.       Application var2 = var1.application();
  263.       if (this.containsDocument() && this.isCurrentDocument()) {
  264.          var2.chooseNextCurrentDocumentWindow(this);
  265.       }
  266.  
  267.       this.visible = false;
  268.       this._invalidateAWTWindow();
  269.       var2.removeObserver(this);
  270.       var2.removeRootView(var1);
  271.       this.panel.rootView.setVisible(false);
  272.       this.panel.rootView = null;
  273.    }
  274.  
  275.    public void setMenu(Menu var1) {
  276.       this.menu = var1;
  277.       if (!this.menu.isTopLevel()) {
  278.          throw new InconsistencyException("menu must be main menu");
  279.       } else {
  280.          this.menu.setApplication(this.rootView().application());
  281.          MenuBar var2 = this.menu.awtMenuBar();
  282.          if (this.awtWindow != null) {
  283.             ((FoundationFrame)this.awtWindow).setMenuBar(var2);
  284.          }
  285.  
  286.       }
  287.    }
  288.  
  289.    public Menu menu() {
  290.       return this.menu;
  291.    }
  292.  
  293.    public void setMenuView(MenuView var1) {
  294.       if (var1 == null || var1 != this.menuView) {
  295.          if (this.menuView != null) {
  296.             this.menuView.removeFromSuperview();
  297.          }
  298.  
  299.          this.menuView = var1;
  300.          int var2 = this.rootView().bounds.x;
  301.          int var3 = this.rootView().bounds.y;
  302.          int var4 = this.rootView().bounds.width;
  303.          int var5 = this.menuView.height();
  304.          this.menuView.setBounds(var2, var3, var4, var5);
  305.          this.addSubview(this.menuView);
  306.       }
  307.    }
  308.  
  309.    public MenuView menuView() {
  310.       return this.menuView;
  311.    }
  312.  
  313.    public RootView rootView() {
  314.       return this.panel.rootView;
  315.    }
  316.  
  317.    Application application() {
  318.       return Application.application();
  319.    }
  320.  
  321.    public void setOwner(WindowOwner var1) {
  322.       this.owner = var1;
  323.    }
  324.  
  325.    public WindowOwner owner() {
  326.       return this.owner;
  327.    }
  328.  
  329.    void didBecomeMain() {
  330.       if (this.owner != null) {
  331.          this.owner.windowDidBecomeMain(this);
  332.       }
  333.  
  334.       if (this.containsDocument()) {
  335.          Application.application().makeCurrentDocumentWindow(this);
  336.       }
  337.  
  338.    }
  339.  
  340.    void didResignMain() {
  341.       if (this.owner != null) {
  342.          this.owner.windowDidResignMain(this);
  343.       }
  344.  
  345.    }
  346.  
  347.    public Size contentSize() {
  348.       RootView var1 = this.rootView();
  349.       return var1 == null ? null : new Size(var1.bounds.width, var1.bounds.height);
  350.    }
  351.  
  352.    public void addSubview(View var1) {
  353.       RootView var2 = this.rootView();
  354.       if (var2 != null) {
  355.          ((View)var2).addSubview(var1);
  356.       }
  357.  
  358.    }
  359.  
  360.    public void setBounds(int var1, int var2, int var3, int var4) {
  361.       boolean var5 = false;
  362.       if (this.owner != null && (this.bounds.width != var3 || this.bounds.height != var4)) {
  363.          Size var6 = new Size(var3 - this.bounds.width, var4 - this.bounds.height);
  364.          this.owner.windowWillSizeBy(this, var6);
  365.          var3 = this.bounds.width + var6.width;
  366.          var4 = this.bounds.height + var6.height;
  367.       }
  368.  
  369.       if (this.bounds.x != var1 || this.bounds.y != var2 || this.bounds.width != var3 || this.bounds.height != var4) {
  370.          if (this.bounds.width != var3 || this.bounds.height != var4) {
  371.             var5 = true;
  372.          }
  373.  
  374.          this.bounds.setBounds(var1, var2, var3, var4);
  375.          if (this.awtWindow != null) {
  376.             this.awtWindow.reshape(var1, var2, var3, var4);
  377.             Rectangle var7 = this.awtWindow.bounds();
  378.             this.bounds.setBounds(var7.x, var7.y, var7.width, var7.height);
  379.             this.awtWindow.layout();
  380.          }
  381.       }
  382.  
  383.       if (var5 && this.awtWindow == null) {
  384.          this.validateAWTWindow(this.type, false);
  385.          this._invalidateAWTWindow();
  386.       }
  387.  
  388.    }
  389.  
  390.    void validateBounds() {
  391.       if (this.awtWindow != null) {
  392.          Point var2 = this.awtWindow.location();
  393.          Dimension var3 = this.awtWindow.size();
  394.          Rect var1 = new Rect(var2.x, var2.y, var3.width, var3.height);
  395.          if (!var1.equals(this.bounds)) {
  396.             Size var4 = new Size(var1.width - this.bounds.width, var1.height - this.bounds.height);
  397.             if (this.owner != null) {
  398.                this.owner.windowWillSizeBy(this, var4);
  399.             }
  400.  
  401.             this.bounds.setBounds(var1.x, var1.y, var1.width, var1.height);
  402.          }
  403.       }
  404.  
  405.    }
  406.  
  407.    public void setBounds(Rect var1) {
  408.       this.setBounds(var1.x, var1.y, var1.width, var1.height);
  409.    }
  410.  
  411.    public void sizeTo(int var1, int var2) {
  412.       this.setBounds(this.bounds.x, this.bounds.y, var1, var2);
  413.    }
  414.  
  415.    public void sizeBy(int var1, int var2) {
  416.       this.setBounds(this.bounds.x, this.bounds.y, this.bounds.width + var1, this.bounds.height + var2);
  417.    }
  418.  
  419.    public void moveBy(int var1, int var2) {
  420.       this.setBounds(this.bounds.x + var1, this.bounds.y + var2, this.bounds.width, this.bounds.height);
  421.    }
  422.  
  423.    public void center() {
  424.       Dimension var1 = Toolkit.getDefaultToolkit().getScreenSize();
  425.       Rect var2 = new Rect(this.bounds());
  426.       var2.x = (int)Math.floor((double)(var1.width - var2.width) / (double)2.0F);
  427.       var2.y = (int)Math.floor((double)(var1.height - var2.height) / (double)2.0F);
  428.       this.setBounds(var2);
  429.    }
  430.  
  431.    public void moveTo(int var1, int var2) {
  432.       this.setBounds(var1, var2, this.bounds.width, this.bounds.height);
  433.    }
  434.  
  435.    public Size windowSizeForContentSize(int var1, int var2) {
  436.       boolean var4 = this.awtWindow != null;
  437.       if (!var4) {
  438.          this.validateAWTWindow(this.type, false);
  439.       }
  440.  
  441.       Insets var3 = this.awtWindow.insets();
  442.       if (!var4) {
  443.          this._invalidateAWTWindow();
  444.       }
  445.  
  446.       return new Size(var1 + var3.left + var3.right, var2 + var3.top + var3.bottom);
  447.    }
  448.  
  449.    public View viewForMouse(int var1, int var2) {
  450.       return this.rootView().viewForMouse(var1, var2);
  451.    }
  452.  
  453.    public void setMinSize(int var1, int var2) {
  454.       this.minimumSize = new Size(var1, var2);
  455.    }
  456.  
  457.    public Size minSize() {
  458.       return this.minimumSize;
  459.    }
  460.  
  461.    public Rect bounds() {
  462.       if (this.awtWindow != null) {
  463.          Point var1 = this.awtWindow.location();
  464.          Dimension var2 = this.awtWindow.size();
  465.          return new Rect(var1.x, var1.y, var2.width, var2.height);
  466.       } else {
  467.          return new Rect(this.bounds);
  468.       }
  469.    }
  470.  
  471.    public void setResizable(boolean var1) {
  472.       this.resizable = var1;
  473.       if (this.awtWindow != null) {
  474.          throw new InconsistencyException("Cannot call setResizable on a visible external window");
  475.       }
  476.    }
  477.  
  478.    public boolean isResizable() {
  479.       return this.resizable;
  480.    }
  481.  
  482.    public FoundationPanel panel() {
  483.       return this.panel;
  484.    }
  485.  
  486.    public void setContainsDocument(boolean var1) {
  487.       this.containsDocument = var1;
  488.       if (!var1 && Application.application().currentDocumentWindow() == this) {
  489.          Application.application().chooseNextCurrentDocumentWindow(this);
  490.       } else {
  491.          if (var1 && Application.application().firstRootView() == this.rootView()) {
  492.             Application.application().makeCurrentDocumentWindow(this);
  493.          }
  494.  
  495.       }
  496.    }
  497.  
  498.    public boolean containsDocument() {
  499.       return this.containsDocument;
  500.    }
  501.  
  502.    public void didBecomeCurrentDocument() {
  503.    }
  504.  
  505.    public void didResignCurrentDocument() {
  506.    }
  507.  
  508.    public boolean isCurrentDocument() {
  509.       return Application.application().currentDocumentWindow() == this;
  510.    }
  511.  
  512.    protected FoundationDialog createDialog() {
  513.       return new FoundationDialog(this.firstRootViewParentFrame(), true);
  514.    }
  515.  
  516.    protected FoundationFrame createFrame() {
  517.       return new FoundationFrame();
  518.    }
  519.  
  520.    protected FoundationWindow createWindow() {
  521.       return new FoundationWindow(this.appletParentFrame());
  522.    }
  523.  
  524.    protected FoundationPanel createPanel() {
  525.       return new FoundationPanel();
  526.    }
  527.  
  528.    public void applicationDidStart(Application var1) {
  529.    }
  530.  
  531.    public void applicationDidStop(Application var1) {
  532.       this.dispose();
  533.    }
  534.  
  535.    public void focusDidChange(Application var1, View var2) {
  536.    }
  537.  
  538.    public void currentDocumentDidChange(Application var1, Window var2) {
  539.    }
  540.  
  541.    public void applicationDidPause(Application var1) {
  542.       if (this.hideOnPause && this.visible) {
  543.          this.hide();
  544.          this.showOnResume = true;
  545.       }
  546.  
  547.    }
  548.  
  549.    public void applicationDidResume(Application var1) {
  550.       if (this.showOnResume) {
  551.          this.show();
  552.       }
  553.  
  554.    }
  555.  
  556.    public void setHidesWhenPaused(boolean var1) {
  557.       this.hideOnPause = var1;
  558.    }
  559.  
  560.    public boolean hidesWhenPaused() {
  561.       return this.hideOnPause;
  562.    }
  563.  
  564.    public void performCommand(String var1, Object var2) {
  565.       if ("show".equals(var1)) {
  566.          this.show();
  567.       } else if ("hide".equals(var1)) {
  568.          this.hide();
  569.       } else {
  570.          throw new NoSuchMethodError("unknown command: " + var1);
  571.       }
  572.    }
  573.  
  574.    public void moveToFront() {
  575.       if (this.isVisible()) {
  576.          this.awtWindow.toFront();
  577.       }
  578.    }
  579.  
  580.    public void moveToBack() {
  581.       if (this.isVisible()) {
  582.          this.awtWindow.toBack();
  583.       }
  584.    }
  585. }
  586.