View Javadoc

1   /*
2    * @(#)$Id: MainView.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author       :   Michele Croci, mcroci@gmail.com
5    *
6    * Purpose      :   GUI for the gesture controller application.
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date             Who         Reason
13   *
14   * Nov 15, 2007     crocimi     Initial Release
15   * Jan 14, 2008     bsigner     Cleanup
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.geco.gui;
28  
29  import java.awt.Color;
30  import java.awt.Component;
31  import java.awt.FlowLayout;
32  import java.awt.GridBagConstraints;
33  import java.awt.GridBagLayout;
34  import java.awt.Insets;
35  import java.awt.event.MouseAdapter;
36  import java.awt.event.MouseEvent;
37  import java.awt.event.WindowEvent;
38  import java.awt.event.WindowListener;
39  import java.util.logging.Level;
40  import java.util.logging.Logger;
41  
42  import javax.swing.JButton;
43  import javax.swing.JFrame;
44  import javax.swing.JLabel;
45  import javax.swing.JList;
46  import javax.swing.JMenu;
47  import javax.swing.JMenuBar;
48  import javax.swing.JMenuItem;
49  import javax.swing.JPanel;
50  import javax.swing.ListCellRenderer;
51  import javax.swing.UIManager;
52  import javax.swing.WindowConstants;
53  import javax.swing.border.BevelBorder;
54  import javax.swing.border.TitledBorder;
55  import javax.swing.event.ListSelectionEvent;
56  import javax.swing.event.ListSelectionListener;
57  
58  import org.sigtec.graphix.GuiTool;
59  import org.ximtec.igesture.core.GestureClass;
60  import org.ximtec.igesture.geco.gui.action.ActionHandler;
61  import org.ximtec.igesture.geco.gui.action.MinimizeAction;
62  import org.ximtec.igesture.geco.mapping.GestureToActionMapping;
63  import org.ximtec.igesture.geco.util.Constant;
64  import org.ximtec.igesture.geco.util.GuiBundleTool;
65  import org.ximtec.igesture.graphics.ScrollableList;
66  import org.ximtec.igesture.graphics.SwingTool;
67  
68  
69  /**
70   * GUI for the gesture controller application.
71   * 
72   * @version 0.9, Nov 2007
73   * @author Michele Croci, mcroci@gmail.com
74   * @author Beat Signer, signer@inf.ethz.ch
75   */
76  
77  public class MainView extends JFrame implements WindowListener {
78  
79     private static final Logger LOGGER = Logger.getLogger(MainView.class
80           .getName());
81  
82     private MainModel model;
83     private ActionHandler handler = new ActionHandler(this);
84     private ComponentHandler compHandler = new ComponentHandler(this);
85     private boolean initialized;
86  
87     private final int WINDOW_HEIGHT = 600;
88     private final int WINDOW_WIDTH = 800;
89  
90     // GUI elements
91     private JPanel leftPanel = new JPanel();
92     private JPanel rightPanel = new JPanel();
93     JPanel contentPanel = new JPanel();
94     private ScrollableList gestureList;
95     private ScrollableList mappingList;
96     private JButton mapButton;
97     private JButton saveButton;
98     private JButton exitButton;
99     private JButton editButton;
100    private JButton removeButton;
101    private JMenuItem saveMenuItem;
102 
103    private boolean first = true;
104 
105 
106    /**
107     * Constructs a new main view.
108     * 
109     * @param model the model for this main view.
110     */
111    public MainView(MainModel model) {
112       super();
113       this.model = model;
114       createVoidDialog();
115       if ((!model.minimizeAsStartup())) {
116          setVisible(true);
117       }
118    }
119 
120 
121    /**
122     * Initialises the main view (create an empty frame).
123     */
124    private void createVoidDialog() {
125       try {
126          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
127       }
128       catch (Exception e) {
129          LOGGER.log(Level.SEVERE, Constant.EMPTY_STRING, e);
130       }
131 
132       this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
133       GridBagLayout gbl = new GridBagLayout();
134       this.getContentPane().setLayout(gbl);
135       setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
136       setLocation(150, 100);
137       this.setTitle(GuiBundleTool.getBundle().getName(GuiBundleTool.KEY));
138       GuiBundleTool.getBundle().getSmallIcon(GuiBundleTool.KEY);
139       setIconImage(GuiBundleTool.getBundle().getSmallIcon(GuiBundleTool.KEY)
140             .getImage());
141 
142       this.getContentPane().add(
143             contentPanel,
144             new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
145                   GridBagConstraints.CENTER, GridBagConstraints.BOTH,
146                   new Insets(20, 20, 20, 20), 0, 0));
147       this.addWindowListener(this);
148       addMenu();
149    }
150 
151 
152    /**
153     * Initialises the view for a project
154     */
155    public void initProjectView(String projectName) {
156       setTitle(GuiBundleTool.getBundle().getName(GuiBundleTool.KEY)
157             + org.sigtec.util.Constant.DASH_S + projectName);
158 
159       if (!initialized) {
160          populateDialog();
161          if ((!first) || (!model.minimizeAsStartup())) {
162             setVisible(true);
163          }
164          else {
165             first = false;
166          }
167       }
168 
169    }
170 
171 
172    /**
173     * Populates the dialog.
174     */
175    private void populateDialog() {
176       initialized = true;
177       leftPanel.setBorder(new TitledBorder(new BevelBorder(0, Color.gray,
178             Color.gray), Constant.USER_DEFINED_MAPPING));
179       rightPanel.setBorder(new TitledBorder(new BevelBorder(0, Color.gray,
180             Color.gray), Constant.GESTURE_SET));
181 
182       contentPanel.setLayout(new GridBagLayout());
183       contentPanel.add(leftPanel, new GridBagConstraints(0, 0, 1, 1, 1, 1,
184             GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0,
185                   0, 20), 10, 10));
186 
187       contentPanel.add(rightPanel, new GridBagConstraints(1, 0, 1, 1, 1, 1,
188             GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0,
189                   0, 0), 0, 0));
190 
191       saveButton = GuiTool.createButton(handler.getSaveProjectAction());
192       exitButton = GuiTool.createButton(handler.getExitApplicationAction());
193       // JButton minimize = SwingTool.createButton(handler.getMinimizeAction());
194       JPanel buttonPanel = new JPanel();
195       buttonPanel.setLayout(new FlowLayout());
196       buttonPanel.add(saveButton);
197       buttonPanel.add(exitButton);
198       // buttonPanel.add(minimize);
199       buttonPanel.setBackground(this.getBackground());
200       this.getContentPane().add(
201             buttonPanel,
202             new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.CENTER,
203                   GridBagConstraints.NONE, new Insets(0, 20, 0, 0), 20, 0));
204 
205       initLeftPanel();
206       initRightPanel();
207       updateLists();
208    }
209 
210 
211    private void addMenu() {
212       this.setJMenuBar(createMenuBar());
213    }
214 
215 
216    /**
217     * Initialises the left panel.
218     */
219    private void initLeftPanel() {
220       mappingList = SwingTool.createScrollableList(null, 0, 0);
221       mappingList.getList().setCellRenderer(new MappingCellRenderer());
222 
223       leftPanel.setLayout(new GridBagLayout());
224       leftPanel.add(mappingList, new GridBagConstraints(0, 0, 2, 1, 1, 1,
225             GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0,
226                   0, 0), 0, 0));
227 
228       editButton = GuiTool.createButton(handler.getEditMappingAction());
229       editButton.setEnabled(false);
230       removeButton = GuiTool.createButton(handler.getRemoveMappingAction());
231       removeButton.setEnabled(false);
232 
233       mappingList.getList().addListSelectionListener(
234             new ListSelectionListener() {
235 
236                public void valueChanged(ListSelectionEvent e) {
237                   MainView.this.editButton.setEnabled(true);
238                   MainView.this.removeButton.setEnabled(true);
239                }
240             });
241 
242       leftPanel.add(editButton, new GridBagConstraints(0, 1, 1, 1, 0.5, 0,
243             GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(20,
244                   0, 20, 0), 50, 0));
245 
246       leftPanel.add(removeButton, new GridBagConstraints(1, 1, 1, 1, 0.5, 0,
247             GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(20,
248                   0, 20, 0), 50, 0));
249 
250    }// initLeftPanel
251 
252 
253    /**
254     * Initialises the right panel.
255     */
256    private void initRightPanel() {
257       gestureList = SwingTool.createScrollableList(null, 0, 0);
258       rightPanel.setLayout(new GridBagLayout());
259       gestureList.getList().addMouseListener(new MouseAdapter() {
260 
261          @Override
262          public void mouseReleased(MouseEvent event) {
263             if (event.getButton() == MouseEvent.BUTTON1) {
264                MainView.this.mapButton.setEnabled(true);
265             }
266          }
267       });
268 
269       rightPanel.add(gestureList, new GridBagConstraints(0, 0, 2, 1, 1, 1,
270             GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0,
271                   0, 0), 0, 0));
272 
273       mapButton = GuiTool.createButton(handler.getAddMappingAction());
274       mapButton.setEnabled(false);
275 
276       JButton loadSetButton = GuiTool.createButton(handler
277             .getLoadGestureSetAction());
278 
279       rightPanel.add(loadSetButton, new GridBagConstraints(0, 1, 1, 1, 0.5, 0,
280             GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(20,
281                   0, 20, 0), 50, 0));
282 
283       rightPanel.add(mapButton, new GridBagConstraints(1, 1, 1, 1, 0.5, 0,
284             GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(20,
285                   0, 20, 0), 50, 0));
286    }// initRightPanel
287 
288 
289    /**
290     * Creates the menu bar.
291     * 
292     * @return the newly created menu bar.
293     */
294    private JMenuBar createMenuBar() {
295       JMenuBar menuBar = new JMenuBar();
296       menuBar.add(createFileMenu());
297       menuBar.add(createInfoMenu());
298       return menuBar;
299    } // createMenuBar
300 
301 
302    private JMenu createFileMenu() {
303       JMenu menu = GuiTool.createMenu(Constant.FILE_MENU, GuiBundleTool.getBundle());
304       menu.add(new JMenuItem(handler.getNewProjectAction()));
305       menu.add(new JMenuItem(handler.getOpenProjectAction()));
306       saveMenuItem = new JMenuItem(handler.getSaveProjectAction());
307       saveMenuItem.setEnabled(false);
308       menu.add(saveMenuItem);
309       menu.add(new JMenuItem(handler.getSaveProjectAsAction()));
310       menu.addSeparator();
311       menu.add(new JMenuItem(handler.getOptionsAction()));
312       menu.addSeparator();
313       menu.add(new JMenuItem(handler.getExitApplicationAction()));
314       return menu;
315    } // createFileMenu
316 
317 
318    private JMenu createInfoMenu() {
319       JMenu menu = GuiTool.createMenu(Constant.HELP_MENU, GuiBundleTool.getBundle());
320       menu.add(new JMenuItem(handler.getAboutAction()));
321       return menu;
322    } // createInfoMenu
323 
324 
325    /**
326     * Returns the main model.
327     * 
328     * @return the main model.
329     */
330    public MainModel getModel() {
331       return model;
332    } // getModel
333 
334 
335    /**
336     * Updates the gesture set.
337     * 
338     */
339    public void updateGestureList() {
340       gestureList.setModel(model.getGestureListModel());
341       mapButton.setEnabled(false);
342    }// updateGestureList
343 
344 
345    /**
346     * Update the mapping Set
347     * 
348     */
349    public void updateMappingList() {
350       mappingList.setModel(model.getMappingListModel());
351       editButton.setEnabled(false);
352       removeButton.setEnabled(false);
353       mapButton.setEnabled(false);
354    }// updateMappingList
355 
356 
357    /**
358     * Update the two lists
359     * 
360     */
361    public void updateLists() {
362       updateGestureList();
363       updateMappingList();
364    }// updateLists
365 
366 
367    /**
368     * Return the selected Gesture Class
369     * 
370     * @return the selected GestureClass
371     * 
372     */
373    public GestureClass getSelectedClass() {
374       return (GestureClass)gestureList.getSelectedValue();
375    }// getSelectedClass
376 
377 
378    /**
379     * Return the selected GestureToAction mapping
380     * 
381     * @return the selected mapping
382     * 
383     */
384    public GestureToActionMapping getSelectedMappping() {
385       return (GestureToActionMapping)mappingList.getSelectedValue();
386    }// getSelectedMappping
387 
388 
389    /**
390     * Return the selected mapping
391     * 
392     * @return the selected GestureMapping
393     * 
394     */
395    public GestureToActionMapping getSelectedMapping() {
396       return (GestureToActionMapping)mappingList.getSelectedValue();
397    }// getSelectedMapping
398 
399 
400    /**
401     * Enable the save in the menu
402     * 
403     */
404    public void enableMenuItem() {
405       saveMenuItem.setEnabled(true);
406    }// enableMenuItem
407 
408 
409    /**
410     * Returns the component handler of the application
411     * 
412     * @return the component Handler
413     * 
414     */
415    public ComponentHandler getComponentHandler() {
416       return compHandler;
417    }// getComponentHandler
418 
419 
420    /**
421     * Returns the component handler of the application
422     * 
423     * @return the component Handler
424     * 
425     */
426    public ActionHandler getActionHandler() {
427       return handler;
428    }// getActionHandler
429 
430 
431    /**
432     * Disable the save operation
433     */
434    public void disableSaveButton() {
435       saveButton.setEnabled(false);
436       saveMenuItem.setEnabled(false);
437    }// disableSaveButton
438 
439 
440    /**
441     * Enable the save operation
442     */
443    public void enableSaveButton() {
444       saveButton.setEnabled(true);
445       saveMenuItem.setEnabled(true);
446    }// enableSaveButton
447 
448    /**
449     * Cell Renderer for the mapping list
450     */
451    public class MappingCellRenderer implements ListCellRenderer {
452 
453       public Component getListCellRendererComponent(JList list, Object value,
454             int index, boolean isSelected, boolean cellHasFocus) {
455 
456          JLabel label = new JLabel();
457          if (value instanceof GestureToActionMapping) {
458             GestureToActionMapping gm = (GestureToActionMapping)value;
459             label.setText(gm.getGestureClass().getName() + Constant.DOUBLE_BLANK
460                   + Constant.ARROW_RIGHT + Constant.DOUBLE_BLANK
461                   + gm.getAction().toString());
462             label.setOpaque(true);
463             label.setBackground(isSelected ? Color.CYAN : list.getBackground());
464          }
465          return label;
466       } // getListCellRendererComponent
467    }
468 
469    /**
470     * Cell Renderer for the gestures list
471     */
472    public class CustomCellRenderer implements ListCellRenderer {
473 
474       public Component getListCellRendererComponent(JList list, Object value,
475             int index, boolean isSelected, boolean cellHasFocus) {
476          final Component component = (Component)value;
477          component.setBackground(isSelected ? Color.gray : Color.white);
478          component.setForeground(isSelected ? Color.white : Color.gray);
479          return component;
480       } // getListCellRendererComponent
481    }
482 
483 
484    public void windowClosed(WindowEvent e) {
485    }
486 
487 
488    public void windowOpened(WindowEvent e) {
489    }
490 
491 
492    public void windowIconified(WindowEvent e) {
493       ((MinimizeAction)handler.getMinimizeAction()).minimizeWindow();
494 
495    }
496 
497 
498    public void windowDeiconified(WindowEvent e) {
499    }
500 
501 
502    public void windowActivated(WindowEvent e) {
503    }
504 
505 
506    public void windowDeactivated(WindowEvent e) {
507    }
508 
509 
510    public void windowClosing(WindowEvent e) {
511 
512    }
513 
514 }