Skip to main content
summaryrefslogtreecommitdiffstats
blob: 03e8bb71eb7f799c4091850e2c930b55be4e3f2f (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
/*******************************************************************************
 * Copyright (c) 2006, 2013 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.jpa.ui.internal.details;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.jpt.common.ui.internal.widgets.AddRemovePane.AbstractAdapter;
import org.eclipse.jpt.common.ui.internal.widgets.AddRemoveListPane;
import org.eclipse.jpt.common.ui.internal.widgets.Pane;
import org.eclipse.jpt.common.utility.internal.model.value.CollectionPropertyValueModelAdapter;
import org.eclipse.jpt.common.utility.internal.model.value.SimpleCollectionValueModel;
import org.eclipse.jpt.common.utility.model.value.CollectionValueModel;
import org.eclipse.jpt.common.utility.model.value.ModifiableCollectionValueModel;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.jpa.core.context.Entity;
import org.eclipse.jpt.jpa.core.context.SecondaryTable;
import org.eclipse.jpt.jpa.core.context.SpecifiedSecondaryTable;
import org.eclipse.jpt.jpa.core.context.orm.OrmEntity;
import org.eclipse.jpt.jpa.ui.details.JptJpaUiDetailsMessages;
import org.eclipse.jpt.jpa.ui.internal.details.orm.OrmEntityComposite;
import org.eclipse.swt.widgets.Composite;

/**
 * Here the layout of this pane:
 * <pre>
 * -----------------------------------------------------------------------------
 * | ------------------------------------------------------------------------- |
 * | |                                                                       | |
 * | | AddRemoveListPane                                                     | |
 * | |                                                                       | |
 * | ------------------------------------------------------------------------- |
 * | ------------------------------------------------------------------------- |
 * | |                                                                       | |
 * | | PrimaryKeyJoinColumnsInSecondaryTableComposite                        | |
 * | |                                                                       | |
 * | ------------------------------------------------------------------------- |
 * -----------------------------------------------------------------------------</pre>
 *
 * @see OrmEntity
 * @see OrmEntityComposite - The container of this pane
 * @see AddRemoveListPane
 * @see PrimaryKeyJoinColumnsInSecondaryTableComposite
 *
 * @version 2.0
 * @since 1.0
 */
public abstract class AbstractSecondaryTablesComposite<T extends Entity> extends Pane<T>
{
	/**
	 * Creates a new <code>SecondaryTablesComposite</code>.
	 *
	 * @param parentPane The parent container of this one
	 * @param parent The parent container
	 */
	public AbstractSecondaryTablesComposite(Pane<? extends T> parentPane,
	                                Composite parent) {

		super(parentPane, parent);
	}

	protected SpecifiedSecondaryTable addSecondaryTableFromDialog(SecondaryTableDialog dialog) {
		if (dialog.open() != Window.OK) {
			return null;
		}

		SpecifiedSecondaryTable secondaryTable = this.getSubject().addSpecifiedSecondaryTable();
		secondaryTable.setSpecifiedName(dialog.getSelectedTable());
		secondaryTable.setSpecifiedCatalog(dialog.getSelectedCatalog());
		secondaryTable.setSpecifiedSchema(dialog.getSelectedSchema());

		return secondaryTable;
	}

	protected ModifiableCollectionValueModel<SpecifiedSecondaryTable> buildSelectedSecondaryTablesModel() {
		return new SimpleCollectionValueModel<SpecifiedSecondaryTable>();
	}

	protected PropertyValueModel<SpecifiedSecondaryTable> buildSelectedSecondaryTableModel(CollectionValueModel<SpecifiedSecondaryTable> selectedSecondaryTablesModel) {
		return new CollectionPropertyValueModelAdapter<SpecifiedSecondaryTable, SpecifiedSecondaryTable>(selectedSecondaryTablesModel) {
			@Override
			protected SpecifiedSecondaryTable buildValue() {
				if (this.collectionModel.size() == 1) {
					return this.collectionModel.iterator().next();
				}
				return null;
			}
		};
	}

	protected ILabelProvider buildSecondaryTableLabelProvider() {
		return new LabelProvider() {
			@Override
			public String getText(Object element) {
				// TODO display a qualified name instead
				SecondaryTable secondaryTable = (SecondaryTable) element;
				if (secondaryTable.getName() != null) {
					return secondaryTable.getName();
				}
				return "";//TODO
			}
		};
	}

	protected SecondaryTableDialog buildSecondaryTableDialogForAdd() {
		return new SecondaryTableDialog(getShell(), getSubject().getJpaProject(), getSubject().getTable().getDefaultCatalog(), getSubject().getTable().getDefaultSchema());
	}
	
	protected AddRemoveListPane.Adapter<SpecifiedSecondaryTable> buildSecondaryTablesAdapter() {
		return new AbstractAdapter<SpecifiedSecondaryTable>() {

			public SpecifiedSecondaryTable addNewItem() {
				SecondaryTableDialog dialog = buildSecondaryTableDialogForAdd();
				return addSecondaryTableFromDialog(dialog);
			}

			@Override
			public boolean hasOptionalButton() {
				return true;
			}

			@Override
			public String optionalButtonText() {
				return JptJpaUiDetailsMessages.SECONDARY_TABLES_COMPOSITE_EDIT;
			}

			@Override
			public void optionOnSelection(CollectionValueModel<SpecifiedSecondaryTable> selectedItemsModel) {
				//assume only 1 item in the list based on the optionalButtonEnabledModel
				SpecifiedSecondaryTable secondaryTable = selectedItemsModel.iterator().next();
				SecondaryTableDialog dialog = new SecondaryTableDialog(getShell(), getSubject().getJpaProject(), secondaryTable);
				editSecondaryTableFromDialog(dialog, secondaryTable);
			}

			public void removeSelectedItems(CollectionValueModel<SpecifiedSecondaryTable> selectedItemsModel) {
				//assume only 1 item since remove button is disabled otherwise
				SpecifiedSecondaryTable secondaryTable = selectedItemsModel.iterator().next();
				getSubject().removeSpecifiedSecondaryTable(secondaryTable);
			}

			/**
			 * If any of the selected secondary tables are virtual, the Remove button is disabled
			 */
			@Override
			public PropertyValueModel<Boolean> buildRemoveButtonEnabledModel(CollectionValueModel<SpecifiedSecondaryTable> selectedItemsModel) {
				return buildOptionalButtonEnabledModel(selectedItemsModel);
			}

			@Override
			public PropertyValueModel<Boolean> buildOptionalButtonEnabledModel(CollectionValueModel<SpecifiedSecondaryTable> selectedItemsModel) {
				return new CollectionPropertyValueModelAdapter<Boolean, SpecifiedSecondaryTable>(selectedItemsModel) {
					@Override
					protected Boolean buildValue() {
						if (this.collectionModel.size() == 1) {
							SpecifiedSecondaryTable secondaryTable = this.collectionModel.iterator().next();
							return Boolean.valueOf(!secondaryTable.isVirtual());				
						}
						return Boolean.FALSE;
					}
				};
			}
		};
	}

	protected void editSecondaryTableFromDialog(SecondaryTableDialog dialog, SpecifiedSecondaryTable secondaryTable) {
		if (dialog.open() != Window.OK) {
			return;
		}

		secondaryTable.setSpecifiedName(dialog.getSelectedTable());
		secondaryTable.setSpecifiedCatalog(dialog.getSelectedCatalog());
		secondaryTable.setSpecifiedSchema(dialog.getSelectedSchema());
	}

}

Back to the top