Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c77017efe7b41e242613d2993c79f4ccddd2e68f (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
/*******************************************************************************
 * 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
 *     Igor Jacy Lino Campista - Java 5 warnings fixed (bug 311325)
 *******************************************************************************/
package org.eclipse.wst.xml.vex.ui.internal.wizards;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.text.MessageFormat;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorRegistry;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.internal.registry.EditorDescriptor;
import org.eclipse.ui.internal.registry.EditorRegistry;
import org.eclipse.ui.internal.registry.FileEditorMapping;
import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
import org.eclipse.wst.xml.vex.core.internal.dom.Document;
import org.eclipse.wst.xml.vex.core.internal.dom.DocumentWriter;
import org.eclipse.wst.xml.vex.core.internal.dom.RootElement;
import org.eclipse.wst.xml.vex.core.internal.widget.CssWhitespacePolicy;
import org.eclipse.wst.xml.vex.ui.internal.VexPlugin;
import org.eclipse.wst.xml.vex.ui.internal.config.Style;
import org.eclipse.wst.xml.vex.ui.internal.editor.DocumentFileCreationPage;
import org.eclipse.wst.xml.vex.ui.internal.editor.DocumentTypeSelectionPage;
import org.eclipse.wst.xml.vex.ui.internal.editor.Messages;
import org.eclipse.wst.xml.vex.ui.internal.editor.VexEditor;
 
/**
 * Wizard for creating a new Vex document.
 */
public class NewDocumentWizard extends BasicNewResourceWizard {

	public void addPages() {
		this.typePage = new DocumentTypeSelectionPage();
		this.filePage = new DocumentFileCreationPage(
				"filePage", this.getSelection()); //$NON-NLS-1$
		addPage(typePage);
		addPage(filePage);
	}

	public void init(IWorkbench workbench, IStructuredSelection currentSelection) {

		super.init(workbench, currentSelection);
		this.setWindowTitle(Messages.getString("NewDocumentWizard.title")); //$NON-NLS-1$
	}

	public boolean performFinish() {
		try {
			RootElement root = new RootElement(this.typePage
					.getRootElementName());
			Document doc = new Document(root);
			doc.setPublicID(this.typePage.getDocumentType().getPublicId());
			doc.setSystemID(this.typePage.getDocumentType().getSystemId());

			Style style = VexEditor.getPreferredStyle(doc.getPublicID());
			if (style == null) {
				MessageDialog
						.openError(
								this.getShell(),
								Messages
										.getString("NewDocumentWizard.noStyles.title"), Messages.getString("NewDocumentWizard.noStyles.message")); //$NON-NLS-1$ //$NON-NLS-2$
				return false;
				// TODO: don't allow selection of types with no stylesheets
			}

			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			DocumentWriter writer = new DocumentWriter();
			writer.setWhitespacePolicy(new CssWhitespacePolicy(style
					.getStyleSheet()));
			writer.write(doc, baos);
			baos.close();
			ByteArrayInputStream bais = new ByteArrayInputStream(baos
					.toByteArray());

			filePage.setInitialContents(bais);
			IFile file = filePage.createNewFile();
			IDE.setDefaultEditor(file, VexEditor.ID);
			this.selectAndReveal(file);

			registerEditorForFilename(
					"*." + file.getFileExtension(), VexEditor.ID); //$NON-NLS-1$

			// Open editor on new file.
			IWorkbenchWindow dw = getWorkbench().getActiveWorkbenchWindow();
			if (dw != null) {
				IWorkbenchPage page = dw.getActivePage();
				if (page != null) {
					IDE.openEditor(page, file, true);
				}
			}

			this.typePage.saveSettings();

			return true;

		} catch (Exception ex) {
			String message = MessageFormat.format(Messages
					.getString("NewDocumentWizard.errorLoading.message"),
					new Object[] { filePage.getFileName(), ex.getMessage() });
			VexPlugin.getInstance().log(IStatus.ERROR, message, ex);
			MessageDialog
					.openError(
							this.getShell(),
							Messages
									.getString("NewDocumentWizard.errorLoading.title"), "Unable to create " + filePage.getFileName()); //$NON-NLS-1$ //$NON-NLS-2$
			return false;
		}
	}

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

	private DocumentTypeSelectionPage typePage;
	private DocumentFileCreationPage filePage;

	/**
	 * Register an editor to use for files with the given filename.
	 * 
	 * NOTE: this method uses internal, undocumented Eclipse functionality. It
	 * may therefore break in a future version of Eclipse.
	 * 
	 * @param fileName
	 *            Filename to be registered. Use the form "*.ext" to register
	 *            all files with a given extension.
	 * @param editorId
	 *            ID of the editor to use for the given filename.
	 */
	private static void registerEditorForFilename(String fileName,
			String editorId) {

		EditorDescriptor ed = getEditorDescriptor(editorId);
		if (ed == null) {
			return;
		}

		IEditorRegistry reg = PlatformUI.getWorkbench().getEditorRegistry();
		EditorRegistry ereg = (EditorRegistry) reg;
		FileEditorMapping[] mappings = (FileEditorMapping[]) ereg
				.getFileEditorMappings();
		FileEditorMapping mapping = null;
		for (FileEditorMapping fem : mappings) {
			if (fem.getLabel().equals(fileName)) {
				mapping = (FileEditorMapping) fem;
				break;
			}
		}

		if (mapping != null) {
			// found mapping for fileName
			// make sure it includes our editor
			for (IEditorDescriptor editor : mapping.getEditors()) {
				if (editor.getId().equals(editorId)) {
					// already mapped
					return;
				}
			}

			// editor not in the list, so add it
			mapping.addEditor(ed);
			ereg.setFileEditorMappings(mappings);
			ereg.saveAssociations();

		} else {
			// no mapping found for the filename
			// let's add one
			String name = null;
			String ext = null;
			int iDot = fileName.lastIndexOf('.');
			if (iDot == -1) {
				name = fileName;
			} else {
				name = fileName.substring(0, iDot);
				ext = fileName.substring(iDot + 1);
			}

			mapping = new FileEditorMapping(name, ext);
			FileEditorMapping[] newMappings = new FileEditorMapping[mappings.length + 1];
			mapping.addEditor(ed);

			System.arraycopy(mappings, 0, newMappings, 0, mappings.length);
			newMappings[mappings.length] = mapping;
			ereg.setFileEditorMappings(newMappings);
			ereg.saveAssociations();
		}

	}

	/**
	 * Return the IEditorDescriptor for the given editor ID.
	 */
	private static EditorDescriptor getEditorDescriptor(String editorId) {
		EditorRegistry reg = (EditorRegistry) PlatformUI.getWorkbench()
				.getEditorRegistry();
		for (IEditorDescriptor editor : reg.getSortedEditorsFromPlugins()) {
			if (editor.getId().equals(editorId)) {
				return (EditorDescriptor) editor;
			}
		}

		return null;
	}
}

Back to the top