KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > widgets > WidgetTable


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.widgets;
12
13
14 import org.eclipse.swt.internal.photon.*;
15
16 class WidgetTable {
17     static int FreeSlot = 0;
18     static int GrowSize = 1024;
19     static int [] IndexTable = new int [GrowSize];
20     static Widget [] WidgetTable = new Widget [GrowSize];
21     static int ArgPtr = OS.malloc (4);
22     static int [] ArgBuffer = new int [1];
23     static int [] GetArgs = new int [] {OS.Pt_ARG_USER_DATA, 0, 0};
24     static {
25         for (int i=0; i<GrowSize-1; i++) IndexTable [i] = i + 1;
26         IndexTable [GrowSize - 1] = -1;
27     }
28 public static synchronized Widget get (int handle) {
29     if (handle == 0) return null;
30     GetArgs [1] = 0;
31     OS.PtGetResources (handle, GetArgs.length / 3, GetArgs);
32     if (GetArgs [1] == 0) return null;
33     OS.memmove (ArgBuffer, GetArgs [1], 4);
34     if (ArgBuffer [0] == 0) return null;
35     int index = ArgBuffer [0] - 1;
36     if (0 <= index && index < WidgetTable.length) return WidgetTable [index];
37     return null;
38 }
39 public synchronized static void put(int handle, Widget widget) {
40     if (handle == 0) return;
41     if (FreeSlot == -1) {
42         int length = (FreeSlot = IndexTable.length) + GrowSize;
43         int[] newIndexTable = new int[length];
44         Widget[] newWidgetTable = new Widget [length];
45         System.arraycopy (IndexTable, 0, newIndexTable, 0, FreeSlot);
46         System.arraycopy (WidgetTable, 0, newWidgetTable, 0, FreeSlot);
47         for (int i = FreeSlot; i < length - 1; i++) {
48             newIndexTable[i] = i + 1;
49         }
50         newIndexTable[length - 1] = -1;
51         IndexTable = newIndexTable;
52         WidgetTable = newWidgetTable;
53     }
54     ArgBuffer [0] = FreeSlot + 1;
55     OS.memmove (ArgPtr, ArgBuffer, 4);
56     OS.PtSetResource (handle, OS.Pt_ARG_USER_DATA, ArgPtr, 4);
57     int oldSlot = FreeSlot;
58     FreeSlot = IndexTable[oldSlot];
59     IndexTable [oldSlot] = -2;
60     WidgetTable [oldSlot] = widget;
61 }
62 public static synchronized Widget remove (int handle) {
63     if (handle == 0) return null;
64     GetArgs [1] = 0;
65     OS.PtGetResources (handle, GetArgs.length / 3, GetArgs);
66     if (GetArgs [1] == 0) return null;
67     OS.memmove (ArgBuffer, GetArgs [1], 4);
68     if (ArgBuffer [0] == 0) return null;
69     int index = ArgBuffer [0] - 1;
70     Widget widget = null;
71     if (0 <= index && index < WidgetTable.length) {
72         widget = WidgetTable [index];
73         WidgetTable [index] = null;
74         IndexTable [index] = FreeSlot;
75         FreeSlot = index;
76         ArgBuffer [0] = 0;
77         OS.memmove (ArgPtr, ArgBuffer, 4);
78         OS.PtSetResource (handle, OS.Pt_ARG_USER_DATA, ArgPtr, 4);
79     }
80     return widget;
81 }
82 public static synchronized Shell [] shells () {
83     int length = 0;
84     for (int i=0; i<WidgetTable.length; i++) {
85         Widget widget = WidgetTable [i];
86         if (widget != null && widget instanceof Shell) length++;
87     }
88     int index = 0;
89     Shell [] result = new Shell [length];
90     for (int i=0; i<WidgetTable.length; i++) {
91         Widget widget = WidgetTable [i];
92         if (widget != null && widget instanceof Shell) {
93             int j = 0;
94             while (j < index) {
95                 if (result [j] == widget) break;
96                 j++;
97             }
98             if (j == index) result [index++] = (Shell) widget;
99         }
100     }
101     if (index == length) return result;
102     Shell [] newResult = new Shell [index];
103     System.arraycopy (result, 0, newResult, 0, index);
104     return newResult;
105 }
106 public static synchronized int size () {
107     int size = 0;
108     for (int i=0; i<WidgetTable.length; i++) {
109         if (WidgetTable [i] != null) size++;
110     }
111     return size;
112 }
113 }
114
Popular Tags