Repository navigation
Expand file tree
/
Copy pathWindowStack.java
More file actions
242 lines (205 loc) · 6.67 KB
/
Copy pathWindowStack.java
File metadata and controls
242 lines (205 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Comparator;
/**
* A stack of windows within the window.
*
* <p>
* Adapterion of Nifty Assignment (http://nifty.stanford.edu/) by Mike Clancy in
* 2001. Original code by Mike Clancy. Updated Fall 2022 by K. Raven
* Russell.</p>
*/
public class WindowStack {
/**
* initialize the stack.
*/
private ThreeTenLinkedList<Window> stack;
/**
* construct the stack.
*/
public WindowStack() {
stack = new ThreeTenLinkedList<>();
}
/**
* gets the top of the stack.
*
* @return head of stack
*/
public Node<Window> getHead() {
return stack.getHead();
}
/**
* gets the bottom of stack.
*
* @return tail of stack
*/
public Node<Window> getTail() {
return stack.getTail();
}
/**
* gets the current size of the stack.
*
* @return size of stack
*/
public int numWindows() {
return stack.getSize();
}
/**
* Adds a window to the top of the stack.
*
* @param r value to pass
*/
public void add(Window r) {
if(r == null){
throw new IllegalArgumentException("Invalid Window");
}
else{
stack.insertAtHead(r);
}
}
/**
* method that moves a window to top of the stack if it's been leftClicked
* and removes it if it was clicked with right.
*
* @param x position
* @param y position
* @param leftClick state of the click
* @return whether or not a window was moved/deleted
*/
public boolean handleClick(int x, int y, boolean leftClick) {
ThreeTenLinkedList<Window> tempStack = new ThreeTenLinkedList<>();
if(stack.getHead() == null){
return false;
}
if(leftClick){
if(stack.getHead().data.contains(x, y)){
stack.getHead().data.handleClick(x, y);
return true;
}
else{
stack.getHead().data.setSelected(false);
Window found = null;
while(stack.getSize() > 0){
Window top = stack.deleteHead();
if(top != null){
if(top.contains(x, y)){
found = top;
break;
}
else{
tempStack.insertAtHead(top);
}
}
}
while(tempStack.getSize() > 0){
Window top = tempStack.deleteHead();
if(top != null){
stack.insertAtHead(top);
}
}
if(found != null){
stack.insertAtHead(found);
stack.getHead().data.setSelected(true);
return true;
}
}
}
else{
if(stack.getHead() != null){
stack.getHead().data.setSelected(false);
if(stack.getHead().data.contains(x, y)){
stack.deleteHead();
return true;
}
}
Window found = null;
while(stack.getSize() > 0){
Window top = stack.deleteHead();
if(top != null){
if(top.contains(x, y)){
found = top;
break;
}
else{
tempStack.insertAtHead(top);
}
}
}
while(tempStack.getSize() > 0){
Window top = tempStack.deleteHead();
if(top != null){
stack.insertAtHead(top);
}
}
if(found != null){
if(stack.getHead() != null){
stack.getHead().data.setSelected(true);
}
return true;
}
}
return false;
}
/**
* Gets an iterator for the stack of windows. Windows are returned from
* bottom to top.
*
* @return the iterator requested
*/
public Iterator<Window> windows() {
return new Iterator<Window>() {
/**
* The current node pointed to by the iterator (containing the next
* value to be returned).
*/
private Node<Window> current = getTail();
/**
* {@inheritDoc}
*/
@Override
public Window next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Window ret = current.data;
current = current.prev;
return ret;
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasNext() {
return (current != null);
}
};
}
/**
* Sorts windows by area.
*/
public void sortSize() {
this.getHead().data.setSelected(false);
Comparator<Window> comp = new Comparator<Window>() {
public int compare(Window w1, Window w2) {
return (w1.getWidth() * w1.getHeight()) - (w2.getWidth() * w2.getHeight());
}
};
ThreeTenLinkedList.NodePair<Window> pair = new ThreeTenLinkedList.NodePair<>(getHead(), getTail());
ThreeTenLinkedList.NodePair<Window> ret = ThreeTenLinkedList.sort(pair, comp);
this.getHead().data.setSelected(true);
}
/**
* Sorts windows by their upper left corner location.
*/
public void sortLoc() {
this.getHead().data.setSelected(false);
Comparator<Window> comp = new Comparator<Window>() {
public int compare(Window w1, Window w2) {
return (w1.getUpperLeftX() * w1.getUpperLeftY()) - (w2.getUpperLeftX() * w2.getUpperLeftY());
}
};
ThreeTenLinkedList.NodePair<Window> pair = new ThreeTenLinkedList.NodePair<>(getHead(), getTail());
ThreeTenLinkedList.NodePair<Window> ret = ThreeTenLinkedList.sort(pair, comp);
this.getHead().data.setSelected(true);
}
}