1 | /**
|
---|
2 | * MagneticConstraints.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.items.MagneticConstraint;
|
---|
20 |
|
---|
21 | import java.util.*;
|
---|
22 | //import java.util.concurrent.*;
|
---|
23 |
|
---|
24 | import org.expeditee.items.Item;
|
---|
25 |
|
---|
26 | public class MagneticConstraints {
|
---|
27 | public static List<String> log = new LinkedList<String>();
|
---|
28 |
|
---|
29 | private static MagneticConstraints instance = null;
|
---|
30 |
|
---|
31 | private MagneticConstraintAction rightBorderHitAction = null;
|
---|
32 | private MagneticConstraintAction endOfLineHitAction = null;
|
---|
33 | private MagneticConstraintAction leftBorderHitAction = null;
|
---|
34 | private MagneticConstraintAction startOfLineHitAction = null;
|
---|
35 | private MagneticConstraintActionWithArguments<Float> textShrunkAction = null;
|
---|
36 | private MagneticConstraintActionWithArguments<Float> textGrownAction = null;
|
---|
37 | private MagneticConstraintAction topBorderHitAction = null;
|
---|
38 | private MagneticConstraintAction bottomBorderHitAction = null;
|
---|
39 | private Map<Integer, MagneticConstraintAction> keyEvents = new LinkedHashMap<Integer, MagneticConstraintAction>();
|
---|
40 |
|
---|
41 | // private final ExecutorService thread = Executors.newSingleThreadExecutor();
|
---|
42 |
|
---|
43 | // private boolean textShrunkRunnable = true;
|
---|
44 | // private boolean textGrownRunnable = true;
|
---|
45 |
|
---|
46 | public static MagneticConstraints getInstance() {
|
---|
47 | if (instance == null) {
|
---|
48 | instance = new MagneticConstraints();
|
---|
49 | return instance;
|
---|
50 | } else
|
---|
51 | return instance;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public static void Log(final Class<? extends MagneticConstraintAction> actionType,
|
---|
55 | final Item[] primaryInvolved, final Item[] line, final String additionalInfo) {
|
---|
56 | final String nl = System.getProperty("line.separator");
|
---|
57 |
|
---|
58 | final String heading = "Action type: " + actionType;
|
---|
59 | final String[] primaryInvolvedStringed = new String[primaryInvolved.length];
|
---|
60 | for(int i = 0; i < primaryInvolved.length; i++)
|
---|
61 | primaryInvolvedStringed[i] = "\t" + "Primary item " + i + ". Text: " + primaryInvolved[i].getText();
|
---|
62 | String lineContent = "\tLine content: ";
|
---|
63 | for(final Item i : line) lineContent += (i.getText() + " ");
|
---|
64 | String logContent = heading + nl;
|
---|
65 | for(final String s : primaryInvolvedStringed) logContent += (s + nl);
|
---|
66 | logContent += (lineContent + nl + additionalInfo + nl);
|
---|
67 |
|
---|
68 | log.add(logContent);
|
---|
69 | }
|
---|
70 |
|
---|
71 | public boolean keyHit(final int key, final Item item) {
|
---|
72 | final MagneticConstraintAction action = keyEvents.get(key);
|
---|
73 | if(action == null && key < 0 && keyEvents.get(-key) != null) {
|
---|
74 | return keyEvents.get(-key).invert(item);
|
---|
75 | } else if(action == null) return false;
|
---|
76 | else return action.exec(item);
|
---|
77 | }
|
---|
78 |
|
---|
79 | public void setKeyAction(final int key, final MagneticConstraintAction action) {
|
---|
80 | keyEvents.put(key, action);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public boolean rightBorderHit(final Item item) {
|
---|
84 | if (rightBorderHitAction != null)
|
---|
85 | return rightBorderHitAction.exec(item);
|
---|
86 | else
|
---|
87 | return false;
|
---|
88 | }
|
---|
89 |
|
---|
90 | // public boolean rightBorderHit(final Item item) {
|
---|
91 | // try {
|
---|
92 | // if(rightBorderHitAction != null) {
|
---|
93 | // final FutureTask<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() {
|
---|
94 | // @Override
|
---|
95 | // public Boolean call() throws Exception {
|
---|
96 | // return rightBorderHitAction.exec(item);
|
---|
97 | // }
|
---|
98 | // });
|
---|
99 | // thread.execute(future);
|
---|
100 | // return future.get();
|
---|
101 | // } else return false;
|
---|
102 | // } catch (final Exception e) {
|
---|
103 | // System.err.println("Concurrent Exception caught during rightBorderHit action");
|
---|
104 | // return false;
|
---|
105 | // }
|
---|
106 | // }
|
---|
107 |
|
---|
108 | public void setRightBorderHitAction(final MagneticConstraintAction action) {
|
---|
109 | rightBorderHitAction = action;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public boolean endOfLineHit(final Item item) {
|
---|
113 | if (endOfLineHitAction != null)
|
---|
114 | return endOfLineHitAction.exec(item);
|
---|
115 | else
|
---|
116 | return false;
|
---|
117 | }
|
---|
118 |
|
---|
119 | // public boolean endOfLineHit(final Item item) {
|
---|
120 | // try {
|
---|
121 | // if(endOfLineHitAction != null) {
|
---|
122 | // final FutureTask<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() {
|
---|
123 | // @Override
|
---|
124 | // public Boolean call() throws Exception {
|
---|
125 | // return endOfLineHitAction.exec(item);
|
---|
126 | // }
|
---|
127 | // });
|
---|
128 | // thread.execute(future);
|
---|
129 | // return future.get();
|
---|
130 | // } else return false;
|
---|
131 | // } catch (final Exception e) {
|
---|
132 | // System.err.println("Concurrent Exception caught during endOfLineHit action");
|
---|
133 | // return false;
|
---|
134 | // }
|
---|
135 | // }
|
---|
136 |
|
---|
137 | public void setEndOfLineHitAction(final MagneticConstraintAction action) {
|
---|
138 | endOfLineHitAction = action;
|
---|
139 | }
|
---|
140 |
|
---|
141 | public boolean leftBorderHit(final Item item) {
|
---|
142 | if (leftBorderHitAction != null)
|
---|
143 | return leftBorderHitAction.exec(item);
|
---|
144 | else
|
---|
145 | return false;
|
---|
146 | }
|
---|
147 |
|
---|
148 | // public boolean leftBorderHit(final Item item) {
|
---|
149 | // try {
|
---|
150 | // if(leftBorderHitAction != null) {
|
---|
151 | // final FutureTask<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() {
|
---|
152 | // @Override
|
---|
153 | // public Boolean call() throws Exception {
|
---|
154 | // return leftBorderHitAction.exec(item);
|
---|
155 | // }
|
---|
156 | // });
|
---|
157 | // thread.execute(future);
|
---|
158 | // return future.get();
|
---|
159 | // } else return false;
|
---|
160 | // } catch (final Exception e) {
|
---|
161 | // System.err.println("Concurrent Exception caught during leftBorderHit action");
|
---|
162 | // return false;
|
---|
163 | // }
|
---|
164 | // }
|
---|
165 |
|
---|
166 | public void setLeftBorderHitAction(final MagneticConstraintAction action) {
|
---|
167 | leftBorderHitAction = action;
|
---|
168 | }
|
---|
169 |
|
---|
170 | public boolean startOfLineHit(final Item item) {
|
---|
171 | if (startOfLineHitAction != null)
|
---|
172 | return startOfLineHitAction.exec(item);
|
---|
173 | else
|
---|
174 | return false;
|
---|
175 | }
|
---|
176 |
|
---|
177 | // public boolean startOfLineHit(final Item item) {
|
---|
178 | // try {
|
---|
179 | // if(startOfLineHitAction != null) {
|
---|
180 | // final FutureTask<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() {
|
---|
181 | // @Override
|
---|
182 | // public Boolean call() throws Exception {
|
---|
183 | // return startOfLineHitAction.exec(item);
|
---|
184 | // }
|
---|
185 | // });
|
---|
186 | // thread.execute(future);
|
---|
187 | // return future.get();
|
---|
188 | // } else return false;
|
---|
189 | // } catch (final Exception e) {
|
---|
190 | // System.err.println("Concurrent Exception caught during startOfLineHit action");
|
---|
191 | // return false;
|
---|
192 | // }
|
---|
193 | // }
|
---|
194 |
|
---|
195 | public void setStartOfLineHitAction(final MagneticConstraintAction action) {
|
---|
196 | startOfLineHitAction = action;
|
---|
197 | }
|
---|
198 |
|
---|
199 | public boolean textShrunk(final Item item, final float delta) {
|
---|
200 | if(/*textShrunkRunnable &&*/ textShrunkAction != null)
|
---|
201 | return textShrunkAction.exec(item, delta);
|
---|
202 | else return false;
|
---|
203 | }
|
---|
204 |
|
---|
205 | // public void suspendTextShrunkAction() {
|
---|
206 | // textShrunkRunnable = false;
|
---|
207 | // }
|
---|
208 | //
|
---|
209 | // public void resumeTextShrunkAction() {
|
---|
210 | // textShrunkRunnable = true;
|
---|
211 | // }
|
---|
212 |
|
---|
213 | // public boolean textShrunk(final Item item, final float delta) {
|
---|
214 | // try {
|
---|
215 | // if(textShrunkAction != null && textShrunkRunnable) {
|
---|
216 | // final FutureTask<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() {
|
---|
217 | // @Override
|
---|
218 | // public Boolean call() throws Exception {
|
---|
219 | // return textShrunkAction.exec(item, delta);
|
---|
220 | // }
|
---|
221 | // });
|
---|
222 | // thread.execute(future);
|
---|
223 | // return future.get();
|
---|
224 | // } else return false;
|
---|
225 | // } catch (final Exception e) {
|
---|
226 | // System.err.println("Concurrent Exception caught during textShrunk action");
|
---|
227 | // return false;
|
---|
228 | // }
|
---|
229 | // }
|
---|
230 |
|
---|
231 | public void setTextShrunkAction(final MagneticConstraintActionWithArguments<Float> action) {
|
---|
232 | textShrunkAction = action;
|
---|
233 | }
|
---|
234 |
|
---|
235 | public boolean textGrown(final Item item, final float delta) {
|
---|
236 | if(/*textGrownRunnable &&*/ textGrownAction != null)
|
---|
237 | return textGrownAction.exec(item, delta);
|
---|
238 | else return false;
|
---|
239 | }
|
---|
240 |
|
---|
241 | // public void suspendTextGrownAction() {
|
---|
242 | // textGrownRunnable = false;
|
---|
243 | // }
|
---|
244 | //
|
---|
245 | // public void resumeTextGrownAction() {
|
---|
246 | // textGrownRunnable = true;
|
---|
247 | // }
|
---|
248 |
|
---|
249 | // public boolean textGrown(final Item item, final float delta) {
|
---|
250 | // try {
|
---|
251 | // if(textGrownAction != null && textGrownRunnable) {
|
---|
252 | // final FutureTask<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() {
|
---|
253 | // @Override
|
---|
254 | // public Boolean call() throws Exception {
|
---|
255 | // return textGrownAction.exec(item, delta);
|
---|
256 | // }
|
---|
257 | // });
|
---|
258 | // thread.execute(future);
|
---|
259 | // return future.get();
|
---|
260 | // } else return false;
|
---|
261 | // } catch (final Exception e) {
|
---|
262 | // System.err.println("Concurrent Exception caught during textGrown action");
|
---|
263 | // return false;
|
---|
264 | // }
|
---|
265 | // }
|
---|
266 |
|
---|
267 | public void setTextGrownAction(final MagneticConstraintActionWithArguments<Float> action) {
|
---|
268 | textGrownAction = action;
|
---|
269 | }
|
---|
270 |
|
---|
271 | public boolean topBorderHit(final Item item) {
|
---|
272 | if(topBorderHitAction != null)
|
---|
273 | return topBorderHitAction.exec(item);
|
---|
274 | else return false;
|
---|
275 | }
|
---|
276 |
|
---|
277 | // public boolean topBorderHit(final Item item) {
|
---|
278 | // try {
|
---|
279 | // if(topBorderHitAction != null) {
|
---|
280 | // final FutureTask<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() {
|
---|
281 | // @Override
|
---|
282 | // public Boolean call() throws Exception {
|
---|
283 | // return topBorderHitAction.exec(item);
|
---|
284 | // }
|
---|
285 | // });
|
---|
286 | // thread.execute(future);
|
---|
287 | // return future.get();
|
---|
288 | // } else return false;
|
---|
289 | // } catch (final Exception e) {
|
---|
290 | // System.err.println("Concurrent Exception caught during topBorderHit action");
|
---|
291 | // return false;
|
---|
292 | // }
|
---|
293 | // }
|
---|
294 |
|
---|
295 | public void setTopBorderHitAction(final MagneticConstraintAction action) {
|
---|
296 | topBorderHitAction = action;
|
---|
297 | }
|
---|
298 |
|
---|
299 | public boolean bottomBorderHit(final Item item) {
|
---|
300 | if(bottomBorderHitAction != null)
|
---|
301 | return bottomBorderHitAction.exec(item);
|
---|
302 | else return false;
|
---|
303 | }
|
---|
304 |
|
---|
305 | // public boolean bottomBorderHit(final Item item) {
|
---|
306 | // try {
|
---|
307 | // if(bottomBorderHitAction != null) {
|
---|
308 | // final FutureTask<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() {
|
---|
309 | // @Override
|
---|
310 | // public Boolean call() throws Exception {
|
---|
311 | // return bottomBorderHitAction.exec(item);
|
---|
312 | // }
|
---|
313 | // });
|
---|
314 | // thread.execute(future);
|
---|
315 | // return future.get();
|
---|
316 | // } else return false;
|
---|
317 | // } catch (final Exception e) {
|
---|
318 | // System.err.println("Concurrent Exception caught during bottomBorderHit action");
|
---|
319 | // return false;
|
---|
320 | // }
|
---|
321 | // }
|
---|
322 |
|
---|
323 | public void setBottomBorderHitAction(final MagneticConstraintAction action) {
|
---|
324 | bottomBorderHitAction = action;
|
---|
325 | }
|
---|
326 | }
|
---|