Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 770648837ae02e760e45d46309690e371b00ee19 (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*******************************************************************************
 * Copyright (c) 2006, 2007 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.mappings.details;

import java.util.Iterator;
import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.Window;
import org.eclipse.jpt.core.internal.mappings.IEntity;
import org.eclipse.jpt.core.internal.mappings.ISecondaryTable;
import org.eclipse.jpt.core.internal.mappings.ITable;
import org.eclipse.jpt.core.internal.mappings.JpaCoreMappingsPackage;
import org.eclipse.jpt.ui.internal.details.BaseJpaComposite;
import org.eclipse.jpt.ui.internal.mappings.JptUiMappingsMessages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;

//TODO handle xml, how to handle virtual secondaryTables, adding them to xml, are they overriden, etc??
public class SecondaryTablesComposite extends BaseJpaComposite 
{
	private IEntity entity;
	private final Adapter entityListener;
	private final Adapter secondaryTableListener;
	
	ListViewer secondaryTablesListViewer;

	private Button addButton;
	private Button editButton;
	private Button removeButton;
	
	private PrimaryKeyJoinColumnsInSecondaryTableComposite pkJoinColumnsComposite;
	
	public SecondaryTablesComposite(Composite parent, CommandStack commandStack, TabbedPropertySheetWidgetFactory widgetFactory) {
		super(parent, SWT.NULL, commandStack, widgetFactory);
		this.entityListener = buildEntityListener();
		this.secondaryTableListener = buildSecondaryTableListener();
	}
	
	private Adapter buildEntityListener() {
		return new AdapterImpl() {
			public void notifyChanged(Notification notification) {
				entityChanged(notification);
			}
		};
	}
	
	private Adapter buildSecondaryTableListener() {
		return new AdapterImpl() {
			public void notifyChanged(Notification notification) {
				seoncaryTableChanged(notification);
			}
		};
	}
	
	@Override
	protected void initializeLayout(Composite composite) {
		GridLayout layout = new GridLayout(2, false);
		layout.marginWidth = 0;
		composite.setLayout(layout);
		
		GridData gridData =  new GridData();
		
		Composite secondaryTablesComposite = new Composite(composite, SWT.NONE);
		layout = new GridLayout(3, false);
		layout.marginWidth = 0;
		secondaryTablesComposite.setLayout(layout);
		gridData =  new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		secondaryTablesComposite.setLayoutData(gridData);
	
		this.secondaryTablesListViewer = buildSecondaryTablesListViewer(secondaryTablesComposite);
		gridData =  new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalSpan = 3;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		this.secondaryTablesListViewer.getList().setLayoutData(gridData);
		//PlatformUI.getWorkbench().getHelpSystem().setHelp(this.secondaryTablesListViewer.getList(), IJpaHelpContextIds.MAPPING_JOIN_TABLE_COLUMNS);
		
		Composite buttonsComposite = new Composite(secondaryTablesComposite, SWT.NONE);
		layout = new GridLayout(3, false);
		layout.marginWidth = 0;
		buttonsComposite.setLayout(layout);
		gridData =  new GridData();
		gridData.horizontalAlignment = SWT.END;
		buttonsComposite.setLayoutData(gridData);
		
		this.addButton = new Button(buttonsComposite, SWT.NONE);
		this.addButton.setText(JptUiMappingsMessages.SecondaryTablesComposite_add);
		gridData =  new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		this.addButton.setLayoutData(gridData);
		this.addButton.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
				// do nothing
			}
		
			public void widgetSelected(SelectionEvent e) {
				addSecondaryTable();
			}
		});
		
		this.editButton = new Button(buttonsComposite, SWT.NONE);
		this.editButton.setText(JptUiMappingsMessages.SecondaryTablesComposite_edit);
		gridData =  new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		this.editButton.setLayoutData(gridData);
		this.editButton.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
				// do nothing
			}
		
			public void widgetSelected(SelectionEvent e) {
				editSecondaryTable();
			}
		});

		this.removeButton = new Button(buttonsComposite, SWT.NONE);
		this.removeButton.setText(JptUiMappingsMessages.SecondaryTablesComposite_remove);
		gridData =  new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.BEGINNING;
		this.removeButton.setLayoutData(gridData);
		this.removeButton.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
				// do nothing
			}
		
			public void widgetSelected(SelectionEvent e) {
				removeSecondaryTable();
			}
		});
		
		this.pkJoinColumnsComposite = new PrimaryKeyJoinColumnsInSecondaryTableComposite(composite, this.commandStack, getWidgetFactory());
		gridData =  new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		this.pkJoinColumnsComposite.getControl().setLayoutData(gridData);
	}
	
	private ListViewer buildSecondaryTablesListViewer(Composite parent) {
		ListViewer listViewer = new ListViewer(parent, SWT.SINGLE | SWT.BORDER);
		listViewer.setLabelProvider(buildSecondaryTablesListLabelProvider());
		listViewer.setContentProvider(buildSecondaryTablesListContentProvider());
		
		listViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				updateEnablement();
				secondaryTablesListSelectionChanged(event);
			}
		});

		return listViewer;
	}
	
	protected void secondaryTablesListSelectionChanged(SelectionChangedEvent event) {
		if (((StructuredSelection) event.getSelection()).isEmpty()) {
			this.pkJoinColumnsComposite.populate(null);
			this.pkJoinColumnsComposite.enableWidgets(false);
		}
		else {
			ISecondaryTable selectedSecondaryTable = getSelectedSecondaryTable();
			this.pkJoinColumnsComposite.populate(selectedSecondaryTable);
			this.pkJoinColumnsComposite.enableWidgets(true);
		}
	}

	private IContentProvider buildSecondaryTablesListContentProvider() {
		return new IStructuredContentProvider(){
			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
				// do nothing
			}
		
			public void dispose() {
				// do nothing
			}
		
			public Object[] getElements(Object inputElement) {
				return ((IEntity) inputElement).getSecondaryTables().toArray();
			}
		};
	}
	
	private ILabelProvider buildSecondaryTablesListLabelProvider() {
		return new LabelProvider() {
			public String getText(Object element) {
				//TODO display a qualified name instead
				ISecondaryTable secondaryTable = (ISecondaryTable) element;
				return secondaryTable.getName();
			}
		};
	}
	
	void addSecondaryTable() {
		SecondaryTableDialog dialog = new SecondaryTableDialog(this.getControl().getShell(), this.entity);
		addSecondaryTableFromDialog(dialog);
	}
	
	private void addSecondaryTableFromDialog(SecondaryTableDialog dialog) {
		if (dialog.open() == Window.OK) {
			int index = this.entity.getSpecifiedSecondaryTables().size();
			String name = dialog.getSelectedName();
			String catalog = dialog.getSelectedCatalog();
			String schema = dialog.getSelectedSchema();
			ISecondaryTable secondaryTable = this.entity.createSecondaryTable(index);
			this.entity.getSpecifiedSecondaryTables().add(secondaryTable);
			secondaryTable.setSpecifiedName(name);
			secondaryTable.setSpecifiedCatalog(catalog);
			secondaryTable.setSpecifiedSchema(schema);
			
			this.secondaryTablesListViewer.setSelection(new StructuredSelection(secondaryTable));
		}
	}
	
	void editSecondaryTable() {
		ISecondaryTable secondaryTable = getSelectedSecondaryTable();
		SecondaryTableDialog dialog = new SecondaryTableDialog(this.getControl().getShell(), secondaryTable, this.entity);
		editSecondaryTableFromDialog(dialog, secondaryTable);
	}
	
	private void editSecondaryTableFromDialog(SecondaryTableDialog dialog, ISecondaryTable secondaryTable) {
		if (dialog.open() == Window.OK) {
			editSecondaryTableDialogOkd(dialog, secondaryTable);
		}
	}
	
	private void editSecondaryTableDialogOkd(SecondaryTableDialog dialog, ISecondaryTable secondaryTable) {
		String name = dialog.getSelectedName();
		String catalog = dialog.getSelectedCatalog();
		String schema = dialog.getSelectedSchema();

		if (secondaryTable.getSpecifiedName() == null || !secondaryTable.getSpecifiedName().equals(name)){
			secondaryTable.setSpecifiedName(name);
		}
		
		if (dialog.isDefaultCatalogSelected()) {
			if (secondaryTable.getSpecifiedCatalog() != null) {
				secondaryTable.setSpecifiedCatalog(null);
			}
		}
		else if (secondaryTable.getSpecifiedCatalog() == null || !secondaryTable.getSpecifiedCatalog().equals(catalog)){
			secondaryTable.setSpecifiedCatalog(catalog);
		}
		
		if (dialog.isDefaultSchemaSelected()) {
			if (secondaryTable.getSpecifiedSchema() != null) {
				secondaryTable.setSpecifiedSchema(null);
			}
		}
		else if (secondaryTable.getSpecifiedSchema() == null || !secondaryTable.getSpecifiedSchema().equals(schema)){
			secondaryTable.setSpecifiedSchema(schema);
		}
	}

	private ISecondaryTable getSelectedSecondaryTable() {
		return (ISecondaryTable) ((StructuredSelection) this.secondaryTablesListViewer.getSelection()).getFirstElement();
	}

	
	void removeSecondaryTable() {
		ISelection selection = this.secondaryTablesListViewer.getSelection();
		if (selection instanceof StructuredSelection) {
			for (Iterator<ISecondaryTable> i = ((StructuredSelection) selection).iterator(); i.hasNext(); ) {
				this.entity.getSecondaryTables().remove(i.next());
			}
		}
	}
	
	void updateEnablement() {
		this.editButton.setEnabled(!((StructuredSelection) this.secondaryTablesListViewer.getSelection()).isEmpty());
		this.removeButton.setEnabled(!((StructuredSelection) this.secondaryTablesListViewer.getSelection()).isEmpty());
	}	
	
	public void doPopulate(EObject obj) {
		this.entity = (IEntity) obj;
		if (this.entity == null) {
			this.secondaryTablesListViewer.setInput(null);
			return;
		}
		
		this.secondaryTablesListViewer.setInput(this.entity);
		if (!this.entity.getSecondaryTables().isEmpty()) {
			this.secondaryTablesListViewer.setSelection(new StructuredSelection(this.entity.getSecondaryTables().get(0)));
		}
		else {
			this.secondaryTablesListViewer.setSelection(null);			
		}
		updateEnablement();
	}

	@Override
	protected void doPopulate() {
	}

	protected void engageListeners() {
		if (this.entity != null) {
			this.entity.eAdapters().add(this.entityListener);
			for (ISecondaryTable secondaryTable : this.entity.getSecondaryTables()) {
				secondaryTable.eAdapters().add(this.secondaryTableListener);
			}	
		}
	}
	
	protected void disengageListeners() {
		if (this.entity != null) {
			for (ISecondaryTable secondaryTable : this.entity.getSecondaryTables()) {
				secondaryTable.eAdapters().remove(this.secondaryTableListener);
			}
			this.entity.eAdapters().remove(this.entityListener);
		}
	}
	
	protected void entityChanged(Notification notification) {
		if (notification.getFeatureID(IEntity.class) == JpaCoreMappingsPackage.IENTITY__SPECIFIED_SECONDARY_TABLES) {
			if (notification.getEventType() == Notification.ADD) {
				((ISecondaryTable) notification.getNewValue()).eAdapters().add(this.secondaryTableListener);
			}
			else if (notification.getEventType() == Notification.REMOVE) {
				((ISecondaryTable) notification.getOldValue()).eAdapters().remove(this.secondaryTableListener);				
			}
			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					if (getControl().isDisposed()) {
						return;
					}
					secondaryTablesListViewer.refresh();
					updateEnablement();
				}
			});
		}
	}

	protected void seoncaryTableChanged(Notification notification) {
		if (notification.getFeatureID(ITable.class) == JpaCoreMappingsPackage.ITABLE__SPECIFIED_NAME
			|| notification.getFeatureID(ITable.class) == JpaCoreMappingsPackage.ITABLE__SPECIFIED_CATALOG
			|| notification.getFeatureID(ITable.class) == JpaCoreMappingsPackage.ITABLE__SPECIFIED_SCHEMA
			|| notification.getFeatureID(ITable.class) == JpaCoreMappingsPackage.ITABLE__DEFAULT_CATALOG
			|| notification.getFeatureID(ITable.class) == JpaCoreMappingsPackage.ITABLE__DEFAULT_SCHEMA) {
			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					if (getControl().isDisposed()) {
						return;
					}
					secondaryTablesListViewer.refresh();
				}
			});
		}
	}
}

Back to the top