📜 ⬆️ ⬇️

Java Gadget Switch

Greetings, habrazhiteli!

Today I want to touch on the subject of fine-tuning my virtual workplace. I ask you not to pin great hopes on this note, since after reading it, the code will still not be written by itself, but we will still overcome a bit of routine in development. Plus, try yourself in writing gadgets for Windows.

Let's start with the end and take a look at the finished result:

image
')
Go to the point. Developing for the Java platform, I am not limited to one version of it. There are many reasons for this: the target hardware platform, the presence of legacy, etc ... And in this connection, I often have to tinker with the JAVA_HOME / JRE_HOME / PATH environment variables.

Hence the idea to implement a switch that allows you to automate these actions in a convenient way. But how to bring the idea to life, to be comfortable? It did not take long to think, the format of the target application became clear immediately after the look fell on the weather widget on the desktop. So this will be the windows sidebar gadget.

For the impatient, let's try:
1) In the user environment variables you need to create: JAVA_HOME and JRE_HOME
2) Add% JAVA_HOME% \ bin to PATH
3) Download and install the gadget - github.com/ice-pro/JavaEnvSwitchGadget/blob/master/JavaEnvSwitchGadget.gadget
4) But, in order for everything to work as needed, I had to take another very non-standard step: in windows / system32 java.exe already lies and the priority of this folder is higher than what is specified in the PATH. So I had to rename the file from system32 to "_java.exe"

Everything, you can configure the paths to the installed JDK / JRE and use.

And now for those who are interested in details.
What is a gadget? Gadget in windows is an application with which you can visualize some data or even perform backward interaction.

The development of gadgets is based on a standard set of web technologies - HTML / CSS / JS. Of course, you can use C # (for example, using this template - visualstudiogallery.msdn.microsoft.com/bf347eb6-99bd-4c99-89d0-6ca3fe1eb54e ), but in this article I will not complicate simple things and choose the native path.

The basic frame of the gadget consists of two files - html page with the visual part and xml descriptor.
A full description of the descriptor tags can be found here msdn.microsoft.com/en-us/library/windows/desktop/ff486356 (v = vs.85) .aspx
Descriptor for my gadget (I will not give the code, as there is nothing remarkable there), along with the rest of the source code is available here github.com/ice-pro/JavaEnvSwitchGadget

Now let's go to the main page of the gadget. There are several points that I want to clarify:
- because the gadget will contain another additional page - settings, then you need to specify it explicitly in this way (for me, the fact that you need to do it not in xml was slightly strange):

document.onreadystatechange = function() { if(document.readyState == 'complete') { System.Gadget.settingsUI = 'settings.html'; } } 

- a simple API is used to work with the setting:
 var val = System.Gadget.Settings.read(key); 

- and the most interesting, work with environment variables:
 var shell = new ActiveXObject('WScript.Shell'); var vars = shell.Environment('User'); vars('JAVA_HOME') = jdk; vars('JRE_HOME') = jre; 


On the parameters page there is only one place on which I would like to focus - the preservation of these very parameters:

 System.Gadget.onSettingsClosing = applySettings; //          function applySettings(event) { if (event.closeAction == event.Action.commit) { // ... } } 


That's all, the implementation is ready. Now you can proceed to the assembly. The gadget in the finished form is a zip archive, only with the extension .gadget.

To build, I wrote a small command script. It looks like this:

 del JavaEnvSwitchGadget.gadget "C:\Program Files\7-Zip\7z.exe" a -tzip -x!.idea -x!.git -x!_stuff -x!build.cmd -r JavaEnvSwitchGadget *.* move JavaEnvSwitchGadget.zip JavaEnvSwitchGadget.gadget pause 


And now a little about debugging (you can read in detail here msdn.microsoft.com/en-us/library/windows/desktop/bb456467 (v = vs.85) .aspx)

For a start, the bad news: quick debugging with the help of alert-s will not work, since Sidebar does not have their implementation (workaround - use MsgBox).

But let's do it right.

1) First of all, we set up Internet Explorer, in the Advanced section we remove the check marks next to “Disable script debugging”
2) Customize Visual Studio, in Tools - Options - Just-In-Time, put a check near Script.
3) The last to interrupt the execution of the work of the gadget and call for help a debugger, you need only one line in the code

 debugger; 


Now when you start the gadget, we will see a window prompting us to connect to the code trace.

Thanks for reading, I hope someone will come in handy.

Useful links:
1) github.com/ice-pro/JavaEnvSwitchGadget - sources and build
2) msdn.microsoft.com/en-us/library/windows/desktop/bb456468 (v = vs.85) .aspx - step-by-step guide for development
3) msdn.microsoft.com/en-us/library/windows/desktop/bb456467 (v = vs.85) .aspx - description of the debugging method
4) msdn.microsoft.com/en-us/library/windows/desktop/ff486356 (v = vs.85) .aspx - description of the xml descriptor
5) msdn.microsoft.com/en-us/magazine/cc163370.aspx - one more article about the development, but older for Vista
6) stackoverflow.com/questions/768477/setting-an-environment-variable-in-javascript - working with environment variables from JS

PS On the issue of the versions between which the switch is proposed, I, I think, I will not say anything. If someone needs to cut / expand the list, then it is done quickly.
PS2. By analogy (yes there, copy-paste), you can implement a switch for Python 2/3.

Source: https://habr.com/ru/post/231969/


All Articles