How to track progress bar status of a java window - java

My application is created in java and it opens several java windows on invoking a event. I could get the active window using **jython **code:
k = KeyboardFocusManager.getCurrentKeyboardFocusManager()
wa = k.getActiveWindow()
print(wa.getTitle())
But, how to get all open java windows ?
And, how to get the status of the progress bar inside the java window?

Related

handling Command+H programmatically in Mac OS

I have a JavaFX application that i have ported to Mac. When the application in foreground Command+H suppose to hide the application but for my ported application it is showing version details in a new popup windows.
I wanted to check if my program is suppose to handle Command+H and then hide the application or should it be handled by OS.
On macOS, it was solved by the following.
//
// Disable default popup window.
//
Desktop.getDesktop().setAboutHandler(null);
//
// Hides the stage when Command + H is pressed.
//
scene.getAccelerators().put(
new KeyCodeCombination(KeyCode.H, KeyCombination.META_DOWN), () -> {
primaryStage.setIconified(true);
}
);

Application menu accelerators and OpenFileHandler not working in Swing application

I'm trying to add proper support for OS X in my Java 8 Swing application. To do this I'm using the com.apple:AppleJavaExtensions:1.4 package from maven.
I initialise the menu bar using the following code:
Application application = Application.getApplication();
JMenu fileMenu = new JMenu("File");
JMenuItem openMenuItem = new JMenuItem("Open...");
openMenuItem.addActionListener(action -> openFileDialog());
openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.META_MASK));
fileMenu.add(openMenuItem);
JMenuBar mainMenuBar = new JMenuBar();
mainMenuBar.add(fileMenu);
application.setDefaultMenuBar(mainMenuBar);
This works just fine the first time, without any Swing windows opened. However, when one file is opened (and a Swing window is created), the shortcut stops working. Pressing CMD+O still highlights the menu item, but the action isn't executed. Using the mouse to select the 'Open...' menu item does work. I'm guessing the Swing window is somehow stealing the event (but the menu is still highlighted when the key combo is pressed.. weird).
When the file open dialog (that openFileDialog() shows) is cancelled, the menu accelerator will keep working. So it is definitely related to the Swing window being opened when a file is opened.
Another problem with opening files with OS X is that the following code never shows 'foo':
application.setOpenFileHandler(e -> System.out.println("foo"));
This code is executed in main() (which returns almost instantly, after creating the menu bar I mentioned earlier). The file handler isn't ever called; not when dropping a file onto the dock icon, nor when double clicking a file and opening it with my application.
What am I doing wrong?
If your app doesn't have many Frames then this could be a workaround.
setJMenuBar(menuBar);
final Dimension invisible = new Dimension(0, 0);
menuBar.setSize(invisible);
menuBar.setMaximumSize(invisible);
menuBar.setPreferredSize(invisible);
menuBar.setMinimumSize(invisible);
Apparently the OpenFileHandler does nothing unless your Java application is running from an application bundle with an Info.plist that specifies appropriate CFBundleDocumentTypes. Here's an example of what it should look like:
http://www.qtcentre.org/wiki/index.php?title=Opening_documents_in_the_Mac_OS_X_Finder
I worked around the menu accelerator not working by simply registering a KeyEventDispatcher to the KeyboardFocusManager on each of the Swing windows, and manually invoking the appropriate ActionListeners when the correct KeyEvent is caught. Very very ugly, but it works. I'm still wondering why this is necessary.

Change the active application on OS X with Java and SWT

Here is my specific scenario:
I have my application running in the background and I want to open a new window from my applications toolbar icon right click menu. For simplicity I am using a MessageDialog, and using the method open() on the dialog, the window should display and make itself the active window.
For some reason I've run into the problem that if I had some other application open and I try to open this new dialog, it opens, but behind the active application. The new dialog will open in front of the other windows of the same application, but not the other(active) application.
Is there a simple way to do this that I'm missing?
I was able to find a solution. Essentially you need to find your active workbench shell(the application shell) and use the following methods on that shell, followed by pushing the actual desired window to the front.
You could easily modify this to make your window appear maximized as well by adding the method shell.setMaximized(true) before the redraw.
private static void bringupDialog(WindowState state) {
final Shell workbenchShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
//bring up the application to front
workbenchShell.setVisible( true );
workbenchShell.setMinimized( false );
workbenchShell.redraw();
//focus on dialog
workbenchShell.setActive();
workbenchShell.forceActive();
workbenchShell.setFocus();
workbenchShell.forceFocus();
workbenchShell.moveAbove( null );
workbenchShell.redraw();
Shell shell = instance.getShell(); // desired window shell
shell.setActive();
shell.forceActive();
shell.setFocus();
shell.forceFocus();
shell.moveAbove( null );
shell.redraw();
}

Set a dynamic Apple menu title for Java program in NetBeans 7.4

I inherited a Java app built (I believe) in Eclipse, which I'm modifying using NetBeans 7.4. I want to set the main menu title which shows up on a Mac next to the Apple menu. Right now that name is MainForm, but I want it to change dynamically to the contents of a specific text file (name.txt). I've looked up tons of info on project.properties, ANT scripts, and the like, but I can't find a definitive (and hopefully cross-platform) way to set this main menu title. I have a function in my code that returns this name, so I can use that if there's a place to do it. Thanks in advance!
I have found that in order to set the App Name in Mac OS X Application Menu, and avoid having it show up as the name of your Java project, you have to set it VERY early in the application cycle, using System.setProperty("apple.awt.application.name", "Your App Name");
Here's how I have mine set in my "main" java method that launches the application:
public static void main(String[] args) {
// the application menu for Mac OS X must be set very early in the cycle
String opSysName = System.getProperty("os.name").toLowerCase();
if (opSysName.contains("mac")) {
// to set the name of the app in the Mac App menu:
System.setProperty("apple.awt.application.name", "Your App Name");
//to show the menu bar at the top of the screen:
System.setProperty("apple.laf.useScreenMenuBar", "true");
// to show a more mac-like file dialog box
System.setProperty("apple.awt.fileDialogForDirectories", "true");
//underlying laf:
javax.swing.UIManager.getInstalledLookAndFeels();
// other set-up code goes here
}
else { // not on Mac OS X
// set-up code for non-Mac systems
}
java.awt.EventQueue.invokeLater(() -> {
// run the program
});
}

Selenium (WebDriver) Junit 4 switching between windows issue

I am testing a web application that creates a new window long after a button is clicked. The sequence is the following
window 1: (parent window) click button to create window 2
window 2: progress window appears until background process on server returns data
window 3: progress window turns into 3rd window (with different handle)
I want to properly wait for the 3rd window to appear. I know what the 'title' of all 3 windows will be however in order to get the titles from WebDriver I have to use the following code:
while(timeout has not occured...){
for (String handle : _driver.getWindowHandles()) {
String myTitle = driver.switchTo().window(handle).getTitle();
if(3rdWindowTitle.equalsIgnoreCase(myTitle)){
return true;
}
}
}
This will effectively switch the active window back and forth every time it loops because of the 'switchTo'. This causes the firefox windows to cycle back and forth really quickly and is obnoxious. What I need is a way to get the title's of the windows that are available without having to 'switchTo' each window in a loop waiting for the 3rd window. Any ideas?
I basically want a method (waitForWindowByTitle(titleIWant)) which will block until the window with the title I want appears.
Well, Better you can wait for your window to appear by checking the number of windows. Like:
for(int i=0; i<noOfTrials;i++){
noOfWindows = driver.getWindowHandles().size();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
if(noOfWindows>currentNoOfWindows){
break;
}
}
}
then for the first and last time you can browse through the windows (using switchTo) and navigate to the window you want.

Categories