1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
47
48
49
50
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
69
70
71 public InfoDialog() {
72 this(null);
73 }
74
75
76
77
78
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
173
174
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
223
224
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 }