david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2001, 2004 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 | * Jens Lukowski/Innoopract - initial renaming/restructuring |
| 11 | * |
| 12 | *******************************************************************************/ |
david_williams | f3680f0 | 2005-04-13 22:43:54 +0000 | [diff] [blame] | 13 | package org.eclipse.wst.xml.ui.internal.actions; |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 14 | |
| 15 | import org.eclipse.core.runtime.Path; |
| 16 | import org.eclipse.jface.action.Action; |
| 17 | import org.eclipse.jface.window.Window; |
| 18 | import org.eclipse.swt.widgets.Display; |
| 19 | import org.eclipse.swt.widgets.Shell; |
| 20 | import org.eclipse.ui.PlatformUI; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 21 | import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 22 | import org.eclipse.wst.xml.core.internal.document.DocumentImpl; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 23 | import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument; |
| 24 | import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocumentType; |
david_williams | 5f2420f | 2005-04-12 12:23:57 +0000 | [diff] [blame] | 25 | import org.eclipse.wst.xml.ui.internal.XMLUIMessages; |
david_williams | f3680f0 | 2005-04-13 22:43:54 +0000 | [diff] [blame] | 26 | import org.eclipse.wst.xml.ui.internal.dialogs.EditDoctypeDialog; |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 27 | import org.w3c.dom.Document; |
| 28 | import org.w3c.dom.DocumentType; |
| 29 | import org.w3c.dom.Element; |
| 30 | import org.w3c.dom.Node; |
| 31 | import org.w3c.dom.NodeList; |
| 32 | |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 33 | /** |
| 34 | * EditDoctypeAction |
| 35 | */ |
| 36 | public class EditDoctypeAction extends Action { |
| 37 | protected DocumentType doctype; |
| 38 | protected Document document; |
| 39 | protected IStructuredModel model; |
| 40 | protected String resourceLocation; |
| 41 | protected String title; |
| 42 | |
| 43 | /** |
| 44 | * This constructor is used to create a new doctype. |
| 45 | */ |
| 46 | public EditDoctypeAction(IStructuredModel model, Document document, String resourceLocation, String title) { |
| 47 | setText(title); |
| 48 | this.model = model; |
| 49 | this.document = document; |
| 50 | this.resourceLocation = resourceLocation; |
| 51 | this.title = title; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * This constructor is used to edit an exisitng doctype. |
| 56 | */ |
| 57 | public EditDoctypeAction(IStructuredModel model, DocumentType doctype, String resourceLocation, String title) { |
| 58 | setText(title); |
| 59 | this.model = model; |
| 60 | this.doctype = doctype; |
| 61 | this.resourceLocation = resourceLocation; |
| 62 | this.title = title; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | protected DocumentType createDoctype(EditDoctypeDialog dialog, Document document) { |
| 67 | DocumentType result = null; |
| 68 | if (document instanceof DocumentImpl) { |
david_williams | c39caaf | 2005-04-05 06:07:16 +0000 | [diff] [blame] | 69 | IDOMDocument documentImpl = (IDOMDocument) document; |
| 70 | IDOMDocumentType doctypeImpl = (IDOMDocumentType) documentImpl.createDoctype(dialog.getName()); |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 71 | doctypeImpl.setPublicId(dialog.getPublicId()); |
| 72 | doctypeImpl.setSystemId(dialog.getSystemId()); |
| 73 | result = doctypeImpl; |
| 74 | } |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | private Display getDisplay() { |
| 79 | |
| 80 | return PlatformUI.getWorkbench().getDisplay(); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | protected String getRootElementName(Document document) { |
| 85 | Element rootElement = null; |
| 86 | NodeList nodeList = document.getChildNodes(); |
| 87 | int nodeListLength = nodeList.getLength(); |
| 88 | for (int i = 0; i < nodeListLength; i++) { |
| 89 | Node childNode = nodeList.item(i); |
| 90 | if (childNode.getNodeType() == Node.ELEMENT_NODE) { |
| 91 | rootElement = (Element) childNode; |
| 92 | break; |
| 93 | } |
| 94 | } |
david_williams | 5f2420f | 2005-04-12 12:23:57 +0000 | [diff] [blame] | 95 | return rootElement != null ? rootElement.getNodeName() : XMLUIMessages._UI_LABEL_ROOT_ELEMENT_VALUE; //$NON-NLS-1$ |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | public String getUndoDescription() { |
| 99 | return title; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | protected void insertDoctype(DocumentType doctype, Document document) { |
| 104 | Node refChild = null; |
| 105 | NodeList nodeList = document.getChildNodes(); |
| 106 | int nodeListLength = nodeList.getLength(); |
| 107 | for (int i = 0; i < nodeListLength; i++) { |
| 108 | Node childNode = nodeList.item(i); |
| 109 | if (childNode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE || childNode.getNodeType() == Node.COMMENT_NODE) { |
| 110 | // continue on to the nextNode |
| 111 | } else { |
| 112 | refChild = childNode; |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | document.insertBefore(doctype, refChild); |
| 118 | //manager.reformat(doctype, false); |
| 119 | } |
| 120 | |
| 121 | public void run() { |
| 122 | model.beginRecording(this, getUndoDescription()); |
| 123 | //Shell shell = |
| 124 | // XMLCommonUIPlugin.getInstance().getWorkbench().getActiveWorkbenchWindow().getShell(); |
| 125 | Shell shell = getDisplay().getActiveShell(); |
| 126 | EditDoctypeDialog dialog = showEditDoctypeDialog(shell); |
| 127 | |
| 128 | if (dialog.getReturnCode() == Window.OK) { |
| 129 | if (doctype != null) { |
| 130 | updateDoctype(dialog, doctype); |
| 131 | } else if (document != null) { |
| 132 | DocumentType doctype = createDoctype(dialog, document); |
| 133 | if (doctype != null) { |
| 134 | insertDoctype(doctype, document); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | model.endRecording(this); |
| 139 | } |
| 140 | |
| 141 | protected EditDoctypeDialog showEditDoctypeDialog(Shell shell) { |
| 142 | EditDoctypeDialog dialog = null; |
| 143 | |
| 144 | if (doctype != null) { |
| 145 | dialog = new EditDoctypeDialog(shell, doctype); |
| 146 | if (title == null) { |
david_williams | 5f2420f | 2005-04-12 12:23:57 +0000 | [diff] [blame] | 147 | title = XMLUIMessages._UI_LABEL_EDIT_DOCTYPE; //$NON-NLS-1$ |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 148 | } |
| 149 | } else if (document != null) { |
| 150 | String rootElementName = getRootElementName(document); |
| 151 | dialog = new EditDoctypeDialog(shell, rootElementName, "", rootElementName + ".dtd"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 152 | if (title == null) { |
david_williams | 5f2420f | 2005-04-12 12:23:57 +0000 | [diff] [blame] | 153 | title = XMLUIMessages._UI_MENU_ADD_DTD_INFORMATION_TITLE; //$NON-NLS-1$ |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | dialog.setComputeSystemId(doctype == null || doctype.getSystemId() == null || doctype.getSystemId().trim().length() == 0); |
| 158 | |
| 159 | dialog.setErrorChecking(false);//!model.getType().equals(IStructuredModel.HTML)); |
| 160 | dialog.create(); |
| 161 | dialog.getShell().setText(title); |
| 162 | dialog.setBlockOnOpen(true); |
| 163 | dialog.setResourceLocation(new Path(resourceLocation)); |
| 164 | dialog.open(); |
| 165 | |
| 166 | return dialog; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | protected void updateDoctype(EditDoctypeDialog dialog, DocumentType doctype) { |
david_williams | c39caaf | 2005-04-05 06:07:16 +0000 | [diff] [blame] | 171 | if (doctype instanceof IDOMDocumentType) { |
| 172 | IDOMDocumentType doctypeImpl = (IDOMDocumentType) doctype; |
david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 173 | if (doctypeImpl.getName().equals(dialog.getName())) { |
| 174 | doctypeImpl.setPublicId(dialog.getPublicId()); |
| 175 | doctypeImpl.setSystemId(dialog.getSystemId()); |
| 176 | } else { |
| 177 | // we need to create a new one and remove the old |
| 178 | // |
| 179 | Document document = doctype.getOwnerDocument(); |
| 180 | DocumentType newDoctype = createDoctype(dialog, document); |
| 181 | document.insertBefore(newDoctype, doctype); |
| 182 | document.removeChild(doctype); |
| 183 | //manager.reformat(newDoctype, false); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |