home *** CD-ROM | disk | FTP | other *** search
- package netscape.application;
-
- import netscape.util.ClassInfo;
- import netscape.util.Codable;
- import netscape.util.CodingException;
- import netscape.util.Decoder;
- import netscape.util.Encoder;
- import netscape.util.InconsistencyException;
- import netscape.util.Vector;
-
- public class Slider extends View implements Target, FormElement {
- Target target;
- Border border;
- Image backgroundImage;
- Image knobImage;
- Color backgroundColor;
- String command;
- int value;
- int minValue;
- int maxValue;
- int sliderX;
- int knobHeight;
- int grooveHeight;
- int clickOffset;
- int imageDisplayStyle;
- boolean enabled;
- int incrementResolution;
- static Vector _fieldDescription;
- private static final int SLIDER_KNOB_WIDTH = 6;
- static final String TARGET_KEY = "target";
- static final String BACKGROUND_IMAGE_KEY = "backgroundImage";
- static final String KNOB_IMAGE_KEY = "knobImage";
- static final String BACKGROUNDC_KEY = "backgroundColor";
- static final String COMMAND_KEY = "command";
- static final String VALUE_KEY = "value";
- static final String MINVALUE_KEY = "minValue";
- static final String MAXVALUE_KEY = "maxValue";
- static final String KNOB_HEIGHT_KEY = "knobHeight";
- static final String GROOVE_HEIGHT_KEY = "grooveHeight";
- static final String BORDER_KEY = "border";
- static final String ENABLED_KEY = "enabled";
- static final String IMAGEDISP_KEY = "imageDisplayStyle";
- static final String INC_RES_KEY = "incrementResolution";
- public static final String INCREASE_VALUE = "increaseValue";
- public static final String DECREASE_VALUE = "decreaseValue";
-
- public Slider() {
- this(0, 0, 0, 0);
- }
-
- public Slider(Rect var1) {
- this(var1.x, var1.y, var1.width, var1.height);
- }
-
- public Slider(int var1, int var2, int var3, int var4) {
- super(var1, var2, var3, var4);
- this.border = BezelBorder.loweredBezel();
- this.backgroundColor = Color.gray;
- this.enabled = true;
- this.knobHeight = 13;
- this.grooveHeight = 8;
- this.minValue = 0;
- this.maxValue = 255;
- this.setValue(0);
- this._setupKeyboard();
- this.incrementResolution = 20;
- }
-
- private static int parseInt(String var0) {
- try {
- return Integer.parseInt(var0);
- } catch (NumberFormatException var1) {
- return 0;
- }
- }
-
- public Size minSize() {
- return super._minSize != null ? new Size(super._minSize) : new Size(this.knobWidth() + 2, this.knobHeight > this.grooveHeight ? this.knobHeight : this.grooveHeight);
- }
-
- public void setLimits(int var1, int var2) {
- if (var1 < var2) {
- this.minValue = var1;
- this.maxValue = var2;
- if (this.value < var1) {
- this.value = var1;
- } else if (this.value > var2) {
- this.value = var2;
- }
-
- int var3 = this.value;
- if (this.value > 0) {
- --this.value;
- } else {
- ++this.value;
- }
-
- this.setValue(var3);
- }
- }
-
- public int minValue() {
- return this.minValue;
- }
-
- public int maxValue() {
- return this.maxValue;
- }
-
- public void setKnobImage(Image var1) {
- if (var1 != this.knobImage) {
- this.knobImage = var1;
- if (var1 != null) {
- this.knobHeight = var1.height();
- }
-
- this.setValue(this.value);
- }
- }
-
- public Image knobImage() {
- return this.knobImage;
- }
-
- public void setBackgroundColor(Color var1) {
- if (this.backgroundColor != null) {
- this.backgroundColor = var1;
- }
-
- }
-
- public Color backgroundColor() {
- return this.backgroundColor;
- }
-
- public void setImage(Image var1) {
- this.backgroundImage = var1;
- }
-
- public Image image() {
- return this.backgroundImage;
- }
-
- public void setImageDisplayStyle(int var1) {
- if (var1 != 0 && var1 != 2 && var1 != 1) {
- throw new InconsistencyException("Unknown image display style: " + var1);
- } else {
- this.imageDisplayStyle = var1;
- ((View)this).draw();
- }
- }
-
- public int imageDisplayStyle() {
- return this.imageDisplayStyle;
- }
-
- public void setTarget(Target var1) {
- this.target = var1;
- }
-
- public Target target() {
- return this.target;
- }
-
- public void setCommand(String var1) {
- this.command = var1;
- }
-
- public String command() {
- return this.command;
- }
-
- public void sendCommand() {
- if (this.target != null) {
- this.target.performCommand(this.command, this);
- }
-
- }
-
- public void setEnabled(boolean var1) {
- if (var1 != this.enabled) {
- this.enabled = var1;
- ((View)this).draw();
- }
-
- }
-
- public boolean isEnabled() {
- return this.enabled;
- }
-
- public int knobWidth() {
- return this.knobImage != null ? this.knobImage.width() : 6;
- }
-
- public void setKnobHeight(int var1) {
- if (var1 > 0) {
- this.knobHeight = var1;
- }
-
- }
-
- public int knobHeight() {
- return this.knobHeight;
- }
-
- public void setGrooveHeight(int var1) {
- if (var1 > 0) {
- this.grooveHeight = var1;
- }
-
- }
-
- public int grooveHeight() {
- return this.grooveHeight;
- }
-
- public void setBorder(Border var1) {
- if (var1 == null) {
- var1 = EmptyBorder.emptyBorder();
- }
-
- this.border = var1;
- this.setValue(this.value);
- }
-
- public Border border() {
- return this.border;
- }
-
- void redrawView(int var1) {
- if (this.sliderX != var1) {
- Rect var2;
- if (this.sliderX < var1) {
- var2 = Rect.newRect(this.sliderX, 0, var1 - this.sliderX + this.knobWidth(), super.bounds.height);
- } else {
- var2 = Rect.newRect(var1, 0, this.sliderX - var1 + this.knobWidth(), super.bounds.height);
- }
-
- ((View)this).draw(var2);
- Rect.returnRect(var2);
- }
- }
-
- void recomputeSliderPosition() {
- int var2 = this.value - this.minValue;
- float var1 = (float)(this.maxValue - this.minValue);
- this.sliderX = (int)((float)var2 / var1 * (float)(super.bounds.width - this.knobWidth()));
- }
-
- public void setValue(int var1) {
- if (var1 >= this.minValue && var1 <= this.maxValue) {
- if (this.value != var1) {
- this.value = var1;
- int var2 = this.sliderX;
- this.recomputeSliderPosition();
- this.redrawView(var2);
- }
-
- }
- }
-
- public int value() {
- return this.value;
- }
-
- public void drawViewGroove(Graphics var1) {
- int var3 = (super.bounds.height - this.grooveHeight) / 2;
- Rect var2 = Rect.newRect(0, var3, super.bounds.width, this.grooveHeight);
- this.border.drawInRect(var1, var2);
- this.border.computeInteriorRect(var2, var2);
- if (this.backgroundImage != null) {
- var1.pushState();
- var1.setClipRect(var2);
- this.backgroundImage.drawWithStyle(var1, var2, this.imageDisplayStyle);
- var1.popState();
- } else {
- if (!this.enabled) {
- var1.setColor(Color.lightGray);
- } else {
- var1.setColor(this.backgroundColor);
- }
-
- var1.fillRect(var2);
- }
-
- Rect.returnRect(var2);
- }
-
- public Rect knobRect() {
- int var1;
- if (this.knobImage != null) {
- var1 = (super.bounds.height - this.knobImage.height()) / 2;
- } else {
- var1 = (super.bounds.height - this.grooveHeight) / 2 - (this.knobHeight - this.grooveHeight) / 2;
- }
-
- return Rect.newRect(this.sliderX, var1, this.knobWidth(), this.knobHeight);
- }
-
- public void drawViewKnob(Graphics var1) {
- Rect var2 = this.knobRect();
- if (this.knobImage != null) {
- this.knobImage.drawAt(var1, var2.x, var2.y);
- } else {
- BezelBorder.raisedButtonBezel().drawInRect(var1, var2);
- var1.setColor(Color.lightGray);
- var1.fillRect(var2.x + 2, var2.y + 2, var2.width - 4, var2.height - 4);
- }
-
- Rect.returnRect(var2);
- }
-
- public void drawView(Graphics var1) {
- this.drawViewGroove(var1);
- this.drawViewKnob(var1);
- }
-
- int _positionFromPoint(int var1) {
- int var2 = this.knobWidth();
- var1 -= var2 / 2;
- if (var1 < 0) {
- var1 = 0;
- } else if (var1 > super.bounds.width - var2) {
- var1 = super.bounds.width - var2;
- }
-
- return var1;
- }
-
- void _moveSliderTo(int var1) {
- int var2 = this.sliderX;
- this.sliderX = this._positionFromPoint(var1);
- this.value = (int)((double)(this.maxValue - this.minValue) / (double)((float)(super.bounds.width - this.knobWidth())) * (double)this.sliderX) + this.minValue;
- this.redrawView(var2);
- }
-
- public void didSizeBy(int var1, int var2) {
- this.recomputeSliderPosition();
- super.didSizeBy(var1, var2);
- }
-
- public boolean mouseDown(MouseEvent var1) {
- if (!this.enabled) {
- return false;
- } else {
- Rect var2 = this.knobRect();
- if (var2.contains(var1.x, var1.y)) {
- this.clickOffset = this._positionFromPoint(var1.x) - this.sliderX;
- } else {
- this.clickOffset = 0;
- this._moveSliderTo(var1.x);
- }
-
- this.sendCommand();
- return true;
- }
- }
-
- public void mouseDragged(MouseEvent var1) {
- this._moveSliderTo(var1.x - this.clickOffset);
- this.sendCommand();
- }
-
- public void describeClassInfo(ClassInfo var1) {
- super.describeClassInfo(var1);
- var1.addClass("netscape.application.Slider", 2);
- var1.addField("target", (byte)18);
- var1.addField("backgroundImage", (byte)18);
- var1.addField("knobImage", (byte)18);
- var1.addField("backgroundColor", (byte)18);
- var1.addField("border", (byte)18);
- var1.addField("command", (byte)16);
- var1.addField("value", (byte)8);
- var1.addField("minValue", (byte)8);
- var1.addField("maxValue", (byte)8);
- var1.addField("knobHeight", (byte)8);
- var1.addField("grooveHeight", (byte)8);
- var1.addField("imageDisplayStyle", (byte)8);
- var1.addField("enabled", (byte)0);
- var1.addField("incrementResolution", (byte)8);
- }
-
- public void encode(Encoder var1) throws CodingException {
- super.encode(var1);
- var1.encodeObject("target", (Codable)this.target);
- var1.encodeObject("backgroundImage", this.backgroundImage);
- var1.encodeObject("knobImage", this.knobImage);
- var1.encodeObject("backgroundColor", this.backgroundColor);
- var1.encodeString("command", this.command);
- var1.encodeInt("value", this.value);
- var1.encodeInt("minValue", this.minValue);
- var1.encodeInt("maxValue", this.maxValue);
- var1.encodeInt("knobHeight", this.knobHeight);
- var1.encodeInt("grooveHeight", this.grooveHeight);
- if (this.border instanceof EmptyBorder) {
- var1.encodeObject("border", (Object)null);
- } else {
- var1.encodeObject("border", this.border);
- }
-
- var1.encodeBoolean("enabled", this.enabled);
- var1.encodeInt("imageDisplayStyle", this.imageDisplayStyle);
- var1.encodeInt("incrementResolution", this.incrementResolution);
- }
-
- public void decode(Decoder var1) throws CodingException {
- int var2 = var1.versionForClassName("netscape.application.Slider");
- super.decode(var1);
- this.target = (Target)var1.decodeObject("target");
- this.backgroundImage = (Image)var1.decodeObject("backgroundImage");
- this.knobImage = (Image)var1.decodeObject("knobImage");
- this.backgroundColor = (Color)var1.decodeObject("backgroundColor");
- this.command = var1.decodeString("command");
- this.value = var1.decodeInt("value");
- this.minValue = var1.decodeInt("minValue");
- this.maxValue = var1.decodeInt("maxValue");
- this.knobHeight = var1.decodeInt("knobHeight");
- this.grooveHeight = var1.decodeInt("grooveHeight");
- this.setBorder((Border)var1.decodeObject("border"));
- this.enabled = var1.decodeBoolean("enabled");
- this.imageDisplayStyle = var1.decodeInt("imageDisplayStyle");
- if (var2 > 1) {
- this.incrementResolution = var1.decodeInt("incrementResolution");
- } else {
- this.incrementResolution = 20;
- }
- }
-
- void _setupKeyboard() {
- ((View)this).removeAllCommandsForKeys();
- ((View)this).setCommandForKey("increaseValue", 1007, 0);
- ((View)this).setCommandForKey("decreaseValue", 1006, 0);
- ((View)this).setCommandForKey("increaseValue", 43, 0);
- ((View)this).setCommandForKey("decreaseValue", 45, 0);
- }
-
- public boolean canBecomeSelectedView() {
- return true;
- }
-
- public void performCommand(String var1, Object var2) {
- if ("increaseValue".equals(var1)) {
- int var4 = this.value() + (int)Math.rint((double)(this.maxValue - this.minValue) / (double)this.incrementResolution);
- if (var4 > this.maxValue) {
- var4 = this.maxValue;
- }
-
- this.setValue(var4);
- this.sendCommand();
- } else {
- if ("decreaseValue".equals(var1)) {
- int var3 = this.value() - (int)Math.rint((double)(this.maxValue - this.minValue) / (double)this.incrementResolution);
- if (var3 < this.minValue) {
- var3 = this.minValue;
- }
-
- this.setValue(var3);
- this.sendCommand();
- }
-
- }
- }
-
- public int incrementResolution() {
- return this.incrementResolution;
- }
-
- public void setIncrementResolution(int var1) {
- this.incrementResolution = var1;
- }
-
- public String formElementText() {
- return Integer.toString(this.value());
- }
- }
-