xml.wsdl.refactoring/src/org/netbeans/modules/xml/wsdl/refactoring/WSDLRefactoringElement.java
author Milutin Kristofic <mkristofic@netbeans.org>
Mon, 30 Jan 2017 14:30:54 +0100
changeset 1583 fe20f672a61a
parent 1305 619e3fca5c2f
permissions -rw-r--r--
Added Missing copyright information in source files
mkristofic@1583
     1
/*
mkristofic@1583
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
mkristofic@1583
     3
 *
mkristofic@1583
     4
 * Copyright 2009-2017 Oracle and/or its affiliates. All rights reserved.
mkristofic@1583
     5
 *
mkristofic@1583
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
mkristofic@1583
     7
 * Other names may be trademarks of their respective owners.
mkristofic@1583
     8
 *
mkristofic@1583
     9
 * The contents of this file are subject to the terms of either the GNU
mkristofic@1583
    10
 * General Public License Version 2 only ("GPL") or the Common
mkristofic@1583
    11
 * Development and Distribution License("CDDL") (collectively, the
mkristofic@1583
    12
 * "License"). You may not use this file except in compliance with the
mkristofic@1583
    13
 * License. You can obtain a copy of the License at
mkristofic@1583
    14
 * http://www.netbeans.org/cddl-gplv2.html
mkristofic@1583
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
mkristofic@1583
    16
 * specific language governing permissions and limitations under the
mkristofic@1583
    17
 * License.  When distributing the software, include this License Header
mkristofic@1583
    18
 * Notice in each file and include the License file at
mkristofic@1583
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
mkristofic@1583
    20
 * particular file as subject to the "Classpath" exception as provided
mkristofic@1583
    21
 * by Oracle in the GPL Version 2 section of the License file that
mkristofic@1583
    22
 * accompanied this code. If applicable, add the following below the
mkristofic@1583
    23
 * License Header, with the fields enclosed by brackets [] replaced by
mkristofic@1583
    24
 * your own identifying information:
mkristofic@1583
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
mkristofic@1583
    26
 *
mkristofic@1583
    27
 * Contributor(s):
mkristofic@1583
    28
 *
mkristofic@1583
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
mkristofic@1583
    30
 * Software is Sun Microsystems, Inc. Portions Copyright 2009-2010 Sun
mkristofic@1583
    31
 * Microsystems, Inc. All Rights Reserved.
mkristofic@1583
    32
 *
mkristofic@1583
    33
 * If you wish your version of this file to be governed by only the CDDL
mkristofic@1583
    34
 * or only the GPL Version 2, indicate your decision by adding
mkristofic@1583
    35
 * "[Contributor] elects to include this software in this distribution
mkristofic@1583
    36
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
mkristofic@1583
    37
 * single choice of license, a recipient has the option to distribute
mkristofic@1583
    38
 * your version of this file under either the CDDL, the GPL Version 2 or
mkristofic@1583
    39
 * to extend the choice of license to its licensees as provided above.
mkristofic@1583
    40
 * However, if you add GPL Version 2 code and therefore, elected the GPL
mkristofic@1583
    41
 * Version 2 license, then the option applies only if the new code is
mkristofic@1583
    42
 * made subject to such option by the copyright holder.
mkristofic@1583
    43
 */
sonali@175
    44
/*
sonali@175
    45
 * WSDLRefactoringElement.java
sonali@175
    46
 *
sonali@175
    47
 * Created on December 30, 2006, 4:41 PM
sonali@175
    48
 *
sonali@175
    49
 * To change this template, choose Tools | Template Manager
sonali@175
    50
 * and open the template in the editor.
sonali@175
    51
 */
sonali@175
    52
sonali@175
    53
package org.netbeans.modules.xml.wsdl.refactoring;
sonali@175
    54
sonali@175
    55
import java.awt.event.ActionEvent;
sonali@175
    56
import javax.swing.Action;
sonali@371
    57
import javax.swing.text.Document;
sonali@371
    58
import javax.swing.text.Position.Bias;
jbecicka@185
    59
import org.netbeans.modules.refactoring.spi.SimpleRefactoringElementImplementation;
sonali@215
    60
import org.netbeans.modules.refactoring.spi.ui.TreeElement;
sonali@215
    61
import org.netbeans.modules.refactoring.spi.ui.TreeElementFactory;
sonali@318
    62
import org.netbeans.modules.xml.refactoring.XMLRefactoringTransaction;
sonali@371
    63
import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
sonali@371
    64
import org.netbeans.modules.xml.schema.model.SchemaComponent;
sonali@175
    65
import org.netbeans.modules.xml.wsdl.model.WSDLModel;
sonali@175
    66
import org.netbeans.modules.xml.wsdl.ui.netbeans.module.WSDLDataObject;
sonali@175
    67
import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.NodesFactory;
sonali@175
    68
import org.netbeans.modules.xml.xam.Component;
sonali@175
    69
import org.netbeans.modules.xml.xam.Model;
sonali@175
    70
import org.netbeans.modules.xml.xam.ModelSource;
sonali@175
    71
import org.netbeans.modules.xml.xam.Referenceable;
sonali@318
    72
import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
sonali@371
    73
import org.netbeans.modules.xml.xam.dom.DocumentComponent;
sonali@371
    74
import org.netbeans.modules.xml.xam.dom.DocumentModelAccess;
sonali@175
    75
import org.netbeans.modules.xml.xam.ui.actions.ShowSourceAction;
sonali@175
    76
import org.openide.filesystems.FileObject;
sonali@175
    77
import org.openide.loaders.DataObject;
sonali@371
    78
import org.openide.loaders.DataObjectNotFoundException;
sonali@215
    79
import org.openide.nodes.AbstractNode;
sonali@215
    80
import org.openide.nodes.Children;
sonali@175
    81
import org.openide.nodes.Node;
sonali@371
    82
import org.openide.text.CloneableEditorSupport;
sonali@175
    83
import org.openide.text.PositionBounds;
sonali@371
    84
import org.openide.text.PositionRef;
sonali@318
    85
import org.openide.util.Exceptions;
jbecicka@185
    86
import org.openide.util.Lookup;
sonali@175
    87
import org.openide.util.actions.SystemAction;
sonali@175
    88
import org.openide.util.lookup.Lookups;
sonali@371
    89
import org.w3c.dom.Element;
sonali@175
    90
sonali@175
    91
/**
sonali@175
    92
 *
sonali@175
    93
 * @author Sonali
sonali@175
    94
 */
jbecicka@185
    95
public class WSDLRefactoringElement extends SimpleRefactoringElementImplementation {
sonali@175
    96
    
sonali@175
    97
    private FileObject source;
sonali@175
    98
    Model model;
sonali@175
    99
    Component comp;
sonali@175
   100
    Node node;
sonali@318
   101
    XMLRefactoringTransaction transaction;
sonali@175
   102
    /**
sonali@175
   103
     * Creates a new instance of WSDLRefactoringElement
sonali@175
   104
     */
sonali@175
   105
    public WSDLRefactoringElement(Model model, Referenceable target, Component comp) {
sonali@175
   106
        this.model=model;
sonali@175
   107
        this.comp=comp;
sonali@215
   108
        
sonali@175
   109
        try {
sonali@175
   110
            if(model instanceof WSDLModel) {
sonali@175
   111
                ModelSource ms = model.getModelSource();
sonali@175
   112
                source = (FileObject) ms.getLookup().lookup(FileObject.class);
sonali@175
   113
                    if(source != null) {
sonali@175
   114
                        DataObject dObj = DataObject.find(source);
sonali@175
   115
                        if(dObj != null && dObj instanceof WSDLDataObject) {
sonali@175
   116
                            this.node = NodesFactory.getInstance().create(comp);
sonali@215
   117
                        } 
sonali@175
   118
                }
sonali@175
   119
            }
sonali@215
   120
            
sonali@175
   121
        }catch (Exception e){
sonali@175
   122
            e.printStackTrace();
sonali@175
   123
        }
sonali@175
   124
        
sonali@175
   125
    }
sonali@175
   126
jbecicka@185
   127
    public Lookup getLookup() {
jbecicka@185
   128
       return Lookups.singleton(comp);
sonali@184
   129
       
sonali@184
   130
    }
sonali@184
   131
sonali@175
   132
    public FileObject getParentFile() {
sonali@175
   133
       if (source == null) {
sonali@175
   134
            assert model != null : "Invalid object, expecting non-null model."; //NOI18N
sonali@175
   135
            source = (FileObject)model.getModelSource().getLookup().lookup(FileObject.class);
sonali@175
   136
            assert source != null : "ModelSource should have FileObject in lookup"; //NOI18N
sonali@175
   137
        }
sonali@175
   138
        return source;
sonali@175
   139
    }
supernikita@1305
   140
sonali@175
   141
    public String getText() {
sonali@215
   142
        if(node == null){
sonali@215
   143
            //in case of embedded SchemaComponent, use the TreeElementFactory as the UIHelper 
sonali@215
   144
           TreeElement elem = TreeElementFactory.getTreeElement(comp);
sonali@215
   145
           return elem.getText(true);
sonali@215
   146
        }
sonali@175
   147
        return node.getName();
sonali@175
   148
    }
sonali@175
   149
sonali@175
   150
    public String getDisplayText() {
sonali@215
   151
        if(node == null){
sonali@215
   152
            //in case of embedded Schema, the component is a SchemaComponent
sonali@215
   153
            //The NodeFactory returns a null node. use the TreeElementFactory as the UIHelper 
sonali@215
   154
            // to locate the factory that handles schema components
sonali@215
   155
           TreeElement elem = TreeElementFactory.getTreeElement(comp);
sonali@215
   156
           return elem.getText(true);
sonali@215
   157
        }
sonali@175
   158
        return node.getHtmlDisplayName();
sonali@175
   159
    }
sonali@175
   160
sonali@175
   161
    public void performChange() {
sonali@175
   162
    }
sonali@175
   163
sonali@175
   164
   public PositionBounds getPosition() {
sonali@371
   165
      if(comp.getModel() instanceof AbstractDocumentModel ) {
sonali@371
   166
          DocumentModelAccess docAcc =null;
sonali@371
   167
          Element elem = null;
sonali@371
   168
          int startPos = 0;
sonali@371
   169
          if(comp instanceof SchemaComponent)
sonali@371
   170
              docAcc = ((AbstractDocumentModel)model).getAccess();
sonali@371
   171
          else
sonali@371
   172
              docAcc  = ((AbstractDocumentModel)comp.getModel()).getAccess();
sonali@371
   173
             
sonali@371
   174
          elem = ((DocumentComponent)comp).getPeer();
sonali@371
   175
          startPos = ((DocumentComponent)comp).findPosition();
sonali@371
   176
           
sonali@371
   177
          String txt = docAcc.getXmlFragmentInclusive(elem);
sonali@371
   178
          int endPos = startPos + txt.length();
sonali@371
   179
          DataObject dob = null;
sonali@371
   180
          try {
sonali@371
   181
              FileObject source = (FileObject)comp.getModel().getModelSource().getLookup().lookup(FileObject.class);
sonali@371
   182
              dob = DataObject.find(source);
sonali@371
   183
           } catch (DataObjectNotFoundException ex) {
sonali@371
   184
              ex.printStackTrace();
sonali@371
   185
           }
sonali@371
   186
           CloneableEditorSupport ces = SharedUtils.findCloneableEditorSupport(dob);
sonali@371
   187
           if(ces == null)
sonali@371
   188
                return null;
sonali@371
   189
        
sonali@371
   190
           PositionRef ref1 = ces.createPositionRef(startPos, Bias.Forward);
sonali@371
   191
           PositionRef ref2 = ces.createPositionRef(endPos, Bias.Forward);
sonali@371
   192
           PositionBounds bounds = new PositionBounds(ref1, ref2);
sonali@371
   193
       
sonali@371
   194
           return bounds;
sonali@371
   195
       }else 
sonali@371
   196
           return null;
sonali@371
   197
       
sonali@175
   198
    }
sonali@175
   199
    
sonali@175
   200
    public void openInEditor(){
sonali@175
   201
         Action preferredAction = SystemAction.get(ShowSourceAction.class);
sonali@175
   202
         String command = (String)preferredAction.getValue(Action.ACTION_COMMAND_KEY);
sonali@215
   203
         if(node == null) {
sonali@215
   204
             node  = new AbstractNode(Children.LEAF);
sonali@215
   205
         }
supernikita@1305
   206
         ActionEvent ae = new ActionEvent(node, 0, command);
supernikita@1305
   207
         preferredAction.actionPerformed(ae);
sonali@175
   208
     }
sonali@215
   209
    
sonali@318
   210
    void addTransactionObject(XMLRefactoringTransaction transaction) {
sonali@318
   211
        this.transaction = transaction;
sonali@318
   212
    }
sonali@318
   213
    
sonali@318
   214
    protected String getNewFileContent() {
sonali@318
   215
       if(comp.getModel() instanceof AbstractDocumentModel && transaction != null) {
sonali@318
   216
             try {
sonali@318
   217
                 
sonali@318
   218
                String refactoredString = transaction.refactorForPreview(comp.getModel());
sonali@318
   219
                return refactoredString;
sonali@318
   220
            } catch (Exception ex) {
sonali@318
   221
                Exceptions.printStackTrace(ex);
sonali@318
   222
               return null;
sonali@318
   223
           }
sonali@318
   224
         }
sonali@318
   225
         return null;
sonali@318
   226
    }
sonali@175
   227
}