This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 50222 - Operation invocation is appended to the end of the method
Summary: Operation invocation is appended to the end of the method
Status: VERIFIED FIXED
Alias: None
Product: webservices
Classification: Unclassified
Component: Code (show other bugs)
Version: 4.x
Hardware: All All
: P2 blocker (vote)
Assignee: Martin Grebac
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-10-11 13:42 UTC by Petr Blaha
Modified: 2005-03-24 14:05 UTC (History)
3 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Petr Blaha 2004-10-11 13:42:59 UTC
[Build 041011]
Originally reported by pcw, see #50096:
When you add web service method invocation in
servlet source, the try/catch containing the
operation invocation is appended to the end of the
method body in which this feature was invoked.
Comment 1 Petr Blaha 2004-10-13 10:39:55 UTC
Remove all issues from temporary component.
Comment 2 Lukas Jungmann 2005-02-10 17:11:35 UTC
I'm increasing priority to P2 because this behaviour is really very
annoying when user wants to work with web service clients (call web
service operations) and because this issue is here for a long time.
Comment 3 _ pcw 2005-03-07 15:57:49 UTC
At invocation of this action, the edit caret is located at the point
where we should insert the portion of the code applicable to the
calling location.  But there is no way (that I know) of to do this
with srcmodel api.  So perhaps the javamodel api is more detailed, or
maybe there is a way to pass the block of "text-to-be-inserted" to a
paste action.
Comment 4 _ rkubacki 2005-03-07 17:21:24 UTC
Sure there is a way how to do it through new Java APIs but still there
are some rough places. It might be difficult to find the caret
position in j2ee (and adjust it to position where you can add a
command). Or you need a utility (preferably in Java modules) that will
do it for you.
Comment 5 Petr Jiricka 2005-03-10 16:53:47 UTC
Not sure if I completely understand this issue, but in order to find out the
current position in the text, something like the following should work:

cloneableEditorSupport.getOpenedPanes[0].getCaret().getDot()

Or is the problem something else?
Comment 6 Miloslav Metelka 2005-03-10 17:19:56 UTC
What Petr has mentioned should work. Instead of cloneableEditorSupport there
could be DataObject.getCookie(EditorCookie.class) as well to be more generic.
Comment 7 _ pcw 2005-03-10 18:05:20 UTC
Let me clarify the problem space.  The user right clicks in the editor, selects
<invoke web service operation> off the popup menu and we need to insert some
support code plus the method call itself.

The method call goes at the caret location, surrounded by a try catch block.  If
we had detailed java information about the code structure surrounding the
current location we could really get into carefully injecting the try/catch
block and method call at reasonable points surrounding, but not actually at the
caret location.  But this is very complicated, cannot be done with srcmodel api,
and may not be possible even with javamodel api - I don't know enough about that
one to evaluate it.

For 4.1 I think we should assume the user right clicked at a good point for us
to insert the entire block (e.g. on a blank line at the correct space in their
method, or something like that) and to insert the new block at the caret location.

So if I have a block of text, and the editor caret is at the location where it
should go, what do I write to paste the block at the caret location?
Comment 8 Miloslav Metelka 2005-03-11 11:03:48 UTC
It depends whether the modification that you plan to do is purely textual i.e.
you know the piece of text that you want to insert or whether you plan to build
a statement through the JMI interfaces. Let's assume that you have the exact
text that you want to insert. You now need to:
1) Find the appropriate place near the caret where to insert the text.
2) Format the text prior insertion to be properly indented.
3) Insert the text into the document.

Ad 1) Assuming that you have a DataObject of the given source you can do:
  EditorCookie ec = (EditorCookie)dataObject.getCookie(EditorCookie.class);
  JEditorPane pane = ec.getOpenedPanes[0];
  int caretOffset = pane.getCaretPosition();

  Now you could use JMI information to better position in the text. You can look
e.g. into java/editor module in NB repository - there is
org.netbeans.modules.editor.java.SelectCodeElementAction which extends/shrinks
selection based on the syntactic elements of the java source. Basically you need
to start an MDR transaction:

    JMManager manager = (JMManager)JMManager.getManager();
    MDRepository repository = JavaModel.getJavaRepository();
    repository.beginTrans(true); // write access to enforce reparsing if necessary
    try {

        ...statements...

    } finally {
        repository.endTrans();
    }

Into the statements section you can put:

    int targetOffset = caretOffset; // target insertion point
    org.netbeans.jmi.javamodel.Element elem =
manager.getElementByOffset(dataObject.getPrimaryFile(), caretOffset);
    while (!(elem instanceof org.netbeans.jmi.javamodel.Statement)) {
        elem = (Element)elem.refImmediateComposite(); // go through parents
    }
    if (elem instanceof org.netbeans.jmi.javamodel.Statement)) {
        targetOffset = elem.getEndOffset(); // statement boundary
    }


ad 2)
To format the text prior insertion please use:

    Document doc = pane.getDocument();
    IndentEngine eng = IndentEngine.find(doc);
    StringWriter textWriter = new StringWriter();
    Writer indentWriter = eng.createWriter(doc, targetOffset, textWriter);
    indentWriter.write("statement-code");
    String textToInsert = textWriter.toString();

ad 3)
To insert the formatted text:
    doc.insertString(targetOffset, textToInsert, null);


This should generally work (I have omitted catching of certain exceptions e.g.
BadLocationException, DataObjectNotFoundException etc.). Please let me know if
you need further information. Adding Martin Matula to cc whether he wants to
speak up regarding JMI related information etc.
    
Comment 9 Martin Grebac 2005-03-23 15:04:00 UTC
FINALLY fixed in trunk:

Checking in
websvc/core/src/org/netbeans/modules/websvc/core/client/actions/Bundle.properties;
/cvs/websvc/core/src/org/netbeans/modules/websvc/core/client/actions/Bundle.properties,v
 <--  Bundle.properties
new revision: 1.4; previous revision: 1.3
done
Checking in
websvc/core/src/org/netbeans/modules/websvc/core/client/actions/InvokeOperationAction.java;
/cvs/websvc/core/src/org/netbeans/modules/websvc/core/client/actions/InvokeOperationAction.java,v
 <--  InvokeOperationAction.java
new revision: 1.8; previous revision: 1.7
done
Checking in
websvc/dev/src/org/netbeans/modules/websvc/dev/wizard/CreateWebServiceFromExistingCodePanel.java;
/cvs/websvc/dev/src/org/netbeans/modules/websvc/dev/wizard/CreateWebServiceFromExistingCodePanel.java,v
 <--  CreateWebServiceFromExistingCodePanel.java
new revision: 1.4; previous revision: 1.3
done
Checking in websvc/dev/build.xml;
/cvs/websvc/dev/build.xml,v  <--  build.xml
new revision: 1.7; previous revision: 1.6
done
Checking in websvc/core/build.xml;
/cvs/websvc/core/build.xml,v  <--  build.xml
new revision: 1.16; previous revision: 1.15
done
Checking in ide/golden/deps.txt;
/cvs/ide/golden/deps.txt,v  <--  deps.txt
new revision: 1.52; previous revision: 1.51
done
Checking in websvc/dev/nbproject/project.properties;
/cvs/websvc/dev/nbproject/project.properties,v  <--  project.properties
new revision: 1.2; previous revision: 1.1
done
Checking in websvc/dev/nbproject/project.xml;
/cvs/websvc/dev/nbproject/project.xml,v  <--  project.xml
new revision: 1.12; previous revision: 1.11
done
Checking in websvc/core/nbproject/project.properties;
/cvs/websvc/core/nbproject/project.properties,v  <--  project.properties
new revision: 1.4; previous revision: 1.3
done
Checking in websvc/core/nbproject/project.xml;
/cvs/websvc/core/nbproject/project.xml,v  <--  project.xml
new revision: 1.16; previous revision: 1.15
done

Comment 10 Lukas Jungmann 2005-03-24 14:05:30 UTC
great :)

v. 200503231900