Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » Global variables per Editor
Global variables per Editor [message #100254] Thu, 16 October 2003 14:57 Go to next message
Eclipse UserFriend
Originally posted by: brian.fernandes.codito.com

I want to be able to number each of the models in my Editor. ie. Assign each
of them a unique index. So I need a global counter maintained for each
Editor - every time I create a new object, I'll assign it an index and
increment the global index. (Obviously I want the numbering to be
independent across the Editors)

Since the Contents of the graphical viewer is unique to each Editor, I
thought of maintaining the counter here. This means that I would have to
pass the instance of the Contents model to every model created in the
editor. I passed it in the constructor of each model - seems to be okay but
then I have problems with SimpleFactory. It creates new objects with
type.newInstance() and I can't seem to be able to pass the Contents model
here. Also, when the SimpleFactory classes are instantiated, the contents
model does not exist.

My java is rusty and so I'm sure I'm going about this pretty badly. Is there
a better way to achieve this ? I could use a static variable but then it
wouldn't be unique across the Editors. The only other unique element per
Editor is the Editor itself.... any way I can access that from the models ?

Thanks,
Brian.
Re: Global variables per Editor [message #100360 is a reply to message #100254] Thu, 16 October 2003 20:16 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: felix.mayer.comcast.net

What are 'Contents' and 'SimpleFactory'?

"Brian Fernandes" <brian.fernandes@codito.com> wrote in message
news:bmmarv$oot$1@eclipse.org...
> I want to be able to number each of the models in my Editor. ie. Assign
each
> of them a unique index. So I need a global counter maintained for each
> Editor - every time I create a new object, I'll assign it an index and
> increment the global index. (Obviously I want the numbering to be
> independent across the Editors)
>
> Since the Contents of the graphical viewer is unique to each Editor, I
> thought of maintaining the counter here. This means that I would have to
> pass the instance of the Contents model to every model created in the
> editor. I passed it in the constructor of each model - seems to be okay
but
> then I have problems with SimpleFactory. It creates new objects with
> type.newInstance() and I can't seem to be able to pass the Contents model
> here. Also, when the SimpleFactory classes are instantiated, the contents
> model does not exist.
>
> My java is rusty and so I'm sure I'm going about this pretty badly. Is
there
> a better way to achieve this ? I could use a static variable but then it
> wouldn't be unique across the Editors. The only other unique element per
> Editor is the Editor itself.... any way I can access that from the models
?
>
> Thanks,
> Brian.
>
>
Re: Global variables per Editor [message #100537 is a reply to message #100254] Fri, 17 October 2003 18:13 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jwoods.journee.com

This is a multi-part message in MIME format.

------=_NextPart_000_0031_01C394B0.85D21B00
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Your terminology is somewhat confusing, but I think I get the jist of =
what you're wanting.

One solution is to have a name/id factory attached to each model. Each =
model gets it's own factory. The factory would contain information =
about used ids/names, a counter, etc. - whatever persisted data you need =
to be able to dole out new ids/names. Depending on your needs there are =
different strategies you could take here.

To get a new id when an object is created, you'll have to create a new =
CreationFactory that passes the factory (or another object that contains =
the factory - like a "root" model object) to the object. I use code =
that looks like this to do something similar in my creation factory:

public Object getNewObject()
{
try {
Constructor[] ctors =3D type.getConstructors();
for ( int i=3D0; i < ctors.length; i++) {
Constructor ctor =3D ctors[i];
Class[] paramTypes =3D ctor.getParameterTypes();
if ( paramTypes.length =3D=3D 1 && paramTypes[0] =3D=3D =
MyClass.class ) {
return ctor.newInstance(new Object[]{getMyClassInstance()});
}
}
return type.newInstance();
} catch ( Exception exc ) { return null; }
}

Depending on how/what you may use this for there may be other issues to =
consider:

- Can the user change the name/id of an object after it is set?
- Do you need to guarantee uniqueness?
- Do you need/want to reclaim unused names/ids after an object is =
deleted? Don't forget about undo...
- etc.

I'm sure there are other approaches you could take that don't involve a =
new CreationFactory - I'm sure you could probably do this just through =
commands if you wanted to. This should hopefully be enough to get the =
creative juices flowing though...

- Jason
=20

"Brian Fernandes" <brian.fernandes@codito.com> wrote in message =
news:bmmarv$oot$1@eclipse.org...
> I want to be able to number each of the models in my Editor. ie. =
Assign each
> of them a unique index. So I need a global counter maintained for each
> Editor - every time I create a new object, I'll assign it an index and
> increment the global index. (Obviously I want the numbering to be
> independent across the Editors)
>=20
> Since the Contents of the graphical viewer is unique to each Editor, I
> thought of maintaining the counter here. This means that I would have =
to
> pass the instance of the Contents model to every model created in the
> editor. I passed it in the constructor of each model - seems to be =
okay but
> then I have problems with SimpleFactory. It creates new objects with
> type.newInstance() and I can't seem to be able to pass the Contents =
model
> here. Also, when the SimpleFactory classes are instantiated, the =
contents
> model does not exist.
>=20
> My java is rusty and so I'm sure I'm going about this pretty badly. Is =
there
> a better way to achieve this ? I could use a static variable but then =
it
> wouldn't be unique across the Editors. The only other unique element =
per
> Editor is the Editor itself.... any way I can access that from the =
models ?
>=20
> Thanks,
> Brian.
>=20
>=20

------=_NextPart_000_0031_01C394B0.85D21B00
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.3502.5390" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>Your terminology is somewhat confusing, =
but I think=20
I get the jist of what you're wanting.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>One solution is to have a name/id =
factory attached=20
to each model.&nbsp; Each model gets it's own factory.&nbsp; The factory =
would=20
contain information about used ids/names, a counter, etc. - whatever =
persisted=20
data you need to be able to dole out new ids/names.&nbsp; Depending on =
your=20
needs there are different strategies you could take here.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>To get a new id when&nbsp;an object is =
created,=20
you'll have to create a new CreationFactory that passes the factory (or =
another=20
object that contains the factory - like a "root" model object) to the=20
object.&nbsp; I use code that looks like this to do something similar in =
my=20
creation factory:</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>public Object =
getNewObject()</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>{</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; try</FONT><FONT face=3DArial=20
size=3D2>&nbsp;{</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; Constructor[] ctors =
=3D=20
type.getConstructors();</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; for ( int i=3D0; i =
&lt;=20
ctors.length; i++)</FONT><FONT face=3DArial size=3D2> {</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
Constructor ctor =3D=20
ctors[i];</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Class[] =
paramTypes =3D=20
ctor.getParameterTypes();</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (=20
paramTypes.length =3D=3D 1 &amp;&amp; paramTypes[0] =3D=3D MyClass.class =
)</FONT><FONT=20
face=3DArial size=3D2>&nbsp;{</FONT></DIV>
<DIV><FONT face=3DArial =
size=3D2> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; return=20
ctor.newInstance(new Object[]{getMyClassInstance()});</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
}</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; }</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; return=20
type.newInstance();</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; } </FONT><FONT face=3DArial =
size=3D2>catch (=20
Exception exc )</FONT><FONT face=3DArial size=3D2> { </FONT><FONT =
face=3DArial=20
size=3D2>return null; </FONT><FONT face=3DArial size=3D2>}</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>}</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Depending on how/what you may use this =
for there=20
may be other issues to consider:</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; - Can the user change the =
name/id of an=20
object after it is set?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; - Do you need to guarantee=20
uniqueness?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; - Do you need/want to reclaim =
unused=20
names/ids after an object is deleted?&nbsp; Don't forget about=20
undo...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; - etc.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I'm sure there are other approaches you =
could take=20
that don't involve a new CreationFactory - I'm sure you could probably =
do this=20
just through commands if you wanted to.&nbsp; This should hopefully be =
enough to=20
get the creative juices flowing though...</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>- Jason</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>"Brian Fernandes" &lt;</FONT><A=20
href=3D"mailto:brian.fernandes@codito.com"><FONT face=3DArial=20
size=3D2>brian.fernandes@codito.com</FONT></A><FONT face=3DArial =
size=3D2>&gt; wrote=20
in message </FONT><A href=3D"news:bmmarv$oot$1@eclipse.org"><FONT =
face=3DArial=20
size=3D2>news:bmmarv$oot$1@eclipse.org</FONT></A><FONT face=3DArial=20
size=3D2>...</FONT></DIV><FONT face=3DArial size=3D2>&gt; I want to be =
able to number=20
each of the models in my Editor. ie. Assign each<BR>&gt; of them a =
unique index.=20
So I need a global counter maintained for each<BR>&gt; Editor - every =
time I=20
create a new object, I'll assign it an index and<BR>&gt; increment the =
global=20
index. (Obviously I want the numbering to be<BR>&gt; independent across =
the=20
Editors)<BR>&gt; <BR>&gt; Since the Contents of the graphical viewer is =
unique=20
to each Editor, I<BR>&gt; thought of maintaining the counter here. This =
means=20
that I would have to<BR>&gt; pass the instance of the Contents model to =
every=20
model created in the<BR>&gt; editor.&nbsp; I passed it in the =
constructor of=20
each model - seems to be okay but<BR>&gt; then I have problems with=20
SimpleFactory. It creates new objects with<BR>&gt; type.newInstance() =
and I=20
can't seem to be able to pass the Contents model<BR>&gt; here. Also, =
when the=20
SimpleFactory classes are instantiated, the contents<BR>&gt; model does =
not=20
exist.<BR>&gt; <BR>&gt; My java is rusty and so I'm sure I'm going about =
this=20
pretty badly. Is there<BR>&gt; a better way to achieve this ? I could =
use a=20
static variable but then it<BR>&gt; wouldn't be unique across the =
Editors. The=20
only other unique element per<BR>&gt; Editor is the Editor itself.... =
any way I=20
can access that from the models ?<BR>&gt; <BR>&gt; Thanks,<BR>&gt;=20
Brian.<BR>&gt; <BR>&gt; </FONT></BODY></HTML>

------=_NextPart_000_0031_01C394B0.85D21B00--
Re: Global variables per Editor [message #100579 is a reply to message #100537] Sat, 18 October 2003 09:41 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: brian.fernandes.codito.com

Hi Jason,

It isn't an exact solution of course, but it defnitely presents a new
approach to the solution. My creative juices are flowin all right.

Thanks again,
Brian.

"Jason Woods" <jwoods@journee.com> wrote in message
news:bmpbqe$fv6$1@eclipse.org...
Your terminology is somewhat confusing, but I think I get the jist of what
you're wanting.

One solution is to have a name/id factory attached to each model. Each
model gets it's own factory. The factory would contain information about
used ids/names, a counter, etc. - whatever persisted data you need to be
able to dole out new ids/names. Depending on your needs there are different
strategies you could take here.

To get a new id when an object is created, you'll have to create a new
CreationFactory that passes the factory (or another object that contains the
factory - like a "root" model object) to the object. I use code that looks
like this to do something similar in my creation factory:

public Object getNewObject()
{
try {
Constructor[] ctors = type.getConstructors();
for ( int i=0; i < ctors.length; i++) {
Constructor ctor = ctors[i];
Class[] paramTypes = ctor.getParameterTypes();
if ( paramTypes.length == 1 && paramTypes[0] == MyClass.class ) {
return ctor.newInstance(new Object[]{getMyClassInstance()});
}
}
return type.newInstance();
} catch ( Exception exc ) { return null; }
}

Depending on how/what you may use this for there may be other issues to
consider:

- Can the user change the name/id of an object after it is set?
- Do you need to guarantee uniqueness?
- Do you need/want to reclaim unused names/ids after an object is deleted?
Don't forget about undo...
- etc.

I'm sure there are other approaches you could take that don't involve a new
CreationFactory - I'm sure you could probably do this just through commands
if you wanted to. This should hopefully be enough to get the creative
juices flowing though...

- Jason


"Brian Fernandes" <brian.fernandes@codito.com> wrote in message
news:bmmarv$oot$1@eclipse.org...
> I want to be able to number each of the models in my Editor. ie. Assign
each
> of them a unique index. So I need a global counter maintained for each
> Editor - every time I create a new object, I'll assign it an index and
> increment the global index. (Obviously I want the numbering to be
> independent across the Editors)
>
> Since the Contents of the graphical viewer is unique to each Editor, I
> thought of maintaining the counter here. This means that I would have to
> pass the instance of the Contents model to every model created in the
> editor. I passed it in the constructor of each model - seems to be okay
but
> then I have problems with SimpleFactory. It creates new objects with
> type.newInstance() and I can't seem to be able to pass the Contents model
> here. Also, when the SimpleFactory classes are instantiated, the contents
> model does not exist.
>
> My java is rusty and so I'm sure I'm going about this pretty badly. Is
there
> a better way to achieve this ? I could use a static variable but then it
> wouldn't be unique across the Editors. The only other unique element per
> Editor is the Editor itself.... any way I can access that from the models
?
>
> Thanks,
> Brian.
>
>
Re: Global variables per Editor [message #100824 is a reply to message #100579] Mon, 20 October 2003 11:08 Go to previous message
Eclipse UserFriend
Originally posted by: g.wagenknecht.intershop.de

Brian Fernandes wrote:

> To get a new id when an object is created, you'll have to create a new
> CreationFactory that passes the factory (or another object that
> contains the factory - like a "root" model object) to the object. I
> use code that looks like this to do something similar in my creation
> factory:

You don't need to use the CreationFactory#getNewObject method. Simply deal
with the class instances in CreationFactory and create the new model objects
yourself in your commands.

Another solution is to use a singelton pattern for the model manager or a
similar solution (depends on your API) because actually not only GEF editors
can create new model structures. It might not be a requirement right now but
in the feature.

Cu, Gunnar
Previous Topic:Draw2D
Next Topic:[ANN] SWT vs Swing Opinion Poll
Goto Forum:
  


Current Time: Sat Oct 19 16:11:02 GMT 2024

Powered by FUDForum. Page generated in 0.07224 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top