Skip to main content
summaryrefslogtreecommitdiffstats
blob: 74b5499853990bb5d10e6fef4046fe50a250273a (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
/*******************************************************************************
 * Copyright (c) 2008, 2016 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.CellEditor;
import org.eclipse.jface.viewers.IBaseLabelProvider;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jpt.common.ui.internal.swt.ColumnAdapter;
import org.eclipse.jpt.common.ui.internal.widgets.AddRemovePane.Adapter;
import org.eclipse.jpt.common.ui.internal.widgets.AddRemoveTablePane;
import org.eclipse.jpt.common.ui.internal.widgets.Pane;
import org.eclipse.jpt.common.utility.internal.iterable.SuperListIterableWrapper;
import org.eclipse.jpt.common.utility.internal.model.value.ListAspectAdapter;
import org.eclipse.jpt.common.utility.internal.model.value.PropertyValueModelTools;
import org.eclipse.jpt.common.utility.internal.model.value.SimpleCollectionValueModel;
import org.eclipse.jpt.common.utility.iterable.ListIterable;
import org.eclipse.jpt.common.utility.model.value.CollectionValueModel;
import org.eclipse.jpt.common.utility.model.value.ListValueModel;
import org.eclipse.jpt.common.utility.model.value.ModifiableCollectionValueModel;
import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel;
import org.eclipse.jpt.common.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.jpa.core.context.NamedQuery;
import org.eclipse.jpt.jpa.core.context.Query;
import org.eclipse.jpt.jpa.core.context.QueryHint;
import org.eclipse.jpt.jpa.ui.details.JptJpaUiDetailsMessages;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

/**
 * Here the layout of this pane:
 * <pre>
 * -----------------------------------------------------------------------------
 * |                                                                           |
 * | AddRemoveTablePane                                                        |
 * |                                                                           |
 * -----------------------------------------------------------------------------</pre>
 *
 * @see Query
 * @see QueryHint
 * @see AddRemoveTablePane
 *
 * @version 2.0
 * @since 2.0
 */
@SuppressWarnings("nls")
public class QueryHintsComposite extends Pane<Query>
{
	private ModifiableCollectionValueModel<QueryHint> selectedQueryHintsModel;

	/**
	 * Creates a new <code>QueryHintsComposite</code>.
	 *
	 * @param parentPane The parent pane of this one
	 * @param parent The parent container
	 */
	public QueryHintsComposite(Pane<? extends Query> parentPane,
	                           Composite container) {

		super(parentPane, container);
	}

	private Adapter<QueryHint> buildQueryHintAdapter() {
		return new AddRemoveTablePane.AbstractAdapter<QueryHint>() {

			public QueryHint addNewItem() {
				return getSubject().addHint(getSubject().getHintsSize());
			}

			@Override
			public PropertyValueModel<Boolean> buildRemoveButtonEnabledModel(CollectionValueModel<QueryHint> selectedItemsModel) {
				//enable the remove button only when 1 item is selected, same as the optional button
				return this.buildSingleSelectedItemEnabledModel(selectedItemsModel);
			}

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

	private ModifiableCollectionValueModel<QueryHint> buildSelectedQueryHintsModel() {
		return new SimpleCollectionValueModel<QueryHint>();
	}

	private ITableLabelProvider buildQueryHintLabelProvider() {
		return new TableLabelProvider();
	}

	private ListValueModel<QueryHint> buildQueryHintListHolder() {
		return new ListAspectAdapter<Query, QueryHint>(getSubjectHolder(), NamedQuery.HINTS_LIST) {
			@Override
			protected ListIterable<QueryHint> getListIterable() {
				return new SuperListIterableWrapper<QueryHint>(this.subject.getHints());
			}

			@Override
			protected int size_() {
				return subject.getHintsSize();
			}
		};
	}

	@Override
	protected void initialize() {
		super.initialize();
		this.selectedQueryHintsModel = buildSelectedQueryHintsModel();
	}

	@Override
	protected Composite addComposite(Composite parent) {
		return this.addTitledGroup(
			parent,
			JptJpaUiDetailsMessages.NAMED_QUERY_PROPERTY_COMPOSITE_QUERY_HINTS_GROUP_BOX
		);
	}

	@Override
	protected void initializeLayout(Composite container) {
		new TablePane(container);
	}

	private static class QueryHintColumnAdapter implements ColumnAdapter<QueryHint> {

		static final int COLUMN_COUNT = 2;
		static final int NAME_COLUMN_INDEX = 0;
		static final int VALUE_COLUMN_INDEX = 1;

		private ModifiablePropertyValueModel<String> buildNameHolder(QueryHint subject) {
			return PropertyValueModelTools.modifiableSubjectAspectAdapter(
					subject,
					QueryHint.NAME_PROPERTY,
					m -> m.getName(),
					(m, value) -> m.setName(value)
				);
		}

		private ModifiablePropertyValueModel<?> buildValueHolder(QueryHint subject) {
			return PropertyValueModelTools.modifiableSubjectAspectAdapter(
					subject,
					QueryHint.VALUE_PROPERTY,
					m -> m.getValue(),
					(m, value) -> m.setValue(value)
				);
		}

		public ModifiablePropertyValueModel<?>[] cellModels(QueryHint subject) {
			ModifiablePropertyValueModel<?>[] models = new ModifiablePropertyValueModel<?>[COLUMN_COUNT];
			models[NAME_COLUMN_INDEX]  = buildNameHolder(subject);
			models[VALUE_COLUMN_INDEX] = buildValueHolder(subject);
			return models;
		}

		public int columnCount() {
			return COLUMN_COUNT;
		}

		public String columnName(int columnIndex) {

			switch (columnIndex) {
				case QueryHintColumnAdapter.NAME_COLUMN_INDEX: {
					return JptJpaUiDetailsMessages.QUERY_HINTS_COMPOSITE_NAME_COLUMN;
				}

				case QueryHintColumnAdapter.VALUE_COLUMN_INDEX: {
					return JptJpaUiDetailsMessages.QUERY_HINTS_COMPOSITE_VALUE_COLUMN;
				}

				default: {
					return null;
				}
			}
		}
	}

	private class TableLabelProvider extends LabelProvider
	                                 implements ITableLabelProvider {

		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}

		public String getColumnText(Object element, int columnIndex) {

			QueryHint queryHint = (QueryHint) element;
			String value = "";

			switch (columnIndex) {
				case QueryHintColumnAdapter.NAME_COLUMN_INDEX: {
					value = queryHint.getName();
					break;
				}

				case QueryHintColumnAdapter.VALUE_COLUMN_INDEX: {
					value = queryHint.getValue();
					break;
				}
			}

			if (value == null) {
				value = "";
			}

			return value;
		}
	}

	private class TablePane extends AddRemoveTablePane<Query, QueryHint> {

		private TablePane(Composite parent) {
			super(QueryHintsComposite.this,
			      parent,
			      buildQueryHintAdapter(),
			      buildQueryHintListHolder(),
			      QueryHintsComposite.this.selectedQueryHintsModel,
			      buildQueryHintLabelProvider());
		}

		private CellEditor[] buildCellEditors(Table table) {
			return new CellEditor[] {
				new TextCellEditor(table),
				new TextCellEditor(table)
			};
		}

		private ICellModifier buildCellModifier() {
			return new ICellModifier() {

				public boolean canModify(Object element, String property) {
					return true;
				}

				public Object getValue(Object element, String property) {
					QueryHint queryHint = (QueryHint) element;
					String value = "";

					if (property == QueryHint.NAME_PROPERTY) {
						value = queryHint.getName();
					}
					else if (property == QueryHint.VALUE_PROPERTY) {
						value = queryHint.getValue();
					}

					if (value == null) {
						value = "";
					}

					return value;
				}

				public void modify(Object element, String property, Object value) {
					QueryHint queryHint;

					if (element instanceof TableItem) {
						TableItem tableItem = (TableItem) element;
						queryHint = (QueryHint) tableItem.getData();
					}
					else {
						queryHint = (QueryHint) element;
					}

					if (property == QueryHint.NAME_PROPERTY) {
						 queryHint.setName(value.toString());
					}
					else if (property == QueryHint.VALUE_PROPERTY) {
						 queryHint.setValue(value.toString());
					}
				}
			};
		}

		@Override
		protected ColumnAdapter<QueryHint> buildColumnAdapter() {
			return new QueryHintColumnAdapter();
		}

		private String[] buildColumnProperties() {
			return new String[] {
				QueryHint.NAME_PROPERTY,
				QueryHint.VALUE_PROPERTY
			};
		}

		@Override
		protected void initializeMainComposite(Composite container,
		                                       Adapter<QueryHint> adapter,
		                                       ListValueModel<?> listHolder,
		                                       ModifiableCollectionValueModel<QueryHint> selectedItemsModel,
		                                       IBaseLabelProvider labelProvider,
		                                       String helpId) {

			super.initializeMainComposite(
				container,
				adapter,
				listHolder,
				selectedItemsModel,
				labelProvider,
				helpId
			);

			Table table = getMainControl();

			TableViewer tableViewer = new TableViewer(table);
			tableViewer.setCellEditors(buildCellEditors(table));
			tableViewer.setCellModifier(buildCellModifier());
			tableViewer.setColumnProperties(buildColumnProperties());
		}
	}
}

Back to the top