How to avoid duplicates when adding custom menu items?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to avoid duplicates when adding custom menu items?

Stein Rørvik
A couple of years ago Wayne posted this example on how to add new menu entries, restoring the missing Bridge example image:

  item = "File>Open Samples>Bridge";
  class = "ij.plugin.URLOpener(\"bridge.gif\")";
  call("ij.Menus.add", item, class);

I would like to do the same, adding a custom menu entry whenever a certain macro toolset is loaded.

The problem is that every time the macro is loaded, a new duplicate menu entry is created.

Does anyone know how to avoid this?
I usually reload toolsets several times during a session.

Perhaps there should have been a

  call("ij.Menus.delete", item);

method as well?

Or a

  call("ij.Menus.update", item, class);

which would replace an existing menu entry with the given class when the method is called.

I can not use "Refresh Menus" as that clears both the window menu and reverts to the toolset loaded from StartupMacros, which is not what I want.

Stein

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: How to avoid duplicates when adding custom menu items?

Fred Damen
Greetings,

A partial answer to your query...

ij.Menus.add places the submenu moniker, i.e., "Bridge" in your example
into a hash.  There is a:
public static boolean commandInUse(String command)
method that should be callable using the macro call method that checks
this hash if this submenu moniker is already there. I do not know how a
boolean is translated into a string and it would not seem to be able to
discern submenu monikers in different parent menus.

Has any one else noticed that the ImageJ Java documentation has disappeared?

Fred

https://imagej.nih.gov/ij/developer/source/ij/Menus.java.html

On Sat, April 17, 2021 4:13 am, Stein Rørvik wrote:

> A couple of years ago Wayne posted this example on how to add new menu
> entries, restoring the missing Bridge example image:
>
>   item = "File>Open Samples>Bridge";
>   class = "ij.plugin.URLOpener(\"bridge.gif\")";
>   call("ij.Menus.add", item, class);
>
> I would like to do the same, adding a custom menu entry whenever a certain
> macro toolset is loaded.
>
> The problem is that every time the macro is loaded, a new duplicate menu
> entry is created.
>
> Does anyone know how to avoid this?
> I usually reload toolsets several times during a session.
>
> Perhaps there should have been a
>
>   call("ij.Menus.delete", item);
>
> method as well?
>
> Or a
>
>   call("ij.Menus.update", item, class);
>
> which would replace an existing menu entry with the given class when the
> method is called.
>
> I can not use "Refresh Menus" as that clears both the window menu and
> reverts to the toolset loaded from StartupMacros, which is not what I
> want.
>
> Stein
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: How to avoid duplicates when adding custom menu items?

jmutterer
Hi,
you could List.setCommands and see if the command already exists:

List.setCommands;
cmd=List.get("Bridge");
print((cmd!=""),cmd);
cmd=List.get("Blobs");
print((cmd!=""),cmd);

Jerome.


On Sat, 17 Apr 2021 at 21:12, Fred Damen <[hidden email]> wrote:

> Greetings,
>
> A partial answer to your query...
>
> ij.Menus.add places the submenu moniker, i.e., "Bridge" in your example
> into a hash.  There is a:
> public static boolean commandInUse(String command)
> method that should be callable using the macro call method that checks
> this hash if this submenu moniker is already there. I do not know how a
> boolean is translated into a string and it would not seem to be able to
> discern submenu monikers in different parent menus.
>
> Has any one else noticed that the ImageJ Java documentation has
> disappeared?
>
> Fred
>
> https://imagej.nih.gov/ij/developer/source/ij/Menus.java.html
>
> On Sat, April 17, 2021 4:13 am, Stein Rørvik wrote:
> > A couple of years ago Wayne posted this example on how to add new menu
> > entries, restoring the missing Bridge example image:
> >
> >   item = "File>Open Samples>Bridge";
> >   class = "ij.plugin.URLOpener(\"bridge.gif\")";
> >   call("ij.Menus.add", item, class);
> >
> > I would like to do the same, adding a custom menu entry whenever a
> certain
> > macro toolset is loaded.
> >
> > The problem is that every time the macro is loaded, a new duplicate menu
> > entry is created.
> >
> > Does anyone know how to avoid this?
> > I usually reload toolsets several times during a session.
> >
> > Perhaps there should have been a
> >
> >   call("ij.Menus.delete", item);
> >
> > method as well?
> >
> > Or a
> >
> >   call("ij.Menus.update", item, class);
> >
> > which would replace an existing menu entry with the given class when the
> > method is called.
> >
> > I can not use "Refresh Menus" as that clears both the window menu and
> > reverts to the toolset loaded from StartupMacros, which is not what I
> > want.
> >
> > Stein
> >
> > --
> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
> >
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: How to avoid duplicates when adding custom menu items?

Stein Rørvik
Thanks Jerome,
this approach works fine!

My example can thus be rewritten as

item = "File>Open Samples>Bridge";
class = "ij.plugin.URLOpener(\"bridge.gif\")";
List.setCommands;
if(List.get("Bridge") == "") call("ij.Menus.add", item, class);

and I will get no duplicate entry.

Stein

-----Original Message-----
Sent: 17. april 2021 21:50
Subject: Re: How to avoid duplicates when adding custom menu items?

Hi,
you could List.setCommands and see if the command already exists:

List.setCommands;
cmd=List.get("Bridge");
print((cmd!=""),cmd);
cmd=List.get("Blobs");
print((cmd!=""),cmd);

Jerome.


On Sat, 17 Apr 2021 at 21:12, Fred Damen <[hidden email]> wrote:

> Greetings,
>
> A partial answer to your query...
>
> ij.Menus.add places the submenu moniker, i.e., "Bridge" in your
> example into a hash.  There is a:
> public static boolean commandInUse(String command) method that should
> be callable using the macro call method that checks this hash if this
> submenu moniker is already there. I do not know how a boolean is
> translated into a string and it would not seem to be able to discern
> submenu monikers in different parent menus.
>
> Has any one else noticed that the ImageJ Java documentation has
> disappeared?
>
> Fred
>
> https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fimag
> ej.nih.gov%2Fij%2Fdeveloper%2Fsource%2Fij%2FMenus.java.html&amp;data=0
> 4%7C01%7Cstein.rorvik%40sintef.no%7Ce6d9da0bea9b4858233508d901da29b5%7
> Ce1f00f39604145b0b309e0210d8b32af%7C1%7C0%7C637542858809305667%7CUnkno
> wn%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiL
> CJXVCI6Mn0%3D%7C1000&amp;sdata=l7sOs%2FsbYt8VcmfBxlGIFZfrKKE96R2%2FdZi
> NxVvfkt8%3D&amp;reserved=0
>
> On Sat, April 17, 2021 4:13 am, Stein Rørvik wrote:
> > A couple of years ago Wayne posted this example on how to add new
> > menu entries, restoring the missing Bridge example image:
> >
> >   item = "File>Open Samples>Bridge";
> >   class = "ij.plugin.URLOpener(\"bridge.gif\")";
> >   call("ij.Menus.add", item, class);
> >
> > I would like to do the same, adding a custom menu entry whenever a
> certain
> > macro toolset is loaded.
> >
> > The problem is that every time the macro is loaded, a new duplicate
> > menu entry is created.
> >
> > Does anyone know how to avoid this?
> > I usually reload toolsets several times during a session.
> >
> > Perhaps there should have been a
> >
> >   call("ij.Menus.delete", item);
> >
> > method as well?
> >
> > Or a
> >
> >   call("ij.Menus.update", item, class);
> >
> > which would replace an existing menu entry with the given class when
> > the method is called.
> >
> > I can not use "Refresh Menus" as that clears both the window menu
> > and reverts to the toolset loaded from StartupMacros, which is not
> > what I want.
> >
> > Stein
> >
> > --
> > ImageJ mailing list:
> > https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fima
> > gej.nih.gov%2Fij%2Flist.html&amp;data=04%7C01%7Cstein.rorvik%40sinte
> > f.no%7Ce6d9da0bea9b4858233508d901da29b5%7Ce1f00f39604145b0b309e0210d
> > 8b32af%7C1%7C0%7C637542858809315620%7CUnknown%7CTWFpbGZsb3d8eyJWIjoi
> > MC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&am
> > p;sdata=43f9FJi3%2FdPoeu8Warqpb8krgxh%2BIuoqYqJJuthhEmU%3D&amp;reser
> > ved=0
> >
>
> --
> ImageJ mailing list:
> https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimage
> j.nih.gov%2Fij%2Flist.html&amp;data=04%7C01%7Cstein.rorvik%40sintef.no
> %7Ce6d9da0bea9b4858233508d901da29b5%7Ce1f00f39604145b0b309e0210d8b32af
> %7C1%7C0%7C637542858809315620%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
> MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=43
> f9FJi3%2FdPoeu8Warqpb8krgxh%2BIuoqYqJJuthhEmU%3D&amp;reserved=0
>

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=04%7C01%7Cstein.rorvik%40sintef.no%7Ce6d9da0bea9b4858233508d901da29b5%7Ce1f00f39604145b0b309e0210d8b32af%7C1%7C0%7C637542858809315620%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=43f9FJi3%2FdPoeu8Warqpb8krgxh%2BIuoqYqJJuthhEmU%3D&amp;reserved=0

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html