summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/ScrollingImagePanel.java
blob: 417662dc61b4bef9fe09445ca3ad676597ff79a4 (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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*     */ package NET.worlds.scape;
/*     */ 
/*     */ import NET.worlds.console.Console;
/*     */ import NET.worlds.console.DialogDisabled;
/*     */ import NET.worlds.console.ExposedPanel;
/*     */ import NET.worlds.console.WiderScrollbar;
/*     */ import NET.worlds.network.URL;
/*     */ import java.awt.BorderLayout;
/*     */ import java.awt.Color;
/*     */ import java.awt.Component;
/*     */ import java.awt.Container;
/*     */ import java.awt.Dimension;
/*     */ import java.awt.Event;
/*     */ import java.awt.Font;
/*     */ import java.awt.FontMetrics;
/*     */ import java.awt.Frame;
/*     */ import java.awt.Graphics;
/*     */ import java.awt.GridBagConstraints;
/*     */ import java.awt.GridBagLayout;
/*     */ import java.awt.Image;
/*     */ import java.awt.MediaTracker;
/*     */ import java.awt.Point;
/*     */ import java.awt.Scrollbar;
/*     */ import java.awt.Toolkit;
/*     */ import java.io.PrintStream;
/*     */ import java.util.Enumeration;
/*     */ import java.util.Vector;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ScrollingImagePanel
/*     */   extends ExposedPanel
/*     */   implements LibraryDropTarget, DialogDisabled
/*     */ {
/*     */   private static final long serialVersionUID = 1L;
/*  47 */   private Scrollbar scrollbar = new WiderScrollbar();
/*     */   private Vector<ScrollingListElement> items;
/*     */   private int cellWidth;
/*     */   private int cellHeight;
/*  51 */   private Font font = new Font(Console.message("ScImageFont"), 1, 12);
/*     */   
/*  53 */   private FontMetrics metrics = getFontMetrics(this.font);
/*  54 */   private int ascent = this.metrics.getAscent();
/*  55 */   private int descent = this.metrics.getDescent();
/*  56 */   private int textHeight = this.ascent + this.descent;
/*  57 */   private int scrollPos = 0;
/*     */   private ClickEventHandler handler;
/*     */   private ScrollingListElement downClicked;
/*     */   private boolean isDownClicked;
/*     */   private int maxScrollPos;
/*  62 */   private boolean useIcons = false;
/*     */   
/*     */   private boolean isDialogDisabled;
/*     */   
/*     */   public ScrollingImagePanel(ClickEventHandler handler, Vector<?> icons, boolean showIcons)
/*     */   {
/*  68 */     this.handler = handler;
/*  69 */     setFont(this.font);
/*  70 */     setForeground(Color.black);
/*  71 */     setBackground(Color.lightGray);
/*  72 */     this.items = new Vector();
/*  73 */     setContents(icons, false);
/*  74 */     setLayout(new BorderLayout());
/*  75 */     add("East", this.scrollbar);
/*  76 */     this.useIcons = showIcons;
/*     */   }
/*     */   
/*     */   public void resetContents(Vector<?> icons)
/*     */   {
/*  81 */     setContents(icons);
/*     */   }
/*     */   
/*     */   protected void add(GridBagLayout gbag, Component comp, GridBagConstraints c)
/*     */   {
/*  86 */     gbag.setConstraints(comp, c);
/*  87 */     add(comp);
/*     */   }
/*     */   
/*     */   @Deprecated
/*     */   public boolean handleEvent(Event e)
/*     */   {
/*  93 */     if (this.isDialogDisabled) {
/*  94 */       return false;
/*     */     }
/*  96 */     switch (e.id) {
/*     */     case 601: 
/*  98 */       return scrollLineUp();
/*     */     case 602: 
/* 100 */       return scrollLineDown();
/*     */     case 603: 
/* 102 */       return scrollPageUp();
/*     */     case 604: 
/* 104 */       return scrollPageDown();
/*     */     case 605: 
/* 106 */       return scrollAbsolute();
/*     */     }
/* 108 */     return super.handleEvent(e);
/*     */   }
/*     */   
/*     */ 
/*     */   public void dialogDisable(boolean disable)
/*     */   {
/* 114 */     this.isDialogDisabled = disable;
/*     */   }
/*     */   
/*     */   private void setContents(Vector<?> icons)
/*     */   {
/* 119 */     setContents(icons, true);
/*     */   }
/*     */   
/*     */   private void setContents(Vector<?> icons, boolean showIt)
/*     */   {
/* 124 */     this.items.removeAllElements();
/* 125 */     int count = icons.size();
/* 126 */     for (int i = 0; i < count; i++)
/* 127 */       addIcon((Iconic)icons.elementAt(i), false);
/* 128 */     if (showIt) {
/* 129 */       recalc(getSize().width, getSize().height);
/* 130 */       repaint();
/*     */     }
/*     */   }
/*     */   
/*     */   private void addIcon(Iconic icon, boolean showIt)
/*     */   {
/* 136 */     String title = icon.getIconCaption();
/* 137 */     int titleWidth = title != null ? this.metrics.stringWidth(title) : 0;
/* 138 */     URL name = icon.getIconURL();
/* 139 */     Image image = null;
/* 140 */     if (name != null) {
/* 141 */       MediaTracker tracker = new MediaTracker(this);
/* 142 */       image = Toolkit.getDefaultToolkit().getImage(name.unalias());
/* 143 */       tracker.addImage(image, 0);
/*     */       try {
/* 145 */         tracker.waitForID(0);
/*     */       }
/*     */       catch (InterruptedException localInterruptedException) {}
/* 148 */       if (tracker.isErrorID(0)) {
/* 149 */         image = null;
/*     */         
/* 151 */         System.out.println("Error loading " + name);
/*     */       }
/*     */     }
/* 154 */     this.items.addElement(new ScrollingListElement(image, title, titleWidth));
/*     */   }
/*     */   
/*     */   private void findCellSize()
/*     */   {
/* 159 */     this.cellWidth = 30;
/* 160 */     this.cellHeight = 0;
/* 161 */     if (this.useIcons) {
/* 162 */       this.cellHeight = 30;
/*     */     }
/* 164 */     Enumeration<ScrollingListElement> e = this.items.elements();
/* 165 */     while (e.hasMoreElements()) {
/* 166 */       ScrollingListElement item = (ScrollingListElement)e.nextElement();
/* 167 */       this.cellWidth = Math.max(this.cellWidth, item.titleWidth);
/* 168 */       if ((this.useIcons) && (item.image != null)) {
/* 169 */         this.cellWidth = Math.max(this.cellWidth, 
/* 170 */           item.image.getWidth(this));
/* 171 */         this.cellHeight = Math.max(this.cellHeight, 
/* 172 */           item.image.getHeight(this));
/*     */       }
/*     */     }
/* 175 */     this.cellWidth += 2;
/* 176 */     this.cellHeight += this.textHeight + 2;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void setBounds(int x, int y, int w, int h)
/*     */   {
/* 188 */     super.setBounds(x, y, w, h);
/* 189 */     recalc(w, h);
/* 190 */     repaint();
/*     */   }
/*     */   
/*     */   private void recalc(int w, int h)
/*     */   {
/* 195 */     findCellSize();
/*     */     
/*     */ 
/*     */ 
/* 199 */     this.scrollPos = 0;
/* 200 */     this.scrollbar.setValue(this.scrollPos);
/*     */     
/*     */ 
/* 203 */     for (int pass = 0; pass < 2; pass++) {
/* 204 */       int scrollbarWidth = 0;
/* 205 */       if (pass == 1)
/* 206 */         scrollbarWidth = this.scrollbar.getSize().width;
/* 207 */       int lines = 0;
/* 208 */       if (this.cellWidth != 0) {
/* 209 */         int imagesPerLine = Math.max(1, (w - scrollbarWidth) / 
/* 210 */           this.cellWidth);
/* 211 */         lines = (this.items.size() + imagesPerLine - 1) / imagesPerLine * 
/* 212 */           this.cellHeight;
/*     */       }
/* 214 */       if (lines > h) {
/* 215 */         if (pass == 0) {
/* 216 */           if (!this.scrollbar.isVisible()) {
/* 217 */             this.scrollbar.setVisible(true);
/* 218 */             validate();
/*     */           }
/*     */         }
/*     */         else {
/* 222 */           this.maxScrollPos = lines;
/* 223 */           this.scrollbar.setValues(this.scrollPos, h, 0, lines);
/* 224 */           this.scrollbar.setPageIncrement(h);
/* 225 */           this.scrollbar.setLineIncrement(this.cellHeight);
/* 226 */           break;
/*     */         }
/*     */       }
/*     */       else {
/* 230 */         if (!this.scrollbar.isVisible()) break;
/* 231 */         this.scrollbar.setVisible(false);
/* 232 */         validate();
/*     */         
/* 234 */         break;
/*     */       }
/*     */     }
/*     */     
/* 238 */     clearHitTest();
/*     */   }
/*     */   
/*     */   private void clearHitTest()
/*     */   {
/* 243 */     int count = this.items.size();
/* 244 */     for (int i = 0; i < count; i++) {
/* 245 */       ScrollingListElement e = (ScrollingListElement)this.items.elementAt(i);
/* 246 */       e.x = (e.y = -1);
/*     */     }
/*     */   }
/*     */   
/*     */   private ScrollingListElement findCell(int x, int y)
/*     */   {
/* 252 */     int count = this.items.size();
/* 253 */     for (int i = 0; i < count; i++) {
/* 254 */       ScrollingListElement e = (ScrollingListElement)this.items.elementAt(i);
/* 255 */       if ((x >= 0) && (y >= 0) && (x < e.x) && (y < e.y))
/* 256 */         return e;
/*     */     }
/* 258 */     return null;
/*     */   }
/*     */   
/*     */   private void buttonAction(boolean down)
/*     */   {
/* 263 */     if (this.isDownClicked != down) {
/* 264 */       this.isDownClicked = down;
/* 265 */       Graphics g = getGraphics();
/* 266 */       drawCell(g, this.downClicked, this.isDownClicked);
/* 267 */       g.dispose();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private Component findComponent(Point p)
/*     */   {
/* 281 */     Point screenLoc = getLocationOnScreen();
/* 282 */     Container cur = null;
/* 283 */     for (Container tmp = getParent(); !(tmp instanceof Frame); 
/* 284 */         tmp = cur.getParent())
/* 285 */       cur = tmp;
/* 286 */     Point parentCoords = cur.getLocationOnScreen();
/* 287 */     p.x = (p.x + screenLoc.x - parentCoords.x);
/* 288 */     p.y = (p.y + screenLoc.y - parentCoords.y);
/*     */     
/*     */ 
/*     */ 
/* 292 */     Component found = cur;
/*     */     for (;;) {
/* 294 */       Component c = found.getComponentAt(p.x, p.y);
/* 295 */       if ((c == found) || (c == null))
/*     */         break;
/* 297 */       Point tmp = c.getLocation();
/* 298 */       p.x -= tmp.x;
/* 299 */       p.y -= tmp.y;
/* 300 */       found = c;
/*     */     }
/* 302 */     return found;
/*     */   }
/*     */   
/*     */   @Deprecated
/*     */   public boolean mouseUp(Event event, int x, int y)
/*     */   {
/* 308 */     if (this.downClicked != null) {
/* 309 */       Point p = new Point(x, y);
/* 310 */       buttonAction(false);
/* 311 */       this.handler.clickEvent(findComponent(p), p, 2);
/* 312 */       this.downClicked = null;
/* 313 */       return true;
/*     */     }
/* 315 */     return false;
/*     */   }
/*     */   
/*     */   public int itemAt(Point p)
/*     */   {
/* 320 */     ScrollingListElement cell = findCell(p.x, p.y);
/* 321 */     if (cell != null)
/* 322 */       return this.items.indexOf(cell);
/* 323 */     return -1;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   @Deprecated
/*     */   public boolean mouseDown(Event event, int x, int y)
/*     */   {
/* 331 */     if ((this.downClicked == null) && 
/* 332 */       ((this.downClicked = findCell(x, y)) != null)) {
/* 333 */       buttonAction(true);
/* 334 */       int flags = 1;
/* 335 */       if (event.metaDown())
/* 336 */         flags |= 0x4;
/* 337 */       this.handler.clickEvent(this, new Point(x, y), flags);
/* 338 */       return true;
/*     */     }
/* 340 */     return false;
/*     */   }
/*     */   
/*     */   @Deprecated
/*     */   public boolean mouseDrag(Event event, int x, int y)
/*     */   {
/* 346 */     if (this.downClicked != null) {
/* 347 */       Point p = new Point(x, y);
/* 348 */       this.handler.clickEvent(findComponent(p), p, 8);
/* 349 */       return true;
/*     */     }
/* 351 */     return false;
/*     */   }
/*     */   
/*     */ 
/*     */   private void drawCell(Graphics g, ScrollingListElement e, boolean depressed)
/*     */   {
/* 357 */     int x = e.x - this.cellWidth;
/* 358 */     int y = e.y - this.cellHeight;
/* 359 */     int imageBoxHeight = this.cellHeight - this.textHeight;
/* 360 */     int yTextOffset = imageBoxHeight + this.ascent;
/* 361 */     Color normColor = g.getColor();
/* 362 */     g.setColor(getBackground());
/* 363 */     g.draw3DRect(x, y, this.cellWidth - 1, this.cellHeight - 1, !depressed);
/* 364 */     g.setColor(normColor);
/* 365 */     if ((this.useIcons) && (e.image != null)) {
/* 366 */       int imageWidth = e.image.getWidth(this);
/* 367 */       int imageHeight = e.image.getHeight(this);
/* 368 */       g.drawImage(e.image, x + (this.cellWidth - imageWidth) / 2, 
/* 369 */         y + (imageBoxHeight - imageHeight) / 2, 
/* 370 */         null);
/*     */     }
/* 372 */     if (e.title != null) {
/* 373 */       g.drawString(e.title, x + (this.cellWidth - e.titleWidth) / 2, 
/* 374 */         y + yTextOffset);
/*     */     }
/*     */   }
/*     */   
/*     */   public void paint(Graphics g) {
/* 379 */     Color normColor = g.getColor();
/* 380 */     super.paint(g);
/* 381 */     g.setColor(normColor);
/*     */     
/* 383 */     clearHitTest();
/* 384 */     int width = getSize().width;
/* 385 */     int height = getSize().height;
/*     */     
/* 387 */     int x = 0;
/* 388 */     int y = -this.scrollbar.getValue();
/* 389 */     int count = this.items.size();
/* 390 */     for (int i = 0; i < count; i++) {
/* 391 */       if (y + this.cellHeight > 0) {
/* 392 */         ScrollingListElement e = 
/* 393 */           (ScrollingListElement)this.items.elementAt(i);
/* 394 */         e.x = (x + this.cellWidth);
/* 395 */         e.y = (y + this.cellHeight);
/* 396 */         drawCell(g, e, false);
/*     */       }
/* 398 */       x += this.cellWidth;
/*     */       
/*     */ 
/* 401 */       if (x + this.cellWidth > width) {
/* 402 */         x = 0;
/* 403 */         if (y += this.cellHeight >= height)
/*     */           break;
/*     */       }
/*     */     }
/*     */   }
/*     */   
/* 409 */   public boolean isIconsVisible() { return this.useIcons; }
/*     */   
/*     */   public void setIconsVisible(boolean showIcons) {
/* 412 */     this.useIcons = showIcons;
/* 413 */     recalc(getSize().width, getSize().height);
/* 414 */     repaint();
/*     */   }
/*     */   
/*     */   private boolean setScrollValue(int value)
/*     */   {
/* 419 */     this.scrollPos = Math.max(this.scrollbar.getMinimum(), value);
/* 420 */     this.scrollPos = 
/* 421 */       Math.min(this.maxScrollPos, this.scrollPos);
/* 422 */     this.scrollbar.setValue(this.scrollPos);
/* 423 */     repaint();
/* 424 */     return true;
/*     */   }
/*     */   
/*     */   private boolean scrollLineUp()
/*     */   {
/* 429 */     return setScrollValue(this.scrollPos - this.scrollbar.getLineIncrement());
/*     */   }
/*     */   
/*     */   private boolean scrollLineDown()
/*     */   {
/* 434 */     return setScrollValue(this.scrollPos + this.scrollbar.getLineIncrement());
/*     */   }
/*     */   
/*     */   private boolean scrollPageUp()
/*     */   {
/* 439 */     return setScrollValue(this.scrollPos - this.scrollbar.getPageIncrement());
/*     */   }
/*     */   
/*     */   private boolean scrollPageDown()
/*     */   {
/* 444 */     return setScrollValue(this.scrollPos + this.scrollbar.getPageIncrement());
/*     */   }
/*     */   
/*     */   private boolean scrollAbsolute()
/*     */   {
/* 449 */     return setScrollValue(this.scrollbar.getValue());
/*     */   }
/*     */ }


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