amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2005, 2006 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
nitind | 65cb13d | 2005-03-10 23:04:22 +0000 | [diff] [blame] | 11 | package org.eclipse.wst.html.ui.internal.wizard; |
| 12 | |
david_williams | 9f46258 | 2005-04-15 23:38:47 +0000 | [diff] [blame] | 13 | import java.io.ByteArrayInputStream; |
| 14 | import java.io.ByteArrayOutputStream; |
| 15 | import java.io.OutputStreamWriter; |
nitind | 65cb13d | 2005-03-10 23:04:22 +0000 | [diff] [blame] | 16 | |
| 17 | import org.eclipse.core.resources.IFile; |
nitind | 861c8e3 | 2008-02-28 05:51:36 +0000 | [diff] [blame] | 18 | import org.eclipse.core.resources.ProjectScope; |
| 19 | import org.eclipse.core.runtime.Platform; |
david_williams | 9f46258 | 2005-04-15 23:38:47 +0000 | [diff] [blame] | 20 | import org.eclipse.core.runtime.Preferences; |
nitind | 861c8e3 | 2008-02-28 05:51:36 +0000 | [diff] [blame] | 21 | import org.eclipse.core.runtime.preferences.IScopeContext; |
| 22 | import org.eclipse.core.runtime.preferences.InstanceScope; |
nitind | 65cb13d | 2005-03-10 23:04:22 +0000 | [diff] [blame] | 23 | import org.eclipse.jface.resource.ImageDescriptor; |
| 24 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 25 | import org.eclipse.jface.viewers.StructuredSelection; |
| 26 | import org.eclipse.jface.wizard.Wizard; |
| 27 | import org.eclipse.ui.INewWizard; |
| 28 | import org.eclipse.ui.IWorkbench; |
| 29 | import org.eclipse.ui.IWorkbenchPage; |
| 30 | import org.eclipse.ui.PartInitException; |
| 31 | import org.eclipse.ui.PlatformUI; |
nitind | 65cb13d | 2005-03-10 23:04:22 +0000 | [diff] [blame] | 32 | import org.eclipse.ui.ide.IDE; |
david_williams | 9f46258 | 2005-04-15 23:38:47 +0000 | [diff] [blame] | 33 | import org.eclipse.wst.html.core.internal.HTMLCorePlugin; |
david_williams | 3a2ab2d | 2005-04-11 16:32:45 +0000 | [diff] [blame] | 34 | import org.eclipse.wst.html.ui.internal.HTMLUIMessages; |
nitind | 65cb13d | 2005-03-10 23:04:22 +0000 | [diff] [blame] | 35 | import org.eclipse.wst.html.ui.internal.Logger; |
david_williams | 385fc5c | 2005-06-07 22:34:13 +0000 | [diff] [blame] | 36 | import org.eclipse.wst.html.ui.internal.editor.HTMLEditorPluginImageHelper; |
| 37 | import org.eclipse.wst.html.ui.internal.editor.HTMLEditorPluginImages; |
david_williams | 9f46258 | 2005-04-15 23:38:47 +0000 | [diff] [blame] | 38 | import org.eclipse.wst.sse.core.internal.encoding.CommonEncodingPreferenceNames; |
nitind | 861c8e3 | 2008-02-28 05:51:36 +0000 | [diff] [blame] | 39 | import org.eclipse.wst.sse.core.utils.StringUtils; |
nitind | 65cb13d | 2005-03-10 23:04:22 +0000 | [diff] [blame] | 40 | |
| 41 | public class NewHTMLWizard extends Wizard implements INewWizard { |
amywu | 9802af8 | 2006-06-01 21:22:40 +0000 | [diff] [blame] | 42 | |
| 43 | private NewHTMLFileWizardPage fNewFilePage; |
david_williams | 9f46258 | 2005-04-15 23:38:47 +0000 | [diff] [blame] | 44 | private NewHTMLTemplatesWizardPage fNewFileTemplatesPage; |
nitind | 65cb13d | 2005-03-10 23:04:22 +0000 | [diff] [blame] | 45 | private IStructuredSelection fSelection; |
nitind | 65cb13d | 2005-03-10 23:04:22 +0000 | [diff] [blame] | 46 | |
| 47 | public void addPages() { |
amywu | 9802af8 | 2006-06-01 21:22:40 +0000 | [diff] [blame] | 48 | fNewFilePage = new NewHTMLFileWizardPage("HTMLWizardNewFileCreationPage", new StructuredSelection(IDE.computeSelectedResources(fSelection))); //$NON-NLS-1$ |
david_williams | 3a2ab2d | 2005-04-11 16:32:45 +0000 | [diff] [blame] | 49 | fNewFilePage.setTitle(HTMLUIMessages._UI_WIZARD_NEW_HEADING); |
| 50 | fNewFilePage.setDescription(HTMLUIMessages._UI_WIZARD_NEW_DESCRIPTION); |
nitind | 65cb13d | 2005-03-10 23:04:22 +0000 | [diff] [blame] | 51 | addPage(fNewFilePage); |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 52 | |
david_williams | 9f46258 | 2005-04-15 23:38:47 +0000 | [diff] [blame] | 53 | fNewFileTemplatesPage = new NewHTMLTemplatesWizardPage(); |
| 54 | addPage(fNewFileTemplatesPage); |
nitind | 65cb13d | 2005-03-10 23:04:22 +0000 | [diff] [blame] | 55 | } |
nitind | 861c8e3 | 2008-02-28 05:51:36 +0000 | [diff] [blame] | 56 | |
| 57 | private String applyLineDelimiter(IFile file, String text) { |
| 58 | String lineDelimiter = Platform.getPreferencesService().getString(Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR, System.getProperty("line.separator"), new IScopeContext[] {new ProjectScope(file.getProject()), new InstanceScope() });//$NON-NLS-1$ |
| 59 | String convertedText = StringUtils.replace(text, "\r\n", "\n"); |
| 60 | convertedText = StringUtils.replace(convertedText, "\r", "\n"); |
| 61 | convertedText = StringUtils.replace(convertedText, "\n", lineDelimiter); |
| 62 | return convertedText; |
| 63 | } |
nitind | 65cb13d | 2005-03-10 23:04:22 +0000 | [diff] [blame] | 64 | |
| 65 | public void init(IWorkbench aWorkbench, IStructuredSelection aSelection) { |
| 66 | fSelection = aSelection; |
david_williams | 3a2ab2d | 2005-04-11 16:32:45 +0000 | [diff] [blame] | 67 | setWindowTitle(HTMLUIMessages._UI_WIZARD_NEW_TITLE); |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 68 | |
david_williams | 385fc5c | 2005-06-07 22:34:13 +0000 | [diff] [blame] | 69 | ImageDescriptor descriptor = HTMLEditorPluginImageHelper.getInstance().getImageDescriptor(HTMLEditorPluginImages.IMG_WIZBAN_NEWHTMLFILE); |
| 70 | setDefaultPageImageDescriptor(descriptor); |
nitind | 65cb13d | 2005-03-10 23:04:22 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | private void openEditor(final IFile file) { |
| 74 | if (file != null) { |
| 75 | getShell().getDisplay().asyncExec(new Runnable() { |
| 76 | public void run() { |
| 77 | try { |
| 78 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
| 79 | IDE.openEditor(page, file, true); |
| 80 | } |
| 81 | catch (PartInitException e) { |
| 82 | Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); |
| 83 | } |
| 84 | } |
| 85 | }); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public boolean performFinish() { |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 90 | boolean performedOK = false; |
david_williams | 9f46258 | 2005-04-15 23:38:47 +0000 | [diff] [blame] | 91 | // save user options for next use |
| 92 | fNewFileTemplatesPage.saveLastSavedPreferences(); |
| 93 | |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 94 | // no file extension specified so add default extension |
| 95 | String fileName = fNewFilePage.getFileName(); |
| 96 | if (fileName.lastIndexOf('.') == -1) { |
amywu | 9802af8 | 2006-06-01 21:22:40 +0000 | [diff] [blame] | 97 | String newFileName = fNewFilePage.addDefaultExtension(fileName); |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 98 | fNewFilePage.setFileName(newFileName); |
| 99 | } |
| 100 | |
david_williams | 9f46258 | 2005-04-15 23:38:47 +0000 | [diff] [blame] | 101 | // create a new empty file |
nitind | 65cb13d | 2005-03-10 23:04:22 +0000 | [diff] [blame] | 102 | IFile file = fNewFilePage.createNewFile(); |
david_williams | 9f46258 | 2005-04-15 23:38:47 +0000 | [diff] [blame] | 103 | |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 104 | // if there was problem with creating file, it will be null, so make |
| 105 | // sure to check |
| 106 | if (file != null) { |
| 107 | // put template contents into file |
| 108 | String templateString = fNewFileTemplatesPage.getTemplateString(); |
| 109 | if (templateString != null) { |
nitind | 861c8e3 | 2008-02-28 05:51:36 +0000 | [diff] [blame] | 110 | templateString = applyLineDelimiter(file, templateString); |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 111 | // determine the encoding for the new file |
| 112 | Preferences preference = HTMLCorePlugin.getDefault().getPluginPreferences(); |
| 113 | String charSet = preference.getString(CommonEncodingPreferenceNames.OUTPUT_CODESET); |
david_williams | 9f46258 | 2005-04-15 23:38:47 +0000 | [diff] [blame] | 114 | |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 115 | try { |
| 116 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 117 | OutputStreamWriter outputStreamWriter = null; |
| 118 | if (charSet == null || charSet.trim().equals("")) { //$NON-NLS-1$ |
| 119 | // just use default encoding |
| 120 | outputStreamWriter = new OutputStreamWriter(outputStream); |
| 121 | } |
| 122 | else { |
| 123 | outputStreamWriter = new OutputStreamWriter(outputStream, charSet); |
| 124 | } |
| 125 | outputStreamWriter.write(templateString); |
| 126 | outputStreamWriter.flush(); |
| 127 | outputStreamWriter.close(); |
| 128 | ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); |
| 129 | file.setContents(inputStream, true, false, null); |
| 130 | inputStream.close(); |
david_williams | 9f46258 | 2005-04-15 23:38:47 +0000 | [diff] [blame] | 131 | } |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 132 | catch (Exception e) { |
| 133 | Logger.log(Logger.WARNING_DEBUG, "Could not create contents for new HTML file", e); //$NON-NLS-1$ |
| 134 | } |
david_williams | 9f46258 | 2005-04-15 23:38:47 +0000 | [diff] [blame] | 135 | } |
david_williams | 9f46258 | 2005-04-15 23:38:47 +0000 | [diff] [blame] | 136 | |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 137 | // open the file in editor |
| 138 | openEditor(file); |
| 139 | |
| 140 | // everything's fine |
| 141 | performedOK = true; |
| 142 | } |
| 143 | return performedOK; |
nitind | 65cb13d | 2005-03-10 23:04:22 +0000 | [diff] [blame] | 144 | } |
| 145 | } |