View Javadoc

1   /*
2    * $Id: SelectionPage.java,v 1.10 2008/05/19 20:41:08 edankert Exp $
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  package org.xmlhammer.gui.util;
23  
24  import java.awt.BorderLayout;
25  import java.awt.event.ItemEvent;
26  import java.awt.event.ItemListener;
27  import java.util.Enumeration;
28  import java.util.Hashtable;
29  
30  import javax.swing.JComboBox;
31  import javax.swing.JPanel;
32  
33  import org.apache.log4j.Logger;
34  import org.xmlhammer.gui.Page;
35  import org.xmlhammer.gui.ProjectView;
36  import org.xmlhammer.gui.actions.PagePropertiesAction;
37  
38  /***
39   * Input Panel.
40   * 
41   * Allows to select either one URI, multiple URIs or 
42   * a range of files.
43   * 
44   * @version $Revision: 1.10 $, $Date: 2008/05/19 20:41:08 $
45   * @author Edwin Dankert <edankert@gmail.com>
46   */
47  
48  public abstract class SelectionPage extends Page implements ItemListener {
49  	private static final long serialVersionUID = 3257852090755134776L;
50  
51  	private Hashtable<String,JPanel> panels = null;
52  	protected JComboBox selector = null;
53  	private JPanel centerPanel = null;
54  
55  	/***
56  	 * Constructs a new Input Panel.
57  	 */
58  	public SelectionPage(ProjectView view, String label, boolean properties) {
59  		super(view);
60  		panels = new Hashtable<String,JPanel>();
61  		
62  		selector = new JComboBox();
63  		selector.addItemListener( this);
64          selector.addItemListener(new UndoableComboBoxItemListener(this, selector));
65          getProjectView().getFieldManager().addField(selector);
66  		
67  		centerPanel = new JPanel( new BorderLayout());
68  //		centerPanel.setBorder( new EmptyBorder( 2, 2, 0, 0));
69          TitleBar bar = new TitleBar(label, selector);
70          if (properties) {
71              bar.add(new PagePropertiesAction(this));
72          }
73  
74  		setTitleBar( bar);
75  		setCenterPanel( centerPanel);
76  	}
77  	
78  	public JPanel getSelectedPanel() { 
79  		return panels.get(selector.getSelectedItem());
80  	}
81  	
82  	public void setSelectedPanel(JPanel panel) { 
83  		Enumeration<String> e = panels.keys();
84  		
85  		while(e.hasMoreElements()) {
86  			String key = e.nextElement();
87  			if (panels.get(key) == panel) {
88  				selector.setSelectedItem(key);
89  			}
90  		}
91  	}
92  
93  	public void addPanel(String name, JPanel panel) {
94          Logger.getLogger(this.getClass()).debug("addPanel("+name+", "+panel+")");
95  		panels.put(name, panel);
96  		
97  		selector.addItem(name);
98  		
99  		if (selector.getSelectedIndex() != 0) {
100 			selector.setSelectedIndex( 0);
101 		}
102 	}
103 	
104     public void itemStateChanged(ItemEvent e) {
105         centerPanel.removeAll();
106         centerPanel.add(panels.get( selector.getSelectedItem()), BorderLayout.CENTER);
107         centerPanel.revalidate();
108         centerPanel.repaint();
109     }
110 }