codecentric / NSMenuFX

Other
127 stars 26 forks source link

Java 9 compatibility issues #10

Closed mainrs closed 3 years ago

mainrs commented 8 years ago

Hello there :)

I know that it will take some time till Java 9 is coming out but they already announced that they want to modulize the JRE. That means that they can tweak which packages will be available to use (in a way that osgi is already doing it with the 'Export-Packages' property). That would sadly mean that your solution (using the sun classes) would not run and throw a SecurityException because you are trying to access classes that can't be accessed from the outside of personal package names.

0x4a616e commented 8 years ago

Hi, that's a good point. To be honest, I haven't looked at Java 9 classes yet. I know, however, that there were at least plans to create an API to access the native OS X menu bar, so that would definitely be the preferred way to solve this. If this API is not going to make it into Java 9, another option would be to use own native methods. I've been thinking about this for some time, as it would allow for some new things like accessing the context menu in the dock icon.

mainrs commented 8 years ago

True, I would really like to get some native stuff going for this. Though that I am not skilled enough for C/C++. I think it should be possible with JNA.

0x4a616e commented 8 years ago

I also thought about using JNA, but it is probably not needed. As it turned out, it is quite simple to write a library in objective-c that can be called from Java. I created a branch where i added some native methods here: https://github.com/codecentric/NSMenuFX/tree/own-native-adapter It currently just implements the "Hide ", "Hide Others" and "Show all" menu items, but it can easily be extended. It might however take some time until the entire menu-bar stuff is implemented... :wink:

mainrs commented 8 years ago

Nice. I would help out but I have zero experience with objective-c (sadly). Even after working for over 4 yours on OS X, I still stick to Java.

mainrs commented 8 years ago

Mind if I ask how far the native implementation is?

0x4a616e commented 8 years ago

Good morning Sir! Well, the native code is all there, what's left is some code to map javafx objects to the respective native objects but that is all happening in Java. I think that half of the stuff (rough estimate) is already done, but right now I don't have much time to continue working on this.

mainrs commented 8 years ago

Good Morning :) Do you have a rough idea how the API should look like? If you are ok with it, I can try to write it later on.

0x4a616e commented 8 years ago

That'd be great! I pushed my latest changes I have locally on the own-native-adapter branch. The current state of the API can be found in de.codecentric.centerdevice.cocoa.SampleApp. However, nothing is really carved in stone... The basic concept is currently like this:

As an example, you can create an adapter like this:

Menu menu = new Menu("Title");
// ...
NSMenuFX menuFX = new NSMenuFX(menu);

NSMenu nsMenu = menuFX.getNsMenu();
NSApplication.sharedApplication().mainMenu().itemAtIndex(0).setSubmenu(nsMenu);

Where the last line adds the menu to some existing menu. This line NSMenu nsMenu = menuFX.getNsMenu(); maps the JavaFX object to a "native" objects, and tries to connect all properties. The goal is that when you change a property in JavaFX after the menu bar has been set, the native object should be updated.

When you start working on that, you'll quickly notice that not everything is working yet 😉 I also left a few TODOs in the SampleApp class. If you have time, you can have a look there and see where you can continue. If you have any questions, i'll try to answer as soon as possible :)

mainrs commented 8 years ago

Sounds cool. Gonna take a look at Friday and start working on it 😄

mipastgt commented 7 years ago

I know it is only just over one year ago but are there any news on this subject? Getting the current code working with Java 9 is actually not so difficult. You only have to adapt the method StageUtils.getStages() and then actually use this method throughout the project instead of the direct use of StageHelper.getStages() which does not exist anymore in Java 9. I have attached a modified version of StageUtils. Maybe someone can have a look at it and see whether it is acceptable. StageUtils.java.zip

mainrs commented 7 years ago

Did you try to run it under Java 9? Tbh, I didn't need Java/JavaFX for the last year so I kind of forgot about this project D: But it should work out of the box if I am not mistaken.

mipastgt commented 7 years ago

Yes, I use it in a project of mine which I can run with JDK9 ea build 176 on my Mac. At run-time I have to specify the additinal java option --add-modules=ALL-SYSTEM though. At compile-time of my project I had to add (ant fragment):

<compilerarg value="--add-exports=javafx.controls/com.sun.javafx.scene.control=ALL-UNNAMED"/>
<compilerarg value="--add-exports=javafx.graphics/com.sun.javafx.stage=ALL-UNNAMED"/>
<compilerarg value="--add-exports=javafx.graphics/com.sun.glass.ui=ALL-UNNAMED"/>
<compilerarg value="--add-exports=javafx.graphics/com.sun.javafx.menu=ALL-UNNAMED"/>
<compilerarg value="--add-exports=javafx.graphics/com.sun.javafx.tk=ALL-UNNAMED"/>

That was it.

mainrs commented 7 years ago

@mipastgt That's the thingI hate about Java 9. That whole module stuff makes it quite a pain to do some specific stuff. There are szenarios where I can't ship a custom launcher script that'll add those modules before wrapping up the VM.

mipastgt commented 7 years ago

Yes, I also don't like this and I just don't understand why they have not made everything accessible by default and then specify a single opt-in switch for this module stuff. But my main concern was to just get the Mac stuff running again.

0x4a616e commented 7 years ago

Hi! I'm sorry guys, but ever since I became a dad, I badly neglected my coding projects. On first sight, the changes look like a good way to get NSMenuFX running on Java 9 at all. I'll have a closer look later today and update NSMenuFX. @mipastgt would you like to create a pull request for your changes?

mipastgt commented 7 years ago

I tried to make a pull request but I don't seem to have the necessary rights.

0x4a616e commented 7 years ago

Have you forked the project first? At least I cannot see any fork from you... https://github.com/codecentric/NSMenuFX/network/members In general, everyone should have sufficient privileges

mipastgt commented 7 years ago

You are right. I did not know that I had to create a fork for this. It's the first time that I contribute to a GitHub project :-) I hope it woked now.

0x4a616e commented 7 years ago

PR has been merged. Thanks again to @mipastgt for digging into this 👍 I'll however leave this issue open as the final goal should be to get everything running without extra compile or runtime flags.

mipastgt commented 7 years ago

What I have done so far is just a quick fix to make the code compilable and usable with Java 9. I hope that it is later possible to get rid of these flags and other issues, at least for the user of this library, by really modularizing the code and modifying the jar packaging. Instead of using reflection it would then for example also be possible to just use separat implementation classes of StageUtils in a multi-relase Jar file and hopefully some flags can be avoided by using a proper module-info.java. But currently I am waiting until the dust has settled over Java 9 and it has officially been released.

0x4a616e commented 7 years ago

Yes, that would be a great thing - provided there will be an officially supported way to access stages in javafx 9 at all

mipastgt commented 7 years ago

The Window.getWindows() method is official API. So that is no problem here. The reason for the flags are the com.sun.javafx.* packages which are used throughout the code.

mainrs commented 7 years ago

@mipastgt I have a solution using JNA lying around somewhere. Took some effort though because there isn't really any good resource on how to communicate with OS X Frameworks... Not sure if I actually deleted it or not. Will check later on today.

0x4a616e commented 3 years ago

Please refer to https://github.com/codecentric/NSMenuFX/issues/36 for recent updates. Those type of issues should be solved with the new major upgrade.

0x4a616e commented 3 years ago

Closing this as obsolete as i just published

<dependency>
  <groupId>de.jangassen</groupId>
  <artifactId>nsmenufx</artifactId>
  <version>3.0.0</version>
</dependency>