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.infopanel;
21  
22  import java.awt.BorderLayout;
23  import java.awt.GridBagConstraints;
24  import java.awt.GridBagLayout;
25  import java.awt.Insets;
26  
27  import javax.swing.DefaultListModel;
28  import javax.swing.JLabel;
29  import javax.swing.JList;
30  import javax.swing.JPanel;
31  
32  import com.cosylab.gui.adapters.Converter;
33  import com.cosylab.gui.adapters.ConverterChain;
34  import com.cosylab.gui.adapters.ConverterUtilities;
35  import com.cosylab.gui.displayers.DataSource;
36  
37  /**
38   * <code>ConverterPanel</code> shows information about the converters and  
39   *  data sources.
40   * 
41   * @author Jaka Bobnar, Cosylab
42   *
43   */
44  public class ConverterPanel extends JPanel {
45  	private static final long serialVersionUID = -7953031747105924874L;
46  	private Converter converter;
47  	private DataSource dataSource;
48  	private JLabel functionLabel;
49  	private JLabel dataSourceLabel;
50  	private JLabel dataSourceL;
51  	private JList converterList;
52  	private DefaultListModel model;
53  	
54  	/**
55  	 * Constructs a new ConverterPanel.
56  	 *
57  	 */
58  	public ConverterPanel() {
59  		super();
60  		initialize();
61  	}
62  	
63  	private void initialize() {
64  		this.setLayout(new BorderLayout());
65  	
66  		dataSourceLabel = new JLabel("Data Source:");
67  		dataSourceL = new JLabel("N/A");
68  		JPanel northPanel = new JPanel(new GridBagLayout());
69  		northPanel.add(dataSourceLabel, new GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1,1,1,1),0,0));
70  		northPanel.add(dataSourceL, new GridBagConstraints(1,0,1,1,1,0,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(1,1,1,1),0,0));
71  		this.add(northPanel, BorderLayout.NORTH);
72  		
73  		converterList = new JList();
74  		model = new DefaultListModel();
75  		converterList.setModel(model);
76  		this.add(converterList, BorderLayout.CENTER);
77  		functionLabel = new JLabel("id");
78  		this.add(functionLabel, BorderLayout.SOUTH);
79  	}
80  	
81  	/**
82  	 * Sets the converter, which data should be presented in the info panel.
83  	 * 
84  	 * @param c
85  	 */
86  	public void setConverter(Converter c) {
87  		this.converter = c;
88  		if (converter != null) {
89  			functionLabel.setText("f(x) = " + ConverterUtilities.composeFunctionString(c, "x"));
90  			model.clear();
91  			if (converter instanceof ConverterChain) {
92  				Converter[] converters = ((ConverterChain)converter).getConverters();
93  				for (Converter co : converters) {
94  					model.addElement(co);
95  				}
96  			} else {
97  				model.addElement(converter);
98  			}
99  		} else {
100 			model.clear();
101 			functionLabel.setText("id");
102 		}
103 	}
104 	
105 	/**
106 	 * Returns the converter, which the info belongs to.
107 	 * 
108 	 * @return
109 	 */
110 	public Converter getConverter() {
111 		return converter;
112 	}
113 	
114 	/**
115 	 * Sets the DataSource, which info is presented in the panel.
116 	 * @param dataSource
117 	 */
118 	public void setDataSource(DataSource dataSource) {
119 		this.dataSource = dataSource;
120 		if (this.dataSource != null) {
121 			dataSourceL.setText(dataSource.toString());
122 		} else {
123 			dataSourceL.setText("N/A");
124 		}
125 	}
126 	
127 	/**
128 	 * Returns the DataSource, which the info belongs to.
129 	 * 
130 	 * @return
131 	 */
132 	public DataSource getDataSource() {
133 		return dataSource;
134 	}
135 	
136 }