001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: StackView.java,v $ 023 Revision 1.5 2004/05/12 19:01:33 markl 024 comment block updates 025 026 Revision 1.4 2003/01/19 09:50:54 markl 027 Javadoc & comment header updates. 028 029 Revision 1.3 2001/03/12 09:28:00 markl 030 Source code and Javadoc cleanup. 031 032 Revision 1.2 1999/01/10 03:00:07 markl 033 added GPL header & RCS tag 034 ---------------------------------------------------------------------------- 035*/ 036 037package kiwi.ui; 038 039import java.util.*; 040import javax.swing.*; 041import javax.swing.event.*; 042 043import kiwi.ui.model.*; 044 045/** A component that displays the contents of a <code>Stack</code> data 046 * structure. This is an MVC class that uses a <code>StackModel</code> as its 047 * data model. 048 * 049 * @see kiwi.ui.model.StackModel 050 * @see java.util.Stack 051 * 052 * @author Mark Lindner 053 */ 054 055public class StackView extends JList 056 { 057 private StackModel model; 058 059 /** Construct a new <code>StackView</code> with a default stack model. 060 */ 061 062 public StackView() 063 { 064 this(new DefaultStackModel()); 065 } 066 067 /** Construct a new <code>StackView</code> with the given stack model. 068 * 069 * @param model The <code>StackModel</code> to use. 070 */ 071 072 public StackView(StackModel model) 073 { 074 super.setModel(model); 075 this.model = model; 076 } 077 078 /** Get the model used by this <code>StackView</code>. */ 079 080 public StackModel getStackModel() 081 { 082 return(model); 083 } 084 085 /** Push a new item on the stack. 086 * 087 * @param obj The object to push on the stack. 088 */ 089 090 public void push(Object obj) 091 { 092 model.push(obj); 093 } 094 095 /** Pop an item off the stack. Pops the top item off the stack. 096 * 097 * @return The popped item. 098 * 099 * @exception java.util.EmptyStackException If the stack is empty. 100 */ 101 102 public Object pop() throws EmptyStackException 103 { 104 return(model.pop()); 105 } 106 107 /** Drop the top item off the stack. Pops and discards the top item off the 108 * stack. 109 * 110 * @exception java.util.EmptyStackException If the stack is empty. 111 */ 112 113 public void drop() throws EmptyStackException 114 { 115 model.drop(); 116 } 117 118 /** Peek at the top item on the stack. 119 * 120 * @return The top item on the stack. The item is not removed from the 121 * stack. 122 */ 123 124 public Object peek() 125 { 126 return(model.peek()); 127 } 128 129 /** Swap the positions of the top two items on the stack. If there is only 130 * one item in the stack, this method has no effect. 131 * 132 * @exception java.util.EmptyStackException If the stack is empty. 133 */ 134 135 public void swap() throws EmptyStackException 136 { 137 model.swap(); 138 } 139 140 /** Return the depth of the stack. 141 * 142 * @return The number of items on the stack. 143 */ 144 145 public int getDepth() 146 { 147 return(model.getDepth()); 148 } 149 150 /** Check if the stack is empty. 151 * 152 * @return <code>true</code> if the stack is empty, <code>false</code> 153 * otherwise. 154 */ 155 156 public boolean isEmpty() 157 { 158 return(model.isEmpty()); 159 } 160 161 /** Remove an item from the stack. Removes the item at the specified index 162 * from the stack. Index position 0 refers to the top of the stack. 163 * 164 * @param index The index of the item to remove. 165 * 166 * @return The removed item. 167 * 168 * @exception java.lang.ArrayIndexOutOfBoundsException If <code>index</code> 169 * is out of range. 170 */ 171 172 public Object pick(int index) throws ArrayIndexOutOfBoundsException 173 { 174 return(model.pick(index)); 175 } 176 177 /** Append an item to the stack. 178 * 179 * @param obj The item to add to the bottom of the stack. 180 */ 181 182 public void append(Object obj) 183 { 184 model.append(obj); 185 } 186 187 /** Replace the top item on the stack. The top item on the stack is replaced 188 * with the item <code>obj</code>. If the stack is empty, the item is merely 189 * pushed on the stack. 190 * 191 * @param obj The new item. 192 */ 193 194 public void replace(Object obj) 195 { 196 try 197 { 198 pop(); 199 } 200 catch(EmptyStackException ex) {} 201 202 push(obj); 203 } 204 205 } 206 207/* end of source file */