Skip to main content
summaryrefslogtreecommitdiffstats
blob: 85cf93c07f14d30f9f1b4d07028379cbda408b56 (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
/*******************************************************************************
 * Copyright (c) 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 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.text.Document;
import org.eclipse.jface.text.ITextListener;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.TextEvent;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.jpt.core.internal.mappings.IGenerator;
import org.eclipse.jpt.core.internal.mappings.IId;
import org.eclipse.jpt.core.internal.mappings.JpaCoreMappingsPackage;
import org.eclipse.jpt.ui.internal.details.BaseJpaComposite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;

/**
 * GeneratorComposite
 */
public abstract class GeneratorComposite<E extends IGenerator> extends BaseJpaComposite
{
	private IId id;
	private E generator;
	private Adapter generatorListener;

	protected ITextViewer nameViewer;

	public GeneratorComposite(Composite parent, CommandStack commandStack, TabbedPropertySheetWidgetFactory widgetFactory) {
		super(parent, SWT.NULL, commandStack, widgetFactory);
		this.generatorListener = buildGeneratorListner();
	}
	
	private Adapter buildGeneratorListner() {
		return new AdapterImpl() {
			public void notifyChanged(Notification notification) {
				generatorChanged(notification);
			}
		};
	}

	/**
	 * Builds the Generator specifiedName viewer.
	 * 
	 * @param parent
	 * @return
	 */
	protected ITextViewer buildNameViewer(Composite parent) {
		final TextViewer textViewer = new TextViewer(parent, SWT.SINGLE | SWT.BORDER);
		textViewer.setDocument(new Document());
		textViewer.addTextListener(new ITextListener() {
			public void textChanged(TextEvent event) {
				if (isPopulating()) {
					return;
				}
				
				String name = textViewer.getDocument().get();
				if (name.equals("")) { //$NON-NLS-1$
					if (getGenerator().getName() == null) {
						return;
					}
					name = null;
				}
				IGenerator generator = getGenerator();
				if (generator == null) {
					generator = createGenerator();
				}
				generator.setName(name);
			}
		});
		return textViewer;
	}

	protected abstract E createGenerator();

	@Override
	protected void doPopulate(EObject obj) {
		this.id = (IId) obj;
		if (this.id  == null) {
			this.generator = null;
			return;
		}
		this.generator = generator(this.id);
		if (this.generator == null) {
			clear();
			return;
		}
		populateNameViewer();
		return;
	}

	@Override
	protected void doPopulate() {
		populateNameViewer();
	}
	
	protected void engageListeners() {
		if (this.generator != null) {
			this.generator.eAdapters().add(this.generatorListener);
		}
	}

	protected void disengageListeners() {
		if (this.generator != null) {
			this.generator.eAdapters().remove(this.generatorListener);
		}
	}

	protected abstract E generator(IId id);
	
	protected IId idMapping() {
		return this.id;
	}

	protected void generatorChanged(Notification notification) {
		if (notification.getFeatureID(IGenerator.class) == JpaCoreMappingsPackage.IGENERATOR__NAME) {
			final String name = notification.getNewStringValue();
			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					if (getControl().isDisposed()) {
						return;
					}
					if (nameViewer.getDocument().get() == null || !nameViewer.getDocument().get().equals(name)) {
						if (name == null) {
							clearNameViewer();
						}
						else {
							nameViewer.getDocument().set(name);
						}
					}
				}
			});
		}
	}

	private void populateNameViewer() {
		String name = this.getGenerator().getName();
		if (name != null) {
			if (!this.nameViewer.getDocument().get().equals(name)) {
				this.nameViewer.getDocument().set(name);
			}
		}
		else {
			this.clearNameViewer();
		}
	}

	protected E getGenerator() {
		return this.generator;
	}

	protected void clear() {
		this.clearNameViewer();
	}

	protected void clearNameViewer() {
		this.nameViewer.getDocument().set(""); //$NON-NLS-1$
	}
	
	public void dispose() {
		disengageListeners();
		super.dispose();
	}
}

Back to the top