home *** CD-ROM | disk | FTP | other *** search
- package netscape.application;
-
- import netscape.util.ClassInfo;
- import netscape.util.CodingException;
- import netscape.util.Decoder;
- import netscape.util.Encoder;
- import netscape.util.Vector;
-
- public class ListView extends View implements Target, FormElement {
- ListItem protoItem;
- ListItem anchorItem;
- ListItem origSelectedItem;
- Vector items;
- Vector selectedItems;
- Vector dirtyItems;
- String command;
- String doubleCommand;
- Target target;
- Color backgroundColor;
- int rowHeight;
- boolean allowsMultipleSelection;
- boolean allowsEmptySelection;
- boolean tracksMouseOutsideBounds;
- boolean tracking;
- boolean enabled;
- boolean transparent;
- static final String PROTOITEM_KEY = "protoItem";
- static final String ANCHORITEM_KEY = "anchorItem";
- static final String ORIGSELECTED_KEY = "origSelectedItem";
- static final String ITEMS_KEY = "items";
- static final String SELECTEDITEMS_KEY = "selectedItems";
- static final String DIRTYITEMS_KEY = "dirtyItems";
- static final String COMMAND_KEY = "command";
- static final String DOUBLECOMMAND_KEY = "doubleCommand";
- static final String TARGET_KEY = "target";
- static final String BACKGROUNDCOLOR_KEY = "backgroundColor";
- static final String ROWHEIGHT_KEY = "rowHeight";
- static final String MULTSELECTION_KEY = "allowsMultipleSelection";
- static final String EMPTYSELECTION_KEY = "allowsEmptySelection";
- static final String TRACKSMOUSE_KEY = "tracksMouseOutsideBounds";
- static final String ENABLED_KEY = "enabled";
- static final String TRANSPARENT_KEY = "isTransparent";
- public static String SELECT_NEXT_ITEM = "selectNext";
- public static String SELECT_PREVIOUS_ITEM = "selectPrevious";
-
- public ListView() {
- this(0, 0, 0, 0);
- }
-
- public ListView(Rect var1) {
- this(var1.x, var1.y, var1.width, var1.height);
- }
-
- public ListView(int var1, int var2, int var3, int var4) {
- super(var1, var2, var3, var4);
- this.items = new Vector();
- this.selectedItems = new Vector();
- this.dirtyItems = new Vector();
- this.backgroundColor = Color.lightGray;
- this.rowHeight = 17;
- this.tracksMouseOutsideBounds = true;
- this.enabled = true;
- this.transparent = false;
- this._setupKeyboard();
- }
-
- public void setPrototypeItem(ListItem var1) {
- if (var1 == null) {
- this.protoItem = new ListItem();
- } else {
- this.protoItem = var1;
- }
-
- if (this.protoItem.font() == null) {
- this.protoItem.setFont(Font.fontNamed("Default"));
- }
-
- this.protoItem.setListView(this);
- }
-
- public ListItem prototypeItem() {
- if (this.protoItem == null) {
- this.setPrototypeItem((ListItem)null);
- }
-
- return this.protoItem;
- }
-
- public void setBackgroundColor(Color var1) {
- this.backgroundColor = var1;
- }
-
- public Color backgroundColor() {
- return this.backgroundColor;
- }
-
- public void setTransparent(boolean var1) {
- this.transparent = var1;
- }
-
- public boolean isTransparent() {
- return this.transparent;
- }
-
- public boolean wantsAutoscrollEvents() {
- return true;
- }
-
- public void setAllowsMultipleSelection(boolean var1) {
- this.allowsMultipleSelection = var1;
- }
-
- public boolean allowsMultipleSelection() {
- return this.allowsMultipleSelection;
- }
-
- public void setAllowsEmptySelection(boolean var1) {
- this.allowsEmptySelection = var1;
- }
-
- public boolean allowsEmptySelection() {
- return this.allowsEmptySelection;
- }
-
- public void setTracksMouseOutsideBounds(boolean var1) {
- this.tracksMouseOutsideBounds = var1;
- }
-
- public boolean tracksMouseOutsideBounds() {
- return this.tracksMouseOutsideBounds;
- }
-
- public void setEnabled(boolean var1) {
- if (this.enabled != var1) {
- this.enabled = var1;
- ((View)this).setDirty(true);
- }
-
- }
-
- public boolean isEnabled() {
- return this.enabled;
- }
-
- public void setRowHeight(int var1) {
- if (var1 > 0) {
- this.rowHeight = var1;
- } else {
- this.rowHeight = this.minItemHeight();
- }
- }
-
- public int rowHeight() {
- if (this.rowHeight > 0) {
- return this.rowHeight;
- } else {
- this.setRowHeight(this.minItemHeight());
- return this.rowHeight;
- }
- }
-
- public int minItemHeight() {
- int var3 = 0;
- int var2 = this.items.size();
-
- for(int var1 = 0; var1 < var2; ++var1) {
- ListItem var5 = (ListItem)this.items.elementAt(var1);
- int var4 = var5.minHeight();
- if (var4 > var3) {
- var3 = var4;
- }
- }
-
- return var3;
- }
-
- public int minItemWidth() {
- int var3 = 0;
- int var2 = this.items.size();
-
- for(int var1 = 0; var1 < var2; ++var1) {
- ListItem var5 = (ListItem)this.items.elementAt(var1);
- int var4 = var5.minWidth();
- if (var4 > var3) {
- var3 = var4;
- }
- }
-
- return var3;
- }
-
- public Size minSize() {
- return new Size(super.bounds.width, this.count() * this.rowHeight());
- }
-
- public ListItem itemForPoint(int var1, int var2) {
- int var4 = this.items.size();
- if (this.rowHeight == 0) {
- return null;
- } else if (var4 == 0) {
- return null;
- } else if (!this.tracksMouseOutsideBounds && !Rect.contains(0, 0, ((View)this).width(), ((View)this).height(), var1, var2)) {
- return null;
- } else {
- int var3 = var2 / this.rowHeight;
- if (var3 < 0) {
- var3 = 0;
- } else if (var3 >= var4) {
- var3 = var4 - 1;
- }
-
- return this.itemAt(var3);
- }
- }
-
- public ListItem itemAt(int var1) {
- return (ListItem)this.items.elementAt(var1);
- }
-
- public int indexOfItem(ListItem var1) {
- return this.items.indexOf(var1);
- }
-
- public Rect rectForItem(ListItem var1) {
- return var1 == null ? null : this.rectForItemAt(this.items.indexOf(var1));
- }
-
- public Rect rectForItemAt(int var1) {
- return var1 >= 0 && var1 < this.items.size() ? new Rect(0, this.rowHeight * var1, super.bounds.width, this.rowHeight) : null;
- }
-
- public boolean multipleItemsSelected() {
- return this.selectedItems.size() > 1;
- }
-
- public int selectedIndex() {
- ListItem var1 = this.selectedItem();
- return var1 == null ? -1 : this.items.indexOf(var1);
- }
-
- public ListItem selectedItem() {
- return this.selectedItems.size() > 0 ? (ListItem)this.selectedItems.elementAt(0) : null;
- }
-
- public Vector selectedItems() {
- return this.selectedItems;
- }
-
- public int count() {
- return this.items.size();
- }
-
- public ListItem addItem() {
- return this.insertItemAt(this.items.size());
- }
-
- public ListItem addItem(ListItem var1) {
- if (var1.font() == null) {
- var1.setFont(Font.defaultFont());
- }
-
- var1.setListView(this);
- return this.insertItemAt(var1, this.items.size());
- }
-
- public ListItem insertItemAt(ListItem var1, int var2) {
- if (var1.font() == null) {
- var1.setFont(Font.defaultFont());
- }
-
- var1.setListView(this);
- this.items.insertElementAt(var1, var2);
- if (!this.allowsEmptySelection && this.selectedItems.isEmpty()) {
- this.selectItem(var1);
- }
-
- return var1;
- }
-
- public ListItem insertItemAt(int var1) {
- ListItem var2 = (ListItem)this.prototypeItem().clone();
- this.items.insertElementAt(var2, var1);
- if (!this.allowsEmptySelection && this.selectedItems.isEmpty()) {
- this.selectItem(var2);
- }
-
- return var2;
- }
-
- public void removeItemAt(int var1) {
- ListItem var2 = (ListItem)this.items.elementAt(var1);
- this.items.removeElementAt(var1);
- this.selectedItems.removeElement(var2);
- if (!this.allowsEmptySelection && this.selectedItems.size() == 0 && this.items.size() > 0) {
- --var1;
- if (var1 < 0) {
- var1 = 0;
- }
-
- this.selectItem(this.itemAt(var1));
- }
-
- }
-
- public void removeItem(ListItem var1) {
- this.removeItemAt(this.items.indexOf(var1));
- }
-
- public void removeAllItems() {
- this.items.removeAllElements();
- this.selectItem((ListItem)null);
- }
-
- public void selectItem(ListItem var1) {
- if (var1 != null && !var1.isEnabled()) {
- var1 = null;
- }
-
- if (var1 == null) {
- int var3 = this.selectedItems.size();
- ListItem var5;
- if (var3 > 0 && !this.allowsEmptySelection) {
- var5 = (ListItem)this.selectedItems.elementAt(var3 - 1);
- this.selectedItems.removeElementAt(var3 - 1);
- --var3;
- } else {
- var5 = null;
- }
-
- for(int var2 = 0; var2 < var3; ++var2) {
- ListItem var4 = (ListItem)this.selectedItems.elementAt(var2);
- var4.setSelected(false);
- this.markDirty(var4);
- }
-
- this.selectedItems.removeAllElements();
- if (var5 != null) {
- this.selectedItems.addElement(var5);
- }
- } else if (!this.selectedItems.contains(var1)) {
- if (!this.allowsMultipleSelection) {
- ListItem var6 = this.selectedItem();
- if (var6 != null) {
- var6.setSelected(false);
- this.selectedItems.removeElement(var6);
- this.markDirty(var6);
- }
- }
-
- var1.setSelected(true);
- this.selectedItems.addElement(var1);
- this.markDirty(var1);
- }
-
- this.drawDirtyItems();
- }
-
- public void selectItemAt(int var1) {
- this.selectItem((ListItem)this.items.elementAt(var1));
- }
-
- public void selectOnly(ListItem var1) {
- boolean var5 = false;
- if (var1.isEnabled()) {
- int var3 = this.selectedItems.size();
- if (var3 != 1 || var1 != this.selectedItems.elementAt(0)) {
- for(int var2 = 0; var2 < var3; ++var2) {
- ListItem var4 = (ListItem)this.selectedItems.elementAt(var2);
- if (var4 != var1) {
- var4.setSelected(false);
- this.markDirty(var4);
- } else {
- var5 = true;
- }
- }
-
- this.selectedItems.removeAllElements();
- this.selectedItems.addElement(var1);
- if (!var5) {
- var1.setSelected(true);
- this.markDirty(var1);
- }
-
- this.drawDirtyItems();
- }
- }
- }
-
- public void deselectItem(ListItem var1) {
- if (var1 != null && (this.selectedItems.size() != 1 || this.allowsEmptySelection)) {
- if (this.items.contains(var1) && this.selectedItems.contains(var1)) {
- this.selectedItems.removeElement(var1);
- var1.setSelected(false);
- this.markDirty(var1);
- this.drawDirtyItems();
- }
- }
- }
-
- public void scrollItemAtToVisible(int var1) {
- ((View)this).scrollRectToVisible(this.rectForItemAt(var1));
- }
-
- public void scrollItemToVisible(ListItem var1) {
- ((View)this).scrollRectToVisible(this.rectForItem(var1));
- }
-
- public boolean mouseDown(MouseEvent var1) {
- if (!this.enabled) {
- return false;
- } else {
- this.tracking = true;
- this.origSelectedItem = this.selectedItem();
- ListItem var2 = this.itemForPoint(var1.x, var1.y);
- if (var2 == null) {
- return true;
- } else {
- if (this.anchorItem != var2 && var1.clickCount > 1) {
- var1.setClickCount(1);
- }
-
- this.selectOnly(var2);
- this.anchorItem = var2;
- if (var1.clickCount == 2) {
- this.sendDoubleCommand();
- return false;
- } else {
- return true;
- }
- }
- }
- }
-
- public void mouseDragged(MouseEvent var1) {
- if (this.tracking) {
- ((View)this).disableDrawing();
- ListItem var2 = this.itemForPoint(var1.x, var1.y);
- if (!this.tracksMouseOutsideBounds && !Rect.contains(0, 0, ((View)this).width(), ((View)this).height(), var1.x, var1.y)) {
- var2 = null;
- }
-
- if (!this.allowsMultipleSelection) {
- if (var2 != this.selectedItem() && (this.allowsEmptySelection || var2 != null)) {
- this.anchorItem = var2;
- this.selectItem(var2);
- }
- } else {
- int var7 = this.items.indexOf(this.anchorItem);
- int var8;
- if (var2 != null) {
- var8 = this.items.indexOf(var2);
- } else if (this.pointCompare(0, ((View)this).height(), var1.y) < 0) {
- var8 = 0;
- } else {
- var8 = this.items.size() - 1;
- }
-
- int var5;
- int var6;
- if (var7 < var8) {
- var5 = var7;
- var6 = var8;
- } else {
- var5 = var8;
- var6 = var7;
- }
-
- int var4 = this.selectedItems.size();
-
- while(var4-- > 0) {
- ListItem var3 = (ListItem)this.selectedItems.elementAt(var4);
- int var9 = this.items.indexOf(var3);
- if (!var3.isSelected() || var9 >= var5 && var9 <= var6) {
- if (!var3.isSelected() && var9 >= var5 && var9 <= var6) {
- this.selectItem(var3);
- }
- } else {
- this.deselectItem(var3);
- }
- }
-
- if (var5 != -1 && var6 != -1) {
- for(int var11 = var5; var11 <= var6; ++var11) {
- ListItem var10 = (ListItem)this.items.elementAt(var11);
- if (!var10.isSelected()) {
- this.selectItem(var10);
- }
- }
- }
- }
-
- ((View)this).reenableDrawing();
- this.autoscroll(var1);
- }
- }
-
- public void mouseUp(MouseEvent var1) {
- if (var1.clickCount == 1) {
- this.sendCommand();
- }
-
- this.tracking = false;
- }
-
- private int pointCompare(int var1, int var2, int var3) {
- if (var3 < var1) {
- return -1;
- } else {
- return var3 >= var1 + var2 ? 1 : 0;
- }
- }
-
- private int rectCompare(Rect var1, Rect var2) {
- if (var2.maxY() <= var1.y) {
- return -1;
- } else {
- return var2.y >= var1.maxY() ? 1 : 0;
- }
- }
-
- private void autoscroll(MouseEvent var1) {
- Rect var2 = Rect.newRect();
- ((View)this).computeVisibleRect(var2);
- this.drawDirtyItems();
- if (!var2.contains(var1.x, var1.y)) {
- if (var1.y < var2.y) {
- Rect var3 = Rect.newRect(var2.x, var1.y, var2.width, this.rowHeight);
- ((View)this).scrollRectToVisible(var3);
- Rect.returnRect(var3);
- } else if (var1.y > var2.maxY()) {
- Rect var4 = Rect.newRect(var2.x, var1.y - this.rowHeight, var2.width, this.rowHeight);
- ((View)this).scrollRectToVisible(var4);
- Rect.returnRect(var4);
- }
- }
-
- Rect.returnRect(var2);
- }
-
- private void markDirty(ListItem var1) {
- if (!this.dirtyItems.contains(var1)) {
- this.dirtyItems.addElement(var1);
- }
- }
-
- private void drawDirtyItems() {
- if (((View)this).canDraw()) {
- int var2 = this.dirtyItems.size();
- if (var2 != 0) {
- Vector var5 = this.dirtyItems;
- this.dirtyItems = null;
- Rect var6 = Rect.newRect(0, 0, super.bounds.width, this.rowHeight);
- int var4 = this.count();
-
- for(int var1 = 0; var1 < var2; ++var1) {
- int var3 = this.items.indexOf(var5.elementAt(var1));
- if (var3 >= 0 && var3 < var4) {
- var6.y = var3 * this.rowHeight;
- var6.height = this.rowHeight;
- if (var1 < var2 - 1) {
- int var7 = this.items.indexOf(var5.elementAt(var1 + 1));
- if (var7 == var3 + 1) {
- var6.height += this.rowHeight;
- ++var1;
- } else if (var7 == var3 - 1) {
- var6.height += this.rowHeight;
- var6.y -= this.rowHeight;
- ++var1;
- }
- }
-
- ((View)this).draw(var6);
- }
- }
-
- Rect.returnRect(var6);
- this.dirtyItems = var5;
- this.dirtyItems.removeAllElements();
- }
- }
- }
-
- public void drawItemAt(int var1) {
- Rect var2 = this.rectForItemAt(var1);
- ((View)this).draw(var2);
- }
-
- public void drawViewBackground(Graphics var1, int var2, int var3, int var4, int var5) {
- if (!this.isTransparent() && this.backgroundColor != null) {
- var1.setColor(this.backgroundColor);
- var1.fillRect(var2, var3, var4, var5);
- }
-
- }
-
- public void drawView(Graphics var1) {
- if (this.dirtyItems != null) {
- this.dirtyItems.removeAllElements();
- }
-
- if (this.rowHeight <= 0) {
- this.drawViewBackground(var1, 0, 0, super.bounds.width, super.bounds.height);
- } else {
- Rect var4 = Rect.newRect(var1.clipRect());
- int var5 = this.items.size();
- int var6 = var4.y / this.rowHeight;
- if (var6 >= 0 && var6 < var5) {
- Rect var3 = this.rectForItemAt(var6);
-
- boolean var7;
- for(var7 = this.rectCompare(var4, var3) == 0; var6 < var5 && var7; var7 = this.rectCompare(var4, var3) == 0) {
- var1.pushState();
- var1.setClipRect(var3);
- ListItem var2 = (ListItem)this.items.elementAt(var6);
- if (!this.isTransparent() && var2.isTransparent()) {
- this.drawViewBackground(var1, var3.x, var3.y, super.bounds.width, this.rowHeight);
- }
-
- var2.drawInRect(var1, var3);
- var1.popState();
- ++var6;
- var3.moveBy(0, this.rowHeight);
- }
-
- if (var7) {
- int var8 = super.bounds.height - var3.y;
- if (var8 > 0 && !this.isTransparent()) {
- this.drawViewBackground(var1, var3.x, var3.y, super.bounds.width, var8);
- }
- }
-
- Rect.returnRect(var4);
- } else {
- this.drawViewBackground(var1, 0, 0, super.bounds.width, super.bounds.height);
- Rect.returnRect(var4);
- }
- }
- }
-
- 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 setDoubleCommand(String var1) {
- this.doubleCommand = var1;
- }
-
- public String doubleCommand() {
- return this.doubleCommand;
- }
-
- public void sendCommand() {
- if (this.target != null) {
- String var1 = null;
- ListItem var2 = this.selectedItem();
- if (var2 != null) {
- var1 = var2.command();
- }
-
- if (var1 == null) {
- var1 = this.command;
- }
-
- this.target.performCommand(var1, this);
- }
-
- }
-
- public void sendDoubleCommand() {
- if (this.target != null && this.doubleCommand != null) {
- this.target.performCommand(this.doubleCommand, this);
- }
-
- }
-
- public void performCommand(String var1, Object var2) {
- if (SELECT_NEXT_ITEM.equals(var1)) {
- this.selectNextItem(true);
- } else {
- if (SELECT_PREVIOUS_ITEM.equals(var1)) {
- this.selectNextItem(false);
- }
-
- }
- }
-
- void selectNextItem(boolean var1) {
- int var2 = this.selectedIndex();
- int var3 = this.count();
- if (var1) {
- if (var2 < var3 - 1) {
- this.selectItemAt(var2 + 1);
- this.sendCommand();
- return;
- }
- } else if (var2 > 0) {
- this.selectItemAt(var2 - 1);
- this.sendCommand();
- }
-
- }
-
- public void describeClassInfo(ClassInfo var1) {
- super.describeClassInfo(var1);
- var1.addClass("netscape.application.ListView", 2);
- var1.addField("protoItem", (byte)18);
- var1.addField("anchorItem", (byte)18);
- var1.addField("origSelectedItem", (byte)18);
- var1.addField("items", (byte)18);
- var1.addField("selectedItems", (byte)18);
- var1.addField("dirtyItems", (byte)18);
- var1.addField("command", (byte)16);
- var1.addField("doubleCommand", (byte)16);
- var1.addField("target", (byte)18);
- var1.addField("backgroundColor", (byte)18);
- var1.addField("rowHeight", (byte)8);
- var1.addField("allowsMultipleSelection", (byte)0);
- var1.addField("allowsEmptySelection", (byte)0);
- var1.addField("tracksMouseOutsideBounds", (byte)0);
- var1.addField("enabled", (byte)0);
- var1.addField("isTransparent", (byte)0);
- }
-
- public void encode(Encoder var1) throws CodingException {
- super.encode(var1);
- var1.encodeObject("protoItem", this.protoItem);
- var1.encodeObject("anchorItem", this.anchorItem);
- var1.encodeObject("origSelectedItem", this.origSelectedItem);
- var1.encodeObject("items", this.items);
- var1.encodeObject("selectedItems", this.selectedItems);
- var1.encodeObject("dirtyItems", this.dirtyItems);
- var1.encodeString("command", this.command);
- var1.encodeString("doubleCommand", this.doubleCommand);
- var1.encodeObject("target", this.target);
- var1.encodeObject("backgroundColor", this.backgroundColor);
- var1.encodeInt("rowHeight", this.rowHeight);
- var1.encodeBoolean("allowsMultipleSelection", this.allowsMultipleSelection);
- var1.encodeBoolean("allowsEmptySelection", this.allowsEmptySelection);
- var1.encodeBoolean("tracksMouseOutsideBounds", this.tracksMouseOutsideBounds);
- var1.encodeBoolean("enabled", this.enabled);
- var1.encodeBoolean("isTransparent", this.transparent);
- }
-
- public void decode(Decoder var1) throws CodingException {
- super.decode(var1);
- this.protoItem = (ListItem)var1.decodeObject("protoItem");
- this.anchorItem = (ListItem)var1.decodeObject("anchorItem");
- this.origSelectedItem = (ListItem)var1.decodeObject("origSelectedItem");
- this.items = (Vector)var1.decodeObject("items");
- this.selectedItems = (Vector)var1.decodeObject("selectedItems");
- this.dirtyItems = (Vector)var1.decodeObject("dirtyItems");
- this.command = var1.decodeString("command");
- this.doubleCommand = var1.decodeString("doubleCommand");
- this.target = (Target)var1.decodeObject("target");
- this.backgroundColor = (Color)var1.decodeObject("backgroundColor");
- this.rowHeight = var1.decodeInt("rowHeight");
- this.allowsMultipleSelection = var1.decodeBoolean("allowsMultipleSelection");
- this.allowsEmptySelection = var1.decodeBoolean("allowsEmptySelection");
- this.tracksMouseOutsideBounds = var1.decodeBoolean("tracksMouseOutsideBounds");
- this.enabled = var1.decodeBoolean("enabled");
- if (var1.versionForClassName("netscape.application.ListView") > 1) {
- this.transparent = var1.decodeBoolean("isTransparent");
- } else {
- this.transparent = false;
- }
- }
-
- void _setupKeyboard() {
- ((View)this).removeAllCommandsForKeys();
- ((View)this).setCommandForKey(SELECT_NEXT_ITEM, 1005, 0);
- ((View)this).setCommandForKey(SELECT_PREVIOUS_ITEM, 1004, 0);
- }
-
- public boolean canBecomeSelectedView() {
- return this.isEnabled();
- }
-
- public String formElementText() {
- return this.selectedItem() != null ? this.selectedItem().title() : "";
- }
- }
-