2929
3030package org .scijava .desktop .options ;
3131
32- import java .io .IOException ;
33- import java .util .stream .Stream ;
34-
3532import org .scijava .ItemVisibility ;
36- import org .scijava .desktop .DesktopIntegrationProvider ;
33+ import org .scijava .desktop .DesktopService ;
3734import org .scijava .log .LogService ;
3835import org .scijava .module .ModuleItem ;
3936import org .scijava .module .MutableModuleItem ;
4037import org .scijava .options .OptionsPlugin ;
41- import org .scijava .platform .PlatformService ;
4238import org .scijava .plugin .Parameter ;
4339import org .scijava .plugin .Plugin ;
4440
5652public class OptionsDesktop extends OptionsPlugin {
5753
5854 @ Parameter
59- private PlatformService platformService ;
55+ private DesktopService desktopService ;
6056
6157 @ Parameter (required = false )
6258 private LogService log ;
@@ -73,50 +69,33 @@ public void initialize() {
7369 "Enable web links" ,
7470 "Allow handling of URI link schemes from web browsers" ,
7571 "Web links always enabled" , "Web links always disabled" ,
76- isWebLinksToggleable (), isWebLinksEnabled ());
72+ desktopService .isWebLinksToggleable (),
73+ desktopService .isWebLinksEnabled ());
7774 }
7875 if (desktopIconItem == null ) {
7976 desktopIconItem = createInput ("desktopIconPresent" ,
8077 "Add desktop icon" ,
8178 "Install application icon in the system menu" ,
8279 "Icon always present" , "Icon installation not implemented" ,
83- isDesktopIconToggleable (), isDesktopIconPresent ());
80+ desktopService .isDesktopIconToggleable (),
81+ desktopService .isDesktopIconPresent ());
8482 }
8583 if (fileTypesItem == null ) {
8684 fileTypesItem = createInput ("fileTypesEnabled" ,
8785 "Enable file type associations" ,
8886 "Register supported file extensions with the operating system" ,
8987 "File types always handled" , "File type registration not supported" ,
90- isFileExtensionsToggleable (), isFileExtensionsEnabled ());
88+ desktopService .isFileExtensionsToggleable (),
89+ desktopService .isFileExtensionsEnabled ());
9190 }
92-
93- // Set module inputs to match current desktop integration values.
94- setInputValue (webLinksItem , isWebLinksEnabled ());
95- setInputValue (desktopIconItem , isDesktopIconPresent ());
96- setInputValue (fileTypesItem , isFileExtensionsEnabled ());
9791 }
9892
9993 @ Override
10094 public void run () {
101- desktopPlatforms ().forEach (p -> {
102- // Resolve each feature's desired state: use the checkbox value
103- // if toggleable, otherwise preserve the current platform state.
104- final boolean webLinks = p .isWebLinksToggleable ()
105- ? Boolean .TRUE .equals (getInputValue (webLinksItem ))
106- : p .isWebLinksEnabled ();
107- final boolean desktopIcon = p .isDesktopIconToggleable ()
108- ? Boolean .TRUE .equals (getInputValue (desktopIconItem ))
109- : p .isDesktopIconPresent ();
110- final boolean fileTypes = p .isFileExtensionsToggleable ()
111- ? Boolean .TRUE .equals (getInputValue (fileTypesItem ))
112- : p .isFileExtensionsEnabled ();
113- try {
114- p .syncDesktopIntegration (webLinks , desktopIcon , fileTypes );
115- }
116- catch (final IOException e ) {
117- if (log != null ) log .error ("Error performing desktop integration" , e );
118- }
119- });
95+ final boolean webLinks = Boolean .TRUE .equals (getInputValue (webLinksItem ));
96+ final boolean desktopIcon = Boolean .TRUE .equals (getInputValue (desktopIconItem ));
97+ final boolean fileTypes = Boolean .TRUE .equals (getInputValue (fileTypesItem ));
98+ desktopService .syncDesktopIntegration (webLinks , desktopIcon , fileTypes );
12099 super .run ();
121100 }
122101
@@ -131,6 +110,7 @@ private MutableModuleItem<?> createInput(final String name,
131110 if (toggleable ) {
132111 item = addInput (name , boolean .class );
133112 item .setLabel (label );
113+ setInput (name , enabled );
134114 }
135115 else {
136116 item = addInput (name , String .class );
@@ -145,43 +125,8 @@ private MutableModuleItem<?> createInput(final String name,
145125 return item ;
146126 }
147127
148- private void setInputValue (final ModuleItem <?> item , boolean value ) {
149- if (item .getType () != boolean .class ) return ;
150- setInput (item .getName (), value );
151- }
152-
153128 private Boolean getInputValue (final ModuleItem <?> item ) {
154- if (item .getType () != boolean .class ) return null ;
155- return (Boolean ) getInput (item .getName ());
156- }
157-
158- private boolean isDesktopIconToggleable () {
159- return desktopPlatforms ().anyMatch (p -> p .isDesktopIconToggleable ());
160- }
161-
162- private boolean isDesktopIconPresent () {
163- return desktopPlatforms ().allMatch (p -> p .isDesktopIconPresent ());
164- }
165-
166- private boolean isWebLinksToggleable () {
167- return desktopPlatforms ().anyMatch (p -> p .isWebLinksToggleable ());
168- }
169-
170- private boolean isWebLinksEnabled () {
171- return desktopPlatforms ().allMatch (p -> p .isWebLinksEnabled ());
172- }
173-
174- private boolean isFileExtensionsToggleable () {
175- return desktopPlatforms ().anyMatch (p -> p .isFileExtensionsToggleable ());
176- }
177-
178- private boolean isFileExtensionsEnabled () {
179- return desktopPlatforms ().allMatch (p -> p .isFileExtensionsEnabled ());
180- }
181-
182- private Stream <DesktopIntegrationProvider > desktopPlatforms () {
183- return platformService .getTargetPlatforms ().stream () //
184- .filter (p -> p instanceof DesktopIntegrationProvider ) //
185- .map (p -> (DesktopIntegrationProvider ) p );
129+ return item .getType () == boolean .class ?
130+ (Boolean ) getInput (item .getName ()) : null ;
186131 }
187132}
0 commit comments