1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
45
46
47
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
70
71
72
73 public DisplayerParametersSelectorDialog(Component comp) {
74 this(comp, TITLE, PROMPT);
75 }
76
77
78
79
80
81
82
83
84
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
181
182
183
184
185
186
187 public int showSelectionDialog(Component c, DisplayerParameters[] paramArray) {
188 return showSelectionDialog(c, toStringArray(paramArray));
189 }
190
191
192
193
194
195
196
197
198
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
211
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 }