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

  1. package netscape.application;
  2.  
  3. import netscape.util.ClassInfo;
  4. import netscape.util.Codable;
  5. import netscape.util.CodingException;
  6. import netscape.util.Decoder;
  7. import netscape.util.Encoder;
  8. import netscape.util.InconsistencyException;
  9. import netscape.util.Vector;
  10.  
  11. public class Slider extends View implements Target, FormElement {
  12.    Target target;
  13.    Border border;
  14.    Image backgroundImage;
  15.    Image knobImage;
  16.    Color backgroundColor;
  17.    String command;
  18.    int value;
  19.    int minValue;
  20.    int maxValue;
  21.    int sliderX;
  22.    int knobHeight;
  23.    int grooveHeight;
  24.    int clickOffset;
  25.    int imageDisplayStyle;
  26.    boolean enabled;
  27.    int incrementResolution;
  28.    static Vector _fieldDescription;
  29.    private static final int SLIDER_KNOB_WIDTH = 6;
  30.    static final String TARGET_KEY = "target";
  31.    static final String BACKGROUND_IMAGE_KEY = "backgroundImage";
  32.    static final String KNOB_IMAGE_KEY = "knobImage";
  33.    static final String BACKGROUNDC_KEY = "backgroundColor";
  34.    static final String COMMAND_KEY = "command";
  35.    static final String VALUE_KEY = "value";
  36.    static final String MINVALUE_KEY = "minValue";
  37.    static final String MAXVALUE_KEY = "maxValue";
  38.    static final String KNOB_HEIGHT_KEY = "knobHeight";
  39.    static final String GROOVE_HEIGHT_KEY = "grooveHeight";
  40.    static final String BORDER_KEY = "border";
  41.    static final String ENABLED_KEY = "enabled";
  42.    static final String IMAGEDISP_KEY = "imageDisplayStyle";
  43.    static final String INC_RES_KEY = "incrementResolution";
  44.    public static final String INCREASE_VALUE = "increaseValue";
  45.    public static final String DECREASE_VALUE = "decreaseValue";
  46.  
  47.    public Slider() {
  48.       this(0, 0, 0, 0);
  49.    }
  50.  
  51.    public Slider(Rect var1) {
  52.       this(var1.x, var1.y, var1.width, var1.height);
  53.    }
  54.  
  55.    public Slider(int var1, int var2, int var3, int var4) {
  56.       super(var1, var2, var3, var4);
  57.       this.border = BezelBorder.loweredBezel();
  58.       this.backgroundColor = Color.gray;
  59.       this.enabled = true;
  60.       this.knobHeight = 13;
  61.       this.grooveHeight = 8;
  62.       this.minValue = 0;
  63.       this.maxValue = 255;
  64.       this.setValue(0);
  65.       this._setupKeyboard();
  66.       this.incrementResolution = 20;
  67.    }
  68.  
  69.    private static int parseInt(String var0) {
  70.       try {
  71.          return Integer.parseInt(var0);
  72.       } catch (NumberFormatException var1) {
  73.          return 0;
  74.       }
  75.    }
  76.  
  77.    public Size minSize() {
  78.       return super._minSize != null ? new Size(super._minSize) : new Size(this.knobWidth() + 2, this.knobHeight > this.grooveHeight ? this.knobHeight : this.grooveHeight);
  79.    }
  80.  
  81.    public void setLimits(int var1, int var2) {
  82.       if (var1 < var2) {
  83.          this.minValue = var1;
  84.          this.maxValue = var2;
  85.          if (this.value < var1) {
  86.             this.value = var1;
  87.          } else if (this.value > var2) {
  88.             this.value = var2;
  89.          }
  90.  
  91.          int var3 = this.value;
  92.          if (this.value > 0) {
  93.             --this.value;
  94.          } else {
  95.             ++this.value;
  96.          }
  97.  
  98.          this.setValue(var3);
  99.       }
  100.    }
  101.  
  102.    public int minValue() {
  103.       return this.minValue;
  104.    }
  105.  
  106.    public int maxValue() {
  107.       return this.maxValue;
  108.    }
  109.  
  110.    public void setKnobImage(Image var1) {
  111.       if (var1 != this.knobImage) {
  112.          this.knobImage = var1;
  113.          if (var1 != null) {
  114.             this.knobHeight = var1.height();
  115.          }
  116.  
  117.          this.setValue(this.value);
  118.       }
  119.    }
  120.  
  121.    public Image knobImage() {
  122.       return this.knobImage;
  123.    }
  124.  
  125.    public void setBackgroundColor(Color var1) {
  126.       if (this.backgroundColor != null) {
  127.          this.backgroundColor = var1;
  128.       }
  129.  
  130.    }
  131.  
  132.    public Color backgroundColor() {
  133.       return this.backgroundColor;
  134.    }
  135.  
  136.    public void setImage(Image var1) {
  137.       this.backgroundImage = var1;
  138.    }
  139.  
  140.    public Image image() {
  141.       return this.backgroundImage;
  142.    }
  143.  
  144.    public void setImageDisplayStyle(int var1) {
  145.       if (var1 != 0 && var1 != 2 && var1 != 1) {
  146.          throw new InconsistencyException("Unknown image display style: " + var1);
  147.       } else {
  148.          this.imageDisplayStyle = var1;
  149.          ((View)this).draw();
  150.       }
  151.    }
  152.  
  153.    public int imageDisplayStyle() {
  154.       return this.imageDisplayStyle;
  155.    }
  156.  
  157.    public void setTarget(Target var1) {
  158.       this.target = var1;
  159.    }
  160.  
  161.    public Target target() {
  162.       return this.target;
  163.    }
  164.  
  165.    public void setCommand(String var1) {
  166.       this.command = var1;
  167.    }
  168.  
  169.    public String command() {
  170.       return this.command;
  171.    }
  172.  
  173.    public void sendCommand() {
  174.       if (this.target != null) {
  175.          this.target.performCommand(this.command, this);
  176.       }
  177.  
  178.    }
  179.  
  180.    public void setEnabled(boolean var1) {
  181.       if (var1 != this.enabled) {
  182.          this.enabled = var1;
  183.          ((View)this).draw();
  184.       }
  185.  
  186.    }
  187.  
  188.    public boolean isEnabled() {
  189.       return this.enabled;
  190.    }
  191.  
  192.    public int knobWidth() {
  193.       return this.knobImage != null ? this.knobImage.width() : 6;
  194.    }
  195.  
  196.    public void setKnobHeight(int var1) {
  197.       if (var1 > 0) {
  198.          this.knobHeight = var1;
  199.       }
  200.  
  201.    }
  202.  
  203.    public int knobHeight() {
  204.       return this.knobHeight;
  205.    }
  206.  
  207.    public void setGrooveHeight(int var1) {
  208.       if (var1 > 0) {
  209.          this.grooveHeight = var1;
  210.       }
  211.  
  212.    }
  213.  
  214.    public int grooveHeight() {
  215.       return this.grooveHeight;
  216.    }
  217.  
  218.    public void setBorder(Border var1) {
  219.       if (var1 == null) {
  220.          var1 = EmptyBorder.emptyBorder();
  221.       }
  222.  
  223.       this.border = var1;
  224.       this.setValue(this.value);
  225.    }
  226.  
  227.    public Border border() {
  228.       return this.border;
  229.    }
  230.  
  231.    void redrawView(int var1) {
  232.       if (this.sliderX != var1) {
  233.          Rect var2;
  234.          if (this.sliderX < var1) {
  235.             var2 = Rect.newRect(this.sliderX, 0, var1 - this.sliderX + this.knobWidth(), super.bounds.height);
  236.          } else {
  237.             var2 = Rect.newRect(var1, 0, this.sliderX - var1 + this.knobWidth(), super.bounds.height);
  238.          }
  239.  
  240.          ((View)this).draw(var2);
  241.          Rect.returnRect(var2);
  242.       }
  243.    }
  244.  
  245.    void recomputeSliderPosition() {
  246.       int var2 = this.value - this.minValue;
  247.       float var1 = (float)(this.maxValue - this.minValue);
  248.       this.sliderX = (int)((float)var2 / var1 * (float)(super.bounds.width - this.knobWidth()));
  249.    }
  250.  
  251.    public void setValue(int var1) {
  252.       if (var1 >= this.minValue && var1 <= this.maxValue) {
  253.          if (this.value != var1) {
  254.             this.value = var1;
  255.             int var2 = this.sliderX;
  256.             this.recomputeSliderPosition();
  257.             this.redrawView(var2);
  258.          }
  259.  
  260.       }
  261.    }
  262.  
  263.    public int value() {
  264.       return this.value;
  265.    }
  266.  
  267.    public void drawViewGroove(Graphics var1) {
  268.       int var3 = (super.bounds.height - this.grooveHeight) / 2;
  269.       Rect var2 = Rect.newRect(0, var3, super.bounds.width, this.grooveHeight);
  270.       this.border.drawInRect(var1, var2);
  271.       this.border.computeInteriorRect(var2, var2);
  272.       if (this.backgroundImage != null) {
  273.          var1.pushState();
  274.          var1.setClipRect(var2);
  275.          this.backgroundImage.drawWithStyle(var1, var2, this.imageDisplayStyle);
  276.          var1.popState();
  277.       } else {
  278.          if (!this.enabled) {
  279.             var1.setColor(Color.lightGray);
  280.          } else {
  281.             var1.setColor(this.backgroundColor);
  282.          }
  283.  
  284.          var1.fillRect(var2);
  285.       }
  286.  
  287.       Rect.returnRect(var2);
  288.    }
  289.  
  290.    public Rect knobRect() {
  291.       int var1;
  292.       if (this.knobImage != null) {
  293.          var1 = (super.bounds.height - this.knobImage.height()) / 2;
  294.       } else {
  295.          var1 = (super.bounds.height - this.grooveHeight) / 2 - (this.knobHeight - this.grooveHeight) / 2;
  296.       }
  297.  
  298.       return Rect.newRect(this.sliderX, var1, this.knobWidth(), this.knobHeight);
  299.    }
  300.  
  301.    public void drawViewKnob(Graphics var1) {
  302.       Rect var2 = this.knobRect();
  303.       if (this.knobImage != null) {
  304.          this.knobImage.drawAt(var1, var2.x, var2.y);
  305.       } else {
  306.          BezelBorder.raisedButtonBezel().drawInRect(var1, var2);
  307.          var1.setColor(Color.lightGray);
  308.          var1.fillRect(var2.x + 2, var2.y + 2, var2.width - 4, var2.height - 4);
  309.       }
  310.  
  311.       Rect.returnRect(var2);
  312.    }
  313.  
  314.    public void drawView(Graphics var1) {
  315.       this.drawViewGroove(var1);
  316.       this.drawViewKnob(var1);
  317.    }
  318.  
  319.    int _positionFromPoint(int var1) {
  320.       int var2 = this.knobWidth();
  321.       var1 -= var2 / 2;
  322.       if (var1 < 0) {
  323.          var1 = 0;
  324.       } else if (var1 > super.bounds.width - var2) {
  325.          var1 = super.bounds.width - var2;
  326.       }
  327.  
  328.       return var1;
  329.    }
  330.  
  331.    void _moveSliderTo(int var1) {
  332.       int var2 = this.sliderX;
  333.       this.sliderX = this._positionFromPoint(var1);
  334.       this.value = (int)((double)(this.maxValue - this.minValue) / (double)((float)(super.bounds.width - this.knobWidth())) * (double)this.sliderX) + this.minValue;
  335.       this.redrawView(var2);
  336.    }
  337.  
  338.    public void didSizeBy(int var1, int var2) {
  339.       this.recomputeSliderPosition();
  340.       super.didSizeBy(var1, var2);
  341.    }
  342.  
  343.    public boolean mouseDown(MouseEvent var1) {
  344.       if (!this.enabled) {
  345.          return false;
  346.       } else {
  347.          Rect var2 = this.knobRect();
  348.          if (var2.contains(var1.x, var1.y)) {
  349.             this.clickOffset = this._positionFromPoint(var1.x) - this.sliderX;
  350.          } else {
  351.             this.clickOffset = 0;
  352.             this._moveSliderTo(var1.x);
  353.          }
  354.  
  355.          this.sendCommand();
  356.          return true;
  357.       }
  358.    }
  359.  
  360.    public void mouseDragged(MouseEvent var1) {
  361.       this._moveSliderTo(var1.x - this.clickOffset);
  362.       this.sendCommand();
  363.    }
  364.  
  365.    public void describeClassInfo(ClassInfo var1) {
  366.       super.describeClassInfo(var1);
  367.       var1.addClass("netscape.application.Slider", 2);
  368.       var1.addField("target", (byte)18);
  369.       var1.addField("backgroundImage", (byte)18);
  370.       var1.addField("knobImage", (byte)18);
  371.       var1.addField("backgroundColor", (byte)18);
  372.       var1.addField("border", (byte)18);
  373.       var1.addField("command", (byte)16);
  374.       var1.addField("value", (byte)8);
  375.       var1.addField("minValue", (byte)8);
  376.       var1.addField("maxValue", (byte)8);
  377.       var1.addField("knobHeight", (byte)8);
  378.       var1.addField("grooveHeight", (byte)8);
  379.       var1.addField("imageDisplayStyle", (byte)8);
  380.       var1.addField("enabled", (byte)0);
  381.       var1.addField("incrementResolution", (byte)8);
  382.    }
  383.  
  384.    public void encode(Encoder var1) throws CodingException {
  385.       super.encode(var1);
  386.       var1.encodeObject("target", (Codable)this.target);
  387.       var1.encodeObject("backgroundImage", this.backgroundImage);
  388.       var1.encodeObject("knobImage", this.knobImage);
  389.       var1.encodeObject("backgroundColor", this.backgroundColor);
  390.       var1.encodeString("command", this.command);
  391.       var1.encodeInt("value", this.value);
  392.       var1.encodeInt("minValue", this.minValue);
  393.       var1.encodeInt("maxValue", this.maxValue);
  394.       var1.encodeInt("knobHeight", this.knobHeight);
  395.       var1.encodeInt("grooveHeight", this.grooveHeight);
  396.       if (this.border instanceof EmptyBorder) {
  397.          var1.encodeObject("border", (Object)null);
  398.       } else {
  399.          var1.encodeObject("border", this.border);
  400.       }
  401.  
  402.       var1.encodeBoolean("enabled", this.enabled);
  403.       var1.encodeInt("imageDisplayStyle", this.imageDisplayStyle);
  404.       var1.encodeInt("incrementResolution", this.incrementResolution);
  405.    }
  406.  
  407.    public void decode(Decoder var1) throws CodingException {
  408.       int var2 = var1.versionForClassName("netscape.application.Slider");
  409.       super.decode(var1);
  410.       this.target = (Target)var1.decodeObject("target");
  411.       this.backgroundImage = (Image)var1.decodeObject("backgroundImage");
  412.       this.knobImage = (Image)var1.decodeObject("knobImage");
  413.       this.backgroundColor = (Color)var1.decodeObject("backgroundColor");
  414.       this.command = var1.decodeString("command");
  415.       this.value = var1.decodeInt("value");
  416.       this.minValue = var1.decodeInt("minValue");
  417.       this.maxValue = var1.decodeInt("maxValue");
  418.       this.knobHeight = var1.decodeInt("knobHeight");
  419.       this.grooveHeight = var1.decodeInt("grooveHeight");
  420.       this.setBorder((Border)var1.decodeObject("border"));
  421.       this.enabled = var1.decodeBoolean("enabled");
  422.       this.imageDisplayStyle = var1.decodeInt("imageDisplayStyle");
  423.       if (var2 > 1) {
  424.          this.incrementResolution = var1.decodeInt("incrementResolution");
  425.       } else {
  426.          this.incrementResolution = 20;
  427.       }
  428.    }
  429.  
  430.    void _setupKeyboard() {
  431.       ((View)this).removeAllCommandsForKeys();
  432.       ((View)this).setCommandForKey("increaseValue", 1007, 0);
  433.       ((View)this).setCommandForKey("decreaseValue", 1006, 0);
  434.       ((View)this).setCommandForKey("increaseValue", 43, 0);
  435.       ((View)this).setCommandForKey("decreaseValue", 45, 0);
  436.    }
  437.  
  438.    public boolean canBecomeSelectedView() {
  439.       return true;
  440.    }
  441.  
  442.    public void performCommand(String var1, Object var2) {
  443.       if ("increaseValue".equals(var1)) {
  444.          int var4 = this.value() + (int)Math.rint((double)(this.maxValue - this.minValue) / (double)this.incrementResolution);
  445.          if (var4 > this.maxValue) {
  446.             var4 = this.maxValue;
  447.          }
  448.  
  449.          this.setValue(var4);
  450.          this.sendCommand();
  451.       } else {
  452.          if ("decreaseValue".equals(var1)) {
  453.             int var3 = this.value() - (int)Math.rint((double)(this.maxValue - this.minValue) / (double)this.incrementResolution);
  454.             if (var3 < this.minValue) {
  455.                var3 = this.minValue;
  456.             }
  457.  
  458.             this.setValue(var3);
  459.             this.sendCommand();
  460.          }
  461.  
  462.       }
  463.    }
  464.  
  465.    public int incrementResolution() {
  466.       return this.incrementResolution;
  467.    }
  468.  
  469.    public void setIncrementResolution(int var1) {
  470.       this.incrementResolution = var1;
  471.    }
  472.  
  473.    public String formElementText() {
  474.       return Integer.toString(this.value());
  475.    }
  476. }
  477.