Subversion Repositories XServices

Rev

Rev 30 | Rev 52 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 brianR 1
/*
2
 *   Mylyn Connector for Serena Business Mashups
3
 * 	 Copyright 2010 Brian Rosenberger (Brutex Network)
4
 *
5
 *   Licensed under the Apache License, Version 2.0 (the "License");
6
 *   you may not use this file except in compliance with the License.
7
 *   You may obtain a copy of the License at
8
 *
9
 *       http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 *   Unless required by applicable law or agreed to in writing, software
12
 *   distributed under the License is distributed on an "AS IS" BASIS,
13
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 *   See the License for the specific language governing permissions and
15
 *   limitations under the License.
16
 *
17
 *   Serena, TeamTrack and Serena Business Mashup are
18
 * 	 registered trademarks of SERENA Software Inc.
19
 */
20
package net.brutex.mylyn.sbmconnector.ui;
21
 
39 brianR 22
import java.util.List;
23
 
30 brianR 24
import net.brutex.mylyn.sbmconnector.SBMConnectorPlugin;
25
import net.brutex.mylyn.sbmconnector.core.SBMClient;
39 brianR 26
import net.brutex.mylyn.sbmconnector.core.SBMRepositoryConnector;
27
import net.brutex.mylyn.sbmconnector.core.model.SBMField;
28
import net.brutex.mylyn.sbmconnector.core.model.SBMSystemFields;
30 brianR 29
 
30
import org.eclipse.core.runtime.CoreException;
31
import org.eclipse.core.runtime.IProgressMonitor;
39 brianR 32
import org.eclipse.jface.dialogs.IMessageProvider;
30 brianR 33
import org.eclipse.mylyn.tasks.core.TaskRepository;
34
import org.eclipse.mylyn.tasks.ui.wizards.AbstractRepositorySettingsPage;
39 brianR 35
import org.eclipse.swt.SWT;
36
import org.eclipse.swt.events.ModifyEvent;
37
import org.eclipse.swt.events.ModifyListener;
38
import org.eclipse.swt.layout.GridData;
39
import org.eclipse.swt.layout.GridLayout;
40
import org.eclipse.swt.widgets.Combo;
30 brianR 41
import org.eclipse.swt.widgets.Composite;
39 brianR 42
import org.eclipse.swt.widgets.Event;
43
import org.eclipse.swt.widgets.Label;
44
import org.eclipse.swt.widgets.Listener;
45
import org.eclipse.swt.widgets.Table;
46
import org.eclipse.swt.widgets.TableColumn;
47
import org.eclipse.swt.widgets.TableItem;
48
import org.eclipse.ui.forms.widgets.ExpandableComposite;
49
import org.eclipse.ui.forms.widgets.FormToolkit;
30 brianR 50
 
51
public class SBMRepositorySettingsPage extends AbstractRepositorySettingsPage {
52
 
39 brianR 53
	private Combo table;
54
	private Table list;
55
 
56
	private String solutionTable = "";
57
	private String solutionTableList = "";
58
	private String tableFieldList = "";
59
	private String tableFullFieldList = "";
60
 
61
	public static final String STRING_SOLUTIONTABLE = "solutionTable";
62
	public static final String STRING_SOLUTIONTABLELIST = "solutionTableList";
63
	public static final String STRING_TABLEFIELDLIST = "tableFieldList";
64
	public static final String STRING_TABLEFULLFIELDLIST = "tableFullFieldList";
65
 
30 brianR 66
	public SBMRepositorySettingsPage(String title, String description,
67
			TaskRepository taskRepository) {
68
		super(title, description, taskRepository);
39 brianR 69
		setNeedsAdvanced(false);
70
		setNeedsValidation(true);
71
		setNeedsEncoding(false);
72
		// initialize table and table list if not null
73
		if (taskRepository.hasProperty(STRING_SOLUTIONTABLE)) {
74
			this.solutionTable = taskRepository
75
					.getProperty(STRING_SOLUTIONTABLE);
76
		}
77
		if (taskRepository.hasProperty(STRING_SOLUTIONTABLELIST)) {
78
			this.solutionTableList = taskRepository
79
					.getProperty(STRING_SOLUTIONTABLELIST);
80
		}
81
		if (taskRepository.hasProperty(STRING_TABLEFIELDLIST)) {
82
			this.tableFieldList = taskRepository
83
					.getProperty(STRING_TABLEFIELDLIST);
84
		}
85
		if (taskRepository.hasProperty(STRING_TABLEFULLFIELDLIST)) {
86
			this.tableFullFieldList = taskRepository
87
					.getProperty(STRING_TABLEFULLFIELDLIST);
88
		}
30 brianR 89
	}
90
 
91
	@Override
39 brianR 92
	public void applyTo(TaskRepository repository) {
93
		super.applyTo(repository);
94
		if (!solutionTable.isEmpty()) {
95
			repository.setProperty(STRING_SOLUTIONTABLE, solutionTable);
96
			repository.setProperty(STRING_SOLUTIONTABLELIST, solutionTableList);
97
			String fieldlist = ",";
98
			String fullfieldlist = ",";
99
			for (TableItem t : list.getItems()) {
100
				if (t.getChecked())
101
					fieldlist = fieldlist + t.getText(2) + ",";
102
				fullfieldlist = fullfieldlist + t.getText(2) + ",";
103
			}
104
			repository.setProperty(STRING_TABLEFIELDLIST, fieldlist);
105
			repository.setProperty(STRING_TABLEFULLFIELDLIST, fullfieldlist);
106
		}
107
	}
30 brianR 108
 
39 brianR 109
	@Override
110
	public boolean isPageComplete() {
111
		if (table != null && table.getText().length() > 4) {
112
			return super.isPageComplete();
113
		}
114
		return false;
30 brianR 115
	}
116
 
117
	@Override
39 brianR 118
	protected void createContributionControls(Composite parentControl) {
119
		ExpandableComposite expander = createSection(parentControl,
120
				"Mashup application");
121
		FormToolkit toolkit = new FormToolkit(parentControl.getDisplay());
122
		Composite control = toolkit.createComposite(expander, SWT.NONE);
123
		GridLayout gridLayout = new GridLayout();
124
		gridLayout.numColumns = 2;
125
		gridLayout.verticalSpacing = 5;
126
		gridLayout.marginWidth = 0;
127
		control.setLayout(gridLayout);
128
		control.setBackground(parentControl.getBackground());
129
 
130
		Label label = new Label(control, SWT.NONE);
131
		label.setText("Table:");
132
 
133
		table = new Combo(control, SWT.SINGLE | SWT.BORDER);
134
		Label label_list = new Label(control, SWT.NONE | SWT.TOP);
135
		label_list.setText("Fields:");
136
		list = new Table(control, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL
137
				| SWT.H_SCROLL | SWT.FULL_SELECTION | SWT.CHECK);
138
		list.setLinesVisible(true);
139
		list.setHeaderVisible(true);
140
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
141
		data.heightHint = 200;
142
		list.setLayoutData(data);
143
		String[] titles = { " ", "Display Name", "DBName", "Type" };
144
		for (int i = 0; i < titles.length; i++) {
145
			TableColumn column = new TableColumn(list, SWT.NONE);
146
			column.setText(titles[i]);
147
		}
148
		list.addListener(SWT.Selection, new Listener() {
149
			@Override
150
			public void handleEvent(Event event) {
151
				//avoid that a system field may be unchecked
152
				try {
153
					SBMSystemFields
154
							.valueOf(((TableItem) event.item).getText(2));
155
					((TableItem) event.item).setChecked(true);
156
				} catch (IllegalArgumentException e) {
157
					// do nothing
158
				}
159
			}
160
		});
161
		table.addListener(SWT.Selection, new Listener() {
162
			@Override
163
			public void handleEvent(Event arg0) {
164
				// when the table has been changed
165
				// try to re-read the field list
166
				tableFieldList = "";
167
				tableFullFieldList = "";
168
				try {
169
					SBMField[] fields = SBMRepositoryConnector.getClient(
170
						createTaskRepository()).getFields(table.getText())
171
						.toArray(new SBMField[0]);
172
				fillList(list, fields);
173
				} catch (Exception e) { }
174
				if (getWizard() != null) {
175
					solutionTable = table.getText();
176
					getWizard().getContainer().updateButtons();
177
				}
178
			}
179
 
180
		});
181
		regenerateTables();
182
		expander.setClient(control);
183
	}
184
 
185
	@Override
30 brianR 186
	public String getConnectorKind() {
187
		return SBMConnectorPlugin.CONNECTOR_KIND;
188
	}
189
 
190
	@Override
191
	protected Validator getValidator(TaskRepository repository) {
192
		return new SBMConnectionValidator(repository);
193
	}
194
 
195
	@Override
196
	protected boolean isValidUrl(String url) {
39 brianR 197
		if (url.contains("<server>"))
198
			return false;
30 brianR 199
		return true;
200
	}
39 brianR 201
 
30 brianR 202
	@Override
203
	protected void applyValidatorResult(Validator validator) {
204
		if (((SBMConnectionValidator) validator).getResult() != null) {
39 brianR 205
			setMessage("Validate first!", IMessageProvider.ERROR);
206
			setNeedsValidation(true);
207
		} else {
208
			regenerateTables();
30 brianR 209
		}
210
	}
39 brianR 211
 
212
	private void regenerateTables() {
213
		try {
214
			List<String> tables = SBMRepositoryConnector.getClient(
215
					createTaskRepository()).getPrimaryTables();
216
			for (String table_name : tables) {
217
				table.add(table_name);
218
				if (solutionTable.equals(table_name)) {
219
					table.setText(table_name);
220
				}
221
			}
222
		} catch (Exception e) {
223
			// it was not possible to read the list of tables
224
			// from the server, so just restore saved settings
225
			table.add(solutionTable);
226
			table.setText(solutionTable);
227
		}
228
		try {
229
		SBMField[] fields = SBMRepositoryConnector.getClient(
230
				createTaskRepository()).getFields(table.getText()).toArray(
231
				new SBMField[0]);
232
				fillList(list, fields);
233
		} catch (Exception e) {
234
			// it was not possible to read the fields for the given table
235
			// do nothing
236
		}
237
 
238
	}
239
 
30 brianR 240
	protected class SBMConnectionValidator extends Validator {
241
		private TaskRepository repository;
242
		private String result = null;
39 brianR 243
 
30 brianR 244
		protected SBMConnectionValidator(TaskRepository repository) {
39 brianR 245
			this.repository = repository;
30 brianR 246
		}
39 brianR 247
 
30 brianR 248
		@Override
249
		public void run(IProgressMonitor monitor) throws CoreException {
39 brianR 250
			monitor.beginTask("Prüfe Verbindung", IProgressMonitor.UNKNOWN);
30 brianR 251
			SBMClient client = new SBMClient(repository);
39 brianR 252
			if (!client.canAuthenticate()) {
30 brianR 253
				result = "Verbindung fehlgeschlagen";
39 brianR 254
			} else {
255
				result = null;
30 brianR 256
			}
257
			monitor.done();
258
		}
39 brianR 259
 
260
		public String getResult() {
261
			return result;
262
		}
30 brianR 263
	}
39 brianR 264
 
265
	private void fillList(Table list, SBMField[] fields) {
266
		list.removeAll();
267
		for (int i = 0; i < fields.length; i++) {
268
			TableItem item = new TableItem(list, 0);
269
			item.setText(0, "");
270
			item.setText(1, fields[i].getLabel());
271
			item.setText(2, fields[i].getName());
272
			item.setText(3, fields[i].getType().getValue().replaceFirst(
273
					"FLDTYPE-", ""));
274
			try {
275
				// auto-select all system fields
276
				SBMSystemFields.valueOf(fields[i].getName());
277
				item.setChecked(true);
278
			} catch (IllegalArgumentException e) {
279
				// do nothing
280
			}
281
			if (tableFieldList.contains(","+fields[i].getName()+",")) {
282
				// item has been checked before
283
				// so check it
284
				item.setChecked(true);
285
			}
286
		}
287
 
288
		for (TableColumn c : list.getColumns()) {
289
			c.pack();
290
		}
291
	}
292
 
293
	@Override
294
	protected void createAdditionalControls(Composite arg0) {
295
		// TODO Auto-generated method stub
296
 
297
	}
298
 
30 brianR 299
}