Nav Helper system.nav.openWindowInstace

Im trying to make a new module to aid in using system.nav.openWindowInstance. Im trying to prevent Bubba form opening 18 copies of the same window, with the same instance id. Here is what i have so far

[code]def checkInstance(window,instanceId):
import system

instances = system.tag.getTagValue("[client]WindowInstances")
for row in range(instances.rowCount):
	if instances.getValueAt(row,"Window") == window and instances.getValueAt(row,"InstanceId") == instanceId:
		return row
return -1

def addInstance(window,instanceId):
import system

instances = system.tag.getTagValue("[client]WindowInstances")
newRow = [window,instanceId]
system.tag.writeToTag("[client]WindowInstances",system.dataset.addRow(instances,newRow))

def delInstance(window,instanceId):
import system,app.helper

row = helper.checkInstance(window,instanceId)
instances = system.tag.getTagValue("[client]WindowInstances")
system.tag.writeToTag("[client]WindowInstances",system.dataset.deleteRow(instances,row))

[/code]

Lets say it is called as follows

			if checkInstance("Transmitter",event.source.Tag) == -1:
				addInstance("Transmitter",event.source.Tag)
				import fpmi
				fpmi.nav.openWindowInstance('Transmitter', {'Tag' : event.source.Tag, 'config':event.source.config})				
				fpmi.nav.centerWindow('Transmitter')
			else:
				***code to bring instance to front***

In the else statement, how do I “get” the instance I want. I store a dataset of open instances in a client tag. Can we add an api reference to return some sort of ID when we use system.nav.openWindowInstance?

Made some changes, almost there, but too tired, going to bed, will post tomorrow.

There is a change needed to some of the functions, so IA is adding this sometime soon… ill post back when its all said and done

The idea is that the reference to the window itself is the “id”. That is what is returned by openWindowInstance. Why isn’t this sufficient?

Ignore the previous code, we do’nt need to track instances, as we can find this at runtime. The following scripts find the window if opened, then either center it and move it to the front, or opens a new instance if need be.

Thanks for the idea Carl.

[code]def findInstance(window,instanceId):
import system

for row in system.gui.getOpenedWindows():
	if row.name == window and row.getRootContainer().Tag == instanceId:
		return row
return None

def openInstance(window,instanceId):
import system, app.instanceControl

windowInstance = app.instanceControl.findInstance(window,instanceId)
if windowInstance == None:
	windowInstance = system.nav.openWindowInstance(window,{"Tag" : instanceId})
system.nav.centerWindow(windowInstance)
windowInstance.toFront()

def closeInstance(window,instanceId):
import system, app.instanceControl

windowInstance = app.instanceControl.findInstance(window,instanceId)
if windowInstance != None:
	system.nav.closeWindow(windowInstance)[/code]

Yeah, that looks just right. Thanks for taking my idea and running with it.

1 Like

Here is an update. Changed to allow more than one property to be passed.

[code]def findInstance(window,instanceId):
import system

for row in system.gui.getOpenedWindows():
	if row.name == window and row.getRootContainer().Tag == instanceId:
		return row
return None

def openInstance(window,properties=None):
import system, app.instanceControl

windowInstance = app.instanceControl.findInstance(window,properties['Tag'])
if windowInstance == None:
	windowInstance = system.nav.openWindowInstance(window,properties)
system.nav.centerWindow(windowInstance)
windowInstance.toFront()
windowInstance.setSelected(1)

def closeInstance(window,instanceId):
import system, app.instanceControl

windowInstance = app.instanceControl.findInstance(window,instanceId)
if windowInstance != None:
	system.nav.closeWindow(windowInstance)[/code]

To open a window, your code would be as follows:

app.instanceControl.openInstance('WINDOW',{'PROP1':value1,'PROP2',value2})

To check to see if a certain instance is open. This will return the actual window that it finds:

app.instanceControl.findInstance('WINDOW',WINDOW_IDENTIFIER)(

R
To close a window:

app.instanceControl.closeInstance('WINDOW',WINDOW_IDENTIFIER)

Thanks Kyle but its still not quite right. The toFront() method is nicer than changing the layer and then changing it back but the window still does not have actual focus.

This is perhaps a minor detail but the original window still has focus even if the new window is in front. Keyboard actions (like tab) would still affect the original window and the Windows menu at the top will not actually swap back to the original window when selected as it still has focus. Using the mouse on the new window will force focus to be acquired on the new window.

This is what we do to give the front window focus (and make the whole app appear/disappear):

[code]def Iconify(Visible, WindowName=“WindowOfChoice”, KeepOnTop=0):
#PJR 20100313
#This Script forces the Ignition App to appear/dissapear.
#It works for both windowed & full screen launch modes
from java.awt import Window
from javax.swing import SwingUtilities
from java.awt import Frame
import system
allWindows = system.gui.getOpenedWindows()
if len(allWindows)==0:
#No Windows open? open one first
system.nav.openWindow(WindowName)
allWindows = system.gui.getOpenedWindows()

topWindow = SwingUtilities.getAncestorOfClass(Window, allWindows[0])
state = topWindow.getExtendedState()
if Visible:
	state &= ~Frame.ICONIFIED
	topWindow.setExtendedState(state)
	topWindow.setAlwaysOnTop(1)
	topWindow.toFront()
	if KeepOnTop==0:
		topWindow.setAlwaysOnTop(0)
else:
	state |= Frame.ICONIFIED
	topWindow.setExtendedState(state)
	topWindow.setAlwaysOnTop(0)
	topWindow.toBack()

[/code]

It works for Us :smiley:

Perhaps a combination of both would give “parameter passing” capabilities to, we didn’t need that so not included.

I have modified the scripts to request focus, we needed to add windowInstance.setSelected(1)

Thanks. The window.SetSelected(1) does the trick correctly for me.

Here is something quick and dirty. Replace your line 6 with the following.: Let me know how it goes.

windows = system.gui.getOpenedWindows()
targetWindowPresent = False
for window in windows:
	if window.name == "01_ValvePopup" and window.rootContainer.TagPath == event.source.Valve_udt.Meta.TagPath:
		targetWindowPresent = True
		break
if not window and targetWindowPresent:
	window = system.nav.openWindowInstance('Popup/01_ValvePopup', {'Tagpath':'sometagpath'})

 #dowindowstuff

Thanks Kyle_Chase,

################################################################
It says that AttributeError: ‘str’ object has no attribute ‘name’
################################################################

AttributeError: 'str' object has no attribute 'name'

Is probably happening because system.gui.getOpenWindowNames() returns a string tuple. Try if window == '01_ValvePopup ... on line 10 instead of window.name, as window is a string not an window object.

getOpenedWindowNames() should be getOpenedWindows(). Script has been edited above.

Thanks Perry & Kyle,

NO error using this script, but wont show the popup window.

I found out that the code works if the window is already open, if not it wont call the Popup.
What I did is i create same instance using my Original Popup Code.

I just edited the script, try it now.