KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.*;
20 import java.beans.*;
21 import java.text.*;
22 import java.util.*;
23 import java.io.*;
24
25 import org.compiere.Compiere;
26 import org.compiere.grid.*;
27 import org.compiere.grid.tree.*;
28 import org.compiere.model.*;
29 import org.compiere.util.*;
30 import org.compiere.apps.form.*;
31 import org.compiere.apps.search.*;
32 import org.compiere.apps.wf.*;
33 import org.compiere.plaf.*;
34 import org.compiere.swing.*;
35
36 /**
37  * Application Menu Controller
38  *
39  * @author Jorg Janke
40  * @version $Id: AMenu.java,v 1.63 2003/10/24 15:27:30 jjanke Exp $
41  */

42 public final class AMenu extends JFrame
43     implements ActionListener, PropertyChangeListener
44 {
45     /**
46      * Application Menu
47      */

48     public AMenu ()
49     {
50         super();
51         Splash splash = Splash.getSplash();
52         m_WindowNo = Env.createWindowNo(this);
53
54         // Login
55
initSystem (splash); // login
56
splash.setText(Msg.getMsg(m_ctx, "Loading"));
57         MSession.getDefault(Env.getCtx()); // Start Session
58

59         // Preparation
60
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
61         wfPanel = new WFPanel(this);
62         treePanel = new VTreePanel (m_WindowNo, true, false); // !editable & hasBar
63

64         try
65         {
66             jbInit();
67             createMenu();
68         }
69         catch(Exception JavaDoc ex)
70         {
71             Log.error("AMenu", ex);
72         }
73
74         // initialize & load tree
75
int AD_Tree_ID = 10;
76         treePanel.initTree(AD_Tree_ID);
77
78         // Translate
79
Env.setContext(m_ctx, m_WindowNo, "WindowName", Msg.getMsg(m_ctx, "Menu"));
80         setTitle(Env.getHeader(m_ctx, m_WindowNo));
81
82         progressBar.setString(Msg.getMsg(m_ctx, "SelectProgram"));
83
84         // Finish UI
85
this.setLocation(0, 0);
86         this.pack();
87         this.setVisible(true);
88         this.setState(Frame.NORMAL);
89         m_AD_User_ID = Env.getContextAsInt(m_ctx, "#AD_User_ID");
90         updateMemoryInfo();
91         //
92
splash.dispose();
93         splash = null;
94     } // AMenu
95

96     private int m_WindowNo;
97     private Properties m_ctx = Env.getCtx();
98     private boolean m_startingItem = false;
99     private int m_AD_User_ID;
100     // Links
101
private int m_request_Menu_ID = 0;
102     private int m_note_Menu_ID = 0;
103
104     /*************************************************************************/
105
106     /**
107      * Init System.
108      * -- do not get Msg as environment not initialized yet --
109      * <pre>
110      * - Login - in not successful, exit
111      * </pre>
112      * @param splash splash window
113      */

114     private void initSystem (Splash splash)
115     {
116         // Image
117
this.setIconImage(Compiere.getImage16());
118
119         // Focus Traversal
120
FocusManager.getCurrentManager().setDefaultFocusTraversalPolicy(AFocusTraversalPolicy.get());
121         this.setFocusTraversalPolicy(AFocusTraversalPolicy.get());
122
123         /**
124          * Show Login Screen - if not successful - exit
125          */

126         ALogin login = new ALogin(splash);
127         if (!login.initLogin()) // no automatic login
128
{
129             // Center the window
130
try
131             {
132                 AEnv.showCenterScreen(login); // HTML load errors
133
}
134             catch (Exception JavaDoc ex)
135             {
136             }
137             if (!login.isConnected())
138                 AEnv.exit(1);
139         }
140
141         // Check DB (AppsServer Version checked in Login)
142
boolean dbOK = DB.isDatabaseOK(m_ctx);
143     // if (!dbOK)
144
// AEnv.exit(1);
145
} // initSystem
146

147     // UI
148
private CPanel mainPanel = new CPanel();
149     private BorderLayout mainLayout = new BorderLayout();
150     private CTabbedPane centerPane = new CTabbedPane();
151     private CPanel southPanel = new CPanel();
152     private BorderLayout southLayout = new BorderLayout();
153     private JMenuBar menuBar = new JMenuBar();
154     protected JProgressBar progressBar = new JProgressBar(0,100);
155     private CPanel infoPanel = new CPanel();
156     private CButton bNotes = new CButton();
157     private CButton bTasks = new CButton();
158     private GridLayout infoLayout = new GridLayout();
159     private JProgressBar memoryBar = new JProgressBar();
160     //
161
private VTreePanel treePanel = null;
162     private WFPanel wfPanel = null;
163
164     /**
165      * Static Init.
166      * <pre>
167      * - mainPanel
168      * - centerPane
169      * - treePanel
170      * - wfPanel
171      * - southPanel
172      * - infoPanel
173      * - bNotes
174      * - bTask
175      * - memoryBar
176      * - wfPanel
177      * - progressBar
178      * </pre>
179      * @throws Exception
180      */

181     void jbInit() throws Exception JavaDoc
182     {
183         this.setName("Menu");
184         this.setLocale(Language.getLanguage().getLocale());
185         this.setJMenuBar(menuBar);
186         CompiereColor.setBackground(this);
187         //
188
mainPanel.setLayout(mainLayout);
189         mainLayout.setHgap(0);
190         mainLayout.setVgap(2);
191         //
192
treePanel.addPropertyChangeListener(VTreePanel.NODE_SELECTION, this);
193         //
194
infoPanel.setLayout(infoLayout);
195         infoLayout.setColumns(2);
196         infoLayout.setHgap(4);
197         infoLayout.setVgap(0);
198         bNotes.setRequestFocusEnabled(false);
199         bNotes.setToolTipText("");
200         bNotes.setActionCommand("Notes");
201         bNotes.addActionListener(this);
202         bNotes.setIcon(Env.getImageIcon("SendMail24.gif"));
203         bNotes.setMargin(new Insets(0, 0, 0, 0));
204         bTasks.setRequestFocusEnabled(false);
205         bTasks.setActionCommand("Tasks");
206         bTasks.addActionListener(this);
207         bTasks.setIcon(Env.getImageIcon("Import24.gif"));
208         bTasks.setMargin(new Insets(0, 0, 0, 0));
209         //
210
southLayout.setHgap(0);
211         southLayout.setVgap(1);
212         //
213
memoryBar.setStringPainted(true);
214         memoryBar.setOpaque(false);
215         memoryBar.setBorderPainted(false);
216         progressBar.setStringPainted(true);
217         progressBar.setOpaque(false);
218         //
219
getContentPane().add(mainPanel);
220         mainPanel.add(centerPane, BorderLayout.CENTER);
221         mainPanel.add(southPanel, BorderLayout.SOUTH);
222         mainPanel.add(Box.createHorizontalStrut(3), BorderLayout.EAST);
223         mainPanel.add(Box.createHorizontalStrut(3), BorderLayout.WEST);
224         //
225
centerPane.add(treePanel, Msg.getMsg(m_ctx, "Menu"));
226         centerPane.add(wfPanel, Msg.translate (m_ctx, "AD_Workflow_ID"));
227         //
228
southPanel.setLayout(southLayout);
229         southPanel.add(infoPanel, BorderLayout.NORTH);
230         southPanel.add(progressBar, BorderLayout.SOUTH);
231         //
232
infoPanel.add(bNotes, null);
233         infoPanel.add(bTasks, null);
234         infoPanel.add(memoryBar, null);
235         //
236
} // jbInit
237

238     /**
239      * Get Preferred Size
240      * @return preferred Size
241      */

242     public Dimension getPreferredSize()
243     {
244         return new Dimension (350, 500);
245     } // getPreferredSize
246

247
248     /**
249      * Create Menu
250      */

251     private void createMenu()
252     {
253         // File
254
JMenu mFile = AEnv.getMenu("File");
255         menuBar.add(mFile);
256         AEnv.addMenuItem("PrintScreen", null, KeyStroke.getKeyStroke(KeyEvent.VK_PRINTSCREEN, 0), mFile, this);
257         AEnv.addMenuItem("ScreenShot", null, KeyStroke.getKeyStroke(KeyEvent.VK_PRINTSCREEN, KeyEvent.SHIFT_MASK), mFile, this);
258         AEnv.addMenuItem("Report", null, KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.ALT_MASK), mFile, this);
259         mFile.addSeparator();
260         AEnv.addMenuItem("Exit", null, KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.SHIFT_MASK+Event.ALT_MASK), mFile, this);
261
262         // View
263
JMenu mView = AEnv.getMenu("View");
264         menuBar.add(mView);
265         AEnv.addMenuItem("InfoProduct", null, KeyStroke.getKeyStroke(KeyEvent.VK_I, Event.CTRL_MASK), mView, this);
266         AEnv.addMenuItem("InfoBPartner", null, KeyStroke.getKeyStroke(KeyEvent.VK_I, Event.SHIFT_MASK+Event.CTRL_MASK), mView, this);
267         if (MRole.getDefault().isShowAcct())
268             AEnv.addMenuItem("InfoAccount", null, KeyStroke.getKeyStroke(KeyEvent.VK_I, Event.ALT_MASK+Event.CTRL_MASK), mView, this);
269         AEnv.addMenuItem("InfoSchedule", null, null, mView, this);
270         mView.addSeparator();
271         AEnv.addMenuItem("InfoOrder", "Info", null, mView, this);
272         AEnv.addMenuItem("InfoInvoice", "Info", null, mView, this);
273         AEnv.addMenuItem("InfoInOut", "Info", null, mView, this);
274         AEnv.addMenuItem("InfoPayment", "Info", null, mView, this);
275         AEnv.addMenuItem("InfoCashLine", "Info", null, mView, this);
276         AEnv.addMenuItem("InfoAssignment", "Info", null, mView, this);
277         AEnv.addMenuItem("InfoAsset", "Info", null, mView, this);
278
279         // Tools
280
JMenu mTools = AEnv.getMenu("Tools");
281         menuBar.add(mTools);
282         AEnv.addMenuItem("Calculator", null, null, mTools, this);
283         AEnv.addMenuItem("Calendar", null, null, mTools, this);
284         AEnv.addMenuItem("Editor", null, null, mTools, this);
285         AEnv.addMenuItem("Script", null, null, mTools, this);
286         mTools.addSeparator();
287         AEnv.addMenuItem("Preference", null, null, mTools, this);
288
289         // Help
290
JMenu mHelp = AEnv.getMenu("Help");
291         menuBar.add(mHelp);
292         AEnv.addMenuItem("Online", null, null, mHelp, this);
293         AEnv.addMenuItem("SendMail", null, null, mHelp, this);
294         AEnv.addMenuItem("About", null, null, mHelp, this);
295     } // createMenu
296

297     /**
298      * Dispose - end system
299      */

300     public void dispose()
301     {
302         // End Session
303
MSession session = MSession.getDefault();
304         session.logout();
305         
306         // clean up - close windows
307
super.dispose();
308         AEnv.exit(0);
309     } // dispose
310

311     /**
312      * Window Events - requestFocus
313      * @param e event
314      */

315     protected void processWindowEvent(WindowEvent e)
316     {
317         super.processWindowEvent(e);
318         if (e.getID() == WindowEvent.WINDOW_OPENED)
319         {
320             treePanel.getSearchField().requestFocusInWindow();
321         // this.toFront();
322
}
323     } // processWindowEvent
324

325     /**
326      * Set Busy
327      * @param value true if buzy
328      */

329     protected void setBusy (boolean value)
330     {
331         m_startingItem = value;
332         if (value)
333             setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
334         else
335             setCursor(Cursor.getDefaultCursor());
336     // setEnabled (!value); // causes flicker
337
} // setBusy
338

339     /**
340      * Selection in tree - launch Application
341      * @param e PropertyChangeEvent
342      */

343     public void propertyChange(PropertyChangeEvent e)
344     {
345         MTreeNode nd = (MTreeNode)e.getNewValue();
346         Log.trace(Log.l1_User, "AMenu => " + nd.getID() + " " + nd.toString());
347
348         // ignore summary items & when loading
349
if (m_startingItem || nd.isSummary())
350             return;
351
352         String JavaDoc sta = nd.toString();
353         progressBar.setString(sta);
354         int cmd = nd.getID();
355
356         (new AMenuStartItem(cmd, true, sta, this)).start(); // async load
357
updateMemoryInfo();
358     } // propertyChange
359

360
361     /*************************************************************************/
362
363     /**
364      * ActionListener
365      * @param e ActionEvent
366      */

367     public void actionPerformed(ActionEvent e)
368     {
369         // Buttons
370
if (e.getSource() == bNotes)
371             gotoNotes();
372         else if (e.getSource() == bTasks)
373             gotoTasks();
374         else if (!AEnv.actionPerformed(e.getActionCommand(), m_WindowNo, this))
375             Log.error("AMenu.actionPerformed - unknown action=" + e.getActionCommand());
376         updateMemoryInfo();
377     } // actionPerformed
378

379     /**
380      * Get number of open Notes
381      * @return bumber of notes
382      */

383     private int getNotes()
384     {
385         int retValue = 0;
386         String JavaDoc sql = "SELECT COUNT(*) FROM AD_Note "
387             + "WHERE AD_Client_ID=? AND AD_User_ID IN (0,?)"
388             + " AND Processed='N'";
389         try
390         {
391             PreparedStatement pstmt = DB.prepareStatement(sql);
392             pstmt.setInt(1, Env.getContextAsInt(Env.getCtx(), "#AD_Client_ID"));
393             pstmt.setInt(2, m_AD_User_ID);
394             ResultSet rs = pstmt.executeQuery();
395             if (rs.next())
396                 retValue = rs.getInt(1);
397             rs.close();
398             pstmt.close();
399         }
400         catch (SQLException e)
401         {
402             Log.error ("AMenu.getNotes", e);
403         }
404         return retValue;
405     } // getNotes
406

407     /**
408      * Open Note Window
409      */

410     private void gotoNotes()
411     {
412         // AD_Table_ID for R_Request = 389 HARDCODED
413
if (m_note_Menu_ID == 0)
414             m_note_Menu_ID = DB.getSQLValue("SELECT AD_Menu_ID "
415                 + "FROM AD_Menu m"
416                 + " INNER JOIN AD_TABLE t ON (t.AD_Window_ID=m.AD_Window_ID) "
417                 + "WHERE t.AD_Table_ID=389");
418         if (m_note_Menu_ID == 0)
419             m_note_Menu_ID = 233; // fallback HARDCODED
420
(new AMenuStartItem (m_note_Menu_ID, true, Msg.translate(m_ctx, "AD_Note_ID"), this)).start(); // async load
421
} // gotoMessage
422

423     /**
424      * Ger Number of open Requests
425      * @return number of requests
426      */

427     private int getRequests()
428     {
429         int retValue = 0;
430         String JavaDoc sql = MRole.getDefault().addAccessSQL ("SELECT COUNT(*) FROM R_Request "
431             + "WHERE SalesRep_ID=? AND Processed='N'"
432             + " AND (DateNextAction IS NULL OR TRUNC(DateNextAction) <= TRUNC(SysDate))",
433             "R_Request", false, true); // not qualified - RW
434
try
435         {
436             PreparedStatement pstmt = DB.prepareStatement(sql);
437             pstmt.setInt(1, m_AD_User_ID);
438             ResultSet rs = pstmt.executeQuery();
439             if (rs.next())
440                 retValue = rs.getInt(1);
441             rs.close();
442             pstmt.close();
443         }
444         catch (SQLException e)
445         {
446             Log.error ("AMenu.getRequests", e);
447         }
448         return retValue;
449     } // getRequests
450

451     /**
452      * Open Request Window
453      */

454     private void gotoTasks()
455     {
456         // AD_Table_ID for R_Request = 417 HARDCODED
457
if (m_request_Menu_ID == 0)
458             m_request_Menu_ID = DB.getSQLValue ("SELECT AD_Menu_ID "
459                 + "FROM AD_Menu m"
460                 + " INNER JOIN AD_TABLE t ON (t.AD_Window_ID=m.AD_Window_ID) "
461                 + "WHERE t.AD_Table_ID=417");
462         if (m_request_Menu_ID == 0)
463             m_request_Menu_ID = 237; // fallback HARDCODED
464
(new AMenuStartItem (m_request_Menu_ID, true, Msg.translate(m_ctx, "R_Request_ID"), this)).start(); // async load
465
} // gotoTasks
466

467     /**
468      * Show Memory Info & run GC if required
469      */

470     protected void updateMemoryInfo()
471     {
472         int total = (int)(Runtime.getRuntime().totalMemory()/1024);
473         int free = (int)(Runtime.getRuntime().freeMemory()/1024);
474         int used = total - free;
475         //
476
memoryBar.setMaximum(total);
477         memoryBar.setValue(used);
478         int percent = used * 100 / total;
479         //
480
DecimalFormat format = DisplayType.getNumberFormat(DisplayType.Number);
481         format.setMaximumFractionDigits(1);
482         double mb = total / 1024;
483         memoryBar.setString(format.format(mb) + " MB - " + percent + "%");
484         //
485
if (percent > 50)
486             System.gc();
487
488         // Repaint Progress bar
489
// progressBar.repaint();
490
//
491
try
492         {
493             bTasks.setText(Msg.translate(m_ctx, "R_Request_ID") + ": " + getRequests());
494             bNotes.setText(Msg.translate(m_ctx, "AD_Note_ID") + ": " + getNotes());
495         }
496         catch (Exception JavaDoc ex)
497         {
498         }
499     } // updateMemoryInfo
500

501     /*************************************************************************/
502
503     /**
504      * Start Workflow
505      * @param AD_Workflow_ID id
506      */

507     protected void startWorkFlow (int AD_Workflow_ID)
508     {
509         centerPane.setSelectedIndex(1); // switch
510
wfPanel.load(AD_Workflow_ID);
511     } // startWorkFlow
512

513     /*************************************************************************/
514
515     /**
516      * OS Start
517      * @param args Array of String arguments (ignored)
518      */

519     public static void main(String JavaDoc[] args)
520     {
521         Splash.getSplash();
522         org.compiere.Compiere.startupClient ();
523         AMenu menu = new AMenu();
524     } // main
525

526 } // AMenu
527
Popular Tags