Consultor Eletrônico



Kbase 21934: PUBLISH & SUBSCRIBE with ADM2
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   4/1/2002
SUMMARY:

The PUBLISH / SUBSCRIBE mechanism can be used within SmartObjects too. The Progress ADM2 implements SmartLinks using the Progress 4GL
PUBLISH & SUBSCRIBE event mechanism.

EXPLANATION:

SmartLinks are based on the PUBLISH / SUBSCRIBE mechanism and are created and maintained by each SmartObject's container. Each link is a property in each SmartObject that supports that type of link. The smart.p super procedure has procedures and functions for link management. Each link is mapped to one or more events that use the PUBLISH and SUBSCRIBE statements. These event lists are also SmartObject properties.

It is possible to dynamically add a SmartLink to any SmartObject with a RUN addLink statement. For example:

RUN addLink (INPUT hSmartPanel, INPUT `Navigation':U, INPUT hdCust).

It is also possible to add your customized links to SmartObjects with addLink; for example, using the addLink procedure to add a customized SmartLink that is not recognized as one of the object's supported links:

RUN addLink(h1, 'newLink', h2).

The addLink procedure now executes the following statement:

SUBSCRIBE PROCEDURE h2 TO 'newlink' IN h1.

Now it is necessary to create a procedure called 'newlink' on the subscriber. The procedure h1 can now execute the PUBLISH statement whenever it wants to invoke the named event in h2:

PUBLISH 'newlink'.

Using the PUBLISH / SUBSCRIBE mechanism with SmartObjects is only recommended for cases in which it is not possible to use the SmartLinks; for example, to communicate between a SmartObject and a non-smart object such as a fill-in.

EXAMPLE:

Example of PUBLISH and SUBSCRIBE with a SmartWindow:

In the example below, SmartWindows show how to use the PUBLISH / SUBSCRIBE mechanism with SmartObjects, although, to reduce the code, no other SmartObject is used in the example.

In the main window, publisher.w, there is a normal browser in one SmartWindow and a fill-in in the SmartWindow subscriber.w. This fill-in merely displays the cust-num field of the selected row in the browser.

In this case it is necessary to override the super procedure InitalizeObject on the subscriber SmartWindow with the Fill-in, and add the subscribe command after the RUN SUPER statement.
In the publisher.w window, the named event is being published in the VALUE-CHANGED of the browse.

****************
****PUBLISHER.W:

&Scoped-define WINDOW-NAME wWin
CREATE WIDGET-POOL.
/* ******************** Preprocessor Definitions ************** */
&Scoped-define PROCEDURE-TYPE SmartWindow
&Scoped-define DB-AWARE no
&Scoped-define ADM-CONTAINER WINDOW
&Scoped-define ADM-SUPPORTED-LINKS Data-Target,Data-Source,Page-Target,Update-Source,Update-Target,Filter-target,Filter-Source
&Scoped-define FRAME-NAME fMain
&Scoped-define BROWSE-NAME BROWSE-1
&Scoped-define INTERNAL-TABLES Customer
&Scoped-define FIELDS-IN-QUERY-BROWSE-1 Customer.Cust-Num Customer.Name Customer.City
&Scoped-define ENABLED-FIELDS-IN-QUERY-BROWSE-1
&Scoped-define OPEN-QUERY-BROWSE-1 OPEN QUERY BROWSE-1 FOR EACH Customer NO-LOCK INDEXED-REPOSITION.
&Scoped-define TABLES-IN-QUERY-BROWSE-1 Customer
&Scoped-define FIRST-TABLE-IN-QUERY-BROWSE-1 Customer
&Scoped-define OPEN-BROWSERS-IN-QUERY-fMain ~{&OPEN-QUERY-BROWSE-1}
&Scoped-Define ENABLED-OBJECTS BROWSE-1 BUTTON-1 BtnDone

DEFINE VAR wWin AS WIDGET-HANDLE NO-UNDO.
DEFINE BUTTON BtnDone DEFAULT LABEL "&Done" SIZE 15 BY 1.14 BGCOLOR 8.
DEFINE BUTTON BUTTON-1 LABEL "Subscriber Window" SIZE 25 BY 1.19.

DEFINE QUERY BROWSE-1 FOR Customer SCROLLING.
DEFINE BROWSE BROWSE-1
QUERY BROWSE-1 NO-LOCK DISPLAY
Customer.Cust-Num FORMAT ">>>>9":U
Customer.Name FORMAT "x(20)":U
Customer.City FORMAT "x(12)":U WIDTH 9
WITH NO-ROW-MARKERS SEPARATORS SIZE 44 BY 7.14 EXPANDABLE.

/* ************************ Frame Definitions ***************** */
DEFINE FRAME fMain
BROWSE-1 AT ROW 2.19 COL 7
BUTTON-1 AT ROW 10.05 COL 4
BtnDone AT ROW 10.05 COL 39
WITH 1 DOWN NO-BOX KEEP-TAB-ORDER OVERLAY
SIDE-LABELS NO-UNDERLINE THREE-D
AT COL 1 ROW 1 SIZE 59.2 BY 12.1
DEFAULT-BUTTON BtnDone.

/* ************************* Create Window ******************** */

IF SESSION:DISPLAY-TYPE = "GUI":U THEN
CREATE WINDOW wWin ASSIGN
HIDDEN = YES
TITLE = "Prinicpal>"
HEIGHT = 12.1 WIDTH = 59.2
MAX-HEIGHT = 28.81 MAX-WIDTH = 146.2
VIRTUAL-HEIGHT = 28.81 VIRTUAL-WIDTH = 146.2
RESIZE = no SCROLL-BARS = no
STATUS-AREA = no BGCOLOR = ?
FGCOLOR = ? THREE-D = yes
MESSAGE-AREA = no SENSITIVE = yes.
ELSE {&WINDOW-NAME} = CURRENT-WINDOW.

/* ************************* Included-Libraries *************** */
{src/adm2/containr.i}

IF SESSION:DISPLAY-TYPE = "GUI":U AND VALID-HANDLE(wWin)
THEN wWin:HIDDEN = yes.

ON END-ERROR OF wWin /* <Prinicpal> */
OR ENDKEY OF {&WINDOW-NAME} ANYWHERE DO:
IF THIS-PROCEDURE:PERSISTENT THEN RETURN NO-APPLY.
END.


ON WINDOW-CLOSE OF wWin /* <Prinicpal> */
DO:
APPLY "CLOSE":U TO THIS-PROCEDURE.
RETURN NO-APPLY.
END.

ON VALUE-CHANGED OF BROWSE-1 IN FRAME fMain
DO:
PUBLISH "custom" ( customer.cust-num ).
END.

ON CHOOSE OF BtnDone IN FRAME fMain /* Done */
DO:
&IF "{&PROCEDURE-TYPE}" EQ "SmartPanel" &THEN
&IF "{&ADM-VERSION}" EQ "ADM1.1" &THEN
RUN dispatch IN THIS-PROCEDURE ('exit').
&ELSE
RUN exitObject.
&ENDIF
&ELSE
APPLY "CLOSE":U TO THIS-PROCEDURE.
&ENDIF
END.

ON CHOOSE OF BUTTON-1 IN FRAME fMain /* Open Subscriber Window */
DO:
RUN subscriber.w.
END.

/* *************************** Main Block ********************** */
/* Include custom Main Block code for SmartWindows. */
{src/adm2/windowmn.i}


/* ********************** Internal Procedures ****************** */
PROCEDURE adm-create-objects :
END PROCEDURE.

PROCEDURE disable_UI :
IF SESSION:DISPLAY-TYPE = "GUI":U AND VALID-HANDLE(wWin)
THEN DELETE WIDGET wWin.
IF THIS-PROCEDURE:PERSISTENT THEN DELETE PROCEDURE THIS-PROCEDURE.
END PROCEDURE.

PROCEDURE enable_UI :
ENABLE BROWSE-1 BUTTON-1 BtnDone
WITH FRAME fMain IN WINDOW wWin.
{&OPEN-BROWSERS-IN-QUERY-fMain}
VIEW wWin.
END PROCEDURE.

PROCEDURE exitObject :
APPLY "CLOSE":U TO THIS-PROCEDURE.
RETURN.
END PROCEDURE.

*****************
****SUBSCRIBER.W:

&Scoped-define WINDOW-NAME wWin
CREATE WIDGET-POOL.

/* ******************** Preprocessor Definitions ************* */
&Scoped-define PROCEDURE-TYPE SmartWindow
&Scoped-define DB-AWARE no
&Scoped-define ADM-CONTAINER WINDOW
&Scoped-define ADM-SUPPORTED-LINKS Data-Target,Data-Source,Page-Target,Update-Source,Update-Target,Filter-target,Filter-Source
&Scoped-define FRAME-NAME fMain
&Scoped-Define ENABLED-OBJECTS BtnDone cust-num
&Scoped-Define DISPLAYED-OBJECTS cust-num

DEFINE VAR wWin AS WIDGET-HANDLE NO-UNDO.
DEFINE BUTTON BtnDone DEFAULT LABEL "&Done" SIZE 15 BY 1.14 BGCOLOR 8.

DEFINE VARIABLE cust-num AS CHARACTER FORMAT "X(15)":U LABEL "Cust-num from publisher:"
VIEW-AS FILL-IN SIZE 14 BY 1 NO-UNDO.


/* ************************ Frame Definitions ****************** */

DEFINE FRAME fMain
BtnDone AT ROW 2.67 COL 51
cust-num AT ROW 3.14 COL 28 COLON-ALIGNED
WITH 1 DOWN NO-BOX KEEP-TAB-ORDER OVERLAY
SIDE-LABELS NO-UNDERLINE THREE-D
AT COL 1 ROW 1 SIZE 80 BY 8.29
DEFAULT-BUTTON BtnDone.

IF SESSION:DISPLAY-TYPE = "GUI":U THEN
CREATE WINDOW wWin ASSIGN
HIDDEN = YES
TITLE = "SMART SUBSCRIBER"
HEIGHT = 4.33 WIDTH = 66.6
MAX-HEIGHT = 34.33 MAX-WIDTH = 204.8
VIRTUAL-HEIGHT = 34.33 VIRTUAL-WIDTH = 204.8
RESIZE = no SCROLL-BARS = no
STATUS-AREA = no BGCOLOR = ?
FGCOLOR = ? THREE-D = yes
MESSAGE-AREA = no SENSITIVE = yes.
ELSE {&WINDOW-NAME} = CURRENT-WINDOW.

/* ************************* Included-Libraries **************** */
{src/adm2/containr.i}

IF SESSION:DISPLAY-TYPE = "GUI":U AND VALID-HANDLE(wWin)
THEN wWin:HIDDEN = yes.

ON END-ERROR OF wWin /* SMART SUBSCRIBER */
OR ENDKEY OF {&WINDOW-NAME} ANYWHERE DO:
IF THIS-PROCEDURE:PERSISTENT THEN RETURN NO-APPLY.
END.

ON WINDOW-CLOSE OF wWin /* SMART SUBSCRIBER */
DO:
APPLY "CLOSE":U TO THIS-PROCEDURE.
RETURN NO-APPLY.
END.

ON CHOOSE OF BtnDone IN FRAME fMain /* Done */
DO:
&IF "{&PROCEDURE-TYPE}" EQ "SmartPanel" &THEN
&IF "{&ADM-VERSION}" EQ "ADM1.1" &THEN
RUN dispatch IN THIS-PROCEDURE ('exit').
&ELSE
RUN exitObject.
&ENDIF
&ELSE
APPLY "CLOSE":U TO THIS-PROCEDURE.
&ENDIF
END.

/* *************************** Main Block ******************** */
/* Include custom Main Block code for SmartWindows. */
{src/adm2/windowmn.i}


/* ********************** Internal Procedures *************** */
PROCEDURE adm-create-objects :
END PROCEDURE.

PROCEDURE custom :
DEF INPUT PARAMETER cust AS CHAR.
cust-num=cust.
DISPLAY cust-num WITH FRAME fmain.
END PROCEDURE.

PROCEDURE disable_UI :
IF SESSION:DISPLAY-TYPE = "GUI":U AND VALID-HANDLE(wWin)
THEN DELETE WIDGET wWin.
IF THIS-PROCEDURE:PERSISTENT THEN DELETE PROCEDURE THIS-PROCEDURE.
END PROCEDURE.

PROCEDURE enable_UI :
DISPLAY cust-num
WITH FRAME fMain IN WINDOW wWin.
ENABLE BtnDone cust-num
WITH FRAME fMain IN WINDOW wWin.
{&OPEN-BROWSERS-IN-QUERY-fMain}
VIEW wWin.
END PROCEDURE.

PROCEDURE exitObject :
APPLY "CLOSE":U TO THIS-PROCEDURE.
RETURN.
END PROCEDURE.

PROCEDURE initializeObject :
RUN SUPER.
SUBSCRIBE TO "custom" ANYWHERE.
END PROCEDURE.


References to Written Documentation:

Progress Knowledge Base Solutions:
21932, "ADM2: PUBLISH / SUBSCRIBE Event Mechanism"
21933, "PUBLISH & SUBSCRIBE Example with Browses"

Progress ADM 2 Guide and Reference, chapters:
3.4 SmartLink Events
3.5 Invoking Behavior in Other Linked Objects
3.7 Defining SmartLinks in Progress Applications: 3.7.2 Defining Dynamic SmartLinks