Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c1bcc67b5d29d6e2bd5ddd94ca09e349c8f22c2a (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
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.compare.examples.xml;

import java.util.HashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

import org.eclipse.compare.examples.xml.ui.StatusDialog;
import org.eclipse.compare.examples.xml.ui.StatusInfo;

/**
 * This class is used to add or edit a particular ID Mapping
 */
public class XMLCompareEditMappingDialog extends StatusDialog {
	
	private Mapping fMapping;
	private HashMap fIdmapHM;
	private boolean fEdit;
	
	private Text fElementText;
	private Text fSignatureText;
	private Text fIdAttributeText;

	private Button fIdTypeAttributeButton;
	private Button fIdTypeChildBodyButton;
	
	/*
	 * Constructs a new edit mapping dialog.
	 */		
	public XMLCompareEditMappingDialog(Shell parent, Mapping mapping, HashMap idmapHM, boolean edit) {
		super(parent);
	
		int shellStyle= getShellStyle();
		setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE);

	
		fEdit= edit;
		if (fEdit)
			setTitle(XMLCompareMessages.XMLCompareEditMappingDialog_editTitle); 
		else
			setTitle(XMLCompareMessages.XMLCompareEditMappingDialog_newTitle); 

		fMapping= mapping;
		fIdmapHM= idmapHM;
	}
	
	/**
	 * Creates and returns the contents of the upper part 
	 * of the dialog (above the button bar).
	 *
	 * Subclasses should override.
	 *
	 * @param ancestor the parent composite to contain the dialog area
	 * @return the dialog area control
	 */	
	@Override
	protected Control createDialogArea(Composite ancestor) {
		Composite composite= (Composite) super.createDialogArea(ancestor);
		
		Composite inner= new Composite(composite, SWT.NONE);
		GridLayout layout= new GridLayout();
		layout.numColumns= 2;
		inner.setLayout(layout);
		inner.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		//Element
		Label label= new Label(inner, SWT.NULL);
		label.setText(XMLCompareMessages.XMLCompareEditMappingDialog_element); 
		label.setLayoutData(new GridData());

		fElementText= new Text(inner, SWT.BORDER);
		fElementText.setText(fMapping.getElement());
		fElementText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		fElementText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e){
				doValidation();
			}
		});

		//Signature
		label= new Label(inner, SWT.NULL);
		label.setText(XMLCompareMessages.XMLCompareEditMappingDialog_signature); 
		label.setLayoutData(new GridData());

		fSignatureText= new Text(inner, SWT.BORDER);
		fSignatureText.setText(fMapping.getSignature());
		GridData data= new GridData(GridData.FILL_HORIZONTAL);
		data.widthHint= convertWidthInCharsToPixels(50);
		fSignatureText.setLayoutData(data);
		fSignatureText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e){
				doValidation();
			}
		});
		
		//Id Attribute
		label= new Label(inner, SWT.NULL);
		label.setText(XMLCompareMessages.XMLCompareEditMappingDialog_idattribute); 
		label.setLayoutData(new GridData());

		fIdAttributeText= new Text(inner, SWT.BORDER);

		fIdAttributeText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		fIdAttributeText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e){
				doValidation();
			}
		});

		//Id Source
		createIdSourceGroup(inner);

		fElementText.setFocus();

		return composite;
	}
	
	/**
	 * Validate user input
	 */	
	private void doValidation() {
		StatusInfo status= new StatusInfo();
		String text= fElementText.getText();
		String mappingKey= Mapping.getKey(fSignatureText.getText(), text);
		String errormsg= ""; //$NON-NLS-1$
		boolean isError= false;
		if (text.length() == 0) {
			errormsg= XMLCompareMessages.XMLCompareEditMappingDialog_error_noname; 
			isError= true;
		} else if (XMLComparePreferencePage.containsInvalidCharacters(text)) {
			if (errormsg == "") errormsg= XMLCompareMessages.XMLCompareEditMappingDialog_error_invalidname;  //$NON-NLS-1$
			isError= true;
		} else if (!fEdit && fIdmapHM != null && fIdmapHM.containsKey(mappingKey)) {
			if (errormsg == "") errormsg= XMLCompareMessages.XMLCompareEditMappingDialog_error_mappingExists;  //$NON-NLS-1$
			isError= true;
		}
		text= fSignatureText.getText();
		if (XMLComparePreferencePage.containsInvalidCharacters(text)) {
			if (errormsg == "") errormsg= XMLCompareMessages.XMLCompareEditMappingDialog_error_invalidsignature;  //$NON-NLS-1$
			isError= true;
		}
		text= fIdAttributeText.getText();
		if (text.length() == 0)
			isError= true;
		else if (XMLComparePreferencePage.containsInvalidCharacters(text)) {
			if (errormsg == "") errormsg= XMLCompareMessages.XMLCompareEditMappingDialog_error_invalididattribute;  //$NON-NLS-1$
			isError= true;
		}
		if (isError) status.setError(errormsg);
		updateStatus(status);
	}
	
	/**
	 * Notifies that the ok button of this dialog has been pressed.
	 */	
	@Override
	protected void okPressed() {
		fMapping.setElement(fElementText.getText());
		fMapping.setSignature(fSignatureText.getText());
		String idtext= fIdAttributeText.getText();
		if (fIdTypeChildBodyButton.getSelection()) {
			idtext= new Character(XMLStructureCreator.ID_TYPE_BODY) + idtext;
		}
		fMapping.setIdAttribute(idtext);
		super.okPressed();
	}
	
	private void createIdSourceGroup(Composite composite) {
		Label titleLabel= new Label(composite, SWT.NONE);
		titleLabel.setText(XMLCompareMessages.XMLCompareEditMappingDialog_idtype); 
		titleLabel.setToolTipText(XMLCompareMessages.XMLCompareEditMappingDialog_idtype_tooltip); 
	
		Composite buttonComposite= new Composite(composite, SWT.LEFT);
		GridLayout layout= new GridLayout();
		layout.numColumns= 2;
		buttonComposite.setLayout(layout);
		composite.setData(new GridData());
	
		//attribute button
		fIdTypeAttributeButton= createRadioButton(buttonComposite, XMLCompareMessages.XMLComparePreference_idtype_attribute); 
		fIdTypeAttributeButton.setToolTipText(XMLCompareMessages.XMLCompareEditMappingDialog_idtype_attribute_tooltip); 
	
		//child body button
		fIdTypeChildBodyButton= createRadioButton(buttonComposite, XMLCompareMessages.XMLComparePreference_idtype_child_body); 
		fIdTypeChildBodyButton.setToolTipText(XMLCompareMessages.XMLCompareEditMappingDialog_idtype_childbody_tooltip); 
	
		String idtext= fMapping.getIdAttribute();
		if (fEdit && idtext.charAt(0) == XMLStructureCreator.ID_TYPE_BODY) {
			idtext= idtext.substring(1,idtext.length());
			fIdTypeChildBodyButton.setSelection(true);
		} else
			fIdTypeAttributeButton.setSelection(true);
		fIdAttributeText.setText(idtext);
	
	}

	private Button createRadioButton(Composite parent, String label) {
		Button button= new Button(parent, SWT.RADIO | SWT.LEFT);
		button.setText(label);
		GridData data= new GridData();
		button.setLayoutData(data);
		return button;
	}	
}

Back to the top