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

  1. package netscape.application;
  2.  
  3. import netscape.util.ClassInfo;
  4. import netscape.util.CodingException;
  5. import netscape.util.Decoder;
  6. import netscape.util.Encoder;
  7.  
  8. public class Popup extends View implements Target, FormElement {
  9.    ListView popupList;
  10.    Window popupWindow;
  11.    ContainerView container;
  12.    ListItem selectedItem;
  13.    ListItem wasSelectedItem;
  14.    Target target;
  15.    Image image;
  16.    boolean _showingPopupForKeyboard;
  17.    boolean enabled;
  18.    static final String LISTVIEW_KEY = "popupList";
  19.    static final String WINDOW_KEY = "popupWindow";
  20.    static final String CONTAINER_KEY = "container";
  21.    static final String SELECTEDITEM_KEY = "selectedItem";
  22.    static final String TARGET_KEY = "target";
  23.    static final String SELECTEDIMAGE_KEY = "selectedImage";
  24.    static final String ENABLED_KEY = "enabled";
  25.    public static final String SELECT_NEXT_ITEM;
  26.    public static final String SELECT_PREVIOUS_ITEM;
  27.    public static final String POPUP = "popup";
  28.    static final String CLOSE_POPUP_AND_CANCEL = "cancel";
  29.    static final String CLOSE_POPUP_AND_COMMIT = "commit";
  30.  
  31.    public Popup() {
  32.       this(0, 0, 0, 0);
  33.    }
  34.  
  35.    public Popup(Rect var1) {
  36.       this(var1.x, var1.y, var1.width, var1.height);
  37.    }
  38.  
  39.    public Popup(int var1, int var2, int var3, int var4) {
  40.       super(var1, var2, var3, var4);
  41.       this.enabled = true;
  42.       InternalWindow var8 = new InternalWindow(var1, var2, var3, var4);
  43.       var8.setType(0);
  44.       var8.setLayer(300);
  45.       var8._contentView.setTransparent(true);
  46.       var8.setScrollsToVisible(true);
  47.       this.container = new ContainerView(0, 0, var3, var4);
  48.       this.container.setTransparent(true);
  49.       this.container.setBorder(BezelBorder.raisedBezel());
  50.       this.container.setVertResizeInstruction(16);
  51.       this.container.setHorizResizeInstruction(2);
  52.       ListView var5 = new ListView(0, 0, var3, var4);
  53.       PopupItem var7 = new PopupItem();
  54.       var7.setPopup(this);
  55.       this.setPopupList(var5);
  56.       this.setPrototypeItem(var7);
  57.       this.setPopupWindow(var8);
  58.       this.setPopupImage(Bitmap.bitmapNamed("netscape/application/PopupKnobH.gif"));
  59.       this._setupKeyboard();
  60.    }
  61.  
  62.    public void setPrototypeItem(ListItem var1) {
  63.       this.popupList.setPrototypeItem(var1);
  64.       if (var1 instanceof PopupItem) {
  65.          ((PopupItem)var1).setPopup(this);
  66.       }
  67.  
  68.    }
  69.  
  70.    public ListItem prototypeItem() {
  71.       return this.popupList.prototypeItem();
  72.    }
  73.  
  74.    public void removeAllItems() {
  75.       this.popupList.removeAllItems();
  76.    }
  77.  
  78.    public ListItem addItem(String var1, String var2) {
  79.       this.hidePopupIfNeeded();
  80.       ListItem var3 = this.popupList.addItem();
  81.       var3.setTitle(var1);
  82.       var3.setCommand(var2);
  83.       return var3;
  84.    }
  85.  
  86.    public void removeItem(String var1) {
  87.       if (var1 != null) {
  88.          int var3 = this.popupList.count();
  89.  
  90.          while(var3-- > 0) {
  91.             ListItem var2 = this.itemAt(var3);
  92.             if (var1.equals(var2.title())) {
  93.                this.hidePopupIfNeeded();
  94.                this.popupList.removeItemAt(var3);
  95.                return;
  96.             }
  97.          }
  98.  
  99.       }
  100.    }
  101.  
  102.    public void removeItemAt(int var1) {
  103.       if (this.popupList.count() > var1) {
  104.          this.hidePopupIfNeeded();
  105.          this.popupList.removeItemAt(var1);
  106.       }
  107.  
  108.    }
  109.  
  110.    public int selectedIndex() {
  111.       int var1 = this.popupList.indexOfItem(this.selectedItem);
  112.       if (var1 < 0 && this.popupList.count() > 0) {
  113.          var1 = 0;
  114.          this.selectItemAt(0);
  115.       }
  116.  
  117.       return var1;
  118.    }
  119.  
  120.    public ListItem selectedItem() {
  121.       int var1 = this.selectedIndex();
  122.       return var1 < 0 ? null : this.popupList.itemAt(var1);
  123.    }
  124.  
  125.    public void selectItem(ListItem var1) {
  126.       this.selectedItem = var1;
  127.       ((View)this).draw();
  128.    }
  129.  
  130.    public void selectItemAt(int var1) {
  131.       this.selectItem(this.popupList.itemAt(var1));
  132.    }
  133.  
  134.    public int count() {
  135.       return this.popupList.count();
  136.    }
  137.  
  138.    public ListItem itemAt(int var1) {
  139.       return this.popupList.itemAt(var1);
  140.    }
  141.  
  142.    public void setBorder(Border var1) {
  143.       this.container.setBorder(var1);
  144.    }
  145.  
  146.    public Border border() {
  147.       return this.container.border();
  148.    }
  149.  
  150.    public void setPopupList(ListView var1) {
  151.       this.popupList = var1;
  152.       this.popupList.setTarget(this);
  153.       this.popupList.setAllowsMultipleSelection(false);
  154.       this.popupList.setAllowsEmptySelection(true);
  155.       this.popupList.setTracksMouseOutsideBounds(false);
  156.       this.container.addSubview(this.popupList);
  157.    }
  158.  
  159.    public ListView popupList() {
  160.       return this.popupList;
  161.    }
  162.  
  163.    public void setPopupWindow(Window var1) {
  164.       this.popupWindow = var1;
  165.       if (var1 instanceof InternalWindow) {
  166.          InternalWindow var2 = (InternalWindow)var1;
  167.          var2.setScrollsToVisible(true);
  168.          var1.addSubview(this.container);
  169.       }
  170.  
  171.    }
  172.  
  173.    public Window popupWindow() {
  174.       return this.popupWindow;
  175.    }
  176.  
  177.    public void setPopupImage(Image var1) {
  178.       this.image = var1;
  179.    }
  180.  
  181.    public Image popupImage() {
  182.       return this.image;
  183.    }
  184.  
  185.    protected void layoutPopupWindow() {
  186.       Border var1 = this.container.border();
  187.       this.popupList.setRowHeight(super.bounds.height - var1.heightMargin());
  188.       this.popupList.setBounds(var1.leftMargin(), var1.topMargin(), super.bounds.width - var1.widthMargin(), this.popupList.rowHeight() * this.popupList.count());
  189.       int var3 = this.selectedIndex();
  190.       Rect var2 = Rect.newRect(0, 0, ((View)this).width(), ((View)this).height());
  191.       ((View)this).convertRectToView((View)null, var2, var2);
  192.       if (((View)this).rootView().windowClipView() != null) {
  193.          ((View)this).rootView().convertRectToView(((View)this).rootView().windowClipView(), var2, var2);
  194.       }
  195.  
  196.       this.popupWindow.setBounds(var2.x, var2.y - var3 * this.popupList.rowHeight(), this.popupList.width() + var1.widthMargin(), this.popupList.height() + var1.heightMargin());
  197.       Rect.returnRect(var2);
  198.    }
  199.  
  200.    protected void showPopupWindow(MouseEvent var1) {
  201.       if (this.popupWindow instanceof InternalWindow) {
  202.          InternalWindow var2 = (InternalWindow)this.popupWindow;
  203.          var2.setRootView(((View)this).rootView());
  204.          Application.application().beginModalSessionForView(var2);
  205.       } else {
  206.          ExternalWindow var3 = (ExternalWindow)this.popupWindow;
  207.          Application.application().beginModalSessionForView(var3.rootView());
  208.       }
  209.  
  210.       this.popupWindow.show();
  211.       ((View)this).rootView().setMouseView(this.popupList);
  212.       if (var1 != null) {
  213.          this.popupList.mouseDown(((View)this).convertEventToView(this.popupList, var1));
  214.       } else {
  215.          this.popupList.selectItem(this.selectedItem());
  216.          ((View)this).rootView().makeSelectedView(this.popupList);
  217.       }
  218.    }
  219.  
  220.    public boolean mouseDown(MouseEvent var1) {
  221.       if (!this.isEnabled()) {
  222.          return false;
  223.       } else {
  224.          this.layoutPopupWindow();
  225.          this.showPopupWindow(var1);
  226.          return true;
  227.       }
  228.    }
  229.  
  230.    public void setEnabled(boolean var1) {
  231.       this.enabled = var1;
  232.       ((View)this).setDirty(true);
  233.    }
  234.  
  235.    public boolean isEnabled() {
  236.       return this.enabled;
  237.    }
  238.  
  239.    public boolean isTransparent() {
  240.       return this.popupList.isTransparent();
  241.    }
  242.  
  243.    public void drawView(Graphics var1) {
  244.       Color var4 = null;
  245.       Border var2 = this.container.border();
  246.       if (this.selectedItem == null && this.popupList.selectedItem() == null) {
  247.          this.selectItem(this.popupList.itemAt(0));
  248.       }
  249.  
  250.       if (!this.popupList.isTransparent() && this.selectedItem != null && this.selectedItem.isTransparent()) {
  251.          var1.setColor(this.popupList.backgroundColor());
  252.          var1.fillRect(0, 0, ((View)this).width(), ((View)this).height());
  253.       }
  254.  
  255.       if (this.selectedItem != null) {
  256.          Rect var3 = Rect.newRect(var2.leftMargin(), var2.topMargin(), super.bounds.width - var2.widthMargin(), super.bounds.height - var2.heightMargin());
  257.          var1.pushState();
  258.          var1.setClipRect(var3);
  259.          if (!this.isEnabled()) {
  260.             var4 = this.selectedItem.textColor();
  261.             this.selectedItem.setTextColor(Color.gray);
  262.          }
  263.  
  264.          this.selectedItem.drawInRect(var1, var3);
  265.          if (!this.isEnabled()) {
  266.             this.selectedItem.setTextColor(var4);
  267.          }
  268.  
  269.          var1.popState();
  270.          Rect.returnRect(var3);
  271.       }
  272.  
  273.       var2.drawInRect(var1, 0, 0, ((View)this).width(), ((View)this).height());
  274.    }
  275.  
  276.    public void setTarget(Target var1) {
  277.       this.target = var1;
  278.    }
  279.  
  280.    public Target target() {
  281.       return this.target;
  282.    }
  283.  
  284.    public void setCommand(String var1) {
  285.       this.popupList.setCommand(var1);
  286.    }
  287.  
  288.    public String command() {
  289.       return this.popupList.command();
  290.    }
  291.  
  292.    public void sendCommand() {
  293.       if (this.target != null) {
  294.          String var1 = null;
  295.          if (this.selectedItem != null) {
  296.             var1 = this.selectedItem.command();
  297.          }
  298.  
  299.          if (var1 == null) {
  300.             var1 = this.command();
  301.          }
  302.  
  303.          this.target.performCommand(var1, this);
  304.       }
  305.  
  306.    }
  307.  
  308.    public void performCommand(String var1, Object var2) {
  309.       if (SELECT_NEXT_ITEM.equals(var1)) {
  310.          this.selectNextItem(true);
  311.       } else if (SELECT_PREVIOUS_ITEM.equals(var1)) {
  312.          this.selectNextItem(false);
  313.       } else if ("popup".equals(var1)) {
  314.          this.layoutPopupWindow();
  315.          this.wasSelectedItem = this.selectedItem();
  316.          this._showingPopupForKeyboard = true;
  317.          this.showPopupWindow((MouseEvent)null);
  318.          this._setupKeyboardToClosePopup(true);
  319.       } else if (this.showingPopupForKeyboard()) {
  320.          boolean var5 = false;
  321.          if ("cancel".equals(var1)) {
  322.             this.selectItem(this.wasSelectedItem);
  323.             var5 = true;
  324.          } else if ("commit".equals(var1)) {
  325.             this.selectItem(this.popupList.selectedItem());
  326.             if (this.popupList.selectedItem() != null) {
  327.                this.sendCommand();
  328.             }
  329.  
  330.             var5 = true;
  331.          }
  332.  
  333.          if (var5) {
  334.             this.popupList.disableDrawing();
  335.             this.popupList.deselectItem(this.popupList.selectedItem());
  336.             this.popupList.reenableDrawing();
  337.             this.popupWindow.hide();
  338.             if (this.popupWindow instanceof InternalWindow) {
  339.                InternalWindow var3 = (InternalWindow)this.popupWindow;
  340.                Application.application().endModalSessionForView(var3);
  341.             } else {
  342.                ExternalWindow var4 = (ExternalWindow)this.popupWindow;
  343.                Application.application().endModalSessionForView(var4.rootView());
  344.             }
  345.  
  346.             this._setupKeyboardToClosePopup(false);
  347.             this._showingPopupForKeyboard = false;
  348.          }
  349.       } else {
  350.          if (this.popupList.selectedItem() != null) {
  351.             this.selectedItem = this.popupList.selectedItem();
  352.          }
  353.  
  354.          if (this.selectedItem != null) {
  355.             this.sendCommand();
  356.          }
  357.  
  358.          this.popupList.disableDrawing();
  359.          this.popupList.deselectItem(this.selectedItem);
  360.          this.popupList.reenableDrawing();
  361.          this.popupWindow.hide();
  362.          if (this.popupWindow instanceof InternalWindow) {
  363.             InternalWindow var6 = (InternalWindow)this.popupWindow;
  364.             Application.application().endModalSessionForView(var6);
  365.          } else {
  366.             ExternalWindow var7 = (ExternalWindow)this.popupWindow;
  367.             Application.application().endModalSessionForView(var7.rootView());
  368.          }
  369.  
  370.          this._setupKeyboardToClosePopup(false);
  371.       }
  372.  
  373.       ((View)this).draw();
  374.    }
  375.  
  376.    public Size minSize() {
  377.       int var3 = 0;
  378.       int var4 = 0;
  379.       if (this.container.border() != null) {
  380.          var3 = this.container.border().widthMargin();
  381.          var4 = this.container.border().heightMargin();
  382.       }
  383.  
  384.       int var1 = 0;
  385.       int var2 = 0;
  386.       if (this.popupList != null) {
  387.          var1 = this.popupList.minItemWidth();
  388.          var2 = this.popupList.minItemHeight();
  389.       }
  390.  
  391.       int var5 = 0;
  392.       int var6 = 0;
  393.       if (this.image != null) {
  394.          var5 = this.image.width();
  395.          var6 = this.image.height();
  396.       }
  397.  
  398.       if (var6 < var2) {
  399.          var6 = 0;
  400.       } else {
  401.          var6 = var2 - var6;
  402.       }
  403.  
  404.       return new Size(var3 + var1 + var5, var4 + var2 + var6);
  405.    }
  406.  
  407.    public void describeClassInfo(ClassInfo var1) {
  408.       super.describeClassInfo(var1);
  409.       var1.addClass("netscape.application.Popup", 2);
  410.       var1.addField("popupList", (byte)18);
  411.       var1.addField("popupWindow", (byte)18);
  412.       var1.addField("container", (byte)18);
  413.       var1.addField("selectedItem", (byte)18);
  414.       var1.addField("target", (byte)18);
  415.       var1.addField("selectedImage", (byte)18);
  416.       var1.addField("enabled", (byte)0);
  417.    }
  418.  
  419.    public void encode(Encoder var1) throws CodingException {
  420.       super.encode(var1);
  421.       var1.encodeObject("popupList", this.popupList);
  422.       var1.encodeObject("popupWindow", this.popupWindow);
  423.       var1.encodeObject("container", this.container);
  424.       var1.encodeObject("selectedItem", this.selectedItem);
  425.       var1.encodeObject("target", this.target);
  426.       var1.encodeObject("selectedImage", this.image);
  427.       var1.encodeBoolean("enabled", this.enabled);
  428.    }
  429.  
  430.    public void decode(Decoder var1) throws CodingException {
  431.       super.decode(var1);
  432.       this.popupList = (ListView)var1.decodeObject("popupList");
  433.       this.popupWindow = (Window)var1.decodeObject("popupWindow");
  434.       this.container = (ContainerView)var1.decodeObject("container");
  435.       this.selectedItem = (ListItem)var1.decodeObject("selectedItem");
  436.       this.target = (Target)var1.decodeObject("target");
  437.       this.image = (Image)var1.decodeObject("selectedImage");
  438.       if (var1.versionForClassName("netscape.application.Popup") > 1) {
  439.          this.enabled = var1.decodeBoolean("enabled");
  440.       }
  441.  
  442.    }
  443.  
  444.    void _setupKeyboard() {
  445.       ((View)this).removeAllCommandsForKeys();
  446.       ((View)this).setCommandForKey(SELECT_NEXT_ITEM, 1005, 0);
  447.       ((View)this).setCommandForKey(SELECT_PREVIOUS_ITEM, 1004, 0);
  448.       ((View)this).setCommandForKey("popup", 10, 0);
  449.       ((View)this).setCommandForKey("popup", 32, 0);
  450.    }
  451.  
  452.    void selectNextItem(boolean var1) {
  453.       int var2 = this.selectedIndex();
  454.       int var3 = this.count();
  455.       if (var1 && var2 < var3 - 1) {
  456.          this.selectItemAt(var2 + 1);
  457.       } else if (!var1 && var2 > 0) {
  458.          this.selectItemAt(var2 - 1);
  459.       }
  460.  
  461.       if (this.selectedItem() != null) {
  462.          this.sendCommand();
  463.       }
  464.  
  465.    }
  466.  
  467.    public boolean canBecomeSelectedView() {
  468.       return true;
  469.    }
  470.  
  471.    void _setupKeyboardToClosePopup(boolean var1) {
  472.       if (var1) {
  473.          ((View)this).setCommandForKey("cancel", 27, 2);
  474.          ((View)this).setCommandForKey("commit", 10, 2);
  475.       } else {
  476.          ((View)this).removeCommandForKey(27);
  477.          ((View)this).setCommandForKey("popup", 10, 0);
  478.       }
  479.    }
  480.  
  481.    protected void ancestorWillRemoveFromViewHierarchy(View var1) {
  482.       super.ancestorWillRemoveFromViewHierarchy(var1);
  483.       this.hidePopupIfNeeded();
  484.    }
  485.  
  486.    public void hidePopupIfNeeded() {
  487.       if (this.showingPopupForKeyboard()) {
  488.          this.performCommand("cancel", this);
  489.       }
  490.  
  491.    }
  492.  
  493.    public boolean showingPopupForKeyboard() {
  494.       return this._showingPopupForKeyboard;
  495.    }
  496.  
  497.    public String formElementText() {
  498.       return this.selectedItem() != null ? this.selectedItem().title() : "";
  499.    }
  500.  
  501.    static {
  502.       SELECT_NEXT_ITEM = ListView.SELECT_NEXT_ITEM;
  503.       SELECT_PREVIOUS_ITEM = ListView.SELECT_PREVIOUS_ITEM;
  504.    }
  505. }
  506.