blob: 55a67c4a82ba66b718255f549515bc5dfd0db2e5 [file] [log] [blame]
kchong7416cdb2005-11-30 22:22:22 +00001/*******************************************************************************
2 * Copyright (c) 2004, 2005 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 *******************************************************************************/
csalter771f5152005-05-16 04:27:36 +000011package org.eclipse.wst.xsd.ui.internal.text;
12
csalterc7926c02006-07-18 16:19:46 +000013import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
csalter8fd5c1e2006-05-09 04:22:25 +000014import org.eclipse.wst.xsd.ui.internal.util.ModelReconcileAdapter;
csalter771f5152005-05-16 04:27:36 +000015import org.eclipse.xsd.XSDConcreteComponent;
16import org.eclipse.xsd.XSDSchema;
csalter9be70e92006-05-09 18:30:28 +000017import org.eclipse.xsd.util.XSDConstants;
csalter771f5152005-05-16 04:27:36 +000018import org.w3c.dom.Document;
19import org.w3c.dom.Element;
20import org.w3c.dom.Node;
21
csalter8fd5c1e2006-05-09 04:22:25 +000022public class XSDModelReconcileAdapter extends ModelReconcileAdapter
csalter771f5152005-05-16 04:27:36 +000023{
csalter8fd5c1e2006-05-09 04:22:25 +000024 protected XSDSchema schema;
csalter771f5152005-05-16 04:27:36 +000025
csalter8fd5c1e2006-05-09 04:22:25 +000026 public XSDModelReconcileAdapter(Document document, XSDSchema schema)
csalter771f5152005-05-16 04:27:36 +000027 {
csalter8fd5c1e2006-05-09 04:22:25 +000028 super(document);
29 this.schema = schema;
csalter771f5152005-05-16 04:27:36 +000030 }
csalter8fd5c1e2006-05-09 04:22:25 +000031
32 protected void handleNodeChanged(Node node)
csalter771f5152005-05-16 04:27:36 +000033 {
csalter9be70e92006-05-09 18:30:28 +000034 if (node instanceof Element)
35 {
36 XSDConcreteComponent concreteComponent = schema.getCorrespondingComponent(node);
37 concreteComponent.elementChanged((Element)node);
38 }
39 else if (node instanceof Document)
40 {
41 // The document changed so we may need to fix up the
42 // definition's root element
43 Document document = (Document)node;
44 Element schemaElement = document.getDocumentElement();
45 if (schemaElement != null && schemaElement != schema.getElement())
46 {
47 // here we handle the case where a new 'schema' element was added
48 //(e.g. the file was totally blank and then we type in the root element)
49 //
50 if (schemaElement.getLocalName().equals(XSDConstants.SCHEMA_ELEMENT_TAG))
51 {
52 //System.out.println("****** Setting new schema");
53 schema.setElement(schemaElement);
54 }
55 }
56 else if (schemaElement != null)
57 {
58 // handle the case where the definition element's content has changed
59 // TODO (cs) do we really need to handle this case?
60 schema.elementChanged(schemaElement);
61 }
62 else if (schemaElement == null)
63 {
64 // if there's no root element clear out the schema
65 //
66 schema.getContents().clear();
67 // TODO (cs) I'm not sure why the above isn't enough
68 // to clear out all of the component lists
69 // for now I've just added a few more lines to do additional clearing
70 //
71 schema.getElementDeclarations().clear();
72 schema.getTypeDefinitions().clear();
73 schema.getAttributeDeclarations().clear();
74 schema.getModelGroupDefinitions().clear();
kchong02892442006-05-15 21:16:59 +000075 schema.getAttributeGroupDefinitions().clear();
76
77 schema.setElement(null);
csalter9be70e92006-05-09 18:30:28 +000078 }
79 }
csalterc7926c02006-07-18 16:19:46 +000080 }
81
82 public void modelDirtyStateChanged(IStructuredModel model, boolean isDirty)
83 {
84 if (!isDirty)
85 {
86 // TODO need a way to tell the views to stop refreshing (temporarily)
87 //
88 /*
89 schema.reset();
90
91 // For some reason type references don't get fixed up when an import is removed
92 // even if we call schema.reset() explictly. To work around this we iterate thru
93 // the type references and recompute them incase a schema did infact change
94 //
95 for (Iterator i = schema.getElementDeclarations().iterator(); i.hasNext(); )
96 {
97 XSDElementDeclarationImpl ed = (XSDElementDeclarationImpl)i.next();
98 //ed.elementAttributesChanged(ed.getElement());
99 XSDTypeDefinition td = ed.getTypeDefinition();
100 td = ed.resolveTypeDefinition(td.getSchema().getTargetNamespace(), td.getName());
101 ed.setTypeDefinition(td);
102 }*/
103 }
csalter8fd5c1e2006-05-09 04:22:25 +0000104 }
csalter771f5152005-05-16 04:27:36 +0000105}