Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 0eaf5a859f8385ef803bfc7102bd84f88bc30509 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*******************************************************************************
 * Copyright (c) 2001, 2004 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsdl.ui.internal.xsd;
                                      
import java.util.ArrayList;

import org.eclipse.core.resources.IFile;
import org.eclipse.gef.EditPart;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.wst.wsdl.Types;
import org.eclipse.wst.wsdl.XSDSchemaExtensibilityElement;
import org.eclipse.wst.wsdl.ui.internal.WSDLEditor;
import org.eclipse.wst.wsdl.ui.internal.WSDLEditorPlugin;
import org.eclipse.wst.wsdl.ui.internal.extension.IMenuActionContributor;
import org.eclipse.wst.wsdl.ui.internal.graph.WSDLComponentViewer;
import org.eclipse.wst.wsdl.ui.internal.model.WSDLGroupObject;
import org.eclipse.wst.wsdl.ui.internal.xsd.actions.AddSchemaAction;
import org.eclipse.wst.wsdl.ui.internal.xsd.actions.BackAction;
import org.eclipse.wst.xml.core.internal.document.DocumentImpl;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.eclipse.wst.xsd.ui.internal.XSDEditorPlugin;
import org.eclipse.wst.xsd.ui.internal.XSDMenuListener;
import org.eclipse.wst.xsd.ui.internal.actions.CreateElementAction;
import org.eclipse.wst.xsd.ui.internal.actions.CreateGroupAction;
import org.eclipse.wst.xsd.ui.internal.actions.DOMAttribute;
import org.eclipse.wst.xsd.ui.internal.gef.util.editparts.AbstractComponentViewerRootEditPart;
import org.eclipse.wst.xsd.ui.internal.graph.editparts.TopLevelComponentEditPart;
import org.eclipse.wst.xsd.ui.internal.graph.model.Category;
import org.eclipse.wst.xsd.ui.internal.provider.CategoryAdapter;
import org.eclipse.wst.xsd.ui.internal.util.XSDDOMHelper;
import org.eclipse.xsd.XSDConcreteComponent;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.util.XSDConstants;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XSDMenuActionContributor implements IMenuActionContributor
{
  Element currElement;
  Document currDocument;
  XSDSchema xsdSchema;
//  protected ISelectionProvider iSelectionProvider;
  WSDLEditor wsdlEditor;

  public XSDMenuActionContributor(WSDLEditor wsdlEditor)
  {
    this.wsdlEditor = wsdlEditor;
  }

  public void contributeMenuActions(IMenuManager manager, Node node, Object object)
  {
    XSDMenuExtensionListener xsdMenuListener = new XSDMenuExtensionListener(wsdlEditor.getSelectionManager());
    xsdMenuListener.contributeMenuActions(manager, node, object);
  }

  class XSDMenuExtensionListener extends XSDMenuListener
  {
    public XSDMenuExtensionListener(ISelectionProvider selectionProvider)
    {
      super(selectionProvider);
    }

    public void contributeMenuActions(IMenuManager manager, Node node, Object object)
    {
      updateXSDSchema();
      AbstractComponentViewerRootEditPart editPart = (AbstractComponentViewerRootEditPart)wsdlEditor.getGraphViewer().getComponentViewer().getRootEditPart().getContents();
//    iSelectionProvider = ((WSDLContentOutlinePage)(wsdlEditor.getWSDLTextEditor().getContentOutlinePage())).getTreeViewer();
      EditPart focusEditPart = wsdlEditor.getGraphViewer().getComponentViewer().getFocusEditPart();

      if (object instanceof WSDLGroupObject)
      {
        WSDLGroupObject group = (WSDLGroupObject)object;
        if (group.getType() == WSDLGroupObject.TYPES_GROUP)
        {
          boolean typesExist = group.getParent().getTypes() != null;
          
          Element typesElement = null;
          if (typesExist)
          {
            Types types = group.getParent().getETypes();
            typesElement = types.getElement();
          }
          if (manager != null)
          {
            Document document = null;
            if (editPart.getViewer() instanceof WSDLComponentViewer) {
              WSDLComponentViewer wsdlComponentViewer = (WSDLComponentViewer) editPart.getViewer();
              document = wsdlComponentViewer.getWSDLEditor().getXMLDocument();
            }

            manager.add(new AddSchemaAction(group.getDefinition(), (Element)node, typesElement, document));
          }
        }
      } 
      else if (object instanceof Types)
      {
        // IMenuManager menu = manager.findMenuUsingPath("addchild");

        if (manager != null)
        {
          Types types = (Types)object;
          manager.add(new AddSchemaAction(types.getEnclosingDefinition(), (Element)node));
        }
      }
      
      if (xsdSchema == null)
      {
        return;
      }
      
      ArrayList attributes = null;
      Node relativeNode = null;
      
      BackAction backAction;
      
      if (object instanceof XSDSchema || focusEditPart instanceof TopLevelComponentEditPart || object instanceof Category)
      {
        backAction = new BackAction(WSDLEditorPlugin.getWSDLString("_UI_BACK_TO", "Definition"));
        backAction.setDefinition(wsdlEditor.getDefinition());
        backAction.setSelectionProvider(selectionProvider);
        backAction.setRootEditPart(editPart);
        backAction.setGraphViewer(wsdlEditor.getGraphViewer());
        manager.add(backAction);
        manager.add(new Separator());
      }
      else if (object instanceof XSDConcreteComponent)
      {
        backAction = new BackAction(XSDEditorPlugin.getXSDString("_UI_ACTION_BACK_TO_SCHEMA_VIEW"));
        // backAction.setXSDSchema(((XSDConcreteComponent)object).getSchema());
        backAction.setXSDSchema(xsdSchema);
        backAction.setSelectionProvider(selectionProvider);
        backAction.setRootEditPart(editPart);
        backAction.setGraphViewer(wsdlEditor.getGraphViewer());
        manager.add(backAction);
        manager.add(new Separator());
      }

      // CS: I'm removing the 'object instanceof Element' case from this test
      // for now the menu actions are driven from 'model' objects
      if (object instanceof XSDConcreteComponent || (object instanceof org.eclipse.wst.wsdl.XSDSchemaExtensibilityElement))
      {
        Element parent;
        if (object instanceof XSDConcreteComponent)
        {
          parent = ((XSDConcreteComponent)object).getElement();
          xsdSchema = ((XSDConcreteComponent)object).getSchema();
        }
        else if (object instanceof Element)
        {
          manager.add(new Separator());
          parent = (Element)object; 
        }
        else if (object instanceof org.eclipse.wst.wsdl.XSDSchemaExtensibilityElement)
        {
          XSDSchemaExtensibilityElement schema = (XSDSchemaExtensibilityElement)object;
          xsdSchema = schema.getSchema();
          manager.add(new Separator());
          parent = xsdSchema.getElement();
        }
        else
        {
          return;
        }
        
        currElement = parent;
        currDocument = parent.getOwnerDocument();
        
//        if (!(currDocument instanceof DocumentImpl))
//        {
//          return;
//        }
        
        addContextItems(manager, currElement, null);

        if (!(object instanceof XSDSchema) ||
           	(object instanceof XSDSchema && !(wsdlEditor.getGraphViewer().getComponentViewer().getInput() instanceof XSDSchema))) {
           	IStructuredSelection selections = (IStructuredSelection) wsdlEditor.getSelectionManager().getSelection();
           	manager.add(new Separator());
           	org.eclipse.wst.wsdl.ui.internal.actions.DeleteWSDLAndXSDAction deleteWSDLAndXSDAction = new org.eclipse.wst.wsdl.ui.internal.actions.DeleteWSDLAndXSDAction(selections.toList(), currElement, wsdlEditor);
           	deleteWSDLAndXSDAction.setEnabled(!isReadOnly);
           	manager.add(deleteWSDLAndXSDAction);
        }
      }
      else if (object instanceof Category
            || object instanceof CategoryAdapter)
      {
        int groupType = -1;

        if (object instanceof Category)
        {
          Category cg = (Category)object;
          xsdSchema = cg.getXSDSchema();
          groupType = cg.getGroupType();
        }
        else  // CategoryAdapter
        {
          CategoryAdapter category = (CategoryAdapter)object;
          groupType = category.getGroupType();
          xsdSchema = category.getXSDSchema();
        }
        Element parent = xsdSchema.getElement();
        currElement = parent;
        currDocument = parent.getOwnerDocument();
        switch (groupType)
        {
          case Category.TYPES:
          {
            attributes = new ArrayList();
            attributes.add(new DOMAttribute(XSDConstants.NAME_ATTRIBUTE, getNewGlobalTypeName("ComplexType")));
            Action action = addCreateElementAction(manager, XSDConstants.COMPLEXTYPE_ELEMENT_TAG, XSDEditorPlugin.getXSDString("_UI_ACTION_ADD_COMPLEX_TYPE"), attributes, parent, relativeNode);
            ((CreateElementAction)action).setIsGlobal(true);
            attributes = new ArrayList();
            attributes.add(new DOMAttribute(XSDConstants.NAME_ATTRIBUTE, getNewGlobalTypeName("SimpleType")));
            Action action2 = addCreateSimpleTypeAction(manager, XSDConstants.SIMPLETYPE_ELEMENT_TAG, XSDEditorPlugin.getXSDString("_UI_ACTION_ADD_SIMPLE_TYPE"), attributes, parent, relativeNode);
            ((CreateElementAction)action2).setIsGlobal(true);
            break;
          }
          case Category.ELEMENTS:
          {
            attributes = new ArrayList();
            attributes.add(new DOMAttribute(XSDConstants.NAME_ATTRIBUTE,
                                            getNewGlobalName(XSDConstants.ELEMENT_ELEMENT_TAG, "GlobalElement")));
            attributes.add(new DOMAttribute(XSDConstants.TYPE_ATTRIBUTE, getBuiltInStringQName()));
            Action action = addCreateElementAction(manager, XSDConstants.ELEMENT_ELEMENT_TAG, XSDEditorPlugin.getXSDString("_UI_ACTION_ADD_ELEMENT"), attributes, parent, relativeNode);
            ((CreateElementAction)action).setIsGlobal(true);
            break;
          }
          case Category.GROUPS:
          {
            attributes = new ArrayList();
            attributes.add(new DOMAttribute(XSDConstants.NAME_ATTRIBUTE,
                                            getNewGlobalName(XSDConstants.GROUP_ELEMENT_TAG, "Group")));
            CreateGroupAction groupAction = addCreateGroupAction(manager, XSDConstants.GROUP_ELEMENT_TAG, XSDEditorPlugin.getXSDString("_UI_ACTION_ADD_GROUP"), attributes, parent, relativeNode);
            groupAction.setIsGlobal(true);
            break;
          }
          case Category.ATTRIBUTES:
          {
            attributes = new ArrayList();
            attributes.add(new DOMAttribute(XSDConstants.NAME_ATTRIBUTE,
                                            getNewGlobalName(XSDConstants.ATTRIBUTE_ELEMENT_TAG, "GlobalAttribute")));
            attributes.add(new DOMAttribute(XSDConstants.TYPE_ATTRIBUTE, getBuiltInStringQName()));
            Action action = addCreateElementAction(manager, XSDConstants.ATTRIBUTE_ELEMENT_TAG, XSDEditorPlugin.getXSDString("_UI_ACTION_ADD_GLOBAL_ATTRIBUTE"), attributes, parent, relativeNode);
            ((CreateElementAction)action).setIsGlobal(true);
            break;
          }
          case Category.ATTRIBUTE_GROUPS:
          {
            attributes = new ArrayList();
            attributes.add(new DOMAttribute(XSDConstants.NAME_ATTRIBUTE,
                                            getNewGlobalName(XSDConstants.ATTRIBUTEGROUP_ELEMENT_TAG, "AttributeGroup")));
            Action action = addCreateElementAction(manager, XSDConstants.ATTRIBUTEGROUP_ELEMENT_TAG, XSDEditorPlugin.getXSDString("_UI_ACTION_ADD_ATTRIBUTE_GROUP"), attributes, parent, relativeNode);
            ((CreateElementAction)action).setIsGlobal(true);
            break; 
          }
          case Category.NOTATIONS:
          {
            attributes = new ArrayList();
            attributes.add(new DOMAttribute(XSDConstants.NAME_ATTRIBUTE, 
                           getNewGlobalName(XSDConstants.NOTATION_ELEMENT_TAG, "Notation")));
            attributes.add(new DOMAttribute(XSDConstants.PUBLIC_ATTRIBUTE, ""));
            Action action = addCreateElementAction(manager, XSDConstants.NOTATION_ELEMENT_TAG, XSDEditorPlugin.getXSDString("_UI_ACTION_ADD_NOTATION"), attributes, parent, relativeNode);
            ((CreateElementAction)action).setIsGlobal(true);
            break;
          }
          case Category.DIRECTIVES:
          {
            boolean b = true;
            NodeList children = parent.getChildNodes();
            Node effectiveRelativeNode = parent.getFirstChild();
            for (int i=0; i < children.getLength() && b; i++)
            {
              Node child = children.item(i);
              if (child != null && child instanceof Element)
              {
                if (XSDDOMHelper.inputEquals((Element)child, XSDConstants.INCLUDE_ELEMENT_TAG, false) ||
                    XSDDOMHelper.inputEquals((Element)child, XSDConstants.IMPORT_ELEMENT_TAG, false) ||
                    XSDDOMHelper.inputEquals((Element)child, XSDConstants.REDEFINE_ELEMENT_TAG, false) ||
                    XSDDOMHelper.inputEquals((Element)child, XSDConstants.ANNOTATION_ELEMENT_TAG, false))
                {
                  effectiveRelativeNode = child;
                }
                else
                {
                  b = false;
                }
              }
            }
            relativeNode = effectiveRelativeNode != null ? effectiveRelativeNode.getNextSibling() : null;
            attributes = new ArrayList();
            attributes.add(new DOMAttribute(XSDConstants.SCHEMALOCATION_ATTRIBUTE, ""));

            addCreateElementAction(manager, XSDConstants.INCLUDE_ELEMENT_TAG,XSDEditorPlugin.getXSDString("_UI_ACTION_ADD_INCLUDE"), attributes, parent, relativeNode);
            addCreateElementAction(manager, XSDConstants.IMPORT_ELEMENT_TAG,XSDEditorPlugin.getXSDString("_UI_ACTION_ADD_IMPORT"), null, parent, relativeNode);
            addCreateElementAction(manager, XSDConstants.REDEFINE_ELEMENT_TAG,XSDEditorPlugin.getXSDString("_UI_ACTION_ADD_REDEFINE"), attributes, parent, relativeNode);
            
            
            break;
          }
        }
      }
    }    

    protected IFile getFileResource()
    {
  ///// WSDL TODO
  //    if (getEditor() != null)
  //      return getEditor().getFileResource();
      return null;
    }

//    protected Object getSelectedElement()
//    {
//      return currElement;
//    }

    protected XSDSchema getXSDSchema()
    {
      return xsdSchema;
    }
  
    protected IDOMModel getXMLModel()
    {
      if (currElement != null)
      {
        Object obj = currElement.getOwnerDocument();
        if (obj instanceof DocumentImpl)
        {
          DocumentImpl xmlDoc = (DocumentImpl) currElement.getOwnerDocument();
          return xmlDoc.getModel();
        }
      }
      return null;
    }
  }

}

Back to the top