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: DefaultStackModel.java,v $ 023 Revision 1.7 2004/05/31 07:30:26 markl 024 Final cleanup and bugfixes of kiwi.ui.model. 025 026 Revision 1.6 2004/05/13 21:40:42 markl 027 comment block updates 028 029 Revision 1.5 2003/01/19 09:33:06 markl 030 Javadoc & comment header updates. 031 032 Revision 1.4 2001/03/12 07:24:00 markl 033 Javadoc cleanup. 034 035 Revision 1.3 2001/03/12 04:11:41 markl 036 Source code cleanup. 037 038 Revision 1.2 1999/01/10 03:12:19 markl 039 added GPL header & RCS tag 040 ---------------------------------------------------------------------------- 041*/ 042 043package kiwi.ui.model; 044 045import java.util.EmptyStackException; 046import javax.swing.*; 047import javax.swing.event.*; 048 049/** This class is a default implementation of the <code>StackModel</code> 050 * interface. It inherits its base functionality from 051 * <code>DefaultListModel</code>, and adds only methods for dealing with 052 * stack operations. 053 * 054 * @author Mark Lindner 055 */ 056 057public class DefaultStackModel extends DefaultListModel implements StackModel 058 { 059 060 /** Construct a new <code>DefaultStackModel</code>. 061 */ 062 063 public DefaultStackModel() 064 { 065 } 066 067 /** Push an object on the stack. The object becomes the topmost item on the 068 * stack. 069 * 070 * @param obj The object to push. 071 */ 072 073 public void push(Object obj) 074 { 075 insertElementAt(obj, 0); 076 fireIntervalAdded(this, 0, 0); 077 } 078 079 /** Pop an object off the stack. Pulls the topmost item off the stack. 080 * 081 * @return The popped object. 082 */ 083 084 public Object pop() throws EmptyStackException 085 { 086 return(pick(0)); 087 } 088 089 /** Drop an item off the stack. Pops and discards the topmost item on the 090 * stack. 091 * 092 * @exception java.util.EmptyStackException If the stack is empty. 093 */ 094 095 public void drop() throws EmptyStackException 096 { 097 pop(); // discard return value 098 } 099 100 /** Retrieve the topmost item from the stack (without removing the item from 101 * the stack). 102 * 103 * @return The topmost item on the stack. 104 * 105 * @exception java.util.EmptyStackException If the stack is empty. 106 */ 107 108 public Object peek() throws EmptyStackException 109 { 110 if(isEmpty()) 111 throw(new EmptyStackException()); 112 113 return(get(0)); 114 } 115 116 /** Get the depth of the stack. 117 * 118 * @return The number of items in the stack. 119 */ 120 121 public int getDepth() 122 { 123 return(getSize()); 124 } 125 126 /** Swap the topmost items on the stack. If the stack contains only one item, 127 * calling this method has no effect. 128 * 129 * @exception java.util.EmptyStackException If the stack is empty. 130 */ 131 132 public void swap() throws EmptyStackException 133 { 134 Object a, b; 135 136 if(getDepth() < 2) throw(new EmptyStackException()); 137 138 a = pop(); 139 b = pop(); 140 push(a); 141 push(b); 142 } 143 144 /** Determine if the stack is empty. 145 * 146 * @return <code>true</code> if there are no items in the stack, and 147 * <code>false</code> otherwise. 148 */ 149 150 public boolean isEmpty() 151 { 152 return(getSize() == 0); 153 } 154 155 /** Remove an object from the stack. Retrieves (and removes) an object from 156 * the given offset in the stack. 157 * 158 * @param index The offset (from the top of the stack) of the item to 159 * remove. 160 * 161 * @return The object that was removed. 162 * 163 * @exception java.util.EmptyStackException If the stack is empty. 164 * @exception java.lang.ArrayIndexOutOfBoundsException If the value of 165 * <code>index</code> is out of range. 166 */ 167 168 public Object pick(int index) throws ArrayIndexOutOfBoundsException, 169 EmptyStackException 170 { 171 if(isEmpty()) 172 throw(new EmptyStackException()); 173 174 Object o = getElementAt(index); 175 remove(index); 176 fireIntervalRemoved(this, index, index); 177 return(o); 178 } 179 180 /** Append an object to the bottom of the stack. 181 * 182 * @param obj The object to append. 183 */ 184 185 public void append(Object obj) 186 { 187 int index = getSize(); 188 insertElementAt(obj, index); 189 fireIntervalAdded(this, index, index); 190 } 191 192 /** Add a <code>ListDataListener</code> to this model's list of listeners. 193 * Since a stack is essentially a list with some special semantics, 194 * <code>ListDataListeners</code> are used. 195 * 196 * @param listener The listener to add. 197 */ 198 199 public void addStackDataListener(ListDataListener listener) 200 { 201 addListDataListener(listener); 202 } 203 204 /** Remove a <code>ListDataListener</code> from this model's list of 205 * listeners. Since a stack is essentially a list with some special 206 * semantics, <code>ListDataListeners</code> are used. 207 * 208 * @param listener The listener to add. 209 */ 210 211 public void removeStackDataListener(ListDataListener listener) 212 { 213 removeListDataListener(listener); 214 } 215 216 } 217 218/* end of source file */