summaryrefslogtreecommitdiff
path: root/NET/worlds/console/TreePanel.java
blob: 5bf81356470b6d52b69e4b75fc9a8b67a9df2b10 (plain) (blame)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*     */ package NET.worlds.console;
/*     */ 
/*     */ import java.awt.BorderLayout;
/*     */ import java.awt.Color;
/*     */ import java.awt.Dimension;
/*     */ import java.awt.Event;
/*     */ import java.awt.Font;
/*     */ import java.awt.FontMetrics;
/*     */ import java.awt.Graphics;
/*     */ import java.awt.Point;
/*     */ import java.awt.Rectangle;
/*     */ import java.awt.Scrollbar;
/*     */ import java.util.Vector;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TreePanel
/*     */   extends ExposedPanel
/*     */   implements DialogDisabled
/*     */ {
/*     */   private static final long serialVersionUID = -5534860983354876334L;
/*  42 */   private Vector<TreeNode> items = new Vector();
/*     */   private boolean delayRepaints;
/*     */   private boolean needRepaint;
/*     */   private boolean needRecalc;
/*     */   private boolean needMakeVisible;
/*  47 */   private Scrollbar scrollbar = new WiderScrollbar();
/*  48 */   private int scrollPos = 0;
/*     */   private int linesVisible;
/*  50 */   private int selectedIndex = -1;
/*  51 */   private boolean hasFocus = true;
/*  52 */   private Font nFont = new Font(Console.message("TreeFont"), 0, 13);
/*     */   
/*  54 */   private FontMetrics nFontMetrics = getFontMetrics(this.nFont);
/*  55 */   private Font bFont = new Font(Console.message("TreeFont"), 1, 11);
/*     */   
/*  57 */   private FontMetrics bFontMetrics = getFontMetrics(this.bFont);
/*  58 */   private int fontHeight = Math.max(this.nFontMetrics.getHeight(), 
/*  59 */     this.bFontMetrics.getHeight());
/*  60 */   private int itemHeight = this.fontHeight;
/*     */   
/*     */   private boolean isDialogDisabled;
/*  63 */   private static final int[] cxs = { 006 };
/*  64 */   private static final int[] cys = { 6, -6 };
/*  65 */   private MoveablePolygon closedIcon = new MoveablePolygon(cxs, cys);
/*     */   
/*  67 */   private static final int[] oxs = { -5, 6 };
/*  68 */   private static final int[] oys = { 006 };
/*  69 */   private MoveablePolygon openedIcon = new MoveablePolygon(oxs, oys);
/*     */   
/*  71 */   private static final Color normBGColor = new Color(80, 80, 80);
/*  72 */   private static final Color normFGColor = new Color(190, 190, 190);
/*  73 */   private static final Color selFGColor = new Color(255, 255, 175);
/*     */   private static final int indentPixels = 14;
/*     */   
/*     */   public TreePanel() {
/*  77 */     setBackground(normBGColor);
/*  78 */     setLayout(new BorderLayout());
/*  79 */     add("East", this.scrollbar);
/*  80 */     this.scrollbar.setVisible(false);
/*     */   }
/*     */   
/*     */   public synchronized void delayRepaints(boolean state) {
/*  84 */     if (!(this.delayRepaints = state)) {
/*  85 */       if (this.needRecalc)
/*  86 */         recalc();
/*  87 */       if (this.needMakeVisible)
/*  88 */         makeVisible(this.selectedIndex);
/*  89 */       if (this.needRepaint) {
/*  90 */         repaint();
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private synchronized void needRepaint(boolean needRecalc, boolean needMakeVisible) {
/*  96 */     this.needRepaint = true;
/*  97 */     this.needRecalc |= needRecalc;
/*  98 */     this.needMakeVisible |= needMakeVisible;
/*  99 */     delayRepaints(this.delayRepaints);
/*     */   }
/*     */   
/*     */   public synchronized int getSelectedIndex() {
/* 103 */     return this.selectedIndex;
/*     */   }
/*     */   
/*     */   public synchronized void select(int item) {
/* 107 */     this.selectedIndex = item;
/* 108 */     needRepaint(false, true);
/*     */   }
/*     */   
/*     */   public void setFocus(boolean hasFocus) {
/* 112 */     if (this.hasFocus != hasFocus) {
/* 113 */       this.hasFocus = hasFocus;
/* 114 */       needRepaint(false, false);
/*     */     }
/*     */   }
/*     */   
/*     */   public boolean hasFocus()
/*     */   {
/* 120 */     return this.hasFocus;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized void setBounds(int x, int y, int w, int h)
/*     */   {
/* 129 */     super.setBounds(x, y, w, h);
/* 130 */     needRepaint(true, false);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized TreeNode getSelectedNode()
/*     */   {
/* 143 */     if (this.selectedIndex != -1)
/* 144 */       return (TreeNode)this.items.elementAt(this.selectedIndex);
/* 145 */     return null;
/*     */   }
/*     */   
/*     */   public synchronized void reset(Vector<TreeNode> items) {
/* 149 */     this.items = items;
/* 150 */     needRepaint(true, false);
/*     */   }
/*     */   
/*     */   public synchronized void removeAllElements() {
/* 154 */     this.items.removeAllElements();
/* 155 */     needRepaint(true, false);
/*     */   }
/*     */   
/*     */   public synchronized void insertElementAt(TreeNode ele, int index) {
/* 159 */     this.items.insertElementAt(ele, index);
/* 160 */     needRepaint(true, false);
/*     */   }
/*     */   
/*     */   public synchronized void removeElementAt(int index) {
/* 164 */     this.items.removeElementAt(index);
/* 165 */     needRepaint(true, false);
/*     */   }
/*     */   
/*     */   public synchronized void addElement(TreeNode ele) {
/* 169 */     this.items.addElement(ele);
/* 170 */     needRepaint(true, false);
/*     */   }
/*     */   
/*     */   public int countElements() {
/* 174 */     return this.items.size();
/*     */   }
/*     */   
/*     */   public TreeNode elementAt(int index) {
/* 178 */     return (TreeNode)this.items.elementAt(index);
/*     */   }
/*     */   
/*     */   public TreeNode elementAt(Point p) {
/* 182 */     int item = this.scrollPos + p.y / this.itemHeight;
/* 183 */     if (item < this.items.size())
/* 184 */       return (TreeNode)this.items.elementAt(item);
/* 185 */     return null;
/*     */   }
/*     */   
/*     */   private void makeVisible(int item) {
/* 189 */     int count = this.items.size();
/* 190 */     if ((count > this.linesVisible) && (
/* 191 */       (item < this.scrollPos) || (item >= this.scrollPos + this.linesVisible))) {
/* 192 */       setScrollValue(Math.min(item * this.itemHeight, this.scrollbar.getMaximum()));
/*     */     }
/* 194 */     this.needMakeVisible = false;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized void recalc()
/*     */   {
/* 204 */     int height = getSize().height;
/* 205 */     this.linesVisible = Math.max(1, height / this.itemHeight);
/* 206 */     int count = this.items.size();
/* 207 */     if (count > this.linesVisible) {
/* 208 */       if (!this.scrollbar.isVisible()) {
/* 209 */         this.scrollbar.setVisible(true);
/* 210 */         validate();
/*     */       }
/* 212 */       this.scrollbar.setValues(this.scrollPos * this.itemHeight, this.linesVisible * 
/* 213 */         this.itemHeight, 0, count * this.itemHeight);
/* 214 */       this.scrollbar.setPageIncrement(this.linesVisible * this.itemHeight);
/* 215 */       this.scrollbar.setLineIncrement(this.itemHeight);
/*     */     } else {
/* 217 */       if (this.scrollbar.isVisible()) {
/* 218 */         this.scrollbar.setVisible(false);
/* 219 */         validate();
/*     */       }
/* 221 */       this.scrollPos = 0;
/*     */     }
/* 223 */     this.needRecalc = false;
/*     */   }
/*     */   
/*     */   public synchronized void paint(Graphics g)
/*     */   {
/* 228 */     super.paint(g);
/*     */     
/* 230 */     int height = getSize().height;
/* 231 */     int count = this.items.size();
/* 232 */     int y = 0;
/* 233 */     for (int i = this.scrollPos; i < count; i++) {
/* 234 */       TreeNode node = elementAt(i);
/* 235 */       String name = node.toString();
/* 236 */       boolean isTitle = node.displayAsTitle();
/* 237 */       Font font = this.nFont;
/* 238 */       FontMetrics metrics = this.nFontMetrics;
/* 239 */       if (isTitle) {
/* 240 */         font = this.bFont;
/* 241 */         metrics = this.bFontMetrics;
/*     */       }
/* 243 */       g.setFont(font);
/* 244 */       int ascent = metrics.getAscent();
/* 245 */       int x = node.getLevel() * 14;
/* 246 */       g.setColor(normFGColor);
/* 247 */       if (node.isOpen()) {
/* 248 */         this.openedIcon.drawFilled(g, x + 5, y + ascent - 5);
/*     */       } else
/* 250 */         this.closedIcon.drawFilled(g, x + 5, y + ascent - 5);
/* 251 */       if (i == this.selectedIndex)
/* 252 */         g.setColor(selFGColor);
/* 253 */       g.drawString(name, x + 11 + 5, y + ascent);
/* 254 */       if ((i == this.selectedIndex) && (this.hasFocus))
/* 255 */         g.drawRect(x + 11 + 3, y, metrics.stringWidth(name) + 3, 
/* 256 */           this.itemHeight);
/* 257 */       y += this.itemHeight;
/* 258 */       if (y >= height)
/*     */         break;
/*     */     }
/* 261 */     this.needRepaint = false;
/*     */   }
/*     */   
/*     */   @Deprecated
/*     */   public boolean handleEvent(Event e)
/*     */   {
/* 267 */     if (this.isDialogDisabled) {
/* 268 */       return false;
/*     */     }
/* 270 */     switch (e.id) {
/*     */     case 601: 
/* 272 */       return scrollLineUp();
/*     */     case 602: 
/* 274 */       return scrollLineDown();
/*     */     case 603: 
/* 276 */       return scrollPageUp();
/*     */     case 604: 
/* 278 */       return scrollPageDown();
/*     */     case 605: 
/* 280 */       return scrollAbsolute();
/*     */     }
/* 282 */     return super.handleEvent(e);
/*     */   }
/*     */   
/*     */   public void dialogDisable(boolean disable)
/*     */   {
/* 287 */     this.isDialogDisabled = disable;
/*     */   }
/*     */   
/*     */   public synchronized boolean mouseDown(Event e, int x, int y)
/*     */   {
/* 292 */     setFocus(true);
/* 293 */     int item = this.scrollPos + y / this.itemHeight;
/* 294 */     if ((item >= 0) && (item < this.items.size()))
/* 295 */       if (e.clickCount == 1) {
/* 296 */         treeSelect(item);
/*     */         
/*     */ 
/* 299 */         int yStart = y / this.itemHeight * this.itemHeight;
/* 300 */         TreeNode node = elementAt(item);
/* 301 */         MoveablePolygon widget = node.isOpen() ? this.openedIcon : 
/* 302 */           this.closedIcon;
/* 303 */         FontMetrics metrics = node.displayAsTitle() ? this.nFontMetrics : 
/* 304 */           this.bFontMetrics;
/* 305 */         widget.moveTo(node.getLevel() * 14 + 5, yStart + 
/* 306 */           metrics.getAscent() - 5);
/* 307 */         if (widget.getBoundingBox().contains(x, y))
/* 308 */           treeOpen(item);
/*     */       } else {
/* 310 */         treeOpen(item);
/*     */       }
/* 312 */     return true;
/*     */   }
/*     */   
/*     */ 
/*     */   public void treeSelect(int item) {}
/*     */   
/*     */   public void treeOpen(int item) {}
/*     */   
/*     */   private boolean setScrollValue(int value)
/*     */   {
/* 322 */     value = Math.max(this.scrollbar.getMinimum(), value);
/* 323 */     value = Math.min(this.scrollbar.getMaximum(), value);
/* 324 */     this.scrollPos = (value / this.itemHeight);
/* 325 */     this.scrollbar.setValue(this.scrollPos * this.itemHeight);
/* 326 */     needRepaint(false, false);
/* 327 */     return true;
/*     */   }
/*     */   
/*     */   private boolean scrollLineUp() {
/* 331 */     return setScrollValue(this.scrollPos * this.itemHeight - 
/* 332 */       this.scrollbar.getLineIncrement());
/*     */   }
/*     */   
/*     */   private boolean scrollLineDown() {
/* 336 */     return setScrollValue(this.scrollPos * this.itemHeight + 
/* 337 */       this.scrollbar.getLineIncrement());
/*     */   }
/*     */   
/*     */   private boolean scrollPageUp() {
/* 341 */     return setScrollValue(this.scrollPos * this.itemHeight - 
/* 342 */       this.scrollbar.getPageIncrement());
/*     */   }
/*     */   
/*     */   private boolean scrollPageDown() {
/* 346 */     return setScrollValue(this.scrollPos * this.itemHeight + 
/* 347 */       this.scrollbar.getPageIncrement());
/*     */   }
/*     */   
/*     */   private boolean scrollAbsolute() {
/* 351 */     return setScrollValue(this.scrollbar.getValue());
/*     */   }
/*     */ }


/* Location:              C:\Program Files (x86)\Worlds Inc\WorldsPlayer - Win7\lib\worlds.jar!\NET\worlds\console\TreePanel.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */