summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/LibrariesTile.java
blob: 1048bb55f3a35bc1bccda287999a4ae0017de98c (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
/*     */ package NET.worlds.scape;
/*     */ 
/*     */ import NET.worlds.console.Console;
/*     */ import NET.worlds.console.Cursor;
/*     */ import NET.worlds.console.GammaFrame;
/*     */ import NET.worlds.console.Main;
/*     */ import NET.worlds.console.MainCallback;
/*     */ import NET.worlds.core.IniFile;
/*     */ import NET.worlds.network.URL;
/*     */ import java.awt.Color;
/*     */ import java.awt.Component;
/*     */ import java.awt.Point;
/*     */ import java.io.File;
/*     */ import java.util.Enumeration;
/*     */ import java.util.Vector;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class LibrariesTile
/*     */   extends TabbedPanel
/*     */   implements LibEventHandler, Properties, MainCallback, LibraryDropTarget
/*     */ {
/*     */   private static final long serialVersionUID = 1L;
/*     */   private static final int NOTHING = 0;
/*     */   private static final int ADD_LIBRARY = 1;
/*     */   private static final int ADD_ELEMENT = 2;
/*     */   private static URL libURL;
/*     */   private static String libSubdir;
/*     */   
/*     */   static
/*     */   {
/*  58 */     libURL = URL.make("home:libraries/");
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*  63 */     String s = libURL.unalias();
/*  64 */     libSubdir = s.substring(0, s.length() - 1);
/*     */   }
/*     */   
/*     */ 
/*  68 */   public static String getLibSubdir() { return libSubdir; }
/*     */   
/*  70 */   private Vector<Library> libraries = new Vector();
/*  71 */   private int queue = 0;
/*     */   
/*  73 */   private boolean iconsVisible = false;
/*     */   private Cursor dragCursor;
/*     */   private Cursor cantCursor;
/*     */   private LibraryEntry leftClickedOn;
/*     */   
/*  78 */   public LibrariesTile() { setBackground(Color.lightGray);
/*     */     
/*     */ 
/*  81 */     Vector<String> libNames = new FileList(libSubdir, "library").getList();
/*     */     
/*     */ 
/*  84 */     Enumeration<String> e = libNames.elements();
/*  85 */     while (e.hasMoreElements()) {
/*  86 */       Library lib = Library.load(
/*  87 */         URL.make(libURL, (String)e.nextElement()));
/*  88 */       if (lib != null)
/*  89 */         addLibrary(lib);
/*     */     }
/*  91 */     if (this.libraries.size() != 0)
/*  92 */       select(0);
/*  93 */     Main.register(this);
/*     */   }
/*     */   
/*     */   public synchronized void addLibrary()
/*     */   {
/*  98 */     if (this.queue == 0) {
/*  99 */       this.queue = 1;
/*     */     }
/*     */   }
/*     */   
/*     */   public synchronized void addElement() {
/* 104 */     if (this.queue == 0) {
/* 105 */       this.queue = 2;
/*     */     }
/*     */   }
/*     */   
/*     */   public synchronized void mainCallback() {
/* 110 */     switch (this.queue) {
/*     */     case 1: 
/* 112 */       syncAddLibrary(new Library());
/* 113 */       break;
/*     */     case 2: 
/* 115 */       syncAddElement(new LibraryEntry());
/*     */     }
/*     */     
/* 118 */     this.queue = 0;
/*     */   }
/*     */   
/*     */ 
/*     */   private boolean isUniqueLibraryURL(URL name)
/*     */   {
/* 124 */     if (name != null) {
/* 125 */       Enumeration<Library> e = this.libraries.elements();
/* 126 */       while (e.hasMoreElements()) {
/* 127 */         Library lib = (Library)e.nextElement();
/* 128 */         if (lib.getSourceURL().equals(name))
/* 129 */           return false;
/*     */       }
/* 131 */       return true;
/*     */     }
/* 133 */     return false;
/*     */   }
/*     */   
/*     */   private boolean isUniqueLibraryName(String name)
/*     */   {
/* 138 */     if (name != null) {
/* 139 */       Enumeration<Library> e = this.libraries.elements();
/* 140 */       while (e.hasMoreElements()) {
/* 141 */         Library lib = (Library)e.nextElement();
/* 142 */         if (lib.getName().equals(name))
/* 143 */           return false;
/*     */       }
/* 145 */       return true;
/*     */     }
/* 147 */     return false;
/*     */   }
/*     */   
/*     */   private void syncAddLibrary(Library lib)
/*     */   {
/* 152 */     boolean newEntry = lib.getNameMaybeNull() == null;
/*     */     
/*     */ 
/* 155 */     if (!isUniqueLibraryURL(lib.getSourceURL()))
/*     */     {
/* 157 */       int i = 1;
/*     */       URL url;
/* 159 */       do { url = URL.make(libURL, "lib" + i++ + ".library");
/* 160 */       } while (!isUniqueLibraryURL(url));
/* 161 */       lib.setSourceURL(url);
/*     */     }
/*     */     
/*     */ 
/* 165 */     if (this.libraries.size() == 0) {
/* 166 */       File f = new File(libSubdir);
/* 167 */       f.mkdir();
/*     */     }
/*     */     
/*     */ 
/* 171 */     if (!isUniqueLibraryName(lib.getNameMaybeNull()))
/*     */     {
/* 173 */       int num = 1;
/*     */       String libName;
/* 175 */       do { libName = "Category" + num++;
/* 176 */       } while (!isUniqueLibraryName(libName));
/* 177 */       lib.setName(libName);
/*     */     }
/* 179 */     if (saveAllowed()) {
/* 180 */       lib.save();
/*     */     } else
/* 182 */       Console.println(Console.message("AllowChangeLibrary"));
/* 183 */     addLibrary(lib);
/*     */     
/* 185 */     if (newEntry) {
/* 186 */       select(this.libraries.indexOf(lib));
/*     */     }
/*     */   }
/*     */   
/*     */   private void syncDeleteLibrary(Library lib) {
/* 191 */     if (saveAllowed()) {
/* 192 */       lib.delete();
/*     */     } else
/* 194 */       Console.println(Console.message("AllowChangeLibrary"));
/* 195 */     int index = this.libraries.indexOf(lib);
/* 196 */     this.libraries.removeElementAt(index);
/* 197 */     removeItem(index);
/*     */   }
/*     */   
/*     */   private void syncAddElement(LibraryEntry ent)
/*     */   {
/* 202 */     if (this.libraries.size() != 0) {
/* 203 */       int selected = selected();
/* 204 */       Library lib = (Library)this.libraries.elementAt(selected);
/* 205 */       lib.add(ent);
/*     */     }
/*     */   }
/*     */   
/*     */   private void addLibrary(Library lib)
/*     */   {
/* 211 */     int count = this.libraries.size();
/* 212 */     String name = lib.getName();
/*     */     
/* 214 */     for (int i = 0; i < count; i++) {
/* 215 */       if (name.compareTo(((Library)this.libraries.elementAt(i)).getName()) < 0)
/*     */         break;
/*     */     }
/* 218 */     this.libraries.insertElementAt(lib, i);
/* 219 */     insertItem(i, lib.getName(), 
/* 220 */       new ScrollingImagePanel(this, lib.getContents(), 
/* 221 */       this.iconsVisible));
/* 222 */     lib.setEventHandler(this);
/* 223 */     lib.setOwningDialog(this);
/*     */   }
/*     */   
/* 226 */   public boolean isIconsVisible() { return this.iconsVisible; }
/*     */   
/*     */   public void setIconsVisible(boolean showIcons) {
/* 229 */     this.iconsVisible = showIcons;
/* 230 */     int count = this.libraries.size();
/*     */     
/* 232 */     for (int i = 0; i < count; i++) {
/* 233 */       ScrollingImagePanel c = (ScrollingImagePanel)getComponent(i);
/* 234 */       c.setIconsVisible(showIcons);
/*     */     }
/*     */   }
/*     */   
/*     */   public void libraryChanged(Library lib)
/*     */   {
/* 240 */     Library selected = (Library)this.libraries.elementAt(selected());
/* 241 */     int index = this.libraries.indexOf(lib);
/* 242 */     this.libraries.removeElementAt(index);
/* 243 */     removeItem(index);
/* 244 */     addLibrary(lib);
/* 245 */     select(this.libraries.indexOf(selected));
/* 246 */     if (saveAllowed()) {
/* 247 */       lib.save();
/*     */     } else {
/* 249 */       Console.println(Console.message("AllowChangeLibrary"));
/*     */     }
/*     */   }
/*     */   
/*     */   private boolean saveAllowed() {
/* 254 */     return IniFile.gamma().getIniInt("AllowChangeLibrary", 0) == 1;
/*     */   }
/*     */   
/*     */   private Library getLibrary(ScrollingImagePanel panel)
/*     */   {
/* 259 */     int count = this.libraries.size();
/* 260 */     for (int i = 0; i < count; i++)
/* 261 */       if (getComponent(i) == panel)
/* 262 */         return (Library)this.libraries.elementAt(i);
/* 263 */     return null;
/*     */   }
/*     */   
/*     */   private LibraryEntry getLibraryEntry(Component comp, Point location)
/*     */   {
/* 268 */     if ((comp instanceof ScrollingImagePanel)) {
/* 269 */       ScrollingImagePanel panel = (ScrollingImagePanel)comp;
/* 270 */       Library lib = getLibrary(panel);
/* 271 */       if (lib != null) {
/* 272 */         int item = panel.itemAt(location);
/* 273 */         if (item >= 0)
/* 274 */           return lib.getEntry(item);
/*     */       }
/*     */     }
/* 277 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */   private boolean maybeMoveEntry(LibraryEntry src, Component comp, Point loc)
/*     */   {
/* 283 */     LibraryEntry dst = getLibraryEntry(comp, loc);
/* 284 */     if ((dst != null) && (dst != src)) {
/* 285 */       Library srcOwner = (Library)src.getOwner();
/* 286 */       Library dstOwner = (Library)dst.getOwner();
/* 287 */       assert (srcOwner == dstOwner);
/* 288 */       srcOwner.move(src, dst);
/* 289 */       return true;
/*     */     }
/* 291 */     if (comp == this) {
/* 292 */       Library newLib = (Library)this.libraries.elementAt(itemAt(loc));
/* 293 */       Library oldLib = (Library)src.getOwner();
/* 294 */       if (newLib != oldLib) {
/* 295 */         oldLib.delete(src);
/* 296 */         newLib.add(src);
/* 297 */         return true;
/*     */       }
/*     */     }
/* 300 */     return false;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void clickEvent(Component who, Point location, int flags)
/*     */   {
/* 309 */     if ((flags & 0x1) != 0)
/*     */     {
/* 311 */       if (((flags & 0x4) != 0) && (who == this)) {
/* 312 */         Console.getFrame().getEditTile().viewProperties(
/* 313 */           this.libraries.elementAt(itemAt(location))); } else { LibraryEntry ent;
/* 314 */         if ((ent = getLibraryEntry(who, location)) != null) {
/* 315 */           this.leftClickedOn = null;
/* 316 */           if (ent != null) {
/* 317 */             if ((flags & 0x4) != 0) {
/* 318 */               Console.getFrame().getEditTile().viewProperties(ent);
/*     */             } else {
/* 320 */               this.leftClickedOn = ent;
/* 321 */               Console console = Console.getActive();
/* 322 */               if (this.dragCursor == null) {
/* 323 */                 this.dragCursor = new Cursor(
/* 324 */                   URL.make("home:drag.cur"));
/*     */               } else {
/* 326 */                 this.dragCursor.detach();
/* 327 */                 console.addCursor(this.dragCursor);
/* 328 */                 this.dragCursor.activate();
/*     */               }
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/* 334 */     } else if ((flags & 0x2) != 0) {
/* 335 */       Cursor active = Cursor.getActive();
/* 336 */       if ((active != null) && ((active == this.dragCursor) || 
/* 337 */         (active == this.cantCursor)))
/* 338 */         Console.getActive().getCursor().activate();
/* 339 */       if (this.leftClickedOn != null) {
/* 340 */         if (!maybeMoveEntry(this.leftClickedOn, who, location)) {
/* 341 */           URL url = this.leftClickedOn.getContentURL();
/* 342 */           String propName = this.leftClickedOn.getPropertyName(true);
/* 343 */           if (url != null)
/* 344 */             Console.getFrame().getEditTile().libraryDrop(
/* 345 */               url, propName, who, location);
/*     */         }
/* 347 */         this.leftClickedOn = null;
/*     */       }
/*     */     }
/*     */     else {
/* 351 */       Cursor active = Cursor.getActive();
/* 352 */       boolean isDropTarget = who instanceof LibraryDropTarget;
/* 353 */       if (active == this.dragCursor) {
/* 354 */         if (!isDropTarget) {
/* 355 */           Console console = Console.getActive();
/* 356 */           if (this.cantCursor == null) {
/* 357 */             this.cantCursor = new Cursor(
/* 358 */               URL.make("system:CANNOT_CURSOR"));
/*     */           } else {
/* 360 */             this.cantCursor.detach();
/* 361 */             console.addCursor(this.cantCursor);
/* 362 */             this.cantCursor.activate();
/*     */           }
/* 364 */           this.cantCursor.activate();
/*     */         }
/* 366 */       } else if ((active != null) && (active == this.cantCursor) && 
/* 367 */         (isDropTarget)) {
/* 368 */         this.dragCursor.activate();
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public Object properties(int index, int offset, int mode, Object value)
/*     */     throws NoSuchPropertyException
/*     */   {
/* 376 */     Object ret = null;
/* 377 */     switch (index - offset) {
/*     */     case 0: 
/* 379 */       if (mode == 0) {
/* 380 */         ret = PropAdder.make(
/* 381 */           new VectorProperty(this, index, "Contents"));
/* 382 */       } else if (mode == 1) {
/* 383 */         ret = this.libraries;
/* 384 */       } else if (mode == 3) {
/* 385 */         syncAddLibrary((Library)value);
/* 386 */       } else if (mode == 4) {
/* 387 */         syncDeleteLibrary((Library)value);
/* 388 */       } else if ((mode == 5) && ((value instanceof Library)))
/* 389 */         ret = value;
/* 390 */       break;
/*     */     default: 
/* 392 */       throw new NoSuchPropertyException();
/*     */     }
/* 394 */     return ret;
/*     */   }
/*     */   
/* 397 */   public Object propertyParent() { return null; }
/*     */   
/* 399 */   public String toString() { return "Libraries"; }
/*     */ }


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