View Javadoc

1   /*
2    * Copyright (c) 2006 Stiftung Deutsches Elektronen-Synchroton,
3    * Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY.
4    *
5    * THIS SOFTWARE IS PROVIDED UNDER THIS LICENSE ON AN "../AS IS" BASIS.
6    * WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
7    * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR PARTICULAR PURPOSE AND
8    * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
9    * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10   * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
11   * THE USE OR OTHER DEALINGS IN THE SOFTWARE. SHOULD THE SOFTWARE PROVE DEFECTIVE
12   * IN ANY RESPECT, THE USER ASSUMES THE COST OF ANY NECESSARY SERVICING, REPAIR OR
13   * CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
14   * NO USE OF ANY SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
15   * DESY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
16   * OR MODIFICATIONS.
17   * THE FULL LICENSE SPECIFYING FOR THE SOFTWARE THE REDISTRIBUTION, MODIFICATION,
18   * USAGE AND OTHER RIGHTS AND OBLIGATIONS IS INCLUDED WITH THE DISTRIBUTION OF THIS
19   * PROJECT IN THE FILE LICENSE.HTML. IF THE LICENSE IS NOT INCLUDED YOU MAY FIND A COPY
20   * AT HTTP://WWW.DESY.DE/LEGAL/LICENSE.HTM
21   */
22  
23  package de.desy.acop.displayers.selector;
24  
25  import java.awt.BorderLayout;
26  import java.awt.Component;
27  import java.awt.GridBagConstraints;
28  import java.awt.GridBagLayout;
29  import java.awt.Insets;
30  import java.awt.event.ActionEvent;
31  import java.awt.event.ActionListener;
32  import java.awt.event.WindowAdapter;
33  import java.awt.event.WindowEvent;
34  
35  import javax.swing.JButton;
36  import javax.swing.JDialog;
37  import javax.swing.JLabel;
38  import javax.swing.JOptionPane;
39  import javax.swing.JPanel;
40  import javax.swing.JTabbedPane;
41  import javax.swing.border.EmptyBorder;
42  
43  import com.cosylab.gui.adapters.ConverterCustomizer;
44  
45  import de.desy.acop.displayers.tools.AcopDisplayerParameters;
46  import de.desy.acop.transport.ConnectionParameters;
47  
48  /**
49   * With help of this dialog special <code>AcopDisplayerParameters</code> 
50   * are created and customized from <code>ConnectionParameters</code> or 
51   * existing <code>AcopDisplayerParameters</code>. 
52   * 
53   * @author tkusterle
54   * @see AcopDisplayerParameters
55   */
56  public abstract class AcopDisplayerParametersDialog<T extends AcopDisplayerParameters> extends JDialog {
57  	
58  	private static final long serialVersionUID = 1L;
59  	
60  	private JPanel contentPanel;
61  	private JPanel labelPanel;
62  	private JLabel connectionLabel;
63  	private JPanel buttonPanel;
64  	private JButton addButton;
65  	private JButton cancelButton;
66  	
67  	private AcopDisplayerParameters parameters;
68  	private T result;
69  	private ConverterCustomizer converterCustomizer;
70  	private JTabbedPane customizerTab;
71  	
72  	/**
73  	 * Constructs a new AcopDisplayerParametersDialog.
74  	 * 
75  	 * @param comp the owner of the dialog
76  	 */
77  	public AcopDisplayerParametersDialog(Component comp) {		
78  		super(JOptionPane.getFrameForComponent(comp), "Adding New Parameters", true);
79  		initialize();
80  	}
81  	
82  	private void initialize() {
83  		this.setLayout(new GridBagLayout());
84  		this.add(getContentPanel(), new GridBagConstraints(0,0,1,1,1,1,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(11,11,11,11),1,1));
85  		this.setSize(330, 300);
86  		this.addWindowListener(new WindowCloser());
87  	}
88  	
89  	private JPanel getContentPanel() {
90  		if (contentPanel == null) {
91  			contentPanel = new JPanel();
92  			contentPanel.setLayout(new BorderLayout());
93  			contentPanel.add(getCustomizerTabPane(), BorderLayout.CENTER);
94  			contentPanel.add(getLabelPanel(), BorderLayout.NORTH);
95  			contentPanel.add(getButtonPanel(), BorderLayout.SOUTH);
96  			
97  		}
98  		return contentPanel;
99  	}
100 	
101 	private JTabbedPane getCustomizerTabPane() {
102 		if (customizerTab == null) {
103 			customizerTab = new JTabbedPane();
104 			customizerTab.add(getCustomizerPanel(), "Properties");
105 			customizerTab.add(getConverterPanel(), "Converter");
106 		}
107 		return customizerTab;
108 	}
109 	
110 	protected ConverterCustomizer getConverterPanel() {
111 		if (converterCustomizer == null) {
112 			converterCustomizer = new ConverterCustomizer();
113 		}
114 		return converterCustomizer;
115 	}
116 	
117 	private JPanel getLabelPanel() {
118 		if (labelPanel == null) {
119 			labelPanel = new JPanel();
120 			labelPanel.setBorder(new EmptyBorder(new Insets(0,0,5,0)));
121 			labelPanel.add(getConnectionLabel());
122 		}
123 		return labelPanel;
124 	}
125 	
126 	private JLabel getConnectionLabel() {
127 		if (connectionLabel == null) {
128 			connectionLabel = new JLabel("Connection");
129 			connectionLabel.setToolTipText("Connection");
130 		}
131 		return connectionLabel;
132 	}
133 	
134 	private void setConnectionLabel(String connection) {
135 		getConnectionLabel().setText(connection);
136 		getConnectionLabel().setToolTipText(connection);
137 	}
138 	
139 	private JPanel getButtonPanel() {
140 		if (buttonPanel == null) {
141 			buttonPanel = new JPanel();
142 			buttonPanel.add(getAddButton());
143 			buttonPanel.add(getCancelButton());
144 		}
145 		return buttonPanel;
146 	}
147 	
148 	protected abstract MultipleDisplayerAbstractSettingsPanel<T> getCustomizerPanel();
149 	
150 	protected abstract T generateParameters();
151 	
152 	private JButton getAddButton() {
153 		if (addButton == null) {
154 			addButton = new JButton("Add");
155 			addButton.addActionListener(new ActionListener() {
156 	        	public void actionPerformed(ActionEvent e) {
157 	        		result = generateParameters();
158 	    		    closeDialog();
159 	        	}
160 	        });
161 	        
162         }
163 		return addButton;
164 	}
165 	
166 	private JButton getCancelButton() {
167 		if (cancelButton == null) {
168 			cancelButton = new JButton("Cancel");
169 			cancelButton.addActionListener(new ActionListener() {
170 				public void actionPerformed(ActionEvent e) {
171 					result = null;
172 					closeDialog();
173 				}
174 			});
175 		}
176 		return cancelButton;
177 	}
178 	
179 	private void closeDialog() {
180 		this.setVisible(false);
181 	}
182 	
183 	/**
184 	 * Applies the given parameters to this customizer and shows this dialog.
185 	 *  
186 	 * @param component the invoker of this dialog
187 	 * @param adp default parameters 
188 	 * @return customized parameters
189 	 */
190 	public T showDialog(Component component, AcopDisplayerParameters adp) {
191 		parameters= adp;
192 		getCustomizerTabPane().setSelectedIndex(0);
193 		getConverterPanel().setConverter(null);
194 		this.setConnectionLabel(adp.getConnectionParameters().getRemoteName());
195 		this.setLocationRelativeTo(component);
196 		this.setVisible(true);
197 		return result;
198 	}
199 	
200 	/**
201 	 * Applies the given parameters to this customizer and shows this dialog.
202 	 *  
203 	 * @param component the invoker of this dialog
204 	 * @param cp default parameters 
205 	 * @return customized parameters
206 	 */
207 	public T showDialog(Component component, ConnectionParameters cp) {
208 		parameters = new AcopDisplayerParameters(cp);
209 		getCustomizerTabPane().setSelectedIndex(0);
210 		this.getConverterPanel().setConverter(null);
211 		this.setConnectionLabel(parameters.getConnectionParameters().getRemoteName());
212 		this.setLocationRelativeTo(component);
213 		this.setVisible(true);
214 		return result;
215 	}
216 	
217 	private class WindowCloser extends WindowAdapter {
218 
219 		/* (non-Javadoc)
220 		 * @see java.awt.event.WindowAdapter#windowClosing(java.awt.event.WindowEvent)
221 		 */
222 		@Override
223 		public void windowClosing(WindowEvent e) {
224 			parameters = null;
225 			super.windowClosed(e);
226 		}
227 		
228 	}
229 	
230 	protected AcopDisplayerParameters getDisplayerParameters() {
231 		return parameters;
232 	}
233 
234 
235 
236 }