View Javadoc

1   package com.explosion.utilities.preferences.editandrender.form;
2   
3   import java.awt.GridBagConstraints;
4   import java.awt.GridBagLayout;
5   import java.awt.Insets;
6   
7   import javax.swing.DefaultComboBoxModel;
8   import javax.swing.JLabel;
9   import javax.swing.JList;
10  import javax.swing.JScrollPane;
11  import javax.swing.event.ListSelectionEvent;
12  import javax.swing.event.ListSelectionListener;
13  
14  import org.apache.log4j.LogManager;
15  import org.apache.log4j.Logger;
16  
17  import com.explosion.utilities.exception.ExceptionManagerFactory;
18  import com.explosion.utilities.preferences.Preference;
19  
20  
21  /*
22   * =============================================================================
23   * 
24   * Copyright 2004 Stephen Cowx
25   * 
26   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
27   * use this file except in compliance with the License. You may obtain a copy of
28   * the License at
29   * 
30   * http://www.apache.org/licenses/LICENSE-2.0
31   * 
32   * Unless required by applicable law or agreed to in writing, software
33   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
34   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
35   * License for the specific language governing permissions and limitations under
36   * the License.
37   * 
38   * =============================================================================
39   */
40  
41  /***
42   * This class provides a way to edit and rebder choice based preferences 
43   * @author Stephen Cowx
44   * @version
45   */
46  public class ListChoicePreferenceEditorAndRenderer extends FormBasedPreferenceRendererAndEditor {
47  
48  	private JList list = new JList();
49  	private JLabel label = new JLabel();
50  	private static Logger log = LogManager.getLogger(ListChoicePreferenceEditorAndRenderer.class);
51  	
52  	/***
53  	 * Constructs an instance of this class 
54  	 * @param preference
55  	 */
56  	public ListChoicePreferenceEditorAndRenderer(Preference preference)
57  	{
58      	super(preference);
59  		init();
60  	}
61  	
62  	/***
63  	 * This method initialises this renderer editor combination
64  	 */
65  	public void init()
66  	{
67  	    this.setLayout(new GridBagLayout());
68  		
69  		//Initialise the layout and bits
70  		label.setText(getPreference().getLongName());
71  		if (getPreference().getChoiceValues() != null)
72  		{
73  		  list.setModel(new DefaultComboBoxModel(getPreference().getChoiceValues().toArray()));
74  		}
75  		else
76  		{
77  		    list.setModel(new DefaultComboBoxModel());
78  		}
79  		    
80  		list.setSelectedValue(getPreference().getValue(), true);
81  		
82  		list.addListSelectionListener(new ListSelectionListener(){
83  			public void valueChanged(ListSelectionEvent e) {
84  			    choiceUpdated();
85  			}
86  		});
87  
88  		JScrollPane scrollPane = new JScrollPane(list);
89  
90  		//Add all of the bits to the panel
91  		this.add(label    	, new GridBagConstraints(0, 0, 1, 1, 0.0, 1.0 ,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); 
92  		this.add(scrollPane , new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0 ,GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
93  	}
94  	
95  	/***
96  	 * @throws Exception
97  	 */
98  	private void choiceUpdated()  {
99  		log.debug("choiceUpdated");
100 		try {
101 			if (getPreference().getType() == Preference.PROPERTY_TYPE_STRING_CHOICE)
102 	        {
103 	            getPreference().setValue(list.getSelectedValue());    
104 	        }
105 	        else
106 	        {
107 	            Object selectedItem = new Integer(list.getSelectedValue().toString());
108 	        }			
109 		} catch (Exception e) {
110 			ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while updating preference.");
111 		}
112 	}
113 	
114 	public void choicesUpdated()
115 	{
116 		list.setModel(new DefaultComboBoxModel(getPreference().getChoiceValues().toArray()));
117 	}
118 }