1 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 41 public final class AEnv 42 { 43 49 public static void showCenterScreen(Window window) 50 { 51 positionCenterScreen(window); 52 window.setVisible(true); 53 } 55 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 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 window.setLocation(x, y); 77 window.toFront(); 78 } 80 87 public static void showCenterWindow(Window parent, Window window) 88 { 89 positionCenterWindow(parent, window); 90 window.setVisible(true); 91 } 93 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 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 if (wSize.height > maxHeight) 114 wSize.height = maxHeight; 115 if (wSize.width > maxWidth) 116 wSize.width = maxWidth; 117 window.setSize(wSize); 118 Rectangle pBounds = parent.getBounds(); 120 if (pBounds.x == pBounds.y && pBounds.x == 0) 122 { 123 positionCenterScreen(window); 124 return; 125 } 126 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 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 window.setLocation(x, y); 143 window.toFront(); 144 } 146 147 148 149 public static CButton getButton (String 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 } 158 159 166 public static JMenu getMenu (String AD_Message) 167 { 168 JMenu menu = new JMenu(); 169 String text = Msg.getMsg(Env.getCtx(), AD_Message); 170 int pos = text.indexOf("&"); 171 if (pos != -1) { 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 } 181 190 public static JMenuItem addMenuItem (String action, String 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 } 206 213 public static boolean actionPerformed (String actionCommand, int WindowNo, Container c) 214 { 215 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 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 else if (actionCommand.equals("Home")) 282 { 283 Env.getWindow(0).toFront(); 284 } 285 286 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 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 return true; 325 } 327 334 public static void setTextMnemonic (JButton b, String text) 335 { 336 if (text == null || b == null) 337 return; 338 int pos = text.indexOf("&"); 339 if (pos != -1) { 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 } 348 353 public static char getMnemonic (String text) 354 { 355 int pos = text.indexOf("&"); 356 if (pos != -1) return text.charAt(pos+1); 358 return 0; 359 } 361 362 363 368 public static void printActionInputMap (JComponent comp) 369 { 370 int ll = 10; if (Log.getTraceLevel() < ll) 372 return; 373 Log.trace(ll-1, "ActionMap for Component", comp.toString()); 375 ActionMap am = comp.getActionMap(); 376 Object [] amKeys = am.allKeys(); for (int i = 0; i < amKeys.length; i++) 378 { 379 Action a = am.get(amKeys[i]); 380 381 StringBuffer sb = new StringBuffer ("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 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 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 } 440 441 442 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 ex) 455 { 456 } 457 } 458 Env.exitEnv(status); 459 } 461 462 463 464 465 private static int s_serverTries = 0; 466 467 private static Server s_server = null; 468 469 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) return false; 483 484 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 ex) 493 { 494 ok = false; 495 s_server = null; 496 } 497 Logger.switchLoggingOn(); 498 return ok; 500 } 502 507 public static String getServerVersion () 508 { 509 return CConnection.get().getServerVersion(); 510 } 512 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 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 e) 544 { 545 Log.error("AEnv.getWindowVO " + e); 546 mWindowVO = null; 547 s_server = null; 548 } 549 catch (Throwable t) 550 { 551 Log.error("AEnv.getWindowVO - " + t); 552 mWindowVO = null; 553 s_server = null; 554 } 555 } 556 557 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 else if (!mWindowVO.ctx.equals(Env.getCtx())) 565 { 566 Enumeration keyEnum = mWindowVO.ctx.keys(); 569 while (keyEnum.hasMoreElements()) 570 { 571 String key = (String )keyEnum.nextElement(); 572 if (key.startsWith(WindowNo+"|")) 573 { 574 String value = mWindowVO.ctx.getProperty (key); 575 Env.setContext(Env.getCtx(), key, value); 576 } 577 } 578 mWindowVO.setCtx(Env.getCtx()); 580 } 581 return mWindowVO; 582 } 584 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 result = null; 599 String msg = null; 600 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 (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 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 } 646 } | Popular Tags |