View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans.
5    *
6    * CosyBeans is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Component;
24  import java.awt.GridBagConstraints;
25  import java.awt.GridBagLayout;
26  import java.awt.Insets;
27  import java.awt.event.ActionEvent;
28  import java.awt.event.ActionListener;
29  import java.beans.PropertyChangeEvent;
30  import java.beans.PropertyChangeListener;
31  
32  import javax.swing.JButton;
33  import javax.swing.JDialog;
34  import javax.swing.JFrame;
35  import javax.swing.JPanel;
36  import javax.swing.JTabbedPane;
37  
38  import com.cosylab.gui.displayers.ConvertibleDisplayer;
39  import com.cosylab.gui.displayers.DataStateProvider;
40  import com.cosylab.gui.displayers.Displayer;
41  import com.cosylab.gui.infopanel.ConverterPanel;
42  import com.cosylab.gui.infopanel.DebugPanel;
43  import com.cosylab.gui.infopanel.PropertiesPanel;
44  
45  /**
46   * <code>InfoDialog</code> is a dialog which can be hooked to a displayer.
47   * Dialog will then shown some common displayer data like converter, data source
48   * etc. All data is in read only mode.
49   * 
50   * @author Jaka Bobnar, Cosylab
51   *
52   */
53  public class InfoDialog extends JDialog {
54  	
55  	private static final long serialVersionUID = 513076582058739594L;
56  	
57  	private JPanel contentPanel;
58  	private JTabbedPane tabPane;
59  	private ConverterPanel converterPanel;
60  	private PropertiesPanel propertiesPanel;
61  	private DebugPanel debugPanel;
62  	private Displayer displayer;
63  	private JButton closeButton;
64  	
65  	private PropertyChangeListener propertyListener;
66  	
67  	/**
68  	 * Constructs a new InfoDialog.
69  	 *
70  	 */
71  	public InfoDialog() {
72  		this(null);
73  	}
74  	
75  	/**
76  	 * Constructs a new InfoDialog which connects to the supplied displayer.
77  	 * 
78  	 * @param displayer
79  	 */
80  	public InfoDialog(Displayer displayer) {
81  		initialize();
82  		setDisplayer(displayer);
83  		addPanel(getDebugPanel(), "Debug", 0, true);
84  		addPanel(getPropertiesPanel(), "Bean properties", 1, false);
85  		addPanel(getConverterPanel(),"Converter", 2, false);
86  		
87  	}
88  	
89  	private void initialize() {
90  		this.setSize(300,300);
91  		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
92  		this.setContentPane(getContentPanel());
93  	}
94  	
95  	private JPanel getContentPanel() {
96  		if (contentPanel == null) {
97  			contentPanel = new JPanel(new BorderLayout());
98  			contentPanel.add(getTabPane(), BorderLayout.CENTER);
99  			JPanel buttonPanel = new JPanel(new GridBagLayout());
100 			buttonPanel.add(getCloseButton(), new GridBagConstraints(0,0,1,1,1,1,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
101 			contentPanel.add(buttonPanel, BorderLayout.SOUTH);
102 			
103 		}
104 		return contentPanel;
105 	}
106 	
107 	private JButton getCloseButton() {
108 		if (closeButton == null) {
109 			closeButton = new JButton("Close");
110 			closeButton.addActionListener(new ActionListener() {
111 				public void actionPerformed(ActionEvent e) {
112 					dispose();					
113 				}
114 			});
115 		}
116 		return closeButton;
117 	}
118 	
119 	private JTabbedPane getTabPane() {
120 		if (tabPane == null) {
121 			tabPane = new JTabbedPane();
122 		}
123 		return tabPane;
124 	}
125 	
126 	protected ConverterPanel getConverterPanel() {
127 		if (converterPanel == null) {
128 			converterPanel = new ConverterPanel();
129 		}
130 		return converterPanel;
131 	}
132 	
133 	protected PropertiesPanel getPropertiesPanel() {
134 		if (propertiesPanel == null) {
135 			propertiesPanel = new PropertiesPanel();
136 		}
137 
138 		return propertiesPanel;
139 	}
140 	
141 	protected DebugPanel getDebugPanel() {
142 		if (debugPanel == null) {
143 			debugPanel = new DebugPanel();
144 		}
145 		return debugPanel;
146 	}
147 	
148 	protected void addPanel(Component panel, String name, int position, boolean select) {
149 		for (int i = 0; i < getTabPane().getTabCount(); i++) {
150 			if (getTabPane().getTitleAt(i).equals(name)) {
151 				getTabPane().removeTabAt(i);
152 				break;
153 			}
154 		}
155 		getTabPane().add(panel, name, position);
156 		if (select) {
157 			getTabPane().setSelectedComponent(panel);
158 		}
159 	}
160 	
161 	protected void addPanel(Component panel, String name) {
162 		for (int i = 0; i < getTabPane().getTabCount(); i++) {
163 			if (getTabPane().getTitleAt(i).equals(name)) {
164 				getTabPane().removeTabAt(i);
165 				break;
166 			}
167 		}
168 		getTabPane().add(panel, name);
169 	}
170 	
171 	/**
172 	 * Hooks this panel to the specified displayer.
173 	 * 
174 	 * @param displayer
175 	 */
176 	public void setDisplayer(Displayer displayer) {
177 		if (this.displayer != null && !this.displayer.equals(displayer)) {
178 			this.displayer.removePropertyChangeListener(getPropertyListener());
179 			if (displayer != null) {
180 				displayer.addPropertyChangeListener(getPropertyListener());
181 			}
182 				
183 		} else if (this.displayer == null && displayer != null) {
184 			displayer.addPropertyChangeListener(getPropertyListener());
185 		}
186 		this.displayer = displayer;
187 		if (this.displayer != null) {
188 			this.setTitle(displayer.getName());
189 			getConverterPanel().setDataSource(this.displayer.getDataSource());
190 			if (displayer instanceof ConvertibleDisplayer)
191 				getConverterPanel().setConverter(((ConvertibleDisplayer)this.displayer).getConverter());
192 			getPropertiesPanel().setBean(displayer);
193 			getDebugPanel().setDataState(this.displayer.getDataState());
194 		} else {
195 			this.setTitle("Info");
196 			getConverterPanel().setConverter(null);
197 			getConverterPanel().setDataSource(null);
198 			getPropertiesPanel().setBean(new Object());
199 			getDebugPanel().setDataState(null);
200 		}
201 	}
202 	
203 	private PropertyChangeListener getPropertyListener() {
204 		if (propertyListener == null) {
205 			propertyListener = new PropertyChangeListener() {
206 				public void propertyChange(PropertyChangeEvent evt) {
207 					if (Displayer.DATA_SOURCE.equals(evt.getPropertyName())) {
208 						getConverterPanel().setDataSource(getDisplayer().getDataSource());
209 					} else if (ConvertibleDisplayer.CONVERTER_PROPERTY.equals(evt.getPropertyName())) {
210 						if (getDisplayer() instanceof ConvertibleDisplayer)
211 							getConverterPanel().setConverter(((ConvertibleDisplayer)getDisplayer()).getConverter());
212 					} else if (DataStateProvider.DATA_STATE.equals(evt.getPropertyName())) {
213 						getDebugPanel().setDataState(getDisplayer().getDataState());
214 					}
215 				}
216 			};
217 		}
218 		return propertyListener;
219 	}
220 	
221 	/**
222 	 * Returns the displayer connected with this InfoPanel.
223 	 * 
224 	 * @return
225 	 */
226 	public Displayer getDisplayer() {
227 		return this.displayer;
228 	}
229 	
230 	public static void main(String[] args) {
231 		new InfoDialog().setVisible(true);
232 	}
233 	
234 }