1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package org.ximtec.igesture.tool.view.admin.panel;
27
28 import java.awt.BorderLayout;
29 import java.awt.Color;
30 import java.awt.Dimension;
31 import java.awt.event.ComponentAdapter;
32 import java.awt.event.ComponentEvent;
33
34 import javax.swing.BorderFactory;
35 import javax.swing.ImageIcon;
36 import javax.swing.JLabel;
37 import javax.swing.JPanel;
38 import javax.swing.JTextField;
39
40 import org.sigtec.graphix.GridBagLayouter;
41 import org.sigtec.ink.Note;
42 import org.ximtec.igesture.core.Gesture;
43 import org.ximtec.igesture.core.GestureClass;
44 import org.ximtec.igesture.core.GestureSet;
45 import org.ximtec.igesture.core.SampleDescriptor;
46 import org.ximtec.igesture.core.SampleDescriptor3D;
47 import org.ximtec.igesture.core.composite.CompositeDescriptor;
48 import org.ximtec.igesture.core.composite.Constraint;
49 import org.ximtec.igesture.tool.GestureConstants;
50 import org.ximtec.igesture.tool.binding.BindingFactory;
51 import org.ximtec.igesture.tool.core.Controller;
52 import org.ximtec.igesture.tool.util.ComponentFactory;
53 import org.ximtec.igesture.tool.util.FormBuilder;
54 import org.ximtec.igesture.tool.util.TitleFactory;
55 import org.ximtec.igesture.tool.view.AbstractPanel;
56 import org.ximtec.igesture.util.GestureTool;
57 import org.ximtec.igesture.util.additions3d.RecordedGesture3D;
58
59 public class GestureSetPanel extends AbstractPanel {
60
61 private GestureSet gestureSet;
62
63 private static final int SPACE_SIZE = 5;
64 private static final int SAMPLE_SIZE = 100;
65
66 public GestureSetPanel(Controller controller, GestureSet gestureSet) {
67 super(controller);
68 this.gestureSet = gestureSet;
69
70 addComponentListener(new ComponentAdapter() {
71 @Override
72 public void componentResized(ComponentEvent e) {
73 updateView();
74 }
75 });
76
77 setTitle(TitleFactory.createDynamicTitle(gestureSet, GestureSet.PROPERTY_NAME));
78 }
79
80 private JPanel createGestureIconList() {
81 JPanel panel = ComponentFactory.createGridBagLayoutPanel();
82
83
84
85 if (getWidth() > 0) {
86
87 int x = 0;
88 int y = 1;
89
90
91 int elementsPerRow = (getWidth() - 20) / (SAMPLE_SIZE + 20);
92 elementsPerRow = elementsPerRow * 2 - 1;
93
94
95 GridBagLayouter.addComponent(panel, createSpacerPanel(SPACE_SIZE), 0, 0);
96
97
98 for (GestureClass gestureClass : gestureSet.getGestureClasses()) {
99 GridBagLayouter.addComponent(panel, createGesturePanel(gestureClass, SAMPLE_SIZE), x, y);
100 GridBagLayouter.addComponent(panel, createSpacerPanel(SPACE_SIZE), x + 1, y);
101 if (x + 1 >= elementsPerRow) {
102 x = 0;
103 y++;
104
105 GridBagLayouter.addComponent(panel, createSpacerPanel(SPACE_SIZE), 0, y);
106 y++;
107 } else {
108
109 x = x + 2;
110 }
111 }
112 }
113
114 return panel;
115 }
116
117 private JPanel createGesturePanel(GestureClass gestureClass, int size) {
118 JPanel gesturePanel = ComponentFactory.createBorderLayoutPanel();
119 JLabel gestureName = new JLabel(gestureClass.getName());
120 gestureName.setHorizontalAlignment(JLabel.CENTER);
121 gesturePanel.add(gestureName, BorderLayout.SOUTH);
122
123 if (gestureClass.hasDescriptor(SampleDescriptor.class)) {
124
125 SampleDescriptor descriptor = gestureClass.getDescriptor(SampleDescriptor.class);
126
127 if (descriptor.getSamples().size() > 0 && descriptor.getSample(0) != null) {
128 Gesture<Note> sample = descriptor.getSample(0);
129 JLabel label = new JLabel(new ImageIcon(GestureTool.createNoteImage(sample.getGesture(), size, size)));
130 gesturePanel.add(label, BorderLayout.CENTER);
131 } else {
132 JLabel labelNsa = getComponentFactory().createLabel(GestureConstants.GESTURE_SET_PANEL_NSA);
133 gesturePanel.add(labelNsa, BorderLayout.CENTER);
134 }
135 } else if(gestureClass.hasDescriptor(SampleDescriptor3D.class)){
136
137 SampleDescriptor3D descriptor = gestureClass.getDescriptor(SampleDescriptor3D.class);
138
139 if (descriptor.getSamples().size() > 0 && descriptor.getSample(0) != null) {
140 Gesture<RecordedGesture3D> sample = descriptor.getSample(0);
141 JLabel label = new JLabel(new ImageIcon(GestureTool.createRecordedGesture3DImage(sample.getGesture(), size, size)));
142 gesturePanel.add(label, BorderLayout.CENTER);
143 } else {
144 JLabel labelNsa = getComponentFactory().createLabel(GestureConstants.GESTURE_SET_PANEL_NSA);
145 gesturePanel.add(labelNsa, BorderLayout.CENTER);
146 }
147 } else if(gestureClass.hasDescriptor(CompositeDescriptor.class)){
148
149 CompositeDescriptor descriptor = gestureClass.getDescriptor(CompositeDescriptor.class);
150
151 if(descriptor.getConstraint() != null)
152 {
153 Constraint c = descriptor.getConstraint();
154 JLabel label = new JLabel(new ImageIcon(GestureTool.createCompositeImage(c, size, size)));
155 gesturePanel.add(label, BorderLayout.CENTER);
156 }
157 else
158 {
159 JLabel labelNca = getComponentFactory().createLabel(GestureConstants.GESTURE_SET_PANEL_NCA);
160 gesturePanel.add(labelNca, BorderLayout.CENTER);
161 }
162
163 } else{
164 JLabel labelNsa = getComponentFactory().createLabel(GestureConstants.GESTURE_SET_PANEL_NSA);
165 gesturePanel.add(labelNsa, BorderLayout.CENTER);
166 }
167
168 gesturePanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
169
170 return gesturePanel;
171 }
172
173 private JPanel createHeader(GestureSet gestureSet) {
174 FormBuilder formBuilder = new FormBuilder();
175
176 JLabel labelName = getComponentFactory().createLabel(GestureConstants.GESTURE_SET_PANEL_NAME);
177 JTextField textField = new JTextField();
178 BindingFactory.createInstance(textField, gestureSet, GestureSet.PROPERTY_NAME);
179
180 formBuilder.addLeft(labelName);
181 formBuilder.addRight(textField);
182 formBuilder.nextLine();
183
184 JLabel labelNogc = getComponentFactory().createLabel(GestureConstants.GESTURE_SET_PANEL_NOGC);
185 JLabel labelNogcValue = new JLabel(Integer.toString(gestureSet.size()));
186 formBuilder.addLeft(labelNogc);
187 formBuilder.addRight(labelNogcValue);
188
189 return formBuilder.getPanel();
190 }
191
192 protected void updateView() {
193 JPanel basePanel = ComponentFactory.createBorderLayoutPanel();
194 basePanel.add(createHeader(gestureSet), BorderLayout.NORTH);
195 JPanel panelIcons = createGestureIconList();
196 basePanel.add(panelIcons, BorderLayout.CENTER);
197 setContent(basePanel);
198
199 }
200
201
202
203
204
205
206
207
208 private JPanel createSpacerPanel(int size) {
209 JPanel panel = ComponentFactory.createBorderLayoutPanel();
210 panel.setPreferredSize(new Dimension(size, size));
211 return panel;
212 }
213
214 }