Author Topic: Subclassing TreeBoxNode  (Read 5455 times)

Offline bitli

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 513
Subclassing TreeBoxNode
« on: 2010 April 17 08:03:02 »
I would like to subclass TreeBoxNode, so I can add some referene to it.
I tried various standard javascript method without sucess. The I copied similar code from other script that were refering to Dialog.
This results in:
Code: [Select]
function FileToAlignTreeBoxNode(parent) {
   this.__base__ = TreeBoxNode;
   this.__base__(parent);
}
FileToAlignTreeBoxNode.prototype = new TreeBoxNode();
However code like:
Code: [Select]
var node = new FileToAlignTreeBoxNode( this.dialog.files_TreeBox );
node.setText( 0, fn );
results in error as setText is not recognized.

Also __base__ is not "documented" in Object.

Any clue?
-- bitli





Offline bitli

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 513
Re: Subclassing TreeBoxNode
« Reply #1 on: 2010 April 19 00:27:25 »
To provide some context, I made a script to star align and integrate images in one step.

I present the list of files to align in a treeboxnode (shamelessly getting inspiration of other scripts). However I would like to show only the file name (or at least to truncate it) so it fits in the window. But I need to keep the full file name somewhere.I did not see any "handle" fits on tree node I could use. Adding a fiel on tree node does not seem to work (it is always undefine). "Subclassing" it à la Javascript does not seems to work either.  Keeping a separate list is not very convenient because the nodes may be sorted, so the list is not in order anymore.

So anyway to attach some information to a tree node is welcome.

I suppose the Javascrip implementation come from a well known existing one. Would it be possibel to know which one so that we can dig for information on non PI specific matters ?

-- bitli

Offline Carlos Milovic

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2172
  • Join the dark side... we have cookies
    • http://www.astrophoto.cl
Re: Subclassing TreeBoxNode
« Reply #2 on: 2010 April 19 04:12:05 »
Sorry, I'm not involved in PJSR development. Let's wait for Juan's answer.
Regards,

Carlos Milovic F.
--------------------------------
PixInsight Project Developer
http://www.pixinsight.com

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: Subclassing TreeBoxNode
« Reply #3 on: 2010 April 20 02:15:01 »
Hi Bitli, and sorry for the delayed answer. These days we are involved in some new (and exciting!) things, which are so demanding.

Let's start with the last of your questions:

Quote
I suppose the Javascrip implementation come from a well known existing one. Would it be possibel to know which one so that we can dig for information on non PI specific matters ?

PixInsight's JavaScript engine is Mozilla's SpiderMonkey version 1.8 RC1 (the latest officially released version). I am working on an adaptation of TraceMonkey version 1.9, which is the version included in Firefox 3.6. This version includes a JIT compiler, so hopefully it will provide a significant speed boost to JavaScript scripts in PixInsight. TraceMonkey will be available somewhere during the 1.6.x cycle.

Quote
__base__ is not "documented" in Object.

__base__ is just a notational convention that I use. It could be any valid identifier, but I use __base__ to avoid conflicts with other property and function names, and because it seems quite self-descriptive. I don't remember where/when/who, but I know I saw it being used by someone and I liked it, and I use it since then.

In a construct such as:

Code: [Select]
function ADerivedObject( parent )
{
   this.__base__ = AParentObject;
   if ( parent )
      this.__base__( parent );
   else
      this.__base__();

   ...
}

I use __base__ to force ADerivedObject to have a property of type AParentObject. Then I execute AParentObject's constructor inside ADerivedObject (the call to this.__base__()). By doing that I force ADerivedObject to inherit all methods and properties from AParentObject. This is a standard inheritance implementation technique in JavaScript, where as you know object orientation is prototype-based, in contrast to other OO languages such as Java and C++, where the OO model is class-centric.

Quote
results in error as setText is not recognized.

Hmm. Your code seems correct, and it works fine for me. This is the proof of concept:

Code: [Select]
function FileToAlignTreeBoxNode( parent )
{
   this.__base__ = TreeBoxNode;
   this.__base__( parent );

   this.serendipitously = function()
   {
      console.writeln( "serendipity!" );
   };

   this.foo = "foo";
}

FileToAlignTreeBoxNode.prototype = new TreeBoxNode();

var tree = new TreeBox;

var node = new FileToAlignTreeBoxNode( tree );
node.setText( 0, "test" );

console.show();

console.writeln( node.text( 0 ) ); // should write "test"
console.writeln( node.foo ); // before assignment: "foo"
node.foo = "bar";
console.writeln( node.foo ); // after assignment: "bar"
node.serendipitously();

Let me know if this code works as expected; I've just checked and it does work fine on PI 1.6.  :angel:
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline bitli

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 513
Re: Subclassing TreeBoxNode
« Reply #4 on: 2010 April 27 01:22:01 »
Thanks for the info. I just came back from a one week trip to see that one computer had a drive dead, so it will take me some time to repair and test again.  In the meantime I did just put the full path in the tooltip, which is convenient anyhow.
-- bitli