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.util;
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.awt.event.WindowAdapter;
30  import java.awt.event.WindowEvent;
31  
32  import javax.swing.JButton;
33  import javax.swing.JDialog;
34  import javax.swing.JLabel;
35  import javax.swing.JList;
36  import javax.swing.JOptionPane;
37  import javax.swing.JPanel;
38  import javax.swing.JScrollPane;
39  import javax.swing.ListSelectionModel;
40  
41  import com.cosylab.gui.displayers.DisplayerParameters;
42  
43  /**
44   * This class offers a dialog where a single object can be selected from a list 
45   * of DisplayerParameters.
46   * 
47   * @author tkusterle
48   *
49   */
50  public class DisplayerParametersSelectorDialog extends JDialog {
51  	
52  	private static final long serialVersionUID = 1L;
53  	private static final String TITLE = "Select parameters";
54  	private static final String PROMPT = "Parameters:";
55  	
56  	private String prompt;
57  	private int selected;
58  	
59  	private JLabel textLabel;
60  	private JPanel labelPanel;
61  	private JButton okButton;
62  	private JButton cancelButton;
63  	private JPanel buttonPanel;
64  	private JList list;
65  	private JScrollPane listPane;
66  	private JPanel contentPanel;
67  
68  	/**
69  	 * Constructs a new dialog that belongs to the given parent component.
70  	 * 
71  	 * @param comp parent component
72  	 */
73  	public DisplayerParametersSelectorDialog(Component comp) {
74  		this(comp, TITLE, PROMPT);
75  	}
76  	
77  	/**
78  	 * Constructs a new dialog that belongs to the supplied parent component
79  	 * and shows the given title and title of the array data among which
80  	 * the user is allowed to choose.
81  	 * 
82  	 * @param comp the parent component
83  	 * @param title the title of the dialog
84  	 * @param prompt title of the array data
85  	 */
86  	public DisplayerParametersSelectorDialog(Component comp, String title, String prompt) {
87  		super(JOptionPane.getFrameForComponent(comp), title, true);
88  		this.prompt = prompt;
89  		initialize();
90  	}
91  	
92  	private void initialize() {
93  		this.setLayout(new GridBagLayout());
94  		this.add(getContentPanel(), new GridBagConstraints(0,0,1,1,1,1,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(11,11,11,11),1,1));
95  		this.setSize(300, 250);
96  		this.addWindowListener(new WindowCloser());
97  	}
98  	
99  	private JPanel getContentPanel() {
100 		if (contentPanel == null) {
101 			contentPanel = new JPanel();
102 			contentPanel.setLayout(new BorderLayout());
103 			contentPanel.add(getLabelPanel(), BorderLayout.NORTH);
104 			contentPanel.add(getListPane(), BorderLayout.CENTER);
105 			contentPanel.add(getButtonPanel(), BorderLayout.SOUTH);
106 		}
107 		return contentPanel;
108 	}
109 	
110 	private JPanel getLabelPanel() {
111 		if (labelPanel == null) {
112 			labelPanel = new JPanel();
113 			labelPanel.add(getTextLabel());
114 		}
115 		return labelPanel;
116 	}
117 	
118 	private JLabel getTextLabel() {
119 		if (textLabel == null) {
120 			textLabel = new JLabel(prompt);
121 		}
122 		return textLabel;
123 	}
124 	
125 	private JScrollPane getListPane() {
126 		if (listPane == null) {
127 			listPane = new JScrollPane(getList());
128 		}
129 		return listPane;
130 	}
131 	
132 	private JList getList() {
133 		if (list == null) {
134 			list = new JList();
135 			list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
136 		}
137 		return list;
138 	}
139 	
140 	private JPanel getButtonPanel() {
141 		if (buttonPanel == null) {
142 			buttonPanel = new JPanel();
143 			buttonPanel.add(getOkButton());
144 			buttonPanel.add(getCancelButton());
145 		}
146 		return buttonPanel;
147 	}
148 	
149 	private JButton getOkButton() {
150 		if (okButton == null) {
151 			okButton = new JButton("OK");
152 			okButton.addActionListener(new ActionListener() {
153 				public void actionPerformed(ActionEvent e) {
154 					selected = getList().getSelectedIndex();
155 					closeDialog();
156 				}
157 			});
158 		}
159 		return okButton;
160 	}
161 	
162 	private JButton getCancelButton() {
163 		if (cancelButton == null) {
164 			cancelButton = new JButton("Cancel");
165 			cancelButton.addActionListener(new ActionListener() {
166 				public void actionPerformed(ActionEvent e) {
167 					selected = -1;
168 					closeDialog();
169 				}
170 			});
171 		}
172 		return cancelButton;
173 	}
174 	
175 	private void closeDialog() {
176 		this.setVisible(false);
177 	}
178 	
179 	/**
180 	 * Shows a dialog relative to the specified component and shows the supplied
181 	 * parameters in a list.
182 	 * 
183 	 * @param c parent component
184 	 * @param paramArray list of parameters
185 	 * @return index of selected parameters
186 	 */
187 	public int showSelectionDialog(Component c, DisplayerParameters[] paramArray) {
188 		return showSelectionDialog(c, toStringArray(paramArray));
189 	}
190 
191 	/**
192 	 * Shows a dialog relative to the specified component and shows the supplied
193 	 * parameters in a list
194 	 * 
195 	 * @param c parent component
196 	 * @param paramArray list of parameters' names
197 	 * 
198 	 * @return index of the selected parameters
199 	 */
200 	public int showSelectionDialog(Component c, String[] paramArray) {
201 		if (paramArray.length == 1) return 0;
202 		getList().setListData(paramArray);
203 		setLocationRelativeTo(c);
204 		this.setVisible(true);
205 		return selected;
206 	}
207 	
208 	private class WindowCloser extends WindowAdapter {
209 
210 		/* (non-Javadoc)
211 		 * @see java.awt.event.WindowAdapter#windowClosing(java.awt.event.WindowEvent)
212 		 */
213 		@Override
214 		public void windowClosing(WindowEvent e) {
215 			selected = -1;
216 			super.windowClosed(e);
217 		}
218 		
219 	}
220 	
221 	private String[] toStringArray(DisplayerParameters[] dp) {
222 		String[] array = new String[dp.length];
223 		for (int i = 0; i < dp.length; i++) {
224 			array[i] = dp[i].getName();
225 		}
226 		return array;
227 	}
228 }