View Javadoc

1   package org.catacomb.druid.swing.dnd;
2   
3   
4   
5   public class RegionStore {
6   
7   
8       int[][] regs;
9       Object[] refs;
10      String[] tags;
11      int[] actions;
12  
13      int nreg;
14  
15      Object active;
16      int[] activeLims;
17      String activeTag;
18  
19  
20      Region dragOverRegion;
21      Region hoverOverRegion;
22      Region pressRegion;
23  
24      public RegionStore() {
25          regs = new int[20][4];
26          refs = new Object[20];
27          tags = new String[20];
28          actions = new int[20];
29      }
30  
31  
32      public void clear() {
33          nreg = 0;
34      }
35  
36  
37      public void addRegion(int[] xywh, Object obj, String s, int acts) {
38          addRegion(xywh[0], xywh[1], xywh[2], xywh[3], obj, s, acts);
39      }
40  
41  
42      public void addLengthenedRegion(int[] xywh, Object obj) {
43          addRegion(xywh[0] - 6, xywh[1], xywh[2] + 12, xywh[3], obj, null, Region.CLICK);
44      }
45  
46  
47  
48      public void addRegion(int x, int y, int w, int h, Object obj, String flav, int acts) {
49          if (nreg == regs.length) {
50              int nn = (3 * nreg) / 2;
51              int[][] newregs = new int[nn][4];
52              Object[] newrefs = new Object[nn];
53              String[] newtags = new String[nn];
54              int[] newactions = new int[nn];
55              // keep memory together - any advantage?
56              for (int i = 0; i < nreg; i++) {
57                  for (int j = 0; j < 4; j++) {
58                      newregs[i][j] = regs[i][j];
59                  }
60                  newrefs[i] = refs[i];
61                  newtags[i] = tags[i];
62                  newactions[i] = actions[i];
63              }
64              regs = newregs;
65              refs = newrefs;
66              tags = newtags;
67              actions = newactions;
68          }
69  
70          regs[nreg][0] = x;
71          regs[nreg][1] = x + w;
72          regs[nreg][2] = y - h;
73          regs[nreg][3] = y;
74  
75          refs[nreg] = obj;
76  
77          tags[nreg] = flav;
78  
79          actions[nreg] = acts;
80  
81          nreg++;
82      }
83  
84  
85  
86      public Object activeRegion(int x, int y) {
87          if (active != null && within(x, y, activeLims)) {
88              // same as before;
89          } else {
90  
91              active = null;
92              for (int i = 0; i < nreg; i++) {
93                  if (within(x, y, regs[i])) {
94                      active = refs[i];
95                      activeLims = regs[i];
96                      activeTag = tags[i];
97                      break;
98                  }
99              }
100         }
101 
102         return active;
103     }
104 
105 
106     public int[] getActiveLimits() {
107         return activeLims;
108     }
109 
110 
111     public String getActiveTag() {
112         return activeTag;
113     }
114 
115 
116     private final boolean within(int x, int y, int[] rr) {
117         return (x > rr[0] && x < rr[1] && y > rr[2] && y < rr[3]);
118     }
119 
120 
121     public Object getActive() {
122         return active;
123     }
124 
125 
126     public boolean hasActive() {
127         return (active != null);
128     }
129 
130 
131     public void clearActive() {
132         active = null;
133     }
134 
135 
136     public void dragOver(int x, int y) {
137 
138         if (dragOverRegion != null && dragOverRegion.contains(x, y)) {
139             // same as before;
140         } else {
141             dragOverRegion = null;
142             for (int i = 0; i < nreg; i++) {
143                 if (within(x, y, regs[i])) {
144 
145                     dragOverRegion = new Region(regs[i], tags[i], refs[i], actions[i]);
146                     break;
147                 }
148             }
149         }
150     }
151 
152 
153     public Region getDragOverRegion() {
154         return dragOverRegion;
155     }
156 
157 
158 
159     public void hoverOver(int x, int y) {
160 
161         if (hoverOverRegion != null && hoverOverRegion.contains(x, y)) {
162             // same as before;
163         } else {
164             hoverOverRegion = null;
165             for (int i = 0; i < nreg; i++) {
166                 if (within(x, y, regs[i]) && Region.canPress(actions[i])) {
167 
168                     hoverOverRegion = new Region(regs[i], tags[i], refs[i], actions[i]);
169                     break;
170                 }
171             }
172         }
173     }
174 
175 
176     public Region getHoverRegion() {
177         return hoverOverRegion;
178     }
179 
180 
181     public void press(int x, int y) {
182         pressRegion = null;
183         for (int i = 0; i < nreg; i++) {
184             if (within(x, y, regs[i])) {
185 
186                 pressRegion = new Region(regs[i], tags[i], refs[i], actions[i]);
187                 break;
188             }
189         }
190     }
191 
192 
193 
194     public Region getPressRegion() {
195         return pressRegion;
196     }
197 
198 }