KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > www > WWindow


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.www;
15
16 import javax.servlet.*;
17 import javax.servlet.http.*;
18 import java.io.*;
19 import java.util.*;
20 import java.sql.*;
21 import java.math.BigDecimal JavaDoc;
22 import java.text.*;
23
24 import org.apache.ecs.*;
25 import org.apache.ecs.xhtml.*;
26
27 import org.compiere.model.*;
28 import org.compiere.util.*;
29
30 /**
31  * Web Window Servlet
32  *
33  * @author Jorg Janke
34  * @version $Id: WWindow.java,v 1.6 2003/10/10 01:04:07 jjanke Exp $
35  */

36 public class WWindow extends HttpServlet
37 {
38     /**
39      * Initialize global variables
40      * @param config
41      * @throws ServletException
42      */

43     public void init(ServletConfig config) throws ServletException
44     {
45         super.init (config);
46         if (!WEnv.initWeb(config))
47             throw new ServletException("WWindow.init");
48     } // init
49

50     /**
51      * Get Servlet information
52      * @return info
53      */

54     public String JavaDoc getServletInfo()
55     {
56         return "Compiere Web Window";
57     } // getServletInfo
58

59     /**
60      * Clean up resources
61      */

62     public void destroy()
63     {
64         Log.trace(Log.l1_User, "WWindow.destroy");
65     } // destroy
66

67     /** Window Number Counter */
68     private static int s_WindowNo = 1;
69     /** Form Name */
70     private static final String JavaDoc FORM_NAME = "WForm";
71
72     /** Hidden Parameter Command - Button */
73     private static final String JavaDoc P_Command = "PCommand";
74     /** Hidden Parameter - Tab No */
75     private static final String JavaDoc P_Tab = "PTab";
76     /** Hidden Parameter - MultiRow Row No */
77     private static final String JavaDoc P_MR_RowNo = "PMRRowNo";
78
79     // CSS Classes
80
private static final String JavaDoc C_MANDATORY = "Cmandatory";
81     private static final String JavaDoc C_ERROR = "Cerror";
82
83     /** Multi Row Lines per Screen */
84     private static final int MAX_LINES = 12;
85     /** Indicator for last line */
86     private static final int LAST_LINE = 999999;
87
88     /** Error Indicator */
89     private static final String JavaDoc ERROR = " ERROR! ";
90
91     /**
92      * Process the HTTP Get request - Initial Call.
93      * <br>
94      * http://localhost/compiere/WWindow?AD_Window_ID=123
95      * <br>
96      * Create Window with request parameters
97      * AD_Window_ID
98      * AD_Menu_ID
99      *
100      * Clean up old/existing window
101      *
102      * @param request
103      * @param response
104      * @throws ServletException
105      * @throws IOException
106      */

107     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
108     {
109         Log.trace(Log.l1_User, "WWindow.doGet");
110         // Get Session attributes
111
HttpSession sess = request.getSession();
112         Properties ctx = (Properties)sess.getAttribute(WEnv.SA_CONTEXT);
113         String JavaDoc loginInfo = (String JavaDoc)sess.getAttribute(WEnv.SA_LOGININFO);
114         if (ctx == null)
115         {
116             WUtil.createTimeoutPage(request, response, this, null, null);
117             return;
118         }
119
120         // Parameter: AD_Window_ID
121
int AD_Window_ID = WUtil.getParameterAsInt(request, "AD_Window_ID");
122         // Get Parameter: Menu_ID
123
int AD_Menu_ID = WUtil.getParameterAsInt(request, "AD_Menu_ID");
124         Log.trace(Log.l4_Data, "AD_Window_ID=" + AD_Window_ID
125             + "; AD_Menu_ID=" + AD_Menu_ID);
126
127         // Clean up old Window
128
WWindowStatus ws = (WWindowStatus)sess.getAttribute(WEnv.SA_WINDOW);
129         if (ws != null)
130         {
131             int WindowNo = ws.mWindow.getWindowNo();
132             Log.trace(Log.l4_Data, "WWindow.doGet - disposing",
133                 "WindowNo=" + WindowNo + ", ID=" + ws.mWindow.getAD_Window_ID());
134             ws.mWindow.dispose();
135             Env.clearWinContext(ctx, WindowNo);
136         }
137
138         /**
139          * New Window data
140          */

141         MWindowVO mWindowVO = MWindowVO.create (ctx, s_WindowNo++, AD_Window_ID, AD_Menu_ID);
142         if (mWindowVO == null)
143         {
144             String JavaDoc msg = Msg.translate(ctx, "AD_Window_ID") + " "
145                 + Msg.getMsg(ctx, "NotFound") + ", ID=" + AD_Window_ID + "/" + AD_Menu_ID;
146             WUtil.createErrorPage(request, response, this, ws==null ? null : ws.ctx, msg);
147             sess.setAttribute(WEnv.SA_WINDOW, null);
148             return;
149         }
150         // Create New Window
151
ws = new WWindowStatus(mWindowVO);
152         sess.setAttribute(WEnv.SA_WINDOW, ws);
153
154
155         // Query
156
ws.curTab.query(ws.mWindow.isTransaction());
157         ws.curTab.navigate(0);
158
159         /**
160          * Build Page
161          */

162         WDoc doc = preparePage(ws);
163
164         // Body
165
body b = doc.getBody();
166         if (ws.curTab.isSingleRow())
167             b.addElement(getSR_Form(request.getRequestURI(), ws, loginInfo));
168         else
169             b.addElement(getMR_Form(request.getRequestURI(), ws, loginInfo));
170
171         // fini
172
Log.trace(Log.l1_User, "WWindow.doGet", "fini");
173     // Log.trace(Log.l6_Database, doc.toString());
174
WUtil.createResponse (request, response, this, null, doc, true);
175         Log.trace(Log.l1_User, "WWindow.doGet - closed");
176     } // doGet
177

178
179     /*************************************************************************/
180
181     /**
182      * Process the HTTP Post request
183      *
184      * @param request
185      * @param response
186      * @throws ServletException
187      * @throws IOException
188      */

189     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
190     {
191         Log.trace(Log.l1_User, "WWindow.doPost");
192
193         // Get Session Info
194
HttpSession sess = request.getSession();
195         WWindowStatus ws = null;
196         if (sess != null)
197             ws = (WWindowStatus)sess.getAttribute(WEnv.SA_WINDOW);
198         if (ws == null)
199         {
200             Properties ctx = (Properties)sess.getAttribute(WEnv.SA_CONTEXT);
201             if (ctx == null)
202                 WUtil.createTimeoutPage(request, response, this, ctx, "No Context");
203             else
204                 doGet(request, response);
205             return;
206         }
207         String JavaDoc loginInfo = (String JavaDoc)sess.getAttribute(WEnv.SA_LOGININFO);
208
209         // Get Parameter: Command
210
String JavaDoc p_cmd = request.getParameter(P_Command);
211
212         /**
213          * Exit
214          */

215         if (p_cmd.equals("Exit"))
216         {
217             WUtil.createLoginPage(request, response, this, ws.ctx, "Exit");
218             return;
219         }
220         executeCommand(request, p_cmd, ws);
221
222         /**************************************************
223          * Build Page
224          */

225         WDoc doc = preparePage(ws);
226
227         // Body
228
body b = doc.getBody();
229         // Create Simgle/Multi Row
230
if (ws.curTab.isSingleRow())
231             b.addElement(getSR_Form(request.getRequestURI(), ws, loginInfo));
232         else
233             b.addElement(getMR_Form(request.getRequestURI(), ws, loginInfo));
234
235         //
236
Log.trace(Log.l1_User, "WWindow.doPost - fini");
237     // Log.trace(Log.l6_Database, doc.toString());
238
WUtil.createResponse (request, response, this, null, doc, true);
239     } // doPost
240

241     /*************************************************************************/
242
243     /**
244      * Prepare Page.
245      * - Set Header
246      *
247      * @param ws
248      * @return WDoc page
249      */

250     private WDoc preparePage(WWindowStatus ws)
251     {
252         WDoc doc = WDoc.create (ws.mWindow.getName());
253         head header = doc.getHead();
254         // add window.js & .css
255
header.addElement(new script("", WEnv.getBaseDirectory("window.js")));
256         header.addElement(new link().setRel("stylesheet").setHref(WEnv.getBaseDirectory("window.css")));
257         //
258
// Set Variables
259
script script = new script("deleteText='" + Msg.getMsg(ws.ctx, "DeleteRecord?") + "';");
260         doc.getBody().addElement(script);
261         //
262
return doc;
263     } // preparePage
264

265     /**
266      * Execute Command
267      *
268      * @param request
269      * @param p_cmd
270      * @param ws
271      */

272     private void executeCommand (HttpServletRequest request, String JavaDoc p_cmd, WWindowStatus ws)
273     {
274         // Get Parameter: Command and Tab changes
275
String JavaDoc p_tab = request.getParameter(P_Tab);
276         String JavaDoc p_row = request.getParameter(P_MR_RowNo); // MR Row Command
277
Log.trace(Log.l4_Data, "WWindow.executeCommand",
278             p_cmd + " - Tab=" + p_tab + " - Row=" + p_row);
279
280         /**
281          * Multi-Row Selection (i.e. display single row)
282          */

283         if (p_row != null && p_row.length() > 0)
284         {
285             try
286             {
287                 int newRowNo = Integer.parseInt (p_row);
288                 ws.curTab.navigate(newRowNo);
289                 ws.curTab.setSingleRow(true);
290             }
291             catch (Exception JavaDoc e)
292             {
293                 Log.error("WWindow.executeCommand - Parse RowNo="+ p_row, e);
294             }
295         }
296
297         /**
298          * Tab Change
299          */

300         else if (p_tab != null && p_tab.length() > 0)
301         {
302             int newTabNo = 0;
303             try
304             {
305                 newTabNo = Integer.parseInt (p_tab);
306             }
307             catch (Exception JavaDoc e)
308             {
309                 Log.error("WWindow.executeCommand - Parse TabNo="+ p_tab, e);
310             }
311             // move to detail
312
if (newTabNo > ws.curTab.getTabNo())
313             {
314                 ws.curTab = ws.mWindow.getTab(newTabNo);
315                 ws.curTab.query(false);
316                 ws.curTab.navigate(0);
317             }
318             // move back
319
else if (newTabNo < ws.curTab.getTabNo())
320             {
321                 ws.curTab = ws.mWindow.getTab(newTabNo);
322                 ws.curTab.dataRefresh();
323             }
324         }
325
326         /**
327          * Multi-Row Toggle
328          */

329         else if (p_cmd.equals("Multi"))
330         {
331             boolean single = ws.curTab.isSingleRow();
332             ws.curTab.setSingleRow(!single);
333             if (single)
334                 ws.curTab.navigate(0);
335         }
336
337         /**
338          * Position Commands
339          */

340         else if (p_cmd.equals("First"))
341         {
342             ws.curTab.navigate(0);
343         }
344         else if (p_cmd.equals("Next"))
345         {
346             ws.curTab.navigateRelative(+1); // multi row is positioned at last displayed row
347
}
348         else if (p_cmd.equals("Previous"))
349         {
350             int rows = ws.curTab.isSingleRow() ? -1 : -2*MAX_LINES;
351             ws.curTab.navigateRelative(rows);
352         }
353         else if (p_cmd.equals("Last"))
354         {
355             ws.curTab.navigateRelative(999999);
356         }
357
358
359         /**
360          * Find
361          */

362         else if (p_cmd.equals("Find"))
363         {
364             /** @todo Find */
365         }
366
367         /**
368          * Refresh
369          */

370         else if (p_cmd.equals("Refresh"))
371         {
372             ws.curTab.dataRefreshAll();
373         }
374
375         /**
376          * Attachment
377          */

378         else if (p_cmd.equals("Attachment"))
379         {
380             /** @todo Attachment */
381         }
382
383         /**
384          * History
385          */

386         else if (p_cmd.equals("History"))
387         {
388             if (ws.mWindow.isTransaction() && ws.curTab.getWindowNo() == 0)
389             {
390                 ws.curTab.query( !ws.curTab.isOnlyCurrentRows() );
391                 ws.curTab.navigate(0);
392             }
393         }
394
395         /**
396          * Report
397          */

398         else if (p_cmd.equals("Report"))
399         {
400             /** @todo Report */
401         }
402
403         /**
404          * Print
405          */

406         else if (p_cmd.equals("Print"))
407         {
408             /** @todo Print */
409         }
410
411         /**
412          * New
413          */

414         else if (p_cmd.equals("New"))
415         {
416             if (!ws.curTab.dataNew(false))
417                 ws.curTab.dataIgnore();
418         }
419
420         /**
421          * Delete
422          */

423         else if (p_cmd.equals("Delete"))
424         {
425             ws.curTab.dataDelete();
426         }
427
428         /**
429          * Save - Check for changed values
430          */

431         else if (p_cmd.equals("Save"))
432         {
433             executeSave (request, ws);
434         }
435     } // executeCommand
436

437     /**
438      * Execute Save
439      * @param request
440      * @param ws
441      */

442     private void executeSave (HttpServletRequest request, WWindowStatus ws)
443     {
444         Log.trace(Log.l5_DData, "WWindow.save");
445         boolean error = false;
446         // loop through parameters
447
Enumeration en = request.getParameterNames();
448         while (en.hasMoreElements())
449         {
450             String JavaDoc key = (String JavaDoc)en.nextElement();
451             // ignore hidden commands
452
if (key.equals(P_Command) || key.equals(P_MR_RowNo) || key.equals(P_Tab))
453                 continue;
454             MField mField = ws.curTab.getField(key);
455             // we found a writable field
456
if (mField != null && mField.isEditable(true))
457             {
458                 String JavaDoc value = request.getParameter(key);
459                 Object JavaDoc dbValue = mField.getValue();
460                 boolean fieldError = false;
461                 Log.trace(Log.l6_Database, mField.getColumnName(),
462                     dbValue==null ? "null" : dbValue.toString() + " -> " + value==null ? "null" : value.toString());
463                     // same = both null
464
if (dbValue == null && value == null)
465                     continue;
466                 // new value null
467
else if (dbValue != null && value == null)
468                     ws.curTab.setValue (mField, null);
469                 // from null to new value
470
else if (dbValue == null && value != null)
471                     fieldError = !setFieldValue (ws, mField, value);
472                 // same
473
else if (dbValue.equals(value))
474                     continue;
475                 else
476                     fieldError = !setFieldValue (ws, mField, value);
477                 //
478
if (!error && fieldError)
479                 {
480                     Log.trace(Log.l1_User, "WWindow.executeSave - Error", mField.getColumnName());
481                     error = fieldError;
482                 }
483             }
484         } // for all parameteres
485

486         // Check Mandatory
487
Log.trace(Log.l5_DData, "WWindow.save - Mandatory check");
488         int size = ws.curTab.getFieldCount();
489         for (int i = 0; i < size; i++)
490         {
491             MField field = ws.curTab.getField(i);
492             if (field.isMandatory(true)) // context check
493
{
494                 Object JavaDoc value = field.getValue();
495                 if (value == null || value.toString().length() == 0)
496                 {
497                     field.setInserting (true); // set editable otherwise deadlock
498
field.setError(true);
499                     field.setErrorValue(value == null ? null : value.toString());
500                     if (!error)
501                         error = true;
502                     Log.trace(Log.l1_User, "WWindow.executeSave - Mandatory Error", field.getColumnName());
503                 }
504                 else
505                     field.setError(false);
506             }
507         }
508
509         if (error)
510         {
511             Log.trace(Log.l4_Data, "WWindow.executeSave - Error");
512             return;
513         }
514
515         // save it - of errors ignore changes
516
if (!ws.curTab.dataSave(true))
517             ws.curTab.dataIgnore();
518         Log.trace(Log.l4_Data, "WWindow.executeSave - Done");
519     } // executeSave
520

521     /**
522      * Set Field Value
523      * @param ws
524      * @param mField
525      * @param value as String
526      * @return true if correct
527      */

528     private boolean setFieldValue(WWindowStatus ws, MField mField, String JavaDoc value)
529     {
530         Object JavaDoc newValue = getFieldValue (ws, mField, value);
531         if (ERROR.equals(newValue))
532         {
533             mField.setErrorValue(value);
534             return false;
535         }
536         Object JavaDoc dbValue = mField.getValue();
537         if ((newValue == null && dbValue != null)
538                 || (newValue != null && !newValue.equals(dbValue)))
539             ws.curTab.setValue(mField, newValue);
540         return true;
541     } // setFieldValue
542

543     /**
544      * Get Field value (convert value to datatype of MField)
545      *
546      * @param ws
547      * @param mField
548      * @param value String Value
549      * @return converted Field Value
550      */

551     private Object JavaDoc getFieldValue (WWindowStatus ws, MField mField, String JavaDoc value)
552     {
553         if (value == null || value.length() == 0)
554             return null;
555
556         int dt = mField.getDisplayType();
557
558         // BigDecimal
559
if (DisplayType.isNumeric(dt))
560         {
561             BigDecimal JavaDoc bd = null;
562             try
563             {
564                 Number JavaDoc nn = null;
565                 if (dt == DisplayType.Amount)
566                     nn = ws.amountFormat.parse(value);
567                 else if (dt == DisplayType.Quantity)
568                     nn = ws.quantityFormat.parse(value);
569                 else
570                     nn = ws.numberFormat.parse(value);
571                 if (nn instanceof BigDecimal JavaDoc)
572                     bd = (BigDecimal JavaDoc)nn;
573                 else
574                     bd = new BigDecimal JavaDoc(nn.toString());
575             }
576             catch (Exception JavaDoc e)
577             {
578                 Log.trace(Log.l6_Database, "BigDecimal", value + ERROR);
579                 return ERROR;
580             }
581             Log.trace(Log.l6_Database, "WWindow.getFieldValue - BigDecimal", value + " -> " + bd);
582             return bd;
583         }
584
585         // ID
586
else if (DisplayType.isID(dt))
587         {
588             Integer JavaDoc ii = null;
589             try
590             {
591                 ii = new Integer JavaDoc (value);
592             }
593             catch (Exception JavaDoc e)
594             {
595                 Log.error("WWindow.getFieldValue - ID=" + value, e);
596                 ii = null;
597             }
598             // -1 indicates NULL
599
if (ii.intValue() == -1)
600                 ii = null;
601             Log.trace(Log.l6_Database, "WWindow.getFieldValue - ID", value + " -> " + ii);
602             return ii;
603         }
604
605         // Date/Time
606
else if (DisplayType.isDate(dt))
607         {
608             Timestamp ts = null;
609             try
610             {
611                 java.util.Date JavaDoc d = null;
612                 if (dt == DisplayType.Date)
613                     d = ws.dateFormat.parse(value);
614                 else
615                     d = ws.dateTimeFormat.parse(value);
616                 ts = new Timestamp(d.getTime());
617             }
618             catch (Exception JavaDoc e)
619             {
620                 Log.trace(Log.l6_Database, "WWindow.getFieldValue - Date", value + ERROR);
621                 return ERROR;
622             }
623             Log.trace(Log.l6_Database, "WWindow.getFieldValue - Date", value + " -> " + ts);
624             return ts;
625         }
626
627         // Checkbox
628
else if (dt == DisplayType.YesNo)
629         {
630             if (value.equals("true"))
631                 return "Y";
632             return "N";
633         }
634
635         // treat as string
636
return value;
637     } // getFieldType
638

639
640     /*************************************************************************/
641
642     /**
643      * Return SingleRow Form details
644      * @param action
645      * @param ws
646      * @param loginInfo
647      * @return Form
648      */

649     private form getSR_Form (String JavaDoc action, WWindowStatus ws, String JavaDoc loginInfo)
650     {
651         Log.trace(Log.l2_Sub, "WWindow.getSR_Form - Tab=" + ws.curTab.getTabNo());
652
653         /**********************
654          * For all Fields
655          */

656         table table = new table().setAlign(AlignType.CENTER);
657         //
658
tr line = new tr();
659         int noFields = ws.curTab.getFieldCount();
660         for (int i = 0; i < noFields; i++)
661         {
662             MField field = ws.curTab.getField(i);
663             String JavaDoc colName = field.getColumnName();
664
665             /**
666              * Get Data and convert to String (singleRow)
667              */

668             Object JavaDoc data = ws.curTab.getValue(field);
669             String JavaDoc info = null;
670             if (data == null)
671                 info = "";
672             else
673             {
674                 switch(field.getDisplayType())
675                 {
676                     case DisplayType.Date:
677                         info = ws.dateFormat.format(data);
678                         break;
679                     case DisplayType.DateTime:
680                         info = ws.dateTimeFormat.format(data);
681                         break;
682                     case DisplayType.Amount:
683                         info = ws.amountFormat.format(data);
684                         break;
685                     case DisplayType.Number:
686                         info = ws.numberFormat.format(data);
687                         break;
688                     case DisplayType.Quantity:
689                         info = ws.quantityFormat.format(data);
690                         break;
691                     case DisplayType.Integer:
692                         info = ws.integerFormat.format(data);
693                         break;
694                     /** @todo output formatting */
695                     default:
696                         info = data.toString();
697                 }
698             }
699             if (info == null)
700                 info = "";
701
702             /**
703              * Display field
704              */

705             if (field.isDisplayed(false))
706             {
707                 if (!field.isSameLine())
708                     line = new tr();
709                 //
710
addField(line, field, data, info, false /*gc.getDisplayDependencies().contains(colName)*/);
711                 table.addElement(line);
712             }
713         } // for all fields
714

715         // Status Line
716
int rowNo = ws.curTab.getCurrentRow();
717         String JavaDoc statusDB = String.valueOf(rowNo+1) + " # " + ws.curTab.getRowCount();
718
719         return createLayout(action, table, ws,
720             loginInfo, "", statusDB);
721     } // getSR_Form
722

723
724     /*************************************************************************/
725
726     /**
727      * Return MultiRow Form details
728      * @param action
729      * @param ws
730      * @param loginInfo
731      * @return Form
732      */

733     private form getMR_Form (String JavaDoc action, WWindowStatus ws, String JavaDoc loginInfo)
734     {
735         Log.trace(Log.l2_Sub, "WWindow.getMR_Form - Tab=" + ws.curTab.getTabNo());
736
737         int initRowNo = ws.curTab.getCurrentRow();
738
739         /**
740          * Table Header
741          */

742         table table = new table().setAlign(AlignType.CENTER);
743         table.setClass("MultiRow");
744         table.setBorder(1);
745         table.setCellSpacing(1);
746         tr line = new tr();
747         // First Column
748
line.addElement(new th().addElement(" "));
749         // for all columns
750
int noFields = ws.curTab.getFieldCount();
751         for (int colNo = 0; colNo < noFields; colNo++)
752         {
753             MField field = ws.curTab.getField(colNo);
754             if (field.isDisplayed(false))
755             {
756                 th th = new th();
757                 th.addElement(field.getHeader()); // Name
758
th.setAbbr(field.getDescription()); // Description
759
line.addElement(th);
760             }
761         } // for all columns
762
table.addElement(new thead().addElement(line));
763
764         /**
765          * Table Lines
766          */

767         int lastRow = initRowNo + MAX_LINES;
768         lastRow = Math.min(lastRow, ws.curTab.getRowCount());
769         for (int lineNo = initRowNo; lineNo < lastRow; lineNo++)
770         {
771             // Row
772
ws.curTab.navigate(lineNo);
773
774             line = new tr();
775             // Selector
776
button selector = new button();
777             selector.addElement("&gt;"); // displays ">"
778
selector.setOnClick("document." + FORM_NAME + "." + P_MR_RowNo + ".value='" + lineNo + "'; submit();");
779             line.addElement(new td().addElement(selector));
780
781             // for all columns
782
for (int colNo = 0; colNo < noFields; colNo++)
783             {
784                 td td = new td();
785                 //
786
MField field = ws.curTab.getField(colNo);
787                 if (!field.isDisplayed(false))
788                     continue;
789
790                 // Get Data - turn to string
791
Object JavaDoc data = ws.curTab.getValue(field.getColumnName());
792                 String JavaDoc info = null;
793                 //
794
if (data == null)
795                     info = "";
796                 else
797                 {
798                     int dt = field.getDisplayType();
799                     switch (dt)
800                     {
801                         case DisplayType.Date:
802                             info = ws.dateFormat.format(data);
803                             td.setAlign("right");
804                             break;
805                         case DisplayType.DateTime:
806                             info = ws.dateTimeFormat.format(data);
807                             td.setAlign("right");
808                             break;
809                         case DisplayType.Amount:
810                             info = ws.amountFormat.format(data);
811                             td.setAlign("right");
812                             break;
813                         case DisplayType.Number:
814                             info = ws.numberFormat.format(data);
815                             td.setAlign("right");
816                             break;
817                         case DisplayType.Quantity:
818                             info = ws.quantityFormat.format(data);
819                             td.setAlign("right");
820                             break;
821                         case DisplayType.Integer:
822                             info = ws.integerFormat.format(data);
823                             td.setAlign("right");
824                             break;
825                         case DisplayType.YesNo:
826                             info = Msg.getMsg(ws.ctx, data.toString());
827                             break;
828                         /** @todo output formatting 2 */
829                         default:
830                             if (DisplayType.isLookup(dt))
831                                 info = field.getLookup().getDisplay(data);
832                             else
833                                 info = data.toString();
834                     }
835                 }
836
837                 // Empty info
838
if (info == null || info.length() == 0)
839                     info = "&nbsp;"; // Space
840
//
841
td.addElement(info);
842                 line.addElement(td);
843             } // for all columns
844
table.addElement(line);
845         } // for all table lines
846

847         // Status Line
848
String JavaDoc statusDB = String.valueOf(initRowNo+1) + "-" + String.valueOf(lastRow) + " # " + ws.curTab.getRowCount();
849
850         return createLayout(action, table, ws,
851             loginInfo, "", statusDB);
852     } // getMR_Form
853

854     /**
855      * Create Layout
856      *
857      * @param action
858      * @param table
859      * @param ws
860      * @param loginInfo
861      * @param statusInfo
862      * @param statusDB
863      * @return Form
864      */

865     private static form createLayout (String JavaDoc action, table table, WWindowStatus ws,
866         String JavaDoc loginInfo, String JavaDoc statusInfo, String JavaDoc statusDB)
867     {
868         Log.trace(Log.l3_Util, "WWindow.createLayout");
869         form myForm = null;
870         myForm = new form(action, form.post, form.ENC_DEFAULT);
871         myForm.setTarget(WEnv.TARGET_WINDOW);
872         String JavaDoc AD_Language = Env.getAD_Language(ws.ctx);
873
874         // Window
875
myForm.setName(FORM_NAME);
876         myForm.addElement(new input("hidden", P_Command, "")); // button commands
877
myForm.addElement(new input("hidden", P_MR_RowNo, "")); // RowNo
878
// Set Title of main window
879
String JavaDoc title = ws.mWindow.getName() + " - " + loginInfo;
880         myForm.addElement(new script("top.document.title='" + title + "';"));
881
882         // Buttons
883
myForm.addElement(createImage(AD_Language, "Ignore", "reset();", true, false));
884         myForm.addElement("&nbsp;");
885         myForm.addElement(createImage(AD_Language, "Help"));
886         myForm.addElement(createImage(AD_Language, "New"));
887         myForm.addElement(createImage(AD_Language, "Delete", "if (confirm(deleteText)) submit();", true, false));
888         myForm.addElement(createImage(AD_Language, "Save"));
889         myForm.addElement("&nbsp;");
890         myForm.addElement(createImage(AD_Language, "Find"));
891         myForm.addElement(createImage(AD_Language, "Refresh"));
892         myForm.addElement(createImage(AD_Language, "Attachment", null, ws.curTab.canHaveAttachment(), ws.curTab.hasAttachment()));
893         myForm.addElement(createImage(AD_Language, "Multi", null, true, !ws.curTab.isSingleRow()));
894         myForm.addElement("&nbsp;");
895         myForm.addElement(createImage(AD_Language, "History", null,
896             ws.mWindow.isTransaction()&&ws.curTab.getTabNo()==0, !ws.curTab.isOnlyCurrentRows()));
897         myForm.addElement("&nbsp;");
898         boolean isFirst = ws.curTab.getCurrentRow() < 1;
899         myForm.addElement(createImage(AD_Language, "First", null, !isFirst, false));
900         myForm.addElement(createImage(AD_Language, "Previous", null, !isFirst, false));
901         boolean isLast = ws.curTab.getCurrentRow()+1 == ws.curTab.getRowCount();
902         myForm.addElement(createImage(AD_Language, "Next", null, !isLast, false));
903         myForm.addElement(createImage(AD_Language, "Last", null, !isLast, false));
904         myForm.addElement("&nbsp;");
905         myForm.addElement(createImage(AD_Language, "Report"));
906         myForm.addElement(createImage(AD_Language, "Print"));
907         myForm.addElement("&nbsp;");
908         myForm.addElement(createImage(AD_Language, "Exit"));
909
910         // Tabs
911
myForm.addElement(new br());
912         myForm.addElement(new br());
913         myForm.addElement(new input("hidden", P_Tab, ""));
914         for (int i = 0; i < ws.mWindow.getTabCount(); i++)
915         {
916             MTab tab = ws.mWindow.getTab(i);
917             big big = new big(tab.getName());
918             if (ws.curTab.getTabNo() == i)
919                 big.setID("tabSelected"); // css
920
else
921             {
922                 big.setID("tab"); // css
923
big.setOnClick("alert('" + tab.getName() + "');");
924                 big.setOnClick("document." + FORM_NAME + "." + P_Tab + ".value='" + i + "';submit();");
925             }
926             // Status: Description
927
if (tab.getDescription().length() > 0)
928                 big.setOnMouseOver("status='" + tab.getDescription() + "';return true;");
929             myForm.addElement(big);
930         }
931
932         // Fields
933
myForm.addElement(new hr());
934         myForm.addElement(table);
935
936         // Status Line
937
tr statusline = new tr();
938         statusline.addElement(new td().setWidth("85%").addElement(statusInfo));
939         statusline.addElement(new td().setWidth("10%").setAlign("right").addElement(new small(statusDB)));
940         statusline.addElement(new td().setWidth("5%").setAlign("right").addElement(createImage(AD_Language, "Save")));
941         //
942
myForm.addElement(new hr());
943         myForm.addElement(new table().setWidth("100%").addElement(statusline));
944
945         // fini
946
myForm.addElement("\n");
947         /** @todo Dynamic Display */
948     // form.addElement(new Script("dynDisplay(); createWCmd();")); // initial Display & set Cmd Window
949
return myForm;
950     } // createLayout
951

952     /*************************************************************************/
953
954     /**
955      * Create Image with name, id of button_name and set P_Command onClick
956      * @param AD_Language
957      * @param name Name of the Image used also for Name24.gif
958      * @param js_command Java script command, null results in 'submit();', an empty string disables OnClick
959      * @param enabled Enable the immage button, if not uses the "D" image
960      * @param pressed If true, use the "X" image
961      * @return Image
962      */

963     private static img createImage (String JavaDoc AD_Language, String JavaDoc name, String JavaDoc js_command, boolean enabled, boolean pressed)
964     {
965         StringBuffer JavaDoc imgName = new StringBuffer JavaDoc(name);
966         if (!enabled)
967             imgName.append("D");
968         else if (pressed)
969             imgName.append("X");
970         imgName.append("24.gif");
971         //
972
img img = new img (WEnv.getImageDirectory(imgName.toString()), name);
973         if (enabled)
974             img.setAlt(Msg.getMsg(AD_Language, name)); // Translate ToolTip
975
//
976
if (!pressed || !enabled)
977             img.setID("imgButton"); // css
978
else
979             img.setID("imgButtonPressed"); // css
980
//
981
if (js_command == null)
982             js_command = "submit();";
983         if (js_command.length() > 0 && enabled)
984             img.setOnClick("document." + FORM_NAME + "." + P_Command + ".value='" + name + "';" + js_command);
985         //
986
return img;
987     } // createImage
988

989     /**
990      * Create enabled Image with name, id of button_name and sumbit command
991      * @param AD_Language
992      * @param name Name of the Image used also for Name24.gif
993      * @return Image
994      */

995     private static img createImage (String JavaDoc AD_Language, String JavaDoc name)
996     {
997         return createImage (AD_Language, name, null, true, false);
998     } // createImage
999

1000    /*************************************************************************/
1001
1002    /**
1003     * Add Field to Line
1004     * @param line
1005     * @param field
1006     * @param oData
1007     * @param data
1008     * @param isDependant - other columns are dependant on this column
1009     */

1010    private static void addField (tr line, MField field, Object JavaDoc oData, String JavaDoc data, boolean isDependant)
1011    {
1012        String JavaDoc columnName = field.getColumnName();
1013        boolean readOnly = !field.isEditable(false); // no context check
1014
boolean mandatory = field.isMandatory(false); // no context check
1015
// Any Error?
1016
boolean error = field.isErrorValue();
1017        if (error)
1018            data = field.getErrorValue();
1019        if (data == null)
1020            data = "";
1021
1022        /**
1023         * HTML Label Element
1024         * ID = ID_columnName
1025         *
1026         * HTML Input Elements
1027         * NAME = columnName
1028         * ID = ID_columnName
1029         */

1030        label label = new label(columnName).addElement(field.getHeader());
1031        label.setID("ID_" + columnName);
1032        label.setTitle(field.getDescription());
1033        int dt = field.getDisplayType();
1034
1035        // String, Date, Timestamp
1036
if (dt == DisplayType.String || DisplayType.isDate(dt))
1037        {
1038            line.addElement(new td().addElement(label).setAlign(AlignType.RIGHT));
1039            //
1040
input string = new input(input.text, columnName, data);
1041            string.setSize(field.getDisplayLength());
1042            if (dt == DisplayType.String)
1043                string.setMaxlength(field.getFieldLength());
1044            else
1045                string.setMaxlength(field.getDisplayLength());
1046            string.setID("ID_" + columnName);
1047            string.setDisabled(readOnly);
1048            if (error)
1049                string.setClass(C_ERROR);
1050            else if (mandatory)
1051                string.setClass(C_MANDATORY);
1052            if (isDependant)
1053                string.setOnChange("dynDisplay();");
1054            td td = new td().addElement(string).setAlign(AlignType.LEFT);
1055            if (field.isLongField())
1056                td.setColSpan(3);
1057            line.addElement(td);
1058        }
1059        // Numeric
1060
else if (DisplayType.isNumeric(dt))
1061        {
1062            line.addElement(new td().addElement(label).setAlign(AlignType.RIGHT));
1063            //
1064
input string = new input(input.text, columnName, data);
1065            string.setSize(field.getDisplayLength());
1066            string.setID("ID_" + columnName);
1067            string.setDisabled(readOnly);
1068            if (error)
1069                string.setClass(C_ERROR);
1070            else if (mandatory)
1071                string.setClass(C_MANDATORY);
1072            if (isDependant)
1073                string.setOnChange("dynDisplay();");
1074            line.addElement(new td().addElement(string).setAlign(AlignType.LEFT));
1075        }
1076        // Text
1077
else if (dt == DisplayType.Text)
1078        {
1079            line.addElement(new td().addElement(label).setAlign(AlignType.RIGHT).setVAlign(AlignType.TOP));
1080            //
1081
textarea text = new textarea(columnName, 3, field.getDisplayLength()).addElement(data);
1082            text.setID("ID_" + columnName);
1083            text.setDisabled(readOnly);
1084            if (error)
1085                text.setClass(C_ERROR);
1086            else if (mandatory)
1087                text.setClass(C_MANDATORY);
1088            if (isDependant)
1089                text.setOnChange("dynDisplay();");
1090            td td = new td().addElement(text).setAlign(AlignType.LEFT);
1091            if (field.isLongField())
1092                td.setColSpan(3);
1093            line.addElement(td);
1094        }
1095        else if (dt == DisplayType.Memo)
1096        {
1097            line.addElement(new td().addElement(label).setAlign(AlignType.RIGHT).setVAlign(AlignType.TOP));
1098            //
1099
textarea text = new textarea(columnName, 25, field.getDisplayLength()).addElement(data);
1100            text.setID("ID_" + columnName);
1101            text.setDisabled(readOnly);
1102            if (error)
1103                text.setClass(C_ERROR);
1104            else if (mandatory)
1105                text.setClass(C_MANDATORY);
1106            if (isDependant)
1107                text.setOnChange("dynDisplay();");
1108            td td = new td().addElement(text).setAlign(AlignType.LEFT);
1109            if (field.isLongField())
1110                td.setColSpan(3);
1111            line.addElement(td);
1112        }
1113        // CheckBox
1114
else if (dt == DisplayType.YesNo)
1115        {
1116            line.addElement(new td());
1117            //
1118
boolean check = data.equals("Y");
1119            input cb = new input(input.checkbox, columnName, "true").setChecked(check).addElement(field.getHeader());
1120            cb.setID("ID_" + columnName);
1121            cb.setDisabled(readOnly);
1122            if (error)
1123                cb.setClass(C_ERROR);
1124        // else if (mandatory) // looks odd
1125
// cb.setClass(C_MANDATORY);
1126
if (isDependant)
1127                cb.setOnChange("dynDisplay();");
1128            line.addElement(new td().addElement(cb).setAlign(AlignType.LEFT));
1129        }
1130        // Search
1131
else if (dt == DisplayType.Search)
1132        {
1133            line.addElement(new td().addElement(label).setAlign(AlignType.RIGHT));
1134            // The hidden field Name=columnName
1135
input hidden = new input(input.hidden, columnName, data);
1136            // The display field Name=columnName_D, ID=ID_columnName
1137
String JavaDoc displayData = field.getLookup().getDisplay(oData);
1138            input display = new input(input.text, columnName + "_D", displayData);
1139        // display.setSize(field.getDisplayLength()).setMaxlength(field.getFieldLength());
1140
display.setID("ID_" + columnName);
1141            display.setReadOnly(true);
1142            // The button Name=columnName_B, ID=ID_columnName
1143
input button = new input(input.image, columnName + "_B", "x");
1144            String JavaDoc gif = "PickOpen10.gif";
1145            if (columnName.equals("C_BPartner_ID"))
1146                gif = "BPartner10.gif";
1147            else if (columnName.equals("M_Product_ID"))
1148                gif = "Product10.gif";
1149            button.setSrc(WEnv.getImageDirectory(gif));
1150            button.setBorder(1);
1151            button.setOnClick("return startLookup(" + columnName + ");");
1152            if (error)
1153                display.setClass(C_ERROR);
1154            else if (mandatory)
1155                display.setClass(C_MANDATORY);
1156            if (isDependant)
1157                display.setOnChange("dynDisplay();");
1158            td td = new td()
1159                .addElement(hidden)
1160                .addElement(display)
1161                .addElement(button)
1162                .setAlign(AlignType.LEFT);
1163            line.addElement(td);
1164        }
1165        // Location
1166
else if (dt == DisplayType.Location)
1167        {
1168            line.addElement(new td().addElement(label).setAlign(AlignType.RIGHT));
1169            // The hidden field Name=columnName
1170
input hidden = new input(input.hidden, columnName, data);
1171            // The display field Name=columnName_D, ID=ID_columnName
1172
String JavaDoc displayData = field.getLookup().getDisplay(oData);
1173            input display = new input(input.text, columnName + "_D", displayData);
1174        // display.setSize(field.getDisplayLength()).setMaxlength(field.getFieldLength());
1175
display.setID("ID_" + columnName);
1176            display.setReadOnly(true);
1177            // The button Name=columnName_B, ID=ID_columnName
1178
input button = new input(input.image, columnName + "_B", "l");
1179            button.setSrc(WEnv.getImageDirectory("Location10.gif")).setBorder(1);
1180            button.setOnClick("return startLocation(" + columnName + ");");
1181            if (error)
1182                display.setClass(C_ERROR);
1183            else if (mandatory)
1184                display.setClass(C_MANDATORY);
1185            if (isDependant)
1186                display.setOnChange("dynDisplay();");
1187            td td = new td()
1188                .addElement(hidden)
1189                .addElement(display)
1190                .addElement(button)
1191                .setAlign(AlignType.LEFT);
1192            line.addElement(td);
1193        }
1194        // Account
1195
else if (dt == DisplayType.Account)
1196        {
1197            line.addElement(new td().addElement(label).setAlign(AlignType.RIGHT));
1198            // The hidden field Name=columnName
1199
input hidden = new input(input.hidden, columnName, data);
1200            // The display field Name=columnName_D, ID=ID_columnName
1201
String JavaDoc displayData = field.getLookup().getDisplay(oData);
1202            input display = new input(input.text, columnName + "_D", displayData);
1203        // display.setSize(field.getDisplayLength()).setMaxlength(field.getFieldLength());
1204
display.setID("ID_" + columnName);
1205            display.setReadOnly(true);
1206            // The button Name=columnName_B, ID=ID_columnName
1207
input button = new input(input.image, columnName + "_B", "x");
1208            button.setSrc(WEnv.getImageDirectory("Account10.gif"));
1209            button.setBorder(1);
1210            button.setOnClick("return startAccount(" + columnName + ");");
1211            if (error)
1212                display.setClass(C_ERROR);
1213            else if (mandatory)
1214                display.setClass(C_MANDATORY);
1215            if (isDependant)
1216                display.setOnChange("dynDisplay();");
1217            td td = new td()
1218                .addElement(hidden)
1219                .addElement(display)
1220                .addElement(button)
1221                .setAlign(AlignType.LEFT);
1222            line.addElement(td);
1223        }
1224        // Lookup, Loactor -> Popup
1225
else if (DisplayType.isLookup(dt) || dt == DisplayType.Locator)
1226        {
1227            line.addElement(new td().addElement(label).setAlign(AlignType.RIGHT));
1228            //
1229
Lookup lookup = field.getLookup();
1230            Object JavaDoc[] list = lookup.getData(mandatory, true, false, false).toArray(); // also inactive
1231
int size = list.length;
1232            option options[] = new option[size];
1233            if (size > 0)
1234            {
1235                boolean isNumber = list[0] instanceof KeyNamePair;
1236                for (int i = 0; i < size; i++)
1237                {
1238                    String JavaDoc key = null;
1239                    if (dt == DisplayType.Locator)
1240                    {
1241                        MLocator loc = (MLocator)list[i];
1242                        key = String.valueOf(loc.getM_Locator_ID());
1243                        options[i] = new option(key).addElement(loc.getValue());
1244                    }
1245                    else if (isNumber)
1246                    {
1247                        KeyNamePair p = (KeyNamePair)list[i];
1248                        key = String.valueOf(p.getKey());
1249                        options[i] = new option(key).addElement(p.getName());
1250                    }
1251                    else
1252                    {
1253                        ValueNamePair p = (ValueNamePair)list[i];
1254                        key = p.getValue();
1255                        if (key == null || key.length() == 0)
1256                            key = "??";
1257                        String JavaDoc name = p.getName();
1258                        if (name == null || name.length() == 0)
1259                            name = "???";
1260                        options[i] = new option(key).addElement(name);
1261                    }
1262                    if (data.equals(key))
1263                        options[i].setSelected(true);
1264                }
1265            }
1266            select sel = new select(columnName, options);
1267            sel.setID("ID_" + columnName);
1268            sel.setDisabled(readOnly);
1269            if (error)
1270                sel.setClass(C_ERROR);
1271            else if (mandatory)
1272                sel.setClass(C_MANDATORY);
1273            if (isDependant)
1274                sel.setOnChange("dynDisplay();");
1275            line.addElement(new td().addElement(sel));
1276        }
1277        // button
1278
else if (dt == DisplayType.Button)
1279        {
1280            line.addElement(new td());
1281            //
1282
input b = new input(input.button, columnName, field.getHeader());
1283            b.setID("ID_" + columnName);
1284            b.setDisabled(readOnly);
1285        // b.setOnClick("submit();");
1286
line.addElement(new td().addElement(b).setAlign(AlignType.LEFT));
1287        }
1288
1289        // Unknown
1290
else
1291        {
1292            line.addElement(new td().addElement(label).setAlign(AlignType.RIGHT));
1293            line.addElement(new td().addElement("[" + data + "]").setID("ID_" + columnName));
1294        }
1295
1296        // Additional Values
1297
String JavaDoc dispLogic = field.getDisplayLogic();
1298        if (dispLogic == null)
1299            dispLogic = "";
1300        if (dispLogic.length() > 0)
1301        {
1302            dispLogic = dispLogic.replace('\'', '"'); // replace ' with "
1303
String JavaDoc attrib = "document." + FORM_NAME + "." + columnName + ".displayLogic='" + dispLogic + "';";
1304            line.addElement(new script(attrib));
1305        }
1306    } // addField
1307

1308} // WWindow
1309
Popular Tags