Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

aboutsummaryrefslogtreecommitdiffstats
blob: d5019737cd2b976b2ef654ef30c1cffada4f8d90 (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
/*******************************************************************************
 * Copyright (c) 2001, 2004 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.xml.ui.internal.preferences;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.wst.sse.core.internal.encoding.CommonCharsetNames;
import org.eclipse.wst.xml.ui.internal.XMLUIMessages;

/**
 * WorkbenchDefaultEncodingSettings is an extension of EncodingSettings. This
 * composite contains EncodingSettings for users to select the encoding they
 * desire as well as a checkbox for users to select to use the default
 * workbench encoding instead.
 * 
 * @see org.eclipse.wst.xml.ui.internal.preferences.EncodingSettings
 */
public class WorkbenchDefaultEncodingSettings extends Composite {

	private final static int INDENT = 15;
	private static final String WORKBENCH_DEFAULT = ""; //$NON-NLS-1$
	private EncodingSettings fEncodingSettings;
	private String fNonDefaultIANA = null;
	private Button fUseDefaultButton;

	public WorkbenchDefaultEncodingSettings(Composite parent) {
		super(parent, SWT.NONE);
		createControls();
	}

	private void createControls() {
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		layout.marginWidth = 0;
		setLayout(layout);
		GridData data = new GridData();
		data.verticalAlignment = GridData.FILL;
		data.horizontalAlignment = GridData.FILL;
		data.grabExcessHorizontalSpace = true;
		setLayoutData(data);

		Composite defaultEncodingComposite = new Composite(this, SWT.NONE);
		layout = new GridLayout();
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		data = new GridData(GridData.FILL_BOTH);
		defaultEncodingComposite.setLayout(layout);
		defaultEncodingComposite.setLayoutData(data);

		fUseDefaultButton = new Button(defaultEncodingComposite, SWT.CHECK);
		fUseDefaultButton.setText(XMLUIMessages.WorkbenchDefaultEncodingSettings_0); //$NON-NLS-1$

		fUseDefaultButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				handleUseDefaultButtonSelected();
			}
		});

		fEncodingSettings = new EncodingSettings(this);
		((GridLayout) fEncodingSettings.getLayout()).marginWidth = 0;
		((GridData) fEncodingSettings.getLayoutData()).horizontalIndent = INDENT;

	}

	private Combo getEncodingCombo() {
		return fEncodingSettings.getEncodingCombo();
	}

	/**
	 * <code>getIANATag</code> Get the IANA tag equivalent of the selected
	 * descriptive encoding name. Returns empty string if using workbench
	 * encoding.
	 * 
	 * @return a <code>String</code> value
	 */
	public String getIANATag() {
		if (!isDefault())
			return fEncodingSettings.getIANATag();
		return WORKBENCH_DEFAULT;
	}

	private String getWorkbenchEncoding() {
		return ResourcesPlugin.getEncoding();
	}

	private void handleUseDefaultButtonSelected() {
		if (fUseDefaultButton.getSelection()) {
			fNonDefaultIANA = fEncodingSettings.getIANATag();
			String workbenchValue = getWorkbenchEncoding();
			workbenchValue = CommonCharsetNames.getIanaPreferredCharsetName(workbenchValue);
			fEncodingSettings.setIANATag(workbenchValue);
		} else if (fNonDefaultIANA != null) {
			fEncodingSettings.setIANATag(fNonDefaultIANA);
		}
		getEncodingCombo().setEnabled(!fUseDefaultButton.getSelection());
		fEncodingSettings.setEnabled(!fUseDefaultButton.getSelection());
	}

	private boolean isDefault() {
		return fUseDefaultButton.getSelection();
	}

	/**
	 * <code>setEncoding</code> Set the selection in the combo to the
	 * descriptive encoding name. Selects use workbench encoding if ianaTag is
	 * null or empty string.
	 * 
	 */
	public void setIANATag(String ianaTag) {
		if (ianaTag == null || ianaTag.equals(WORKBENCH_DEFAULT)) {
			fUseDefaultButton.setSelection(true);
			handleUseDefaultButtonSelected();
		} else {
			fUseDefaultButton.setSelection(false);
			handleUseDefaultButtonSelected();
			if (!isDefault())
				fEncodingSettings.setIANATag(ianaTag);
		}
	}
}

Back to the top