Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1c04312695c788b6b54e6f50557434879ad1bead (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*******************************************************************************
 * Copyright (c) 2006, 2008 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.ui.internal.wizards;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.jpt.core.JpaProject;
import org.eclipse.jpt.db.ConnectionAdapter;
import org.eclipse.jpt.db.ConnectionListener;
import org.eclipse.jpt.db.ConnectionProfile;
import org.eclipse.jpt.db.JptDbPlugin;
import org.eclipse.jpt.db.Schema;
import org.eclipse.jpt.db.Table;
import org.eclipse.jpt.db.ui.internal.DTPUiTools;
import org.eclipse.jpt.ui.internal.JpaHelpContextIds;
import org.eclipse.jpt.ui.internal.JptUiMessages;
import org.eclipse.jpt.utility.internal.CollectionTools;
import org.eclipse.jpt.utility.internal.StringTools;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.ui.PlatformUI;

public class DatabaseReconnectWizardPage extends WizardPage
{
	private JpaProject jpaProject;

	private ConnectionProfile profile;

	private ConnectionListener connectionListener;

	private DatabaseGroup databaseGroup;

	public DatabaseReconnectWizardPage(JpaProject jpaProject) {
		super("Database Settings"); //$NON-NLS-1$
		this.jpaProject = jpaProject;
		setTitle(JptUiMessages.DatabaseReconnectWizardPage_databaseConnection);
		setMessage(JptUiMessages.DatabaseReconnectWizardPage_reconnectToDatabase);
	}

	public void createControl(Composite parent) {
		this.setPageComplete(false);
		Composite top = this.createTopLevelComposite(parent);
		this.setControl(top);
	}

	protected Composite createTopLevelComposite(Composite parent) {
		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		this.databaseGroup = new DatabaseGroup(composite);
		Dialog.applyDialogFont(parent);
		// TODO Add Help - testing
		PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, JpaHelpContextIds.PROPERTIES_JAVA_PERSISTENCE_CONNECTION);
		return composite;
	}

	private Label createLabel(Composite container, int span, String text) {
		Label label = new Label(container, SWT.NONE);
		label.setText(text);
		GridData gd = new GridData();
		gd.horizontalIndent = 30;
		gd.horizontalSpan = span;
		label.setLayoutData(gd);
		return label;
	}

	private Combo createCombo(Composite container, boolean fillHorizontal) {
		Combo combo = new Combo(container, SWT.BORDER | SWT.SINGLE);
		if (fillHorizontal) {
			combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		}
		else {
			combo.setLayoutData(new GridData());
		}
		return combo;
	}

	/**
	 * Initialize a grid layout with the default Dialog settings.
	 */
	protected GridLayout initGridLayout(GridLayout layout, boolean margins) {
		layout.horizontalSpacing = this.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
		layout.verticalSpacing = this.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
		if (margins) {
			layout.marginWidth = this.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
			layout.marginHeight = this.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
		}
		else {
			layout.marginWidth = 0;
			layout.marginHeight = 0;
		}
		return layout;
	}

	public Collection<Table> getTables() {
		Schema schema = this.getDefaultSchema();
		if (schema != null && schema.getName() != null) {
			return CollectionTools.collection(schema.tables());
		}
		return Collections.<Table> emptyList();
	}

	private void updateGenerateEntitiesPage(Schema schema) {
		if (this.getWizard() instanceof GenerateEntitiesWizard) {
			GenerateEntitiesWizard generateEntitiesWizard = ((GenerateEntitiesWizard) this.getWizard());
			generateEntitiesWizard.updatePossibleTables(CollectionTools.collection(schema.tables()));
		}
	}

	@Override
	public void dispose() {
		this.removeConnectionListener();
		super.dispose();
	}

	private void removeConnectionListener() {
		if (this.connectionListener != null) {
			if (this.profile != null) {
				this.profile.removeConnectionListener(this.connectionListener);
			}
			this.connectionListener = null;
		}
	}

	ConnectionProfile getProjectConnectionProfile() {
		String profileName = this.jpaProject.getDataSource().getConnectionProfileName();
		return this.connectionProfileNamed(profileName);
	}

	ConnectionProfile connectionProfileNamed(String profileName) {
		return JptDbPlugin.instance().getConnectionProfileRepository().connectionProfileNamed(profileName);
	}

	Schema getDefaultSchema() {
		return this.jpaProject.getDefaultSchema();
	}

	public String getSelectedConnectionProfileName() {
		return this.databaseGroup.getConnectionProfileName();
	}

	public void clearConnectionProfileName() {
		this.databaseGroup.clearConnectionProfileName();
	}
	// ********** member classes **********
	private final class DatabaseGroup
	{
		private final Group group;

		private final Combo connectionCombo;

		private final Combo schemaCombo;

		private Link addConnectionLink;

		private Link reconnectLink;

		public DatabaseGroup(Composite composite) {
			this.group = new Group(composite, SWT.NONE);
			GridLayout layout = new GridLayout();
			layout.numColumns = 2;
			this.group.setLayout(layout);
			this.group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			this.group.setText(JptUiMessages.DatabaseReconnectWizardPage_database);
			// TODO Add Help
			// PlatformUI.getWorkbench().getHelpSystem().setHelp( this.group,
			// IDaliHelpContextIds.XXX);
			createLabel(this.group, 1, JptUiMessages.DatabaseReconnectWizardPage_connection);
			this.connectionCombo = createCombo(this.group, true);
			this.connectionCombo.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetDefaultSelected(SelectionEvent e) {
					widgetSelected(e);
				}

				@Override
				public void widgetSelected(SelectionEvent e) {
					handleConnectionChange();
				}
			});
			createLabel(this.group, 1, JptUiMessages.DatabaseReconnectWizardPage_schema);
			this.schemaCombo = createCombo(this.group, true);
			this.schemaCombo.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetDefaultSelected(SelectionEvent e) {
					widgetSelected(e);
				}

				@Override
				public void widgetSelected(SelectionEvent e) {
					handleSchemaChange();
				}
			});
			createLabel(this.group, 2, JptUiMessages.DatabaseReconnectWizardPage_schemaInfo);
			this.addConnectionLink = new Link(this.group, SWT.NONE);
			GridData data = new GridData(GridData.END, GridData.CENTER, false, false);
			data.horizontalSpan = 2;
			this.addConnectionLink.setLayoutData(data);
			this.addConnectionLink.setText(JptUiMessages.DatabaseReconnectWizardPage_addConnectionLink);
			this.addConnectionLink.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					openNewConnectionWizard();
				}
			});
			this.reconnectLink = new Link(this.group, SWT.NONE);
			data = new GridData(GridData.END, GridData.CENTER, false, false);
			data.horizontalSpan = 2;
			this.reconnectLink.setLayoutData(data);
			this.reconnectLink.setText(JptUiMessages.DatabaseReconnectWizardPage_reconnectLink);
			this.reconnectLink.setEnabled(false);
			this.reconnectLink.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					openConnectionProfileNamed(getConnectionProfileName());
				}
			});
			this.populateConnectionCombo();
			this.populateSchemaCombo();
		}

		private ConnectionProfile connectionProfileNamed(String profileName) {
			return DatabaseReconnectWizardPage.this.connectionProfileNamed(profileName);
		}

		private Iterator<String> dtpConnectionProfileNames() {
			return JptDbPlugin.instance().getConnectionProfileRepository().connectionProfileNames();
		}

		private String getProjectConnectionProfileName() {
			return jpaProject.getDataSource().getConnectionProfileName();
		}

		private void openConnectionProfileNamed(String connectionProfileName) {
			DatabaseReconnectWizardPage.this.removeConnectionListener();
			DatabaseReconnectWizardPage.this.profile = this.connectionProfileNamed(connectionProfileName);
			DatabaseReconnectWizardPage.this.profile.connect();
			if (DatabaseReconnectWizardPage.this.profile.isActive()) {
				this.populateSchemaCombo();
				DatabaseReconnectWizardPage.this.connectionListener = this.buildConnectionListener();
				DatabaseReconnectWizardPage.this.profile.addConnectionListener(DatabaseReconnectWizardPage.this.connectionListener);
			}
			return;
		}

		private void populateConnectionCombo() {
			// clear out connection entries from previous login.
			this.connectionCombo.removeAll();
			for (Iterator<String> i = CollectionTools.sort(this.dtpConnectionProfileNames()); i.hasNext();) {
				this.connectionCombo.add(i.next());
			}
			String connectionName = getProjectConnectionProfileName();
			if (!StringTools.stringIsEmpty(connectionName)) {
				this.connectionCombo.select(connectionCombo.indexOf(connectionName));
				this.reconnectLink.setEnabled(true);
			}
		}

		private void handleConnectionChange() {
			this.reconnectLink.setEnabled(true);
			this.populateSchemaCombo();
		}

		private void handleSchemaChange() {
			ConnectionProfile connectionProfile = this.connectionProfileNamed(getConnectionProfileName());
			Schema schema = connectionProfile.getDatabase().schemaNamed(this.getSchemaName());
			DatabaseReconnectWizardPage.this.updateGenerateEntitiesPage(schema);
			DatabaseReconnectWizardPage.this.setPageComplete(true);
		}

		private void populateSchemaCombo() {
			// clear out schema entries from previous connection selection
			this.schemaCombo.removeAll();
			ConnectionProfile connectionProfile = this.connectionProfileNamed(getConnectionProfileName());
			for (Iterator<String> stream = CollectionTools.sort(connectionProfile.getDatabase().schemaNames()); stream.hasNext();) {
				this.schemaCombo.add(stream.next());
			}
			// get default schema name from the project
			Schema schema = getDefaultSchema();
			if (schema != null && schema.getName() != null) {
				schema = connectionProfile.getDatabase().schemaNamed(schema.getName()); // verify
																						// schema
																						// exist
				if (schema != null) {
					this.schemaCombo.select(this.schemaCombo.indexOf(schema.getName()));
					DatabaseReconnectWizardPage.this.updateGenerateEntitiesPage(schema);
					DatabaseReconnectWizardPage.this.setPageComplete(true);
				}
			}
		}

		private String getConnectionProfileName() {
			return this.connectionCombo.getText();
		}

		private String getSchemaName() {
			return this.schemaCombo.getText();
		}

		void clearConnectionProfileName() {
			this.connectionCombo.setText("");
		}

		private void openNewConnectionWizard() {
			String addedProfileName = DTPUiTools.createNewProfile();
			ConnectionProfile addedProfile = this.connectionProfileNamed(addedProfileName);
			if (!addedProfile.isNull()) {
				addedProfile.connect();
				this.populateConnectionCombo();
				this.connectionCombo.select(connectionCombo.indexOf(addedProfile.getName()));
				this.handleConnectionChange();
			}
		}

		private ConnectionListener buildConnectionListener() {
			return new ConnectionAdapter() {
				@Override
				public void opened(ConnectionProfile profile) {
					if (DatabaseReconnectWizardPage.this.profile.equals(profile)) {
						DatabaseGroup.this.populateSchemaCombo();
					}
				}

				@Override
				public void aboutToClose(ConnectionProfile profile) {
					if (DatabaseReconnectWizardPage.this.profile.equals(profile)) {
						DatabaseReconnectWizardPage.this.removeConnectionListener();
					}
				}
			};
		}
	}
}

Back to the top