6 Replies Latest reply on Nov 14, 2009 10:56 AM by mattcom

    Simple ID Question

    mattcom

      Ok, so this is probably very simple, but I have yet to find any documentation on it.  I would appreciate any insight you might bestow.

       

      If I have a code like this:

          <mx:Script>
              <![CDATA[
              import mx.core.DragSource;
              import mx.managers.DragManager;
              import mx.events.*;
              import mx.containers.Canvas;
              import mx.controls.Button;
                  [Bindable]
                  var editorVisibility:Boolean = false;
                  [Bindable]
                  var editingID:String = "";

                  private function saveHandler(event:Event):void {
                      // This works:  exampleBox1Text.htmlText = editorText.htmlText;
                      editorVisibility = false;
                     // How can I get this to work so that the Text with the ID of editingID will be saved with the editorText.htmlText??: 

                     // EX: editingID.htmlText = editorText.htmlText;
                  }

                  public function addSaveButton():void {
                      var but:Button = new Button();
                      but.label = "Save";
                      but.addEventListener("click",saveHandler);
                      editorText.toolbar.addChild(but);
                  }

                  private function openEditor(event:MouseEvent):void {
                      editorVisibility = true;
                      editorText.htmlText = event.currentTarget.htmlText;
                      editingID = event.currentTarget.id;
                      editorText.title = "Currently Editing: " + editingID;
                  }

              ]]>
          </mx:Script>

      <mx:Text width="100%" height="100%" id="exampleBox1Text" text="Text Goes Here" mouseWheel="openEditor(event)"/>

      <mx:Text width="100%" height="100%" id="exampleBox2Text" text="Text Goes Here" mouseWheel="openEditor(event)"/>

      <mx:RichTextEditor visible="{editorVisibility}" x="347" y="10" title="Title" id="editorText" initialize="addSaveButton()"/>

        • 1. Re: Simple ID Question
          Subeesh Arakkan Level 4

          Hi,

           

          Try this

           

          (getChildByName(editingID) as Text).htmlText = editorText.htmlText;

          1 person found this helpful
          • 2. Re: Simple ID Question
            mattcom Level 1

            No luck, but it did start me on a train of thought that might lead to the answer.

             

            This is the error I got from it.

             

            Error #1009: Cannot access a property or method of a null object reference

             

            I appreciate the suggestion!

            • 3. Re: Simple ID Question
              Subeesh Arakkan Level 4

              It worked for me. Here is the code I used for testing

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  width="780"
                      layout="vertical"
                      verticalAlign="middle"
                      backgroundColor="white">

               

              <mx:Script>
                      <![CDATA[
                      import mx.core.DragSource;
                      import mx.managers.DragManager;
                      import mx.events.*;
                      import mx.containers.Canvas;
                      import mx.controls.Button;
                          [Bindable]
                          var editorVisibility:Boolean = false;
                          [Bindable]
                          var editingID:String = "";
                          private function saveHandler(event:Event):void {
                              // This works:  exampleBox1Text.htmlText = editorText.htmlText;
                              editorVisibility = false;
                              (getChildByName(editingID) as Text).htmlText = editorText.htmlText;
                             // EX: *editingID.htmlText = editorText.htmlText;*
                          }
                          public function addSaveButton():void {
                              var but:Button = new Button();
                              but.label = "Save";
                              but.addEventListener("click",saveHandler);
                              editorText.toolbar.addChild(but);
                          }
                          private function openEditor(event:MouseEvent):void {
                              editorVisibility = true;
                              editorText.htmlText = event.currentTarget.htmlText;
                              editingID = event.currentTarget.id;
                              editorText.title = "Currently Editing: " + editingID;
                          }
                      ]]>
                  </mx:Script>
              <mx:Text width="100%" height="100%" id="exampleBox1Text" text="Text Goes Here" mouseWheel="openEditor(event)"/> <mx:Text width="100%" height="100%" id="exampleBox2Text" text="Text Goes Here" mouseWheel="openEditor(event)"/> <mx:RichTextEditor visible="{editorVisibility}" x="347" y="10" title="Title" id="editorText" initialize="addSaveButton()"/>

               


              </mx:Application>

              • 4. Re: Simple ID Question
                mattcom Level 1

                I see that your example is working fine.  I think the problem lies in the nesting of my label

                 

                 

                This is my complete application that I was getting the error on.

                 

                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
                    <mx:Script>
                        <![CDATA[
                        import mx.core.DragSource;
                        import mx.managers.DragManager;
                        import mx.events.*;
                        import mx.containers.Canvas;
                        import mx.controls.Button;
                            [Bindable]
                            var editorVisibility:Boolean = false;
                            [Bindable]
                            var editingID:String = "";
                            private function saveHandler(event:Event):void {
                                //exampleBox1Text.htmlText = editorText.htmlText;
                                editorVisibility = false;
                               (getChildByName(editingID) as Text).htmlText  = editorText.htmlText;
                            }
                            private function mouseMoveHandler(event:MouseEvent):void {
                                // Get the drag initiator component from the event object.
                                var dragInitiator:Panel = Panel(event.currentTarget);
                               
                                // Get the color of the drag initiator component.
                                var dragColor:int = dragInitiator.getStyle('backgroundColor');
                   
                                // Create a DragSource object.
                                var ds:DragSource = new DragSource();
                   
                                // Add the data to the object.
                                ds.addData(dragColor, 'color');
                   
                                // Call the DragManager doDrag() method to start the drag.
                                DragManager.doDrag(dragInitiator, ds, event);
                            }
                            private function dragDropHandler(event:MouseEvent):void {
                                var dropCanvas:Canvas = event.currentTarget as Canvas;
                                exampleBox1.x = stageCanvas.mouseX;
                                exampleBox1.y = stageCanvas.mouseY;
                                dropCanvas.addChild(exampleBox1);
                            }
                            private function dragOverHandler(event:DragEvent):void
                            {
                               if (event.ctrlKey) {                   
                                        DragManager.showFeedback(DragManager.COPY);
                                        return;
                               } else {
                                        DragManager.showFeedback(DragManager.MOVE);
                                        return;
                               }
                                DragManager.showFeedback(DragManager.NONE);
                            }
                            private function dragEnterHandler(event:DragEvent):void {
                                DragManager.acceptDragDrop(Canvas(event.currentTarget));
                            }
                            private function dragCompleteHandler(event:DragEvent):void {
                                if (event.action == DragManager.MOVE) {
                               
                                 }
                            }
                            private function openEditor(event:MouseEvent):void {
                                editorVisibility = true;
                                editorText.htmlText = event.currentTarget.htmlText;
                                editingID = event.currentTarget.id;
                                editorText.title = "Currently Editing: " + editingID;
                            }
                            public function addSaveButton():void {
                                var but:Button = new Button();
                                but.label = "Save";
                                but.addEventListener("click",saveHandler);
                                editorText.toolbar.addChild(but);
                            }

                 

                           
                        ]]>
                    </mx:Script>
                    <mx:Canvas id="stageCanvas" width="100%" height="100%" backgroundColor="#FFFFFF"
                        dragEnter="dragEnterHandler(event)"
                        dragOver="dragOverHandler(event);"
                        dragDrop="dragDropHandler(event);">
                        <mx:RichTextEditor visible="{editorVisibility}" x="347" y="10" title="Title" id="editorText" initialize="addSaveButton()"/>
                        <mx:Panel title="Example Box 1" id="exampleBox1" x="31" y="10" width="250" height="200" layout="absolute"
                            mouseDown="mouseMoveHandler(event)"
                            dragComplete="dragCompleteHandler(event);"
                              >
                            <mx:Text width="100%" height="100%" id="exampleBox1Text" text="Text Goes Here" mouseWheel="openEditor(event)"/>
                        </mx:Panel>
                        <mx:Panel  title="Example Box 2" id="exampleBox2" x="31" y="210" width="250" height="200" layout="absolute"
                            mouseDown="mouseMoveHandler(event)"
                            dragComplete="dragCompleteHandler(event)"
                              >
                            <mx:Text width="100%" height="100%" id="exampleBox2Text" text="Text Goes Here" mouseWheel="openEditor(event)"/>
                        </mx:Panel>
                    </mx:Canvas>
                </mx:Application>

                • 5. Re: Simple ID Question
                  Subeesh Arakkan Level 4

                  Hi,

                   

                  getChildByName wont work in your example as Text controls are inside Panel containers. So I  suggest to save reference to the Text control rather than ID. I modified your code a little and it is working

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
                      <mx:Script>
                          <![CDATA[
                          import mx.core.DragSource;
                          import mx.managers.DragManager;
                          import mx.events.*;
                          import mx.containers.Canvas;
                          import mx.controls.Button;
                              [Bindable]
                              var editorVisibility:Boolean = false;
                              [Bindable]
                              var editingText:Text;
                              private function saveHandler(event:Event):void {
                                  //exampleBox1Text.htmlText = editorText.htmlText;
                                  editorVisibility = false;
                                 editingText.htmlText  = editorText.htmlText;
                              }
                              private function mouseMoveHandler(event:MouseEvent):void {
                                  // Get the drag initiator component from the event object.
                                  var dragInitiator:Panel = Panel(event.currentTarget);
                                 
                                  // Get the color of the drag initiator component.
                                  var dragColor:int = dragInitiator.getStyle('backgroundColor');
                     
                                  // Create a DragSource object.
                                  var ds:DragSource = new DragSource();
                     
                                  // Add the data to the object.
                                  ds.addData(dragColor, 'color');
                     
                                  // Call the DragManager doDrag() method to start the drag.
                                  DragManager.doDrag(dragInitiator, ds, event);
                              }
                              private function dragDropHandler(event:MouseEvent):void {
                                  var dropCanvas:Canvas = event.currentTarget as Canvas;
                                  exampleBox1.x = stageCanvas.mouseX;
                                  exampleBox1.y = stageCanvas.mouseY;
                                  dropCanvas.addChild(exampleBox1);
                              }
                              private function dragOverHandler(event:DragEvent):void
                              {
                                 if (event.ctrlKey) {                   
                                          DragManager.showFeedback(DragManager.COPY);
                                          return;
                                 } else {
                                          DragManager.showFeedback(DragManager.MOVE);
                                          return;
                                 }
                                  DragManager.showFeedback(DragManager.NONE);
                              }
                              private function dragEnterHandler(event:DragEvent):void {
                                  DragManager.acceptDragDrop(Canvas(event.currentTarget));
                              }
                              private function dragCompleteHandler(event:DragEvent):void {
                                  if (event.action == DragManager.MOVE) {
                                 
                                   }
                              }
                              private function openEditor(event:MouseEvent):void {
                                  editorVisibility = true;
                                  editorText.htmlText = event.currentTarget.htmlText;
                                  editingText = event.currentTarget as Text;
                                  editorText.title = "Currently Editing: " + event.currentTarget.id;
                              }
                              public function addSaveButton():void {
                                  var but:Button = new Button();
                                  but.label = "Save";
                                  but.addEventListener("click",saveHandler);
                                  editorText.toolbar.addChild(but);
                              }


                          ]]>
                      </mx:Script>
                      <mx:Canvas id="stageCanvas" width="100%" height="100%" backgroundColor="#FFFFFF"
                          dragEnter="dragEnterHandler(event)"
                          dragOver="dragOverHandler(event);"
                          dragDrop="dragDropHandler(event);">
                          <mx:RichTextEditor visible="{editorVisibility}" x="347" y="10" title="Title" id="editorText" initialize="addSaveButton()"/>
                          <mx:Panel title="Example Box 1" id="exampleBox1" x="31" y="10" width="250" height="200" layout="absolute"
                              mouseDown="mouseMoveHandler(event)"
                              dragComplete="dragCompleteHandler(event);"
                                >
                              <mx:Text width="100%" height="100%" id="exampleBox1Text" text="Text Goes Here" mouseWheel="openEditor(event)"/>
                          </mx:Panel>
                          <mx:Panel  title="Example Box 2" id="exampleBox2" x="31" y="210" width="250" height="200" layout="absolute"
                              mouseDown="mouseMoveHandler(event)"
                              dragComplete="dragCompleteHandler(event)"
                                >
                              <mx:Text width="100%" height="100%" id="exampleBox2Text" text="Text Goes Here" mouseWheel="openEditor(event)"/>
                          </mx:Panel>
                      </mx:Canvas>
                  </mx:Application>

                  • 6. Re: Simple ID Question
                    mattcom Level 1

                    Subeesh,

                     

                    I can't thank you enough!  You've been extremely helpful!  Thanks for everything!