chris
chris

Reputation: 1785

How to create a command in part view that fires Java code

We want to add a command (a button, a menu item, an entry in a context menu or similar) in the part view to execute Java code. In the Java code there should be a reference to the part.

We tried to add entries in psb-actionmodels.xml and cat-actions.xml without success - no menu item appears.

What are we missing?

enter image description here

Upvotes: 0

Views: 1390

Answers (2)

chris
chris

Reputation: 1785

Read the docs (hard job)

Thanks to Vignesh Vino who told me to read the documentation...

I did read it. I did read it before but i was lost. Reading wasn't that easy because (in my opinion) the docs doesn't dive deep enough to get good examples out of it.
With this answer i wanna share what i have learned. If i'm wrong or if you know more details please leave a comment.
$windchill is the path to your installation.

custom-actions.xml

$windchill\codebase\config\actions\custom-actions.xml:
This is the place to define actions.
I defined only one, the name is gbaction1, embeded in an objecttype named gbactiontype1.
To see a text in the menu you have to create a so called 'resource bundle'. Details follow below - it's just one file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE listofactions SYSTEM "actions.dtd">

<listofactions>
    <objecttype name="gbactiontype1" resourceBundle="com.gb.actions.GbActions1rb">
        <action name="gbaction1">
        <!—call function in a java class: -->
        <!-- <command class="com.gb.actions.GbActionA" method="exec"/> -->

        <!— or display a jsp site in a popup window: -->
        <command url="netmarkets/jsp/gbactiontype1/gbaction1.jsp" windowType="popup"/>
            <supportedTypes>
                <!-- add this if you want to the action to be displayed for wtparts -->
                <type value="wt.part.WTPart"/>
                <!-- ECN (for change notices) -->
                <type value="wt.change2.WTChangeOrder2"/>
                <!-- for documents -->
                <type value="wt.doc.WTDocument" />
            </supportedTypes>
        </action>
    </objecttype>
</listofactions>

Customize the menus

$windchill\codebase\config\actions\PartClient-actionmodels.xml:
Here i placed a reference to my action defined above

 <!-- Part information page Actions list -->
   <model name="more parts actions" menufor="wt.part.WTPart">
        <action name="gbaction1" type="gbactiontype1"/>
...
   

Resouce bundle

The resource bundle file in $windchill/src/com/gb/actions/GbActions1rb.rbInfo:

ResourceInfo.class=wt.tools.resource.StringResourceInfo
ResourceInfo.customizable=false 
ResourceInfo.deprecated=false 

gbactiontype1.gbaction1.description.value=menutext gbaction1

#doc: Note that icons for actions in the menus are only displayed for actions that also display in the toolbar
#gbactiontype1.gbaction1.tooltip.value=tooltip gbaction1

#relative to <windchill>/netmarkets/images?
#gbactiontype1.gbaction1.icon.value=multi_update.gif


gbactiontype1.gbaction1.icon.value=../../wtcore/images/gb/gb.png

Java source

The java source which is fired when uncommented from the above shown custom-actions.xml (the configured 'exec' method has to be public static and with the shown parameter NmCommand Bean):

public class GbActionA extends JCAAction {

    private static final Logger logger = LogR.getLogger(GbActionA.class.getName());

    public GbActionA(ActionDefinition ad) {
        super(ad); //never called?
    }

    public static void exec(NmCommandBean cmdBean) {
        System.out.println("### GbActionA exec ###");
        //how to get the WTPart:
        cmdBean.getActionOid().getOidObject(); //ie. wt.part.WTPart:681208
        ...
    }
    ...
}

Compile java source

You have to compile the code and the resource file from a windchill shell (command line with several environment variables set):

windchill shell> ant -f bin\tools.xml class -Dclass.includes=com/gb/** -Dclass.force=true

Restart server

After this is done you can restart the windchill service or (much faster) reload all actions here:
enter image description here

Reload the WTPart info page and find your menu here:
enter image description here

JSP

If you prefer to fire a jsp page:
The path to the jsp file is given in the url attribute of the command tag in the custom-actions.xml (see above):

<html>
    <head>
        <link rel="stylesheet" href="../../netmarkets/css/windchill-base.css">
        <link rel="stylesheet" href="../../netmarkets/themes/windchill/xtheme-windchill.css">
    </head>
    <body>
        <hr>    
        <%@ page import = "java.util.Map" %>
        <%@page import="java.util.Enumeration"%>
        <%@page import="java.lang.Exception"%>
        
        <%  request.setAttribute("hulla","true"); %><br>
        <%  out.println("hulla is " + request.getAttribute("hulla")); %><br>

        <%
            Enumeration e = request.getParameterNames();
            while(e.hasMoreElements()) {
                String paramName = e.nextElement().toString();
                out.println(paramName + " = " + request.getParameter(paramName)+"<br>");
            }
        %>
        
        <hr>
    </body>
</html>

I added some code to get the parameter names. The most interesting one is probably oid. In my test that parameter contained VR:wt.part.WTPart:626136.

Tomcat mode

Set tomcat mode=dev to force the jsp's to be compiled everytime before delivery:

windchill shell> ant -f WindchillConfigAssistant.xml configureTomcat

This command asks you for the mode to set. set to dev if you want to compile the jsp's before delivery and to prod in production mode.

Upvotes: 1

Vignesh Vino
Vignesh Vino

Reputation: 1238

I guess you're trying to create an action under part information page's menu. Your approach of adding the action button is not the PTC's recommended way. Although it will work but your customizations will become difficult to maintain during the upgrade.

Try the following steps to add an action button to call your java code

  • Find the action model for the menu by enabling jcaDebug on your browser.
  • Use the files custom-action.xml and custom-actionmodels.xml to add your actionmodels and to define your action. This will basically override the ootb action entries.

Windchill help center has detailed procedure for this https://support.ptc.com/help/windchill/whc/whc_en/#page/Windchill_Help_Center%2FWCCG_UICust_AddActionsHook_WCClientArchAction.html

Upvotes: 0

Related Questions