18 Replies Latest reply on Jul 23, 2009 11:14 PM by zakabalt

    RemoteObject Flex -> Java

    zakabalt Level 1

      Hi, I'm using Flex + Java in my applicatio and I had some problems to use RemoteObject between Java and Flex but I have solucinated this problems. Now I want to change one think in Java when I do one think in Flex. I have a tree and I delete, rename, add folders in Flex, but I have to say to Java, I have delete this folder. But i don't know how can I do this comunnication. Is the same Java-Flex or Flex-Java.

       

      I have this function for example:

       

      private function deleteFolder():void {
                   var node:XML = XML(newTree.selectedItem);
                   if( node == null ) return;
                   var children:XMLList = XMLList(node.parent()).children();
                   for(var i:Number=0; i < children.length(); i++) {
                       if( children[i].@label == node.@label ) {
                           delete children[i];
                       }
                   }
               }

       

      And I want to call to my Java method to say, we have delete this folder, remove you also.

       

      Here mi Java method. I recibe which folder I want to delete, but I don't now how use the RemoteObject to do that.

       

      public static void eliminarCarpeta(String foldername) throws MessagingException
          {
               Folder delfolder = store.getFolder(foldername);
              delfolder.delete(false);
          }

       

      To do Java-Flex I have this Remote Object

      <mx:RemoteObject id="srv" destination="conex" fault="Alert.show(event.fault.faultString), 'Error'">
                          <mx:method name="getFolder" result="treeLoadedResult(event)"/>

      But i don't know how is Flex-Java comunication.

       

      Thanks!

        • 1. Re: RemoteObject Flex -> Java
          AKloft Level 2

          You need to add a new <mx:Method> that calls eliminarCarpeta in the RemoteObject.  Then you need to call the method from somewhere in your script (deleteFolder):

           

          <mx:RemoteObject id="srv" destination="conex" fault="Alert.show(event.fault.faultString), 'Error'">
                              <mx:method name="getFolder" result="treeLoadedResult(event)"/>

                              <mx:Method name="eliminarCarpeta"/>

          </mx:RemoteObject>

           

           

          private function deleteFolder():void {
                       var node:XML = XML(newTree.selectedItem);
                       if( node == null ) return;
                       var children:XMLList = XMLList(node.parent()).children();
                       for(var i:Number=0; i < children.length(); i++) {
                           if( children[i].@label == node.@label ) {
                               delete children[i];

                               srv.eliminarCarpeta(children[i].@label); //possibly
                           }
                       }
                   }

          • 2. Re: RemoteObject Flex -> Java
            zakabalt Level 1

            Ok, I'm sorry, I thought I had tried that and it did not work, but you're right, now works. Now I have another question, a little bit more complicated. I have two functions. The first one is the same.

             

            deleteFolder:

             

            private function deleteFolder():void {
                         var node:XML = XML(newTree.selectedItem);
                         if( node == null ) return;
                         var children:XMLList = XMLList(node.parent()).children();
                         for(var i:Number=0; i < children.length(); i++) {
                             if( children[i].@label == node.@label ) {
                                 delete children[i];
                                 srv.deleteFolder('INBOX.Name.jj.oo'); //the folder that I want delete
                             }
                         }
                     }

            And the other function to get the string, in this case 'INBOX.Name.jj.oo", but i want to change, depend of in which node I do click.

             

            getRuta:

             

            private function getRuta(event:ListEvent):String {
                         var item:Object = event.currentTarget.selectedItem;
                          var ruta:String=item.@label
                          while (true){
                               if (newTree.getParentItem(item) != null){
                                   item=newTree.getParentItem(item)
                                ruta = item.@label + "." + ruta;
                               }else {break;}
                           }
                           trace('Ruta '+ ruta);
                          return ruta;
                    }

             

            This function give me one String with my tree node complete route

             

            I use this function to execute the function:

             

            <mx:Button x="286" y="236" label="Delete Folder" click="deleteFolder()"/>

             

            But i can't convinate the two functions, because I can't use event in Button, because one event is MouseEvent and the other ListEvent.

             

            How can I do to tell to one conbined function with one button. First get the route and the use this route to send to java method.

             

            Thanks

            • 3. Re: RemoteObject Flex -> Java
              AKloft Level 2

              So you are wanting to do the following:

               

              getRuta -- get the route of the tree/file pat

              which will then call -- deleteFolder(String returnFromGetRuta)

               

              Is that correct?

              • 4. Re: RemoteObject Flex -> Java
                zakabalt Level 1

                Yes, but I don't know how can I do click="deleteFolder(....)"

                 

                I have to declarate ruta like a global variable and [Bindable]? Or exist another form to do that.

                • 5. Re: RemoteObject Flex -> Java
                  AKloft Level 2

                   

                  private function deleteFolder(filePath:String):void {
                               var node:XML = XML(newTree.selectedItem);
                               if( node == null ) return;
                               var children:XMLList = XMLList(node.parent()).children();
                               for(var i:Number=0; i < children.length(); i++) {
                                   if( children[i].@label == node.@label ) {
                                       delete children[i];
                                       srv.deleteFolder(filePath); //the folder that I want delete
                                   }
                               }
                           }

                  //this is what is returning INBOX.Name.jj.oo

                  private function getRuta(event:ListEvent):String{

                       var item:Object = event.currentTarget.selectedItem;

                       var ruta:String = item.@label;

                       while (true){

                            if (newTree.getParentItem(item) != null){
                                  item=newTree.getParentItem(item)
                                  ruta = item.@label + "." + ruta;
                            }else {break;}

                       }

                       trace('Ruta' + ruta);

                       deleteFolder(ruta);

                       return ruta;

                  }

                   

                  <mx:Button x="286" y="236" label="Delete Folder" click="getRuta(...)"/>

                   

                   

                  instead of calling "deleteFolder" in your button's click event, try calling getRuta to get the file path, then within getRuta you can call deleteFolder(filepath:String) (see above).  Or better yet (maybe), create a new function that does the following:

                   

                  private function getAndDeleteFolder(event:ListEvent):void{

                       var folderPath:String = getRuta(event);

                       deleteFolder(folderPath);

                  }

                   

                  Let me know if this works and if you need anything else

                  • 6. Re: RemoteObject Flex -> Java
                    zakabalt Level 1

                    I understand this code and I do something similar before, but how do you do to call to getRuta function in the button? I have to use a button, and I can't call to getRuta(event) (because this event is a mouse event not a list event). I don't know another form to do that. I understand all, my problem is what put it <mx:Button click""/>

                    • 7. Re: RemoteObject Flex -> Java
                      AKloft Level 2

                      You may not need the event in getRuta if you can access the tree's selected item by calling:

                       

                      getRuta(treeName.selectedItem);

                       

                      or some variation of that, and that would go in your click event.  Or remove the parameter from getRuta and put the treeName.selectedItem into the item var.

                       

                      Does that make sense?  That was a stream of my quick thinking thoughts.

                      • 8. Re: RemoteObject Flex -> Java
                        zakabalt Level 1

                        Probably your right and your solution is easy, but I don't understand very good. I will explain again somethings ok?

                         

                        I have to use one button to deleteFolder.

                         

                        This button have to delete the item that I have selected in the tree. (using ListEvent I think)

                         

                        I can't delete the node when I do click in the item. I have to select the node and then click in delete button.(the two things).

                         

                        I repeat, probably you're right but I don't understand. If you're sure that your solution is correct  I will understand better if you put it the complete code (the two funtions and the button click..).

                         

                        Thanks again!

                        • 9. Re: RemoteObject Flex -> Java
                          AKloft Level 2

                          No worries.  Hopefully this code gives you a better understanding.  I added some comments to help you see my understanding of your problem.  Hope it helps/is correct to meet your needs!

                           

                          //Function name: deleteFolder

                          //Function purpose: delete a folder from the filesystem

                          //Function parameters: filePath:String - the path to the folder that is to be deleted

                           

                          private function deleteFolder(filePath:String):void {
                                       var node:XML = XML(newTree.selectedItem);
                                       if( node == null ) return;
                                       var children:XMLList = XMLList(node.parent()).children();
                                       for(var i:Number=0; i < children.length(); i++) {
                                           if( children[i].@label == node.@label ) {
                                               delete children[i];
                                               srv.deleteFolder(filePath); //the folder that I want deleted
                                           }
                                       }
                                   }

                          //Function name: getRuta

                          //Function purpose: return the path of the currently selected item in the event.currentTarget tree structure

                           

                          private function getRuta():String{

                               var item:Object = newTree.selectedItem; //newTree is whatever the user must click on before clicking button

                               var ruta:String = item.@label;

                               while (true){

                                    if (newTree.getParentItem(item) != null){
                                          item=newTree.getParentItem(item)
                                          ruta = item.@label + "." + ruta;
                                    }else {break;}

                               }

                               trace('Ruta' + ruta);

                               deleteFolder(ruta);

                               return ruta;

                          }

                           

                          private function getAndDeleteFolder():void{

                               var path:String = getRuta();

                               deleteFolder(path);

                          }

                           

                          <mx:Button x="286" y="236" label="Delete Folder" click="getAndDeleteFolder()"/>

                          • 10. Re: RemoteObject Flex -> Java
                            zakabalt Level 1

                            edited, because I have written before read your code

                            • 11. Re: RemoteObject Flex -> Java
                              AKloft Level 2

                              Here's a bit of a use case for you to see how the functions are being called:

                               

                              1. User selects the folder they wish to delete from the tree.  This sets the newTree.selectedItem value

                              2. User selects the Delete Button

                              3. An event is triggered called Click which calls "getAndDeleteFolder()"

                              4. This function first calls getRuta() and stores the result in variable path of type String

                              5. The getAndDeleteFolder() function then calls deleteFolder(...) passing in the variable path as the parameter/argument

                              6. deleteFolder(...) then proceeds to delete the folder and contents within it. (you may need to move srv.deleteFolder(filePath) outside of the for loop)

                              1 person found this helpful
                              • 12. Re: RemoteObject Flex -> Java
                                zakabalt Level 1

                                Perfect, thanks.

                                 

                                I had understood bad one thing, but now works perfect.

                                 

                                Thanks for all!

                                • 13. Re: RemoteObject Flex -> Java
                                  zakabalt Level 1

                                  This is the function to delete, but I see that you know a lot trees and I have another question. I want to add a node like a child in one seleccted node. Like to delete but to add. Get the route and add a child. I have a function to add a node but I add in the last of the list, I don't know how do to add like a selected node child.

                                   

                                  My code(to add in the list, but not like a selected node child:

                                   

                                  private function addFolder():void { 
                                              var newNode:XML = <node/>;
                                                  newNode.@label = folderName.text;// lo consigo desde un textInput
                                                  var dept:XMLList = xmlTree.Cuenta.(@id == "cuenta"+idCuenta);//nos da cuentaX
                                                  if( dept.length() > 0 ) {
                                                      dept[0].appendChild(newNode);
                                                      folderName.text = "";
                                                  }
                                           }
                                  • 14. Re: RemoteObject Flex -> Java
                                    AKloft Level 2

                                    No problem.  If you just want to append a folder to the currently selectedItem, you could try instead of doing:

                                     

                                    var dept: XMLList = xmlTree.Cuenta.(@id == "cuenta"+idCuenta);

                                    if (dept.length() > 0){

                                         dept[0].appendChild(newNode);

                                         folderName.text = "";

                                    }

                                     

                                    try:

                                    var currentSelectedItem: XMLList = xmlTree.selectedItem;
                                    currentSelectedItem.appendChild(newNode);
                                    folderName.text = ""; //clear text from the text input
                                    Hope that helps
                                    • 15. Re: RemoteObject Flex -> Java
                                      zakabalt Level 1

                                      I have tryed this code and didn't work. The error is: The method appendChild only works when the list have only one item(or something similar I'm traducing).

                                      • 16. Re: RemoteObject Flex -> Java
                                        AKloft Level 2

                                        What you can try is this:

                                         

                                        Assuming you have a dataProvider for the Tree structure, use this section of code:

                                         

                                        IList(xmlTree.dataProvider).addItemAt(nodeToAdd, indexOfParent+1);

                                         

                                        Where nodeToAdd is the new folder to insert, and indexOfParent is the xmlTree.selectedItem's index.

                                         

                                         

                                        source: http://www.actionscript.org/forums/showthread.php3?p=768785

                                         

                                        Not sure if that will work, but give it a try and tweak it as needed.

                                        • 17. Re: RemoteObject Flex -> Java
                                          zakabalt Level 1

                                          I can't get it and the problem is that I don't understand very good, if I understand good the structure I could try to change but I don't know. I have to improve my xml concepts. If you can, explain in another form, or put it the posible code or..

                                           

                                          I'm reading one book but now I don't know how can I do.

                                           

                                          Thanks

                                          • 18. Re: RemoteObject Flex -> Java
                                            zakabalt Level 1

                                            Hi, I have tried today again and it works with this code. Similar than you give me yesterday:

                                             

                                            private function addFolder(ruta:String):void {
                                                         var newNode:XML = <node/>;
                                                            newNode.@label = folderName.text;
                                                            newTree.selectedItem.appendChild(newNode);
                                                            ruta = ruta + '.' + String(folderName.text);
                                                            srv.addFolder(ruta);
                                                            folderName.text = ""; //clear text from the text input
                                            }

                                             

                                            Thanks!