Skip to main content
summaryrefslogtreecommitdiffstats
blob: 76c3a7de735b4b26bb944ef6de1803c39f33c239 (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*******************************************************************************
 * Copyright (c) 2004, 2010 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
 *     David Schneider, david.schneider@unisys.com - [142500] WTP properties pages fonts don't follow Eclipse preferences
 *******************************************************************************/
package org.eclipse.wst.html.ui.internal.contentproperties.ui;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.PropertyPage;
import org.eclipse.wst.css.core.internal.contentproperties.CSSContentProperties;
import org.eclipse.wst.css.core.internal.metamodel.CSSProfile;
import org.eclipse.wst.css.core.internal.metamodel.CSSProfileRegistry;
import org.eclipse.wst.html.core.internal.contentproperties.HTMLContentProperties;
import org.eclipse.wst.html.core.internal.document.HTMLDocumentTypeEntry;
import org.eclipse.wst.html.core.internal.document.HTMLDocumentTypeRegistry;
import org.eclipse.wst.html.ui.internal.HTMLUIMessages;
import org.eclipse.wst.html.ui.internal.Logger;
import org.eclipse.wst.html.ui.internal.editor.IHelpContextIds;

public class WebContentSettingsPropertyPage extends PropertyPage {
	private static final String SELECT_NONE = HTMLUIMessages.UI_none;
	private String maxLengthStringInHTMLDocumentTypeRegistry = ""; //$NON-NLS-1$

	private class ComboSelectionListener implements SelectionListener {
		public void widgetDefaultSelected(SelectionEvent e) {
			// do nothing
		}

		public void widgetSelected(SelectionEvent e) {
			int index = fDocumentTypeCombo.getSelectionIndex();
			String doctype = (String) fDocumentTypeIds.get(index);
			updateDoctypeText(index, doctype);
		}
	}

	Combo fDocumentTypeCombo;
	List fDocumentTypeIds;
	private Text fPublicIdText;
	private Text fSystemIdText;
	private Combo fProfileCombo;
	private List fProfileIds;
	private SelectionListener fListener;

	public WebContentSettingsPropertyPage() {
		super();
		setDescription(HTMLUIMessages.WebContentSettingsPropertyPage_0);
	}

	private Composite createComposite(Composite parent, int numColumns) {
		Composite composite = new Composite(parent, SWT.NULL);

		// GridLayout
		GridLayout layout = new GridLayout();
		layout.numColumns = numColumns;
		composite.setLayout(layout);

		// GridData
		GridData data = new GridData(GridData.FILL, GridData.FILL, true, false);
		data.horizontalIndent = 0;
		composite.setLayoutData(data);

		return composite;
	}

	protected Control createContents(Composite parent) {
		Composite propertyPage = createComposite(parent, 2);

		createDoctypeContents(propertyPage);
		createCSSProfileContents(propertyPage);

		populateValues();
		initializeValues();
		computeMaxWidthHint();

		fListener = new ComboSelectionListener();
		fDocumentTypeCombo.addSelectionListener(fListener);

		PlatformUI.getWorkbench().getHelpSystem().setHelp(propertyPage, IHelpContextIds.WEB_CONTENT_SETTINGS_HELPID);
		Dialog.applyDialogFont(parent);
		return propertyPage;
	}

	private void createCSSProfileContents(Composite parent) {
		// CSS Profile
		Label languageLabel = new Label(parent, SWT.NONE);
		languageLabel.setText(HTMLUIMessages.UI_CSS_profile___2);
		fProfileCombo = new Combo(parent, SWT.READ_ONLY);
		GridData data = new GridData(GridData.FILL, GridData.FILL, true, false);
		data.horizontalIndent = 0;
		fProfileCombo.setLayoutData(data);
	}

	private void createDoctypeContents(Composite parent) {
		// create description of implicit DOCTYPE
		Text doctypeLabel = new Text(parent, SWT.READ_ONLY);
		doctypeLabel.setText(HTMLUIMessages.UI_Description_of_role_of_following_DOCTYPE);
		GridData data = new GridData(GridData.FILL, GridData.FILL, true, false);
		data.horizontalIndent = 0;
		data.horizontalSpan = 2;
		doctypeLabel.setLayoutData(data);

		// document type
		Label languageLabel = new Label(parent, SWT.NONE);
		languageLabel.setText(HTMLUIMessages.UI_Default_HTML_DOCTYPE_ID___1);
		fDocumentTypeCombo = new Combo(parent, SWT.READ_ONLY);
		data = new GridData(GridData.FILL, GridData.FILL, true, false);
		data.horizontalIndent = 0;
		fDocumentTypeCombo.setLayoutData(data);

		// public ID
		Label publicIdLabel = new Label(parent, SWT.NONE);
		publicIdLabel.setText(HTMLUIMessages.UI_Public_ID);
		fPublicIdText = new Text(parent, SWT.READ_ONLY | SWT.BORDER);
		data = new GridData(GridData.FILL, GridData.FILL, true, false);
		data.horizontalIndent = 0;
		fPublicIdText.setLayoutData(data);

		// system ID
		Label systemIdLabel = new Label(parent, SWT.NONE);
		systemIdLabel.setText(HTMLUIMessages.UI_System_ID);
		fSystemIdText = new Text(parent, SWT.READ_ONLY | SWT.BORDER);
		data = new GridData(GridData.FILL, GridData.FILL, true, false);
		data.horizontalIndent = 0;
		fSystemIdText.setLayoutData(data);

		// create separator
		Label label = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
		data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		data.horizontalSpan = 2;
		data.verticalSpan = 8;
		label.setLayoutData(data);

	}

	/**
	 * Get the resource this properties page is for
	 * 
	 * @return IResource for this properties page or null if there is no
	 *         IResource
	 */
	private IResource getResource() {
		IResource resource = null;
		IAdaptable adaptable = getElement();
		if (adaptable instanceof IResource) {
			resource = (IResource) adaptable;
		} else if (adaptable != null) {
			Object o = adaptable.getAdapter(IResource.class);
			if (o instanceof IResource) {
				resource = (IResource)o;
			}
		}
		return resource;
	}

	private String getSystemIdFrom(String publicId) {
		if (publicId == null || publicId.length() == 0)
			return null;
		HTMLDocumentTypeRegistry reg = HTMLDocumentTypeRegistry.getInstance();
		Enumeration e = reg.getEntries();
		while (e.hasMoreElements()) {
			HTMLDocumentTypeEntry entry = (HTMLDocumentTypeEntry) e.nextElement();
			if (entry.getPublicId().equals(publicId))
				return entry.getSystemId();
		}
		return null;
	}

	private void initializeValues() {
		initializeDoctypeValues();
		initializeCSSProfileValues();
	}

	private void initializeCSSProfileValues() {
		int index = 0;
		String profile = CSSContentProperties.getProperty(CSSContentProperties.CSS_PROFILE, getResource(), false);
		if (profile != null && profile.length() > 0) {
			/*
			 * If item is already part of combo, select it. Otherwise, select
			 * none.
			 */
			index = fProfileIds.indexOf(profile);
		}
		index = index >= 0 ? index : 0;
		fProfileCombo.select(index);
	}

	private void initializeDoctypeValues() {
		int index = 0;
		String doctype = HTMLContentProperties.getProperty(HTMLContentProperties.DOCUMENT_TYPE, getResource(), false);
		if (doctype != null) {
			/*
			 * If item is already part of combo, select it. Otherwise, select
			 * none.
			 */
			index = fDocumentTypeIds.indexOf(doctype);
		}

		// set combobox
		index = index >= 0 ? index : 0;
		fDocumentTypeCombo.select(index);

		updateDoctypeText(index, doctype);
	}

	void updateDoctypeText(int index, String doctype) {
		if (index > 0) {
			// set public/system id text
			fPublicIdText.setText(doctype);
			String systemId = getSystemIdFrom(doctype);
			if (systemId != null)
				fSystemIdText.setText(systemId);
			else
				fSystemIdText.setText(""); //$NON-NLS-1$
		}
		else {
			// set public/system id text
			fPublicIdText.setText(""); //$NON-NLS-1$
			fSystemIdText.setText(""); //$NON-NLS-1$
		}
	}

	private void populateValues() {
		populateDoctypeValues();
		populateCSSProfileValues();
	}

	private void populateCSSProfileValues() {
		fProfileIds = new ArrayList();
		// add none first
		fProfileCombo.add(SELECT_NONE);
		fProfileIds.add(null);

		CSSProfileRegistry reg = CSSProfileRegistry.getInstance();
		Iterator i = reg.getProfiles();
		while (i.hasNext()) {
			CSSProfile profile = (CSSProfile) i.next();
			String id = profile.getProfileID();
			String name = profile.getProfileName();
			fProfileCombo.add(name);
			fProfileIds.add(id);
		}
	}

	private void populateDoctypeValues() {
		fDocumentTypeIds = new ArrayList();
		// add none first
		fDocumentTypeCombo.add(SELECT_NONE);
		fDocumentTypeIds.add(null);

		HTMLDocumentTypeRegistry reg = HTMLDocumentTypeRegistry.getInstance();
		Enumeration e = reg.getEntries();
		while (e.hasMoreElements()) {
			HTMLDocumentTypeEntry entry = (HTMLDocumentTypeEntry) e.nextElement();
			String publicId = entry.getPublicId();
			String displayName = entry.getDisplayName();
			displayName = displayName != null ? displayName : publicId;

			fDocumentTypeCombo.add(displayName);
			fDocumentTypeIds.add(publicId);

			if (displayName.length() > maxLengthStringInHTMLDocumentTypeRegistry.length()) {
				maxLengthStringInHTMLDocumentTypeRegistry = displayName;
			}

			if (entry.getSystemId() == null)
				continue; // if HTML entry


			if (entry.getSystemId().length() > maxLengthStringInHTMLDocumentTypeRegistry.length())
				maxLengthStringInHTMLDocumentTypeRegistry = entry.getSystemId();
		}
	}

	private void computeMaxWidthHint() {
		// maxLengthString was set populateDoctypeValues was called
		String maxLengthString = maxLengthStringInHTMLDocumentTypeRegistry;
		String backup = fSystemIdText.getText();
		fSystemIdText.setText(maxLengthString);
		int maxWidthHint = fSystemIdText.computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
		fSystemIdText.setText(backup);

		if (fDocumentTypeCombo.getLayoutData() != null)
			((GridData) fDocumentTypeCombo.getLayoutData()).widthHint = maxWidthHint;
		if (fPublicIdText.getLayoutData() != null)
			((GridData) fPublicIdText.getLayoutData()).widthHint = maxWidthHint;
		if (fSystemIdText.getLayoutData() != null)
			((GridData) fSystemIdText.getLayoutData()).widthHint = maxWidthHint;
		if (fProfileCombo.getLayoutData() != null)
			((GridData) fProfileCombo.getLayoutData()).widthHint = maxWidthHint;
	}

	private void performCSSProfileDefaults() {
		int index = fProfileCombo.indexOf(SELECT_NONE);
		if (index > -1)
			fProfileCombo.select(index);

		super.performDefaults();
	}

	private boolean performCSSProfileOk() {
		int index = fProfileCombo.getSelectionIndex();
		if (index > -1) {
			String id = (String) fProfileIds.get(index);
			if (id == null || id.length() == 0 || id.equalsIgnoreCase(SELECT_NONE)) {
				// if none, use null
				id = null;
			}
			try {
				CSSContentProperties.setProperty(CSSContentProperties.CSS_PROFILE, getResource(), id);
			}
			catch (CoreException e) {
				// maybe in future, let user know there was a problem saving
				// file
				Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
			}
		}
		return true;
	}

	protected void performDefaults() {
		super.performDefaults();

		performDoctypeDefaults();
		performCSSProfileDefaults();
	}

	private void performDoctypeDefaults() {
		fPublicIdText.setText("");//$NON-NLS-1$
		fSystemIdText.setText(""); //$NON-NLS-1$
	}

	private boolean performDoctypeOk() {
		int index = fDocumentTypeCombo.getSelectionIndex();
		if (index > -1) {
			String id = (String) fDocumentTypeIds.get(index);
			if (id == null || id.equalsIgnoreCase(SELECT_NONE)) {
				// if none, use null
				id = null;
			}
			try {
				HTMLContentProperties.setProperty(HTMLContentProperties.DOCUMENT_TYPE, getResource(), id);
			}
			catch (CoreException e) {
				// maybe in future, let user know there was a problem saving
				// file
				Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
			}
		}
		return true;
	}

	public boolean performOk() {
		performDoctypeOk();
		performCSSProfileOk();

		return super.performOk();
	}
}

Back to the top