Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9b9175d26e6328bff9001f7f5662eae862812617 (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
/*******************************************************************************
 * Copyright (c) 2004, 2008 John Krasnay 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:
 *     John Krasnay - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.xml.vex.ui.internal.editor;

import java.text.MessageFormat;
import java.util.Arrays;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wst.xml.vex.ui.internal.VexPlugin;
import org.eclipse.wst.xml.vex.ui.internal.config.DocumentType;

/**
 * Dialog presented to the user to select a document type it cannot be
 * determined from the document being opened.
 */
public class DocumentTypeSelectionDialog extends MessageDialog {

	/**
	 * Class constructor.
	 * 
	 * @param parentShell
	 *            Parent Shell with respect to which this dialog is modal.
	 */
	protected DocumentTypeSelectionDialog(Shell parentShell, String title,
			String message) {
		super(
				parentShell,
				title,
				null,
				message,
				MessageDialog.QUESTION,
				new String[] {
						Messages.getString("DocumentTypeSelectionDialog.ok"), Messages.getString("DocumentTypeSelectionDialog.cancel") }, 0); //$NON-NLS-1$ //$NON-NLS-2$
		this.setShellStyle(SWT.RESIZE);
	}

	/**
	 * Creates a new instance of the dialog. The caller must call open() on the
	 * returned dialog to prompt the user. The open() method blocks until the
	 * user has closed the window. Once open() returns, the caller should call
	 * getDoctype() to get the selected doctype (or null if the dialog was
	 * canceled.
	 * 
	 * @param parentShell
	 *            Parent Shell of the dialog.
	 * @param publicId
	 *            Public ID of the document being opened, or null if the
	 *            document does not have a PUBLIC DOCTYPE declaration.
	 */
	public static DocumentTypeSelectionDialog create(Shell parentShell,
			String publicId) {
		String message;

		if (publicId == null) {
			message = Messages
					.getString("DocumentTypeSelectionDialog.noDoctype"); //$NON-NLS-1$
		} else {
			message = Messages
					.getString("DocumentTypeSelectionDialog.unknownDoctype"); //$NON-NLS-1$
		}

		return new DocumentTypeSelectionDialog(parentShell, Messages
				.getString("DocumentTypeSelectionDialog.selectDoctype"), //$NON-NLS-1$
				MessageFormat.format(message, new Object[] { publicId }));
	}

	protected Control createCustomArea(Composite parent) {

		this.typeList = new ListViewer(parent, SWT.SINGLE | SWT.H_SCROLL
				| SWT.V_SCROLL);

		List list = typeList.getList();

		list.addMouseListener(this.mouseListener);

		GridData gd = new GridData();
		gd.grabExcessHorizontalSpace = true;
		gd.grabExcessVerticalSpace = true;
		gd.horizontalAlignment = GridData.FILL;
		gd.verticalAlignment = GridData.FILL;
		gd.heightHint = 120;
		list.setLayoutData(gd);

		this.alwaysUseButton = new Button(parent, SWT.CHECK);
		this.alwaysUseButton.setText(Messages
				.getString("DocumentTypeSelectionDialog.alwaysUse")); //$NON-NLS-1$

		DocumentType[] doctypes = VexPlugin.getInstance().getConfigurationRegistry().getDocumentTypesWithStyles();
		Arrays.sort(doctypes);
		this.typeList.add(doctypes);

		return list;
	}

	protected void buttonPressed(int buttonId) {
		if (buttonId == 0) {
			IStructuredSelection selection = (IStructuredSelection) this.typeList
					.getSelection();
			this.doctype = (DocumentType) selection.getFirstElement();
			this.alwaysUseThisDoctype = this.alwaysUseButton.getSelection();
		}
		super.buttonPressed(buttonId);
	}

	/**
	 * Returns the document type selected by the user, or null if none was
	 * selected.
	 */
	public DocumentType getDoctype() {
		return this.doctype;
	}

	/**
	 * Returns true if Vex should always use this document type for the selected
	 * file.
	 */
	public boolean alwaysUseThisDoctype() {
		return this.alwaysUseThisDoctype;
	}

	// ======================================================= PRIVATE

	private DocumentType doctype;
	private boolean alwaysUseThisDoctype;

	private ListViewer typeList;
	private Button alwaysUseButton;

	private MouseListener mouseListener = new MouseListener() {
		public void mouseDoubleClick(MouseEvent e) {
			buttonPressed(0);
		}

		public void mouseDown(MouseEvent e) {
		}

		public void mouseUp(MouseEvent e) {
		}
	};

}

Back to the top