View Javadoc

1   /*
2    * @(#)$Id:$
3    *
4    * Author		:	Ueli Kurmann, igesture@uelikurmann.ch
5    *                  
6    *
7    * Purpose		: 
8    *
9    * -----------------------------------------------------------------------
10   *
11   * Revision Information:
12   *
13   * Date				Who			Reason
14   *
15   * 16.12.2008			ukurmann	Initial Release
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  
27  package org.ximtec.igesture.tool.gesturevisualisation;
28  
29  import java.awt.Color;
30  import java.awt.Dimension;
31  import java.lang.reflect.InvocationTargetException;
32  import java.lang.reflect.Method;
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  import javax.swing.BorderFactory;
37  
38  import org.ximtec.igesture.core.Gesture;
39  import org.ximtec.igesture.core.GestureSample;
40  import org.ximtec.igesture.core.GestureSample3D;
41  import org.ximtec.igesture.io.GestureDevice;
42  import org.ximtec.igesture.io.GestureDevicePanel;
43  import org.ximtec.igesture.io.mouseclient.SwingMouseReader;
44  
45  
46  /**
47   * Comment
48   * @version 1.0 16.12.2008
49   * @author Ueli Kurmann
50   */
51  public class InputPanelFactory {
52  
53     private static final int INPUTAREA_SIZE = 200;
54     
55     private static Map<Class< ? >, Class< ? extends GesturePanel>> gesturePanels;
56     private static Map<Class< ? >, Class< ? extends InputPanel>> inputPanels;
57  
58     static {
59        gesturePanels = new HashMap<Class< ? >, Class< ? extends GesturePanel>>();
60        gesturePanels.put(GestureSample.class, NoteGesturePanel.class);
61        gesturePanels.put(GestureSample3D.class, RecordedGesture3DPanel.class);
62        //TODO create one for Gesture3D
63  
64        //TODO load from config file
65        inputPanels = new HashMap<Class< ? >, Class< ? extends InputPanel>>();
66        inputPanels.put(SwingMouseReader.class, SwingMouseReaderPanel.class);
67     }
68  
69  
70     /**
71      * Creates a new gesture panel
72      * @param clazz
73      * @param gesture
74      * @return
75      */
76     public static GesturePanel createGesturePanel(Gesture< ? > gesture) {
77  
78        GesturePanel panel = null;
79        if (gesture != null) {
80           Class< ? extends GesturePanel> panelClass = gesturePanels.get(gesture
81                 .getClass());
82           if (panelClass == null) {
83              panelClass = NotSupportedGesturePanel.class;
84           }
85  
86           try {
87              panel = panelClass.newInstance();
88              panel.init(gesture);
89           }
90           catch (Exception e) {
91  
92              e.printStackTrace();
93           }
94        }
95  
96        return panel;
97     }
98  
99  
100    /**
101     * Creates a new input panel.
102     * @param gestureDevice
103     * @return
104     */
105    public static InputPanel createInputPanel(GestureDevice< ? , ? > gestureDevice) {
106 
107       InputPanel panel = null;
108 
109       if (gestureDevice != null) {
110 
111          Class< ? extends InputPanel> panelClass = null;
112          Class< ? > clazz = gestureDevice.getClass();
113          
114          // lookup super classes
115          while (panelClass == null && !clazz.equals(Object.class)) {
116             panelClass = inputPanels.get(clazz);
117             clazz = clazz.getSuperclass();
118          }
119 
120          if (panelClass == null) {
121             panelClass = NotSupportedInputPanel.class;
122          }
123 
124          try {
125             panel = panelClass.newInstance();
126             panel.init(gestureDevice);
127          }
128          catch (Exception e) {
129 
130             e.printStackTrace();
131          }
132       }
133       return panel;
134    }
135    
136    public static GestureDevicePanel createPanel(GestureDevice<?,?> gestureDevice)
137    {
138 	   GestureDevicePanel panel = null;
139 	   if(gestureDevice != null)
140 	   {
141 		   Class<?> clazz = gestureDevice.getClass();
142 		   try {
143 			Method getPanelMethod = clazz.getMethod("getPanel");
144 			getPanelMethod.setAccessible(true);
145 			panel = (GestureDevicePanel) getPanelMethod.invoke(gestureDevice);
146 			panel.setSize(new Dimension(INPUTAREA_SIZE,INPUTAREA_SIZE));
147 			panel.setPreferredSize(new Dimension(INPUTAREA_SIZE,INPUTAREA_SIZE));
148 			panel.setOpaque(true);
149 			panel.setBackground(Color.WHITE);
150 			panel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
151 		} catch (SecurityException e) {
152 			e.printStackTrace();
153 		} catch (NoSuchMethodException e) {
154 			e.printStackTrace();
155 		} catch (IllegalArgumentException e) {
156 			e.printStackTrace();
157 		} catch (IllegalAccessException e) {
158 			e.printStackTrace();
159 		} catch (InvocationTargetException e) {
160 			e.printStackTrace();
161 		}
162 		   
163 		   
164 	   }
165 	   return panel;
166    }
167 
168 }