How to open a page in new browser window and specify the features of that window.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on advanced page topics in the Programmer's Guide |
This example uses the agScriptHelper utility object's various openWindow()
methods. The agScriptHelper methods perform tasks that are similar to JavaScript functions, saving you from having to write JavaScript code directly (such as when you need to manipulate browser windows).
The following code shows the features()
method, which returns a string that specifies the features of the window to open. The string is used by AgpScriptHelper's openWindow()
method.
The controls menubar
, toolbar
, resizable
, status
, and scrollbars
are checkbox controls. The code uses getState()
to determine if each checkbox is set or cleared. It uses the features
variable to contain the concatenation of all checkbox states. The features()
method returns the features
variable, which specifies the type of window to open.
public String features() { String features = "width=550,height=275"; if (menubar.getState()) { features += ",menubar=yes"; } else { features += ",menubar=no"; } if (toolbar.getState()) { features += ",toolbar=yes"; } else { features += ",toolbar=no"; } if (resizable.getState()) { features += ",resizable=yes"; } else { features += ",resizable=no"; } if (status.getState()) { features += ",status=yes"; } else { features += ",status=no"; } if (scrollbars.getState()) { features += ",scrollbars=yes"; } else { features += ",scrollbars=no"; } return features; }
features
variable to the string specifying the window's initial width and height.
menubar.getState()
. This method returns true if the checkbox is selected.
,menubar=yes
" to the features
variable to indicate that the window to open has a menu bar.
,menubar=no
" instead to indicate that the window to open does not have a menu bar.
getState()
on the toolbar, resizable, status, and scroll bar objects to get the user-specified features for the window.
features
string.
The following code opens a window when the user clicks the first button (Button1). It calls the features()
method to get the user specifications for the window's features. It then calls the 3-argument openWindow()
method on the agScriptHelper object, which is an instance of the AgpScriptHelper class. This is a utility class that provides methods that create JavaScript code so that you can access certain JavaScript functionality without having to know JavaScript. This instantiation occurs automatically with each page.
private void handle_Button1_pageActionPerformed(ActionEvent evt) throws Exception { String features = features(); agScriptHelper.openWindow("pgWindowToBeOpened.html", "new", features); }
features()
.
openWindow()
on agScriptHelper and specifying the following:
The following code opens a window when the user clicks the second button (Button2). It calls the 4-argument openWindow()
method on the agScriptHelper object, which is an instance of the AgpScriptHelper class. The additional parameter is params
, which is a Hashtable of parameters to send with the request to open the window. The displayed page (that is, the document that the client gets) will extract and use the parameters as appropriate.
private void handle_Button2_pageActionPerformed(ActionEvent evt) throws Exception { String features = features(); Hashtable params = new Hashtable(); params.put("param1", "ONE"); params.put("param2", "TWO"); params.put("param3", "THREE"); agScriptHelper.openWindow("pgWindowToBeOpened.html", "new", features, params); }
The following code opens a window when the user clicks the third button (Button3). It calls the 5-argument openWindow()
method on the agScriptHelper object, which is an instance of the AgpScriptHelper class. The parameter query
specifies the rows (of the data bound to the page) to display in the opened window. The parameter orderby
specifies the display order of the rows. In this example, it is set to NULL.
private void handle_Button3_pageActionPerformed(ActionEvent evt) throws Exception { String features = features(); agScriptHelper.openWindow("pgWindowToBeOpened.html", "new", features, "contacts.id > 0", null); }
openWindow()
method, set the query
parameter to "contacts.id > 0
" to specify that the page in the new window displays rows of the contacts table whose id column values are greater than 0.
The following code shows the pageRequestBegin event on the page pgWindowToBeOpened. This code runs immediately after the page is first loaded and at the beginning of every subsequent page request. It attempts to get specific data from the servlet request and uses them appropriately.
For example, if the user clicked on the second button in the pgWindowOpenClose page, the 4-argument openWindow()
method executes with a Hashtable as one of the parameters. The following method is able to get the Hashtable's values to display them in text fields. If, however, the user clicks the first button, the 3-argument openWindow()
method executes, and there will be no Hashtable values to extract from the servlet request.
//Method in pgWindowToBeOpened protected void pageRequestBegin(AgiHttpServletRequest req, AgiHttpServletResponse res) throws Exception { String[] values = req.getParameterValues("param1"); if (values != null) Label1.setText(values[0]); else Label1.setText(""); values = req.getParameterValues("param2"); if (values != null) Label2.setText(values[0]); else Label2.setText(""); values = req.getParameterValues("param3"); if (values != null) Label3.setText(values[0]); else Label3.setText(""); String queryString = req.getQueryString(); System.out.println("QUERYSTRING = " + queryString); if (queryString != null) { if (queryString.indexOf("query") >= 0) agData.gotoFirst(); else agData.clearRows(); } else agData.clearRows(); }
param1
value from the servlet request by calling getParameterValues()
, and assign it to the values
string array.
getQueryString()
on the request object.
indexOf()
does not return -1.
agData.gotoFirst()
; otherwise, clear the rows with agData.clearRows()
.
The following code closes the window when the user clicks the Close window button in the pgWindowToBeOpened page.
//Method in pgWindowToBeOpened private void handle_close_pageActionPerformed(ActionEvent evt) throws Exception { agScriptHelper.closeWindow(); }
agScriptHelper.closeWindow()
to close the current browser window.