## ============================================================================ ## Filename : DataObjectViewerTemplate.vm ## Note(s) : This template is used to generate a data object viewer class ## based on a database table/view. ## ## Note: The generated code is dependent on the ThinkUI framework. ## Please refer to the SQL Client documentation for details. ## ## Copyright (c) 2007 ThinkUI Software Inc. All rights reserved. ## ============================================================================ ## ## **************************************************************************** ## The following variables are available in this template. For more ## information on any of the following variable, please refer to the user guide. ## **************************************************************************** ## $projectName ## $authorName ## $headerText ## $tableName ## $schemaName ## $className ## $packageName ## $prjPkgName ## $prjClassPrefix ## $dataClass ## $objectName ## $objectVar ## $superClassName ## $date ## $codeGen ## #if ($packageName) package $packageName; #end #parse( "ClassHeaderInclude.vm" ) import java.awt.BorderLayout; import java.awt.Container; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import thinkui.common.Utils; import thinkui.desktop.FrameViewer; import thinkui.desktop.${superClassName}; import thinkui.desktop.SwingAction; import thinkui.desktop.SwingCommandContext; import thinkui.desktop.SwingDataObjectRenderer; import thinkui.desktop.SwingRendererData; import thinkui.desktop.components.ActionContainer; import thinkui.desktop.components.AttributeComponent; import thinkui.desktop.components.TextAttributeComponent; import thinkui.ui.swing.LookAndFeelResource; import thinkui.ui.swing.SwingUtils; /** * Implements the ${className}. */ public class ${className} extends ${superClassName} { // Stores references to data object or null. private $objectName $objectVar; // Stores references to the various UI components. #foreach( $attr in $dataClass.getAttributes() ) private AttributeComponent ${codeGen.toVarName($attr.getName())}UI; #end /** * ${className} constructor. */ public ${className}(FrameViewer parentViewer) { #if ($superClassName.equals("JDialogViewer")) super(parentViewer, true); #end // Build the viewer components. JPanel panel = new JPanel(); buildViewer(panel); JComponent scrollPane = new JScrollPane(panel); scrollPane.setName("scrollPane"); getContentPane().add( scrollPane, BorderLayout.CENTER); // Build the viewer actions. ActionContainer actionContainer = buildActionContainer(); getContentPane().add(actionContainer.getComponent(), BorderLayout.SOUTH); setTitle("$codeGen.toCaption($objectName)"); showMessageBar(false); } /** * Build the action panel for the viewer. */ private ActionContainer buildActionContainer() { ActionContainer actionContainer = new ActionContainer("actions"); SwingAction okAction = new SwingAction("ok", "OK", new ActionListener() { public final void actionPerformed(ActionEvent ae) { SwingCommandContext ctx = (SwingCommandContext) getCommandManager().getCommandContext(); try { saveDataObject(ctx); ctx.setMessage("Successfully saved "+ ${objectVar}.getCaption() +"."); close(); } catch (Exception e) { ctx.setMessage(Utils.getMessage(e)); ctx.getLogger().error(Utils.getStackTrace(e)); } } }); actionContainer.addAction(okAction); SwingAction cancelAction = new SwingAction("cancel", "Cancel", new ActionListener() { public final void actionPerformed(ActionEvent ae) { close(); } }); actionContainer.addAction(cancelAction); return actionContainer; } /** * Build the viewer GUI elements and add to the given container. */ private void buildViewer(Container container) { GridBagLayout gridBagLayout = new GridBagLayout(); container.setLayout(gridBagLayout); Font normalFont = LookAndFeelResource.getInstance().getNormalFont(); Font boldFont = LookAndFeelResource.getInstance().getBoldFont(); JLabel label; SwingRendererData data = new SwingRendererData().setEditable(true); GridBagConstraints c = new GridBagConstraints(); c.gridwidth = 1; c.weightx = 0; //1; c.weighty = 0; //1; c.anchor = GridBagConstraints.NORTHWEST; c.insets = new Insets(1, 2, 1, 2); c.gridx = 0; c.gridy = 0; c.gridwidth = 2; label = new JLabel("Please provide the required information."); //label.setToolTipText("."); label.setForeground(LookAndFeelResource.getInstance().getReadOnlyFgColor()); SwingUtils.addComponent(container, label, gridBagLayout, c); c.gridwidth = 1; // Restore gridwidth back to 1. #foreach( $attr in $dataClass.getAttributes() ) c.gridx = 0; c.gridy++; label = new JLabel("$attr.getCaption()"); label.setToolTipText("$codeGen.getHintText($attr)"); label.setFont(boldFont); SwingUtils.addComponent(container, label, gridBagLayout, c); c.gridx = 1; ${codeGen.toVarName($attr.getName())}UI = (AttributeComponent) SwingDataObjectRenderer.${codeGen.getAttributeTypeConstant($attr.getType())}_RENDERER.render( ${objectVar}.getAttribute(${objectName}.$attr.getName().toUpperCase()), null, data); addComponent(${codeGen.toVarName($attr.getName())}UI); #if ($codeGen.isTextAttributeType($attr.getType())) //((TextAttributeComponent) ${codeGen.toVarName($attr.getName())}UI).setLineWrap(true); #end SwingUtils.addComponent(container, ${codeGen.toVarName($attr.getName())}UI.getComponent(), gridBagLayout, c); #end } /** * Set the $codeGen.toCaption($objectName) to display. */ public final void set$objectName($objectName $objectVar) { this.$objectVar = $objectVar; #foreach( $attr in $dataClass.getAttributes() ) ${codeGen.toVarName($attr.getName())}UI.setValue(${objectVar}.getValue(${attr.getName().toUpperCase()})); #end } /** * Helper method to save the data object in the data object viewer. * @return whether or not the save was successful. */ private boolean saveDataObject(SwingCommandContext ctx) throws Exception { JDBCDataObjectManager objectMgr = (JDBCDataObjectManager) ctx.getRequestValue("dataObjectMgr"); ValidatorContext validatorContext = new DefaultValidatorContext(context); DataObject dataObject = updateDataObject(validatorContext); DataObjectValidator validator = getDataObjectValidator(dataObject); validator.validate(validatorContext); // Clear any error indicator from the viewer. clearErrors(); if (validatorContext.hasErrors()) { renderErrors(validatorContext); ctx.getLogger().error(validatorContext.getErrors()); return false; } else { int saveCount = 0; try { saveCount = objectMgr.save(dataObject); objectMgr.commit(); } finally { if (objectMgr.isModified()) { objectMgr.rollback(); } } // Update viewer so we can display the auto // generated keys (i.e. the PK) as well as // remove the (*) modified indicator. if (saveCount > 0) { update(null); } } return true; } /** * Helper method to create and configure a DataObjectValidator for the given * data object. This code is similar to the generated code in the ThinkUI SQL * project "SaveCmdTemplate.vm" template file. */ private DataObjectValidator getDataObjectValidator(DataObject dataObject) { DataObjectValidator validator = DataObjectValidator.getValidator(dataObject); java.util.Collection validateAttrNames = new HashSet(); java.util.List attributes = dataObject.getDataClass().getAttributes(); Attribute attr; for (Iterator iter=attributes.iterator(); iter.hasNext(); ) { attr = (Attribute) iter.next(); if (!attr.isUpdatedByTrigger() && attr.isVisible()) { validateAttrNames.add(attr.getName()); } } validator.setValidateAttrNames(validateAttrNames); return validator; } }