1
2
3
4 package org.ximtec.igesture.core.composite;
5
6 import java.awt.geom.Rectangle2D;
7 import java.net.URL;
8 import java.util.ArrayList;
9 import java.util.Calendar;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Set;
14 import java.util.logging.Level;
15
16 import org.sigtec.ink.Note;
17 import org.sigtec.ink.NoteTool;
18 import org.w3c.dom.Node;
19 import org.w3c.dom.NodeList;
20 import org.ximtec.igesture.util.XMLParser;
21
22
23
24
25
26 public class ConstraintTool {
27
28 private static final Map<String, String> deviceMapping = new HashMap<String,String>();
29
30 static{
31
32 XMLParser parser = new XMLParser(){
33
34 @Override
35 public void execute(ArrayList<NodeList> nodeLists) {
36 String name = ((Node)nodeLists.get(0).item(0)).getNodeValue();
37 String type = ((Node)nodeLists.get(1).item(0)).getNodeValue();
38 deviceMapping.put(name, type);
39 }
40
41 };
42 ArrayList<String> nodes = new ArrayList<String>();
43 nodes.add("name");
44 nodes.add("type");
45 try {
46 URL path = ConstraintTool.class.getClassLoader().getResource("config.xml");
47 parser.parse(path.getFile(),"device", nodes);
48 } catch (Exception e) {
49 e.printStackTrace();
50 };
51 }
52
53 public static String getGestureType(String deviceType)
54 {
55 return deviceMapping.get(deviceType);
56 }
57
58 public static boolean isBoundsDiagonalValid(List<Note> notes, double minDistance, double maxDistance)
59 {
60 boolean valid = true;
61 Rectangle2D bounds = NoteTool.getBounds2D(notes);
62 double w = bounds.getWidth();
63 double h = bounds.getHeight();
64 double d = Math.sqrt(Math.pow(w, 2) + Math.pow(h, 2));
65 if(d < minDistance || d > maxDistance)
66 valid = false;
67 return valid;
68 }
69
70 public static long calculateTimeInMillis(Calendar time)
71 {
72 long t = 0;
73 t += time.get(Calendar.MILLISECOND);
74 t += time.get(Calendar.SECOND)*1000;
75 t += time.get(Calendar.MINUTE)*60*1000;
76 t += time.get(Calendar.HOUR)*24*60*1000;
77 return t;
78 }
79
80 public static void permute(int level, String permuted, boolean used[], String original, Set<String> results)
81 {
82 int length = original.length();
83 if (level == length)
84 {
85 results.add(permuted);
86 }
87 else
88 {
89 for (int i = 0; i < length; i++)
90 {
91 if (!used[i]) {
92 used[i] = true;
93 permute(level + 1, permuted + original.charAt(i), used, original, results);
94 used[i] = false;
95 }
96 }
97 }
98 }
99 }