Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8fe0ffcf4c27cd64cdeb057eaa8d7ba235ad6281 (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
/*******************************************************************************
 * 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.Arrays;
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.jpt.core.internal.mappings.IEntity;
import org.eclipse.jpt.core.internal.mappings.JpaCoreMappingsPackage;
import org.eclipse.jpt.ui.internal.details.BaseJpaController;
import org.eclipse.jpt.ui.internal.mappings.JptUiMappingsMessages;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;

// TODO get Default updating appropriately based on Entity name default
	
public class EntityNameCombo extends BaseJpaController
{
	private IEntity entity;
	private Adapter entityListener;
	
	private CCombo combo;
	
	
	public EntityNameCombo(Composite parent, CommandStack commandStack, TabbedPropertySheetWidgetFactory widgetFactory) {
		super(parent, commandStack, widgetFactory);
		buildEntityListener();
	}
	
	
	private void buildEntityListener() {
		entityListener = new AdapterImpl() {
			public void notifyChanged(Notification notification) {
				entityChanged(notification);
			}
		};
	}
	
	@Override
	protected void buildWidget(Composite parent) {
		combo = this.widgetFactory.createCCombo(parent, SWT.FLAT);
		combo.addModifyListener(
			new ModifyListener() {
				public void modifyText(ModifyEvent e) {
					comboModified(e);
				}
			});
	}
	
	private void comboModified(ModifyEvent e) {
		if (isPopulating()) {
			return;
		}
		
		String text = ((CCombo) e.getSource()).getText();
		if (text.equals(combo.getItem(0))) {
			text = null;
		}
		
		entity.setSpecifiedName(text);
		
		// TODO Does this need to be done?
		//this.editingDomain.getCommandStack().execute(SetCommand.create(this.editingDomain, this.entity, MappingsPackage.eINSTANCE.getEntity_SpecifiedName(), text));
	}
	
	private void entityChanged(Notification notification) {
		switch (notification.getFeatureID(IEntity.class)) {
			case JpaCoreMappingsPackage.IENTITY__SPECIFIED_NAME :
				Display.getDefault().asyncExec(
					new Runnable() {
						public void run() {
							populate();
						}
					});
				break;
			case JpaCoreMappingsPackage.IENTITY__DEFAULT_NAME :
				Display.getDefault().asyncExec(
					new Runnable() {
						public void run() {
							populate();
						}
					});
				break;
		}
	}
	
	@Override
	protected void engageListeners() {
		if (entity != null) {
			entity.eAdapters().add(entityListener);
		}
	}
	
	@Override
	protected void disengageListeners() {
		if (entity != null) {
			entity.eAdapters().remove(entityListener);
		}
	}
	
	@Override
	public void doPopulate(EObject obj) {
		entity = (IEntity) obj;
		populateCombo();
	}
	
	@Override
	protected void doPopulate() {
		populateCombo();
	}
	
	private void populateCombo() {
		if (entity == null) {
			combo.clearSelection();
			combo.setItems(new String[] {});
			return;
		}
		
		String defaultItem = NLS.bind(JptUiMappingsMessages.EntityGeneralSection_nameDefaultWithOneParam, entity.getDefaultName());
		String specifiedName = entity.getSpecifiedName();
		
		if (specifiedName == null) {
			setComboData(defaultItem, new String[] {defaultItem});
		}
		else {
			setComboData(specifiedName, new String[] {defaultItem});
		}
	}
	
	private void setComboData(String text, String[] items) {
		if (! Arrays.equals(items, combo.getItems())) {
			combo.setItems(items);
		}
		
		if (! text.equals(combo.getText())) {
			combo.setText(text);
		}
	}
	
	public CCombo getCombo() {
		return combo;
	}
	
	@Override
	public Control getControl() {
		return getCombo();
	}
}

Back to the top