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
9 import javax.swing.JComboBox;
10 import javax.swing.JLabel;
11 import javax.swing.JOptionPane;
12
13 import org.rundeck.api.RundeckClient;
14 import org.rundeck.api.domain.RundeckProject;
15
16 import com.github.sbugat.rundeckmonitor.configuration.RundeckMonitorConfiguration;
17 import com.github.sbugat.rundeckmonitor.tools.RundeckClientTools;
18
19
20
21
22
23
24
25 public final class ProjectConfigurationWizardPanelDescriptor extends WizardPanelDescriptor {
26
27
28 private final Container container = new Container();
29
30
31 private final JComboBox<String> rundeckProjectNameTextField = new JComboBox<>();
32
33
34 private final JComboBox<RundeckAPIVersion> rundeckRundeckAPIVersionTextField = new JComboBox<>();
35
36
37
38
39
40
41
42
43 public ProjectConfigurationWizardPanelDescriptor(final ConfigurationWizardStep backArg, final ConfigurationWizardStep nextArg, final RundeckMonitorConfiguration rundeckMonitorConfigurationArg) {
44 super(ConfigurationWizardStep.PROJECT_STEP, backArg, nextArg, rundeckMonitorConfigurationArg);
45
46 container.setLayout(new GridBagLayout());
47 final JLabel rundeckProjectlabel = new JLabel("Rundeck project:");
48 final JLabel rundeckApiVersionlabel = new JLabel("Rundeck API version:");
49
50 final GridBagConstraints gridBagConstraits = new GridBagConstraints();
51 gridBagConstraits.insets = new Insets(2, 2, 2, 2);
52 gridBagConstraits.fill = GridBagConstraints.HORIZONTAL;
53 gridBagConstraits.gridwidth = 1;
54
55 gridBagConstraits.gridx = 0;
56 gridBagConstraits.gridy = 0;
57 container.add(rundeckProjectlabel, gridBagConstraits);
58 gridBagConstraits.gridx = 1;
59 container.add(rundeckProjectNameTextField, gridBagConstraits);
60
61 gridBagConstraits.gridx = 0;
62 gridBagConstraits.gridy = 1;
63 container.add(rundeckApiVersionlabel, gridBagConstraits);
64 gridBagConstraits.gridx = 1;
65 container.add(rundeckRundeckAPIVersionTextField, gridBagConstraits);
66 }
67
68 @Override
69 public Component getPanelComponent() {
70
71 return container;
72 }
73
74 @Override
75 public void aboutToDisplayPanel() {
76
77
78 final RundeckClient rundeckClient = RundeckClientTools.buildMinimalRundeckClient(getRundeckMonitorConfiguration());
79
80 rundeckProjectNameTextField.removeAllItems();
81
82 boolean existingOldConfiguredProject = false;
83 for (final RundeckProject rundeckProject : rundeckClient.getProjects()) {
84
85 final String currentProjectName = rundeckProject.getName();
86 rundeckProjectNameTextField.addItem(currentProjectName);
87
88 if (!currentProjectName.isEmpty() && currentProjectName.equals(getRundeckMonitorConfiguration().getRundeckProject())) {
89
90 existingOldConfiguredProject = true;
91 }
92 }
93
94 if (existingOldConfiguredProject) {
95 rundeckProjectNameTextField.setSelectedItem(getRundeckMonitorConfiguration().getRundeckProject());
96 }
97
98 final String rundeckVersion = rundeckClient.getSystemInfo().getVersion();
99
100 rundeckRundeckAPIVersionTextField.removeAllItems();
101 RundeckAPIVersion oldApiVersion = null;
102 for (final RundeckAPIVersion version : RundeckAPIVersion.values()) {
103
104 if (rundeckVersion.compareTo(version.getSinceReturnVersion()) >= 0) {
105 rundeckRundeckAPIVersionTextField.addItem(version);
106
107 if (getRundeckMonitorConfiguration().getRundeckAPIversion() > 0) {
108 if (version.getVersion().getVersionNumber() == getRundeckMonitorConfiguration().getRundeckAPIversion()) {
109
110 oldApiVersion = version;
111 }
112 }
113 else if (version.getVersion().getVersionNumber() <= RundeckAPIVersion.RUNDECK_APIVERSION_13.getVersion().getVersionNumber()) {
114
115 oldApiVersion = version;
116 }
117 }
118 }
119
120 if (null != oldApiVersion) {
121 rundeckRundeckAPIVersionTextField.setSelectedItem(oldApiVersion);
122 }
123 }
124
125 @Override
126 public boolean validate() {
127
128
129 final RundeckClient minimalRundeckClient = RundeckClientTools.buildMinimalRundeckClient(getRundeckMonitorConfiguration());
130
131
132 boolean existingProject = false;
133 for (final RundeckProject rundeckProject : minimalRundeckClient.getProjects()) {
134
135 if (rundeckProjectNameTextField.getSelectedItem().equals(rundeckProject.getName())) {
136 existingProject = true;
137 break;
138 }
139 }
140
141 if (!existingProject) {
142 JOptionPane.showMessageDialog(null, "Unknown Rundeck project," + System.lineSeparator() + "check and change this project name:" + System.lineSeparator() + '"' + rundeckProjectNameTextField.getSelectedItem() + "\".", "RundeckMonitor wizard error", JOptionPane.ERROR_MESSAGE);
143 return false;
144 }
145
146
147 final int selectedRundeckAPIVersion = rundeckRundeckAPIVersionTextField.getItemAt(rundeckRundeckAPIVersionTextField.getSelectedIndex()).getVersion().getVersionNumber();
148
149 try {
150 RundeckClientTools.buildRundeckClient(selectedRundeckAPIVersion, getRundeckMonitorConfiguration());
151 }
152 catch (final Exception exception) {
153 JOptionPane.showMessageDialog(null, "Invalid Rundeck API version," + System.lineSeparator() + "check and change the selected Rundeck API version:" + System.lineSeparator() + '"' + rundeckRundeckAPIVersionTextField.getSelectedItem() + "\".", "RundeckMonitor wizard error", JOptionPane.ERROR_MESSAGE);
154 return false;
155 }
156
157 getRundeckMonitorConfiguration().setRundeckProject(rundeckProjectNameTextField.getItemAt(rundeckProjectNameTextField.getSelectedIndex()));
158 getRundeckMonitorConfiguration().setRundeckAPIversion(selectedRundeckAPIVersion);
159
160 return true;
161 }
162 }