put dx.pl into an explicit handle sub
[spider.git] / SpiderConsole / src / CommandBuffer.java
1 /**
2  * Command Buffer for the cluster window of the spider GUI.
3  * @author Ian Norton
4  * @version 1.00 - 20010418.
5  *
6  * Copyright (C) 2001 Ian Norton.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public Licence as published by
10  * the Free Software Foundation; either version 2 of the Licence, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public Licence for more details.
17  *
18  * You should have received a copy of the GNU General Public Licence
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * Contacting the author :
23  * Ian Norton
24  * i.norton@lancaster.ac.uk
25  * http://www.lancs.ac.uk/~norton/                                               **/
26
27 import java.util.Vector ;
28
29 class CommandBuffer
30     {
31     private int top, bottom, pointer, size ;
32     private Vector buffer ;
33     private boolean rolled ;
34
35     /**
36      * main - testing purposes only.
37      **/
38     public static void main(String[] args)
39         {
40         CommandBuffer c = new CommandBuffer(5) ;
41         c.addCommand("1") ;
42         System.out.println(c.getPreviousCommand()) ;
43         System.out.println(c.getNextCommand()) ;
44         }
45
46     /**
47      * CommandBuffer
48      * @param int - Number of lines of buffer.
49      **/
50      public CommandBuffer(int i)
51         {
52         // Size of the buffer.
53         size = i ;
54
55         // "Pointers"
56         bottom = 0 ;
57         pointer = 0 ;
58
59         top = size - 1 ;
60
61         // Vector that does that actual storage.
62         buffer = new Vector(size) ;
63         }
64
65     /**
66      * addCommand
67      * @param String - command to add to the buffer
68      **/
69     public void addCommand(String s)
70         {
71         // Is it an empty string
72         if(s.length() == 0) return ;
73         
74         // Add the command to the buffer
75         buffer.addElement(s) ;
76
77         // Check the buffer remains the correct size.
78         while(buffer.size() > size) buffer.removeElementAt(0) ;
79         
80         // Pointer to the last command
81         pointer = buffer.indexOf(s) ;
82         }
83
84     /**
85      * getPreviousCommand - get the previous command (recursive)
86      * @returns String - previous command
87      **/
88     public String getPreviousCommand()
89         {
90         String output = (String)buffer.elementAt(pointer) ;
91         if(pointer != 0) pointer-- ;
92         return output ;
93         }
94
95     /**
96      * getNextCommand - get the next command (recursive)
97      * @returns String - next command
98      **/
99     public String getNextCommand()
100         {
101         pointer++ ;
102         if(pointer == buffer.size())
103             {
104             pointer-- ;
105             return "" ;
106             }
107         String output = (String)buffer.elementAt(pointer) ;
108         return output ;
109         }
110     } // End of class.