View Javadoc

1   /***
2    * Created on Jun 1, 2005, Copyright UC Regents
3    */
4   package org.telscenter.pas.steps;
5   
6   import java.util.logging.Logger;
7   
8   import java.awt.Component;
9   import java.awt.event.ActionEvent;
10  import java.awt.event.ActionListener;
11  import java.beans.beancontext.BeanContextServices;
12  import java.util.TooManyListenersException;
13  
14  import javax.swing.JButton;
15  import javax.swing.JPanel;
16  
17  import org.telscenter.pas.beans.PasStep;
18  import org.telscenter.pas.common.ui.CommonUI;
19  import org.telscenter.pas.service.IHingeState;
20  
21  import net.sf.sail.core.util.SailBeanUtils;
22  
23  /***
24   * @author turadg
25   */
26  public class HingeSelector extends PasStep {
27  	/***
28  	 * Logger for this class
29  	 */
30  	private static final Logger logger = Logger.getLogger(HingeSelector.class
31  			.getName());
32  
33  	private IHingeState hingeService;
34  
35  	private JButton currentlySelectedButton = new JButton();
36  
37  	protected void consumeService(BeanContextServices bcs, Class<? extends Object> serviceClass) {
38  		if (serviceClass == IHingeState.class) {
39  			try {
40  				hingeService = (IHingeState) bcs.getService(this, this,
41  						IHingeState.class, this, this);
42  			} catch (TooManyListenersException e) {
43  				// TODO Auto-generated catch block
44  				logger
45  						.severe("BeanContextServices, Class -  : exception: " + e); //$NON-NLS-1$
46  			}
47  		}
48  	}
49  
50  	public Component getComponent() {
51  		BeanContextServices bcs = SailBeanUtils.findServicesContextOf(this);
52  		consumeService(bcs, IHingeState.class);
53  
54  		String[] states = hingeService.getDefinedStates();
55  
56  		JPanel panel = new JPanel();
57  		for (int i = 0; i < states.length; i++) {
58  			String state = states[i];
59  			JButton stateButton = makeStateButton(state);
60  			
61  			panel.add(CommonUI.makeEnhancedButton(stateButton));
62  			panel.setOpaque(false);
63  		}
64  
65  		return panel;
66  	}
67  
68  	private JButton makeStateButton(String state) {
69  		final JButton button = new JButton(state);
70  
71  		if (state == hingeService.getState()) {
72  			currentlySelectedButton = button;
73  			button.setEnabled(false);
74  		}
75  		
76  		button.addActionListener(new ActionListener() {
77  			public void actionPerformed(ActionEvent e) {
78  				currentlySelectedButton.setEnabled(true);
79  				currentlySelectedButton = button;
80  				currentlySelectedButton.setEnabled(false);
81  				hingeService.setState(button.getText());
82  			}
83  		});
84  
85  		return button;
86  	}
87  
88  }