View Javadoc
1   package com.github.sbugat.rundeckmonitor.wizard;
2   
3   import java.awt.Component;
4   import java.awt.Container;
5   import java.awt.GridBagConstraints;
6   import java.awt.GridBagLayout;
7   import java.awt.Insets;
8   import java.io.IOException;
9   
10  import javax.swing.JComboBox;
11  import javax.swing.JLabel;
12  import javax.swing.JTextField;
13  
14  import org.rundeck.api.RundeckClient;
15  
16  import com.github.sbugat.rundeckmonitor.configuration.RundeckMonitorConfiguration;
17  import com.github.sbugat.rundeckmonitor.tools.EnvironmentTools;
18  import com.github.sbugat.rundeckmonitor.tools.RundeckClientTools;
19  
20  /**
21   * RunDeck monitor wizard panel.
22   * 
23   * @author Sylvain Bugat
24   * 
25   */
26  public final class MonitorConfigurationWizardPanelDescriptor extends WizardPanelDescriptor {
27  
28  	/** Main container. */
29  	private final Container container = new Container();
30  
31  	/** RunDeck monitor name input. */
32  	private final JTextField rundeckMonitorName = new JTextField(20);
33  	/** Refresh delay input. */
34  	private final JComboBox<RefreshDelay> rundeckMonitorRefreshDelay = new JComboBox<>();
35  	/** Late threshold input. */
36  	private final JComboBox<LateExecutionThreshold> rundeckMonitorLateExecutionThreshold = new JComboBox<>();
37  	/** Number of displayed failed jobs input. */
38  	private final JComboBox<FailedJobsNumber> rundeckMonitorFailedJobNumber = new JComboBox<>();
39  	/** Date format input. */
40  	private final JComboBox<DateFormat> rundeckMonitorDateFormat = new JComboBox<>();
41  	/** Open a job redirection input. */
42  	private final JComboBox<JobTabRedirection> rundeckMonitorJobTabRedirection = new JComboBox<>();
43  	/** Interface type input. */
44  	private final JComboBox<InterfaceType> rundeckMonitorInterfaceType = new JComboBox<>();
45  
46  	/**
47  	 * Copy arguments and initialize the RunDeck monitor configuration wizard panel.
48  	 * 
49  	 * @param backArg previous panel
50  	 * @param nextArg next panel
51  	 * @param rundeckMonitorConfigurationArg RunDeck monitor common configuration
52  	 */
53  	public MonitorConfigurationWizardPanelDescriptor(final ConfigurationWizardStep backArg, final ConfigurationWizardStep nextArg, final RundeckMonitorConfiguration rundeckMonitorConfigurationArg) {
54  		super(ConfigurationWizardStep.MONITOR_STEP, backArg, nextArg, rundeckMonitorConfigurationArg);
55  
56  		container.setLayout(new GridBagLayout());
57  		final JLabel rundeckMonitorNameLabel = new JLabel("Tray-icon monitor name:"); //$NON-NLS-1$
58  		final JLabel rundeckMonitorRefreshDelayLabel = new JLabel("Failed/late jobs refresh delay:"); //$NON-NLS-1$
59  		final JLabel rundeckMonitorLateExecutionThresholdLabel = new JLabel("Late execution detection threshold:"); //$NON-NLS-1$
60  		final JLabel rundeckMonitorFailedJobNumberLabel = new JLabel("Number of failed/late jobs to display:"); //$NON-NLS-1$
61  		final JLabel rundeckMonitorDateFormatLabel = new JLabel("Failed/late jobs displayed date format:"); //$NON-NLS-1$
62  		final JLabel rundeckMonitorJobTabRedirectionLabel = new JLabel("Failed/late job tab redirection: "); //$NON-NLS-1$
63  		final JLabel rundeckMonitorInterfaceTypeLabel = new JLabel("Type of Java interface: "); //$NON-NLS-1$
64  
65  		// Fields initialization
66  		if (null == rundeckMonitorConfigurationArg.getRundeckMonitorName() || rundeckMonitorConfigurationArg.getRundeckMonitorName().isEmpty()) {
67  			rundeckMonitorName.setText(RundeckMonitorConfiguration.RUNDECK_MONITOR_PROPERTY_NAME_DEFAULT_VALUE);
68  		}
69  		else {
70  			rundeckMonitorName.setText(rundeckMonitorConfigurationArg.getRundeckMonitorName());
71  		}
72  
73  		RefreshDelay oldConfiguredRefreshDelay = null;
74  		for (final RefreshDelay refreshDelay : RefreshDelay.values()) {
75  
76  			rundeckMonitorRefreshDelay.addItem(refreshDelay);
77  			if (refreshDelay.getDelay() == rundeckMonitorConfigurationArg.getRefreshDelay()) {
78  				oldConfiguredRefreshDelay = refreshDelay;
79  			}
80  		}
81  
82  		if (null != oldConfiguredRefreshDelay) {
83  			rundeckMonitorRefreshDelay.setSelectedItem(oldConfiguredRefreshDelay);
84  		}
85  		else {
86  			rundeckMonitorRefreshDelay.setSelectedItem(RefreshDelay.REFRESH_DELAY_1M);
87  		}
88  
89  		LateExecutionThreshold oldLateExecutionThreshold = null;
90  		for (final LateExecutionThreshold lateExecutionThreshold : LateExecutionThreshold.values()) {
91  
92  			rundeckMonitorLateExecutionThreshold.addItem(lateExecutionThreshold);
93  			if (lateExecutionThreshold.getThreshold() == rundeckMonitorConfigurationArg.getLateThreshold()) {
94  				oldLateExecutionThreshold = lateExecutionThreshold;
95  			}
96  		}
97  
98  		if (null != oldLateExecutionThreshold) {
99  			rundeckMonitorLateExecutionThreshold.setSelectedItem(oldLateExecutionThreshold);
100 		}
101 		else {
102 			rundeckMonitorLateExecutionThreshold.setSelectedItem(LateExecutionThreshold.LATE_EXECUTION_THRESHOLD_30M);
103 		}
104 
105 		FailedJobsNumber oldFailedJobsNumber = null;
106 		for (final FailedJobsNumber failedJobsNumber : FailedJobsNumber.values()) {
107 
108 			rundeckMonitorFailedJobNumber.addItem(failedJobsNumber);
109 			if (failedJobsNumber.getFailedJobsNumber() == rundeckMonitorConfigurationArg.getFailedJobNumber()) {
110 				oldFailedJobsNumber = failedJobsNumber;
111 			}
112 		}
113 
114 		if (null != oldFailedJobsNumber) {
115 			rundeckMonitorFailedJobNumber.setSelectedItem(oldFailedJobsNumber);
116 		}
117 		else {
118 			rundeckMonitorFailedJobNumber.setSelectedItem(FailedJobsNumber.FAILED_JOBS_10);
119 		}
120 
121 		DateFormat oldDateFormat = null;
122 		for (final DateFormat dateFormat : DateFormat.values()) {
123 
124 			rundeckMonitorDateFormat.addItem(dateFormat);
125 
126 			if (dateFormat.getDateFormat().equals(rundeckMonitorConfigurationArg.getDateFormat())) {
127 				oldDateFormat = dateFormat;
128 			}
129 		}
130 
131 		if (null != oldDateFormat) {
132 			rundeckMonitorDateFormat.setSelectedItem(oldDateFormat);
133 		}
134 		else {
135 			rundeckMonitorDateFormat.setSelectedItem(DateFormat.DATE_FORMAT_STANDARD);
136 		}
137 
138 		JobTabRedirection oldJobTabRedirection = null;
139 		for (final JobTabRedirection jobTabRedirection : JobTabRedirection.values()) {
140 
141 			rundeckMonitorJobTabRedirection.addItem(jobTabRedirection);
142 
143 			if (jobTabRedirection.name().equals(rundeckMonitorConfigurationArg.getJobTabRedirection())) {
144 				oldJobTabRedirection = jobTabRedirection;
145 			}
146 		}
147 
148 		if (null != oldJobTabRedirection) {
149 			rundeckMonitorJobTabRedirection.setSelectedItem(oldJobTabRedirection);
150 		}
151 		else {
152 			rundeckMonitorJobTabRedirection.setSelectedItem(JobTabRedirection.SUMMARY);
153 		}
154 
155 		InterfaceType oldInterfaceType = null;
156 		for (final InterfaceType interfaceType : InterfaceType.values()) {
157 
158 			if (InterfaceType.SWING.equals(interfaceType)) {
159 
160 				if (EnvironmentTools.isWindows()) {
161 
162 					rundeckMonitorInterfaceType.addItem(interfaceType);
163 
164 					if (interfaceType.name().equals(rundeckMonitorConfigurationArg.getInterfaceType())) {
165 						oldInterfaceType = interfaceType;
166 					}
167 				}
168 			}
169 			else {
170 				rundeckMonitorInterfaceType.addItem(interfaceType);
171 
172 				if (interfaceType.name().equals(rundeckMonitorConfigurationArg.getInterfaceType())) {
173 					oldInterfaceType = interfaceType;
174 				}
175 			}
176 		}
177 
178 		if (null != oldInterfaceType) {
179 			rundeckMonitorInterfaceType.setSelectedItem(oldInterfaceType);
180 		}
181 		else {
182 			rundeckMonitorInterfaceType.setSelectedItem(InterfaceType.SWING);
183 		}
184 
185 		final GridBagConstraints gridBagConstraits = new GridBagConstraints();
186 		gridBagConstraits.insets = new Insets(2, 2, 2, 2);
187 		gridBagConstraits.fill = GridBagConstraints.HORIZONTAL;
188 		gridBagConstraits.gridwidth = 1;
189 
190 		gridBagConstraits.gridx = 0;
191 		gridBagConstraits.gridy = 0;
192 		container.add(rundeckMonitorNameLabel, gridBagConstraits);
193 		gridBagConstraits.gridx = 1;
194 		container.add(rundeckMonitorName, gridBagConstraits);
195 
196 		gridBagConstraits.gridx = 0;
197 		gridBagConstraits.gridy = 1;
198 		container.add(rundeckMonitorRefreshDelayLabel, gridBagConstraits);
199 		gridBagConstraits.gridx = 1;
200 		container.add(rundeckMonitorRefreshDelay, gridBagConstraits);
201 
202 		gridBagConstraits.gridx = 0;
203 		gridBagConstraits.gridy = 2;
204 		container.add(rundeckMonitorLateExecutionThresholdLabel, gridBagConstraits);
205 		gridBagConstraits.gridx = 1;
206 		container.add(rundeckMonitorLateExecutionThreshold, gridBagConstraits);
207 
208 		gridBagConstraits.gridx = 0;
209 		gridBagConstraits.gridy = 3;
210 		container.add(rundeckMonitorFailedJobNumberLabel, gridBagConstraits);
211 		gridBagConstraits.gridx = 1;
212 		container.add(rundeckMonitorFailedJobNumber, gridBagConstraits);
213 
214 		gridBagConstraits.gridx = 0;
215 		gridBagConstraits.gridy = 4;
216 		container.add(rundeckMonitorDateFormatLabel, gridBagConstraits);
217 		gridBagConstraits.gridx = 1;
218 		container.add(rundeckMonitorDateFormat, gridBagConstraits);
219 
220 		gridBagConstraits.gridx = 0;
221 		gridBagConstraits.gridy = 5;
222 		container.add(rundeckMonitorJobTabRedirectionLabel, gridBagConstraits);
223 		gridBagConstraits.gridx = 1;
224 		container.add(rundeckMonitorJobTabRedirection, gridBagConstraits);
225 
226 		gridBagConstraits.gridx = 0;
227 		gridBagConstraits.gridy = 6;
228 		container.add(rundeckMonitorInterfaceTypeLabel, gridBagConstraits);
229 		gridBagConstraits.gridx = 1;
230 		container.add(rundeckMonitorInterfaceType, gridBagConstraits);
231 	}
232 
233 	/**
234 	 * Return the main panel component.
235 	 * 
236 	 * @return main component
237 	 */
238 	@Override
239 	public Component getPanelComponent() {
240 
241 		return container;
242 	}
243 
244 	/**
245 	 * Refresh the panel with possible values.
246 	 */
247 	@Override
248 	public void aboutToDisplayPanel() {
249 
250 		// Initialize the rundeck client with the minimal rundeck version (1)
251 		final RundeckClient rundeckClient = RundeckClientTools.buildMinimalRundeckClient(getRundeckMonitorConfiguration());
252 
253 		final String rundeckVersion = rundeckClient.getSystemInfo().getVersion();
254 
255 		JobTabRedirection oldJobTabRedirection = null;
256 		rundeckMonitorJobTabRedirection.removeAllItems();
257 		for (final JobTabRedirection jobTabRedirection : JobTabRedirection.values()) {
258 
259 			if (rundeckVersion.compareTo(jobTabRedirection.getSinceRundeckVersion()) >= 0) {
260 				rundeckMonitorJobTabRedirection.addItem(jobTabRedirection);
261 
262 				if (jobTabRedirection.name().equals(getRundeckMonitorConfiguration().getJobTabRedirection())) {
263 					oldJobTabRedirection = jobTabRedirection;
264 				}
265 			}
266 		}
267 
268 		if (null != oldJobTabRedirection) {
269 			rundeckMonitorJobTabRedirection.setSelectedItem(oldJobTabRedirection);
270 		}
271 		else {
272 			rundeckMonitorJobTabRedirection.setSelectedItem(JobTabRedirection.SUMMARY);
273 		}
274 	}
275 
276 	/**
277 	 * Validate the RunDeck monitor panel inputs.
278 	 * 
279 	 * @return true if the configuration is valid
280 	 */
281 	@Override
282 	public boolean validate() {
283 
284 		getRundeckMonitorConfiguration().setRundeckMonitorName(rundeckMonitorName.getText());
285 		getRundeckMonitorConfiguration().setRefreshDelay(rundeckMonitorRefreshDelay.getItemAt(rundeckMonitorRefreshDelay.getSelectedIndex()).getDelay());
286 		getRundeckMonitorConfiguration().setLateThreshold(rundeckMonitorLateExecutionThreshold.getItemAt(rundeckMonitorLateExecutionThreshold.getSelectedIndex()).getThreshold());
287 		getRundeckMonitorConfiguration().setFailedJobNumber(rundeckMonitorFailedJobNumber.getItemAt(rundeckMonitorFailedJobNumber.getSelectedIndex()).getFailedJobsNumber());
288 		getRundeckMonitorConfiguration().setDateFormat(rundeckMonitorDateFormat.getItemAt(rundeckMonitorDateFormat.getSelectedIndex()).getDateFormat());
289 		getRundeckMonitorConfiguration().setJobTabRedirection(rundeckMonitorJobTabRedirection.getItemAt(rundeckMonitorJobTabRedirection.getSelectedIndex()).name());
290 		getRundeckMonitorConfiguration().setInterfaceType(rundeckMonitorInterfaceType.getItemAt(rundeckMonitorInterfaceType.getSelectedIndex()).name());
291 
292 		try {
293 			getRundeckMonitorConfiguration().saveMonitorConfigurationPropertieFile();
294 		}
295 		catch (final IOException e) {
296 			return false;
297 		}
298 
299 		return true;
300 	}
301 }