Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 36194e5b66951144d111580afd76f78f68d48d62 (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
/*******************************************************************************
 * 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.xml.details;

import java.util.Iterator;
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.jpt.core.internal.IJpaContentNode;
import org.eclipse.jpt.core.internal.IPersistentType;
import org.eclipse.jpt.core.internal.content.orm.OrmPackage;
import org.eclipse.jpt.core.internal.content.orm.XmlAttributeMapping;
import org.eclipse.jpt.core.internal.content.orm.XmlPersistentAttribute;
import org.eclipse.jpt.core.internal.content.orm.XmlPersistentType;
import org.eclipse.jpt.ui.internal.details.PersistentAttributeDetailsPage;
import org.eclipse.jpt.ui.internal.widgets.CComboViewer;
import org.eclipse.jpt.utility.internal.iterators.ArrayIterator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.part.PageBook;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;

public class XmlPersistentAttributeDetailsPage
	extends PersistentAttributeDetailsPage 
{
	private XmlJavaAttributeChooser javaAttributeChooser;
	
	private Adapter persistentTypeListener;
	
	private IPersistentType persistentType;
	
	public XmlPersistentAttributeDetailsPage(Composite parent, TabbedPropertySheetWidgetFactory widgetFactory) {
		super(parent, widgetFactory);
		buildPersistentTypeListener();
	}
	
	private void buildPersistentTypeListener() {
		this.persistentTypeListener = new AdapterImpl() {
			@Override
			public void notifyChanged(Notification notification) {
				persistentTypeChanged(notification);
			}
		};
	}
	
	void persistentTypeChanged(Notification notification) {
		if (notification.getFeatureID(XmlPersistentType.class) == 
			OrmPackage.XML_PERSISTENT_TYPE__SPECIFIED_ATTRIBUTE_MAPPINGS) {
			Display.getDefault().asyncExec(
				new Runnable() {
					public void run() {
						updateEnbabledState();
					}
				});
		}
	}
	
	@Override
	protected void initializeLayout(Composite composite) {
		composite.setLayout(new GridLayout(2, false));
		
		GridData gridData;
		
		CommonWidgets.buildJavaAttributeNameLabel(composite, getWidgetFactory());
		
		this.javaAttributeChooser = CommonWidgets.buildJavaAttributeChooser(composite, this.commandStack, getWidgetFactory());
		gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		gridData.verticalAlignment = SWT.BEGINNING;
		gridData.grabExcessHorizontalSpace = true;
		this.javaAttributeChooser.getControl().setLayoutData(gridData);

		
		buildMappingLabel(composite);
		
		CComboViewer mappingCombo = buildMappingCombo(composite);
		gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		gridData.verticalAlignment = SWT.BEGINNING;
		gridData.grabExcessHorizontalSpace = true;
		mappingCombo.getCombo().setLayoutData(gridData);
		
		PageBook book = buildMappingPageBook(composite);
		gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		gridData.verticalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalSpan = 2;
		book.setLayoutData(gridData);
	}
	
	@Override
	protected void engageListeners() {
		super.engageListeners();
		if (getAttribute() != null) {
			this.persistentType = getAttribute().typeMapping().getPersistentType();
			this.persistentType.eAdapters().add(this.persistentTypeListener);
		}
	}
	
	@Override
	protected void disengageListeners() {
		if (this.persistentType != null) {
			this.persistentType.eAdapters().remove(this.persistentTypeListener);
			this.persistentType = null;
		}
		super.disengageListeners();
	}
	
	@Override
	protected void doPopulate(IJpaContentNode persistentAttributeNode) {
		super.doPopulate(persistentAttributeNode);
		if (persistentAttributeNode == null) {
			this.javaAttributeChooser.populate(null);
		}
		else {
			XmlAttributeMapping mapping = ((XmlPersistentAttribute) persistentAttributeNode).getMapping();
			this.javaAttributeChooser.populate(mapping);
			updateEnbabledState();
		}
	}
	
	@Override
	protected void doPopulate() {
		super.doPopulate();
		this.javaAttributeChooser.populate();
		updateEnbabledState();
	}
	
	@Override
	public void dispose() {
		this.javaAttributeChooser.dispose();
		super.dispose();
	}
	
	public void updateEnbabledState() {
		if (getAttribute() == null || getAttribute().eContainer() == null) {
			return;
		}
		boolean enabled = !((XmlPersistentAttribute) getAttribute()).isVirtual();
		updateEnabledState(enabled, getControl());
	}
	
	public void updateEnabledState(boolean enabled, Control control) {
		control.setEnabled(enabled);
		if (control instanceof Composite) {
			for (Iterator<Control> i = new ArrayIterator<Control>(((Composite) control).getChildren()); i.hasNext(); ) {
				updateEnabledState(enabled, i.next());
			}
		}
	}
}

Back to the top