blob: 0a7c2bfb093054fac0ccce7479c72036e3a3d181 [file] [log] [blame]
david_williams96213482004-11-11 09:07:12 +00001/*******************************************************************************
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_williamsf3680f02005-04-13 22:43:54 +000013package org.eclipse.wst.xml.ui.internal.actions;
david_williams96213482004-11-11 09:07:12 +000014
15import org.eclipse.core.runtime.Path;
16import org.eclipse.jface.action.Action;
17import org.eclipse.jface.window.Window;
18import org.eclipse.swt.widgets.Display;
19import org.eclipse.swt.widgets.Shell;
20import org.eclipse.ui.PlatformUI;
david_williams4ad020f2005-04-18 08:00:30 +000021import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
david_williams96213482004-11-11 09:07:12 +000022import org.eclipse.wst.xml.core.internal.document.DocumentImpl;
david_williams4ad020f2005-04-18 08:00:30 +000023import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
24import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocumentType;
david_williams5f2420f2005-04-12 12:23:57 +000025import org.eclipse.wst.xml.ui.internal.XMLUIMessages;
david_williamsf3680f02005-04-13 22:43:54 +000026import org.eclipse.wst.xml.ui.internal.dialogs.EditDoctypeDialog;
david_williams96213482004-11-11 09:07:12 +000027import org.w3c.dom.Document;
28import org.w3c.dom.DocumentType;
29import org.w3c.dom.Element;
30import org.w3c.dom.Node;
31import org.w3c.dom.NodeList;
32
david_williams96213482004-11-11 09:07:12 +000033/**
34 * EditDoctypeAction
35 */
36public 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_williamsc39caaf2005-04-05 06:07:16 +000069 IDOMDocument documentImpl = (IDOMDocument) document;
70 IDOMDocumentType doctypeImpl = (IDOMDocumentType) documentImpl.createDoctype(dialog.getName());
david_williams96213482004-11-11 09:07:12 +000071 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_williams5f2420f2005-04-12 12:23:57 +000095 return rootElement != null ? rootElement.getNodeName() : XMLUIMessages._UI_LABEL_ROOT_ELEMENT_VALUE; //$NON-NLS-1$
david_williams96213482004-11-11 09:07:12 +000096 }
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_williams5f2420f2005-04-12 12:23:57 +0000147 title = XMLUIMessages._UI_LABEL_EDIT_DOCTYPE; //$NON-NLS-1$
david_williams96213482004-11-11 09:07:12 +0000148 }
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_williams5f2420f2005-04-12 12:23:57 +0000153 title = XMLUIMessages._UI_MENU_ADD_DTD_INFORMATION_TITLE; //$NON-NLS-1$
david_williams96213482004-11-11 09:07:12 +0000154 }
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_williamsc39caaf2005-04-05 06:07:16 +0000171 if (doctype instanceof IDOMDocumentType) {
172 IDOMDocumentType doctypeImpl = (IDOMDocumentType) doctype;
david_williams96213482004-11-11 09:07:12 +0000173 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}