KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > AEnv


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.apps;
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import javax.swing.*;
19 import java.rmi.*;
20 import java.io.*;
21 import java.net.*;
22 import java.util.*;
23 import javax.naming.*;
24
25 import org.compiere.*;
26 import org.compiere.db.*;
27 import org.compiere.util.*;
28 import org.compiere.swing.*;
29 import org.compiere.server.*;
30 import org.compiere.model.*;
31
32 import org.compiere.interfaces.*;
33 import javax.ejb.*;
34
35 /**
36  * Windows Application Environment and utilities
37  *
38  * @author Jorg Janke
39  * @version $Id: AEnv.java,v 1.45 2003/11/01 02:32:21 jjanke Exp $
40  */

41 public final class AEnv
42 {
43     /**
44      * Show in the center of the screen.
45      * (pack, set location and set visibility)
46      *
47      * @param window Window to position
48      */

49     public static void showCenterScreen(Window window)
50     {
51         positionCenterScreen(window);
52         window.setVisible(true);
53     } // showCenterScreen
54

55     /**
56      * Position window in center of the screen
57      *
58      * @param window Window to position
59      */

60     public static void positionCenterScreen(Window window)
61     {
62         window.pack();
63         Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
64         Dimension wSize = window.getSize();
65         int maxWidth = (int)(sSize.width*.97);
66         int maxHeight = (int)(sSize.height*.97);
67         // fit on window
68
if (wSize.height > maxHeight)
69             wSize.height = maxHeight;
70         if (wSize.width > maxWidth)
71             wSize.width = maxWidth;
72         window.setSize(wSize);
73         int x = (sSize.width - wSize.width) / 2;
74         int y = (sSize.height - wSize.height) / 2;
75         //
76
window.setLocation(x, y);
77         window.toFront();
78     } // positionCenterScreen
79

80     /**
81      * Position in center of the parent window.
82      * (pack, set location and set visibility)
83      *
84      * @param parent Parent Window
85      * @param window Window to position
86      */

87     public static void showCenterWindow(Window parent, Window window)
88     {
89         positionCenterWindow(parent, window);
90         window.setVisible(true);
91     } // showCenterWindow
92

93     /**
94      * Position in center of the parent window
95      *
96      * @param parent Parent Window
97      * @param window Window to position
98      */

99     public static void positionCenterWindow(Window parent, Window window)
100     {
101         if (parent == null)
102         {
103             positionCenterScreen(window);
104             return;
105         }
106         window.pack();
107         //
108
Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
109         Dimension wSize = window.getSize();
110         int maxWidth = (int)(sSize.width*.97);
111         int maxHeight = (int)(sSize.height*.97);
112         // fit on window
113
if (wSize.height > maxHeight)
114             wSize.height = maxHeight;
115         if (wSize.width > maxWidth)
116             wSize.width = maxWidth;
117         window.setSize(wSize);
118         // center in parent
119
Rectangle pBounds = parent.getBounds();
120         // Parent is in upper left corner
121
if (pBounds.x == pBounds.y && pBounds.x == 0)
122         {
123             positionCenterScreen(window);
124             return;
125         }
126         // Find middle
127
int x = pBounds.x + ((pBounds.width-wSize.width)/2);
128         if (x < 0)
129             x = 0;
130         int y = pBounds.y + ((pBounds.height-wSize.height)/2);
131         if (y < 0)
132             y = 0;
133
134         // Is it on Screen?
135
if (x + wSize.width > sSize.width)
136             x = sSize.width - wSize.width;
137         if (y + wSize.height > sSize.height)
138             y = sSize.height - wSize.height;
139         //
140
// System.out.println("Position: x=" + x + " y=" + y + " w=" + wSize.getWidth() + " h=" + wSize.getHeight()
141
// + " - Parent loc x=" + pLoc.x + " y=" + y + " w=" + pSize.getWidth() + " h=" + pSize.getHeight());
142
window.setLocation(x, y);
143         window.toFront();
144     } // positionCenterScreen
145

146     /*************************************************************************/
147
148     
149     public static CButton getButton (String JavaDoc iconName)
150     {
151         CButton button = new CButton(Env.getImageIcon(iconName + "16.gif"));
152         button.setMargin(new Insets (0, 0, 0, 0));
153         button.setToolTipText(Msg.getMsg(Env.getCtx(), iconName));
154         button.setDefaultCapable(false);
155         return button;
156     } // getButton
157

158
159     /**
160      * Create Menu Title (translate it and set Mnemonics).
161      * Based on MS notation of &Help => H is Mnemonics
162      *
163      * @param AD_Message message
164      * @return JMenu
165      */

166     public static JMenu getMenu (String JavaDoc AD_Message)
167     {
168         JMenu menu = new JMenu();
169         String JavaDoc text = Msg.getMsg(Env.getCtx(), AD_Message);
170         int pos = text.indexOf("&");
171         if (pos != -1) // We have a nemonic
172
{
173             char ch = text.charAt(pos+1);
174             text = text.substring(0, pos) + text.substring(pos+1);
175             menu.setMnemonic(ch);
176         }
177         menu.setText(text);
178         return menu;
179     } // getMenu
180

181     /**
182      * Create Menu Item
183      * @param action action command
184      * @param iconName optional name of the icon, defaults to action if null
185      * @param ks optional key stroke
186      * @param menu menu to add menu item to
187      * @param al action listener to register
188      * @return MenuItem
189      */

190     public static JMenuItem addMenuItem (String JavaDoc action, String JavaDoc iconName, KeyStroke ks,
191         JMenu menu, ActionListener al)
192     {
193         if (iconName == null)
194             iconName = action;
195         JMenuItem mi = new JMenuItem(Msg.getMsg(Env.getCtx(), action), Env.getImageIcon(iconName + "16.gif"));
196         mi.setActionCommand(action);
197         if (ks != null)
198             mi.setAccelerator(ks);
199         if (menu != null)
200             menu.add(mi);
201         if (al != null)
202             mi.addActionListener(al);
203         return mi;
204     } // addMeniItem
205

206     /**
207      * Perform action command for common menu items
208      * @param actionCommand known action command
209      * @param WindowNo window no
210      * @param c Container parent
211      * @return true if actionCommand was found and performed
212      */

213     public static boolean actionPerformed (String JavaDoc actionCommand, int WindowNo, Container c)
214     {
215         // File Menu ------------------------
216
if (actionCommand.equals("PrintScreen"))
217         {
218             PrintScreenPainter.printScreen (Env.getFrame(c));
219         }
220         else if (actionCommand.equals("ScreenShot"))
221         {
222             ScreenShot.createJPEG(Env.getFrame(c), null);
223         }
224         else if (actionCommand.equals("Report"))
225         {
226             AEnv.showCenterScreen (new ProcessStart());
227         }
228         else if (actionCommand.equals("Exit"))
229         {
230             if (ADialog.ask(WindowNo, c, "ExitApplication?"))
231                 Env.exitEnv(0);
232         }
233
234         // View Menu ------------------------
235
else if (actionCommand.equals("InfoProduct"))
236         {
237             org.compiere.apps.search.Info.showProduct (Env.getFrame(c), WindowNo);
238         }
239         else if (actionCommand.equals("InfoBPartner"))
240         {
241             org.compiere.apps.search.Info.showBPartner (Env.getFrame(c), WindowNo);
242         }
243         else if (actionCommand.equals("InfoAsset"))
244         {
245             org.compiere.apps.search.Info.showAsset (Env.getFrame(c), WindowNo);
246         }
247         else if (actionCommand.equals("InfoAccount") && MRole.getDefault().isShowAcct())
248         {
249             new org.compiere.acct.AcctViewer();
250         }
251         else if (actionCommand.equals("InfoSchedule"))
252         {
253             new org.compiere.apps.search.InfoSchedule (Env.getFrame(c), null, false);
254         }
255         else if (actionCommand.equals("InfoOrder"))
256         {
257             org.compiere.apps.search.Info.showOrder (Env.getFrame(c), WindowNo, "");
258         }
259         else if (actionCommand.equals("InfoInvoice"))
260         {
261             org.compiere.apps.search.Info.showInvoice (Env.getFrame(c), WindowNo, "");
262         }
263         else if (actionCommand.equals("InfoInOut"))
264         {
265             org.compiere.apps.search.Info.showInOut (Env.getFrame(c), WindowNo, "");
266         }
267         else if (actionCommand.equals("InfoPayment"))
268         {
269             org.compiere.apps.search.Info.showPayment (Env.getFrame(c), WindowNo, "");
270         }
271         else if (actionCommand.equals("InfoCashLine"))
272         {
273             org.compiere.apps.search.Info.showCashLine (Env.getFrame(c), WindowNo, "");
274         }
275         else if (actionCommand.equals("InfoAssignment"))
276         {
277             org.compiere.apps.search.Info.showAssignment (Env.getFrame(c), WindowNo, "");
278         }
279
280         // Go Menu ------------------------
281
else if (actionCommand.equals("Home"))
282         {
283             Env.getWindow(0).toFront();
284         }
285
286         // Tools Menu ------------------------
287
else if (actionCommand.equals("Calculator"))
288         {
289             AEnv.showCenterScreen (new org.compiere.grid.ed.Calculator(Env.getFrame(c)));
290         }
291         else if (actionCommand.equals("Calendar"))
292         {
293             AEnv.showCenterScreen (new org.compiere.grid.ed.Calendar(Env.getFrame(c)));
294         }
295         else if (actionCommand.equals("Editor"))
296         {
297             AEnv.showCenterScreen (new org.compiere.grid.ed.Editor(Env.getFrame(c)));
298         }
299         else if (actionCommand.equals("Script"))
300         {
301             new ScriptEditor();
302         }
303         else if (actionCommand.equals("Preference"))
304         {
305             AEnv.showCenterScreen(new Preference(Env.getFrame(c), WindowNo, true));
306         }
307
308         // Help Menu ------------------------
309
else if (actionCommand.equals("Online"))
310         {
311             Env.startBrowser(org.compiere.Compiere.getURL());
312         }
313         else if (actionCommand.equals("SendMail"))
314         {
315             ADialog.createSupportEMail(Env.getFrame(c), Env.getFrame(c).getTitle(), "\n\n");
316         }
317         else if (actionCommand.equals("About"))
318         {
319             AEnv.showCenterScreen(new AboutBox(Env.getFrame(c)));
320         }
321         else
322             return false;
323         //
324
return true;
325     } // actionPerformed
326

327     /**
328      * Set Text and Mnemonic for Button.
329      * Create Mnemonics of text containing "&".
330      * Based on MS notation of &Help => H is Mnemonics
331      * @param b The button
332      * @param text The text with optional Mnemonics
333      */

334     public static void setTextMnemonic (JButton b, String JavaDoc text)
335     {
336         if (text == null || b == null)
337             return;
338         int pos = text.indexOf("&");
339         if (pos != -1) // We have a nemonic
340
{
341             char ch = text.charAt(pos+1);
342             b.setMnemonic(ch);
343             b.setText(text.substring(0, pos) + text.substring(pos+1));
344         }
345         b.setText(text);
346     } // setTextMnemonic
347

348     /**
349      * Get Mnemonic character from text.
350      * @param text text with "&"
351      * @return Mnemonic or 0
352      */

353     public static char getMnemonic (String JavaDoc text)
354     {
355         int pos = text.indexOf("&");
356         if (pos != -1) // We have a nemonic
357
return text.charAt(pos+1);
358         return 0;
359     } // getMnemonic
360

361     /*************************************************************************/
362
363     /**
364      * Print Action and Input Map for component
365      *
366      * @param comp Component with ActionMap
367      */

368     public static void printActionInputMap (JComponent comp)
369     {
370         int ll = 10; // log level
371
if (Log.getTraceLevel() < ll)
372             return;
373         //
374
Log.trace(ll-1, "ActionMap for Component", comp.toString());
375         ActionMap am = comp.getActionMap();
376         Object JavaDoc[] amKeys = am.allKeys(); // including Parents
377
for (int i = 0; i < amKeys.length; i++)
378         {
379             Action a = am.get(amKeys[i]);
380
381             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Name=");
382             sb.append(a.getValue(Action.NAME))
383                 .append(", Acc=").append(a.getValue(Action.ACCELERATOR_KEY))
384                 .append(", Mem=").append(a.getValue(Action.MNEMONIC_KEY))
385                 .append(" - ").append(a.getValue(Action.SHORT_DESCRIPTION));
386             if (a.getValue(Action.ACTION_COMMAND_KEY) == null)
387                 Log.trace(ll, a.toString());
388             else
389                 Log.trace(ll, a.getValue(Action.ACTION_COMMAND_KEY).toString(),
390                     sb.toString());
391         }
392         //
393
Log.trace(ll-1, "InputMap for Component ", comp.toString());
394         InputMap im = comp.getInputMap();
395         KeyStroke[] kStrokes = im.allKeys();
396         if (kStrokes != null)
397         {
398             for (int i = 0; i < kStrokes.length; i++)
399             {
400                 Log.trace(ll, kStrokes[i].toString(),
401                     im.get(kStrokes[i]).toString());
402             }
403         }
404         //
405
Log.trace(ll-1, "InputMap for Component When Focused ", comp.toString());
406         im = comp.getInputMap(JComponent.WHEN_FOCUSED);
407         kStrokes = im.allKeys();
408         if (kStrokes != null)
409         {
410             for (int i = 0; i < kStrokes.length; i++)
411             {
412                 Log.trace(ll, kStrokes[i].toString(),
413                     im.get(kStrokes[i]).toString());
414             }
415         }
416         Log.trace(ll-1, "InputMap for Component When Focused in Window", comp.toString());
417         im = comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
418         kStrokes = im.allKeys();
419         if (kStrokes != null)
420         {
421             for (int i = 0; i < kStrokes.length; i++)
422             {
423                 Log.trace(ll, kStrokes[i].toString(),
424                     im.get(kStrokes[i]).toString());
425             }
426         }
427         Log.trace(ll-1, "InputMap for Component When Ancestor ", comp.toString());
428         im = comp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
429         kStrokes = im.allKeys();
430         if (kStrokes != null)
431         {
432             for (int i = 0; i < kStrokes.length; i++)
433             {
434                 Log.trace(ll, kStrokes[i].toString(),
435                     im.get(kStrokes[i]).toString());
436             }
437         }
438     } // printActionInputMap
439

440     /*************************************************************************/
441
442     /**
443      * Exit System
444      * @param status System exit status (usually 0 for no error)
445      */

446     public static void exit (int status)
447     {
448         if (s_server != null)
449         {
450             try
451             {
452                 s_server.remove();
453             }
454             catch (Exception JavaDoc ex)
455             {
456             }
457         }
458         Env.exitEnv(status);
459     } // exit
460

461
462     /*************************************************************************/
463
464     /** Server Re-tries */
465     private static int s_serverTries = 0;
466     /** Server Session */
467     private static Server s_server = null;
468
469     /**
470      * Is AppsServer Active ?
471      * @return true if active
472      */

473     public static boolean isServerActive()
474     {
475         boolean ok = CConnection.get().isAppsServerOK(false);
476         if (ok)
477         {
478             s_serverTries = 0;
479             return true;
480         }
481         if (s_serverTries > 1) // try twice
482
return false;
483
484         // Try to connect
485
Logger.switchLoggingOff();
486         try
487         {
488             s_serverTries++;
489             Log.trace(Log.l3_Util, "AEnv.isServerActive - try #" + s_serverTries);
490             ok = CConnection.get().isAppsServerOK(true);
491         }
492         catch (Exception JavaDoc ex)
493         {
494             ok = false;
495             s_server = null;
496         }
497         Logger.switchLoggingOn();
498         //
499
return ok;
500     } // isServerActive
501

502     /**
503      * Get Server Version
504      * @return Apps Server Version
505      * @see ALogin#checkVersion
506      */

507     public static String JavaDoc getServerVersion ()
508     {
509         return CConnection.get().getServerVersion();
510     } // getServerVersion
511

512     /**
513      * Get Window Model
514      *
515      * @param WindowNo Window No
516      * @param AD_Window_ID window
517      * @param AD_Menu_ID menu
518      * @return Model Window Value Obkect
519      */

520     public static MWindowVO getMWindowVO (int WindowNo, int AD_Window_ID, int AD_Menu_ID)
521     {
522         Log.trace(Log.l2_Sub, "AEnv.getWindowVO", "Window=" + WindowNo + ", AD_Window_ID=" + AD_Window_ID);
523         MWindowVO mWindowVO = null;
524         // try to get from Server when enabled
525
if (Ini.getPropertyBool(Ini.P_OBJECTS) && isServerActive())
526         {
527             Log.trace(Log.l3_Util, "AEnv.getWindowVO", "trying server");
528             try
529             {
530                 Server server = CConnection.get().getServer();
531                 if (server != null)
532                 {
533                     mWindowVO = server.getWindowVO(Env.getCtx(), WindowNo, AD_Window_ID, AD_Menu_ID);
534                     Log.trace(Log.l3_Util, "AEnv.getWindowVO", "from Server: success");
535                 }
536             }
537             catch (RemoteException e)
538             {
539                 Log.error("AEnv.getWindowVO (RE) " + e);
540                 mWindowVO = null;
541                 s_server = null;
542             }
543             catch (Exception JavaDoc e)
544             {
545                 Log.error("AEnv.getWindowVO " + e);
546                 mWindowVO = null;
547                 s_server = null;
548             }
549             catch (Throwable JavaDoc t)
550             {
551                 Log.error("AEnv.getWindowVO - " + t);
552                 mWindowVO = null;
553                 s_server = null;
554             }
555         }
556
557         // Create Window Model on Client
558
if (mWindowVO == null)
559         {
560             Log.trace(Log.l3_Util, "AEnv.getWindowVO", "create local");
561             mWindowVO = MWindowVO.create (Env.getCtx(), WindowNo, AD_Window_ID, AD_Menu_ID);
562         }
563         // Check (remote) context
564
else if (!mWindowVO.ctx.equals(Env.getCtx()))
565         {
566             // Remote Context is called by value, not reference
567
// Add Window properties to context
568
Enumeration keyEnum = mWindowVO.ctx.keys();
569             while (keyEnum.hasMoreElements())
570             {
571                 String JavaDoc key = (String JavaDoc)keyEnum.nextElement();
572                 if (key.startsWith(WindowNo+"|"))
573                 {
574                     String JavaDoc value = mWindowVO.ctx.getProperty (key);
575                     Env.setContext(Env.getCtx(), key, value);
576                 }
577             }
578             // Sync Context
579
mWindowVO.setCtx(Env.getCtx());
580         }
581         return mWindowVO;
582     } // getWindow
583

584     /**
585      * Post Immediate
586      *
587      * @param WindowNo window
588      * @param AD_Table_ID Table ID of Document
589      * @param AD_Client_ID Client ID of Document
590      * @param Record_ID Record ID of this document
591      * @param force force posting
592      * @return true if success
593      */

594     public static boolean postImmediate (int WindowNo, int AD_Client_ID, int AD_Table_ID, int Record_ID, boolean force)
595     {
596         Log.trace(Log.l2_Sub, "AEnv.postImmediate", "Window=" + WindowNo + ", AD_Table_ID=" + AD_Table_ID + "/" + Record_ID);
597
598         Boolean JavaDoc result = null;
599         String JavaDoc msg = null;
600         // try to get from Server when enabled
601
if (isServerActive())
602         {
603             Log.trace(Log.l3_Util, "AEnv.postImmediate", "trying server");
604             try
605             {
606                 Server server = CConnection.get().getServer();
607                 if (server != null)
608                 {
609                     result = new Boolean JavaDoc (server.postImmediate(AD_Table_ID, AD_Client_ID, Record_ID, force));
610                     Log.trace(Log.l3_Util, "AEnv.postImmediate", "from Server: success");
611                 }
612                 else
613                 {
614                     ADialog.error(WindowNo, null, "NoAppsServer", msg);
615                     return false;
616                 }
617             }
618             catch (RemoteException e)
619             {
620                 Log.error("AEnv.postImmediate (RE)", e);
621                 msg = e.getMessage();
622                 result = null;
623                 s_server = null;
624             }
625             catch (Exception JavaDoc e)
626             {
627                 Log.error("AEnv.postImmediate", e);
628                 msg = e.getMessage();
629                 result = null;
630                 s_server = null;
631             }
632         }
633         else
634         {
635             ADialog.error(WindowNo, null, "NoAppsServer", msg);
636             return false;
637         }
638         if (result == null)
639         {
640             ADialog.error(WindowNo, null, "PostServerError", msg);
641             return false;
642         }
643         return result.booleanValue();
644     } // postImmediate
645

646 } // AEnv
647
Popular Tags