| /******************************************************************************* |
| * 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; |
| } |
| } |