View Javadoc

1   package org.minetti.astrodevice.client.gui.menu;
2   
3   import java.util.List;
4   import org.eclipse.swt.SWT;
5   import org.eclipse.swt.widgets.Event;
6   import org.eclipse.swt.widgets.Listener;
7   import org.eclipse.swt.widgets.Menu;
8   import org.eclipse.swt.widgets.MenuItem;
9   import org.eclipse.swt.widgets.Shell;
10  import org.minetti.astrodevice.client.business.process.ObservingSiteProcess;
11  import org.minetti.astrodevice.client.business.process.PlatformProcess;
12  import org.minetti.astrodevice.client.i18n.I18n;
13  import org.minetti.astrodevice.common.object.Entity;
14  
15  /**
16   * Menu of <b>observing site</b>.
17   * @author Jean-Philippe MINETTI
18   */
19  public final class SwtObservingSiteMenu
20  {
21  
22     /**
23      * Listener for each sub-menu showing.
24      */
25     protected final class ShowListener
26              implements Listener
27     {
28  
29        /**
30         * {@inheritDoc}
31         * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
32         */
33        public void handleEvent (final Event event)
34        {
35  
36           // Removing
37           for (final MenuItem menuItem : SwtObservingSiteMenu.this.subMenu.getItems ()) {
38              menuItem.dispose ();
39           }
40  
41           // Observing sites
42           final Long selectedObservingSiteId = SwtObservingSiteMenu.this.platformProcess.getObservingSiteId ();
43           final List<Entity> entityList = SwtObservingSiteMenu.this.observingSiteProcess.find ();
44           for (final Entity entity : entityList) {
45              final MenuItem menuItem = new MenuItem (SwtObservingSiteMenu.this.subMenu, SWT.RADIO);
46              menuItem.setText (entity.getName ());
47              menuItem.addListener (SWT.Selection, new ObservingSiteSelectionListener (entity));
48              menuItem.setSelection ((selectedObservingSiteId != null) && (selectedObservingSiteId.longValue () == entity.getId ()));
49           }
50  
51           // Separator
52           MenuItem menuItem;
53           if (entityList.size () > 0) {
54              menuItem = new MenuItem (SwtObservingSiteMenu.this.subMenu, SWT.SEPARATOR);
55           }
56  
57           // GPS
58           menuItem = new MenuItem (SwtObservingSiteMenu.this.subMenu, SWT.PUSH);
59           menuItem.setText (I18n.getGui ().getString ("menu.observingsite.gps.text"));
60           menuItem.addListener (SWT.Selection, new GpsSelectionListener ());
61  
62        }
63  
64     }
65  
66     /**
67      * Listener for each observing site item selection.
68      */
69     protected final class ObservingSiteSelectionListener
70              implements Listener
71     {
72  
73        /**
74         * Observing site (as entity).
75         */
76        private final Entity entity;
77  
78        /**
79         * Constructor.
80         * @param entity Observing site (as entity).
81         */
82        protected ObservingSiteSelectionListener (final Entity entity)
83        {
84           super ();
85           this.entity = entity;
86        }
87  
88        /**
89         * {@inheritDoc}
90         * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
91         */
92        public void handleEvent (final Event event)
93        {
94           final MenuItem menuItem = (MenuItem) event.widget;
95           if (menuItem.getSelection ()) {
96              SwtObservingSiteMenu.this.platformProcess.setObservingSiteId (this.entity.getId ());
97           }
98        }
99  
100    }
101 
102    /**
103     * Listener for each <b>GPS</b> item selection.
104     */
105    protected final class GpsSelectionListener
106             implements Listener
107    {
108 
109       /**
110        * {@inheritDoc}
111        * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
112        */
113       public void handleEvent (final Event event)
114       {
115 
116          System.out.println ("Sélection GPS");
117          // TODO Auto-generated method stub
118 
119       }
120 
121    }
122 
123    /**
124     * Process on AstroDev platform.
125     */
126    protected final PlatformProcess platformProcess = new PlatformProcess ();
127 
128    /**
129     * Process on observing sites.
130     */
131    protected final ObservingSiteProcess observingSiteProcess = new ObservingSiteProcess ();
132 
133    /**
134     * Menu of observing site.
135     */
136    protected Menu subMenu;
137 
138    /**
139     * Builds the menu of observing site.
140     * @param shell SWT shell.
141     * @param contextMenu Context menu.
142     */
143    public void build (final Shell shell, final Menu contextMenu)
144    {
145       MenuItem menuItem = new MenuItem (contextMenu, SWT.CASCADE);
146       menuItem.setText (I18n.getGui ().getString ("menu.observingsite.text"));
147       this.subMenu = new Menu (shell, SWT.DROP_DOWN);
148       this.subMenu.addListener (SWT.Show, new ShowListener ());
149       menuItem.setMenu (this.subMenu);
150    }
151 
152    /**
153     * {@inheritDoc}
154     * @see java.lang.Object#toString()
155     */
156    @Override
157    public String toString ()
158    {
159       return "SwtObservingSiteMenu";
160    }
161 
162 }