KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > search > InfoSchedule


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-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.apps.search;
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import javax.swing.*;
19 import javax.swing.event.*;
20 import java.sql.*;
21 import java.util.*;
22
23 import org.compiere.apps.*;
24 import org.compiere.grid.ed.*;
25 import org.compiere.util.*;
26 import org.compiere.model.*;
27 import org.compiere.plaf.*;
28
29
30 /**
31  * Schedule - Resource availability & assigment.
32  *
33  * @author Jorg Janke
34  * @version $Id: InfoSchedule.java,v 1.10 2003/10/22 05:48:17 jjanke Exp $
35  */

36 public class InfoSchedule extends JDialog
37     implements ActionListener, ChangeListener
38 {
39     /**
40      * Constructor
41      * @param frame Parent
42      * @param mAssignment optional assignment
43      * @param createNew if true, allows to create new assignments
44      */

45     public InfoSchedule (Frame frame, MAssignment mAssignment, boolean createNew)
46     {
47         super(frame, Msg.getMsg(Env.getCtx(), "InfoSchedule"), frame != null && createNew);
48         if (mAssignment == null)
49             m_mAssignment = new MAssignment(Env.getCtx(), 0);
50         else
51             m_mAssignment = mAssignment;
52         Log.trace(Log.l1_User, "InfoSchedule", mAssignment);
53         m_dateFrom = m_mAssignment.getAssignDateFrom();
54         if (m_dateFrom == null)
55             m_dateFrom = new Timestamp(System.currentTimeMillis());
56         m_createNew = createNew;
57         try
58         {
59             jbInit();
60             dynInit(createNew);
61         }
62         catch(Exception JavaDoc ex)
63         {
64             Log.error("InfoSchedule", ex);
65         }
66         AEnv.showCenterWindow(frame, this);
67     } // InfoSchedule
68

69     /**
70      * IDE Constructor
71      */

72     public InfoSchedule()
73     {
74         this (null, null, false);
75     } // InfoSchedule
76

77     /** Resource */
78     private MAssignment m_mAssignment;
79     /** Date */
80     private Timestamp m_dateFrom = null;
81     /** Loading */
82     private boolean m_loading = false;
83     /** Ability to create new assignments */
84     private boolean m_createNew;
85
86     private JPanel mainPanel = new JPanel();
87     private BorderLayout mainLayout = new BorderLayout();
88     private JPanel parameterPanel = new JPanel();
89     private GridBagLayout parameterLayout = new GridBagLayout();
90     private JLabel labelResourceType = new JLabel();
91     private JComboBox fieldResourceType = new JComboBox();
92     private JLabel labelResource = new JLabel();
93     private JComboBox fieldResource = new JComboBox();
94     private JButton bPrevious = new JButton();
95     private JLabel labelDate = new JLabel();
96     private VDate fieldDate = new VDate();
97     private JButton bNext = new JButton();
98     private JTabbedPane timePane = new JTabbedPane();
99     private VSchedule daySchedule = new VSchedule(this, VSchedule.TYPE_DAY);
100     private VSchedule weekSchedule = new VSchedule(this, VSchedule.TYPE_WEEK);
101     private VSchedule monthSchedule = new VSchedule(this, VSchedule.TYPE_MONTH);
102     private StatusBar statusBar = new StatusBar();
103     private ConfirmPanel confirmPanel = new ConfirmPanel(true);
104
105     /**
106      * Static Layout
107      * @throws Exception
108      */

109     private void jbInit() throws Exception JavaDoc
110     {
111         CompiereColor.setBackground(this);
112         mainPanel.setLayout(mainLayout);
113         parameterPanel.setLayout(parameterLayout);
114         labelResourceType.setHorizontalTextPosition(SwingConstants.LEADING);
115         labelResourceType.setText(Msg.translate(Env.getCtx(), "S_ResourceType_ID"));
116         labelResource.setHorizontalTextPosition(SwingConstants.LEADING);
117         labelResource.setText(Msg.translate(Env.getCtx(), "S_Resource_ID"));
118         bPrevious.setMargin(new Insets(0, 0, 0, 0));
119         bPrevious.setText("<");
120         labelDate.setText(Msg.translate(Env.getCtx(), "Date"));
121         bNext.setMargin(new Insets(0, 0, 0, 0));
122         bNext.setText(">");
123         getContentPane().add(mainPanel, BorderLayout.CENTER);
124         mainPanel.add(parameterPanel, BorderLayout.NORTH);
125         parameterPanel.add(labelResourceType, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
126             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(8, 8, 0, 0), 0, 0));
127         parameterPanel.add(fieldResourceType, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
128             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 8, 8, 4), 0, 0));
129         parameterPanel.add(labelResource, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
130             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(8, 4, 0, 4), 0, 0));
131         parameterPanel.add(fieldResource, new GridBagConstraints(1, 1, 1, 1, 0.5, 0.0
132             ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 8, 4), 0, 0));
133         parameterPanel.add(bPrevious, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
134             ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 8, 8, 0), 0, 0));
135         parameterPanel.add(labelDate, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0
136             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(8, 0, 0, 0), 0, 0));
137         parameterPanel.add(fieldDate, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0
138             ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 8, 0), 0, 0));
139         parameterPanel.add(bNext, new GridBagConstraints(4, 1, 1, 1, 0.0, 0.0
140             ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 8, 8), 0, 0));
141         //
142
mainPanel.add(timePane, BorderLayout.CENTER);
143         timePane.add(daySchedule, Msg.getMsg(Env.getCtx(), "Day"));
144         timePane.add(weekSchedule, Msg.getMsg(Env.getCtx(), "Week"));
145         timePane.add(monthSchedule, Msg.getMsg(Env.getCtx(), "Month"));
146     // timePane.add(daySchedule, Msg.getMsg(Env.getCtx(), "Day"));
147
// timePane.add(weekSchedule, Msg.getMsg(Env.getCtx(), "Week"));
148
// timePane.add(monthSchedule, Msg.getMsg(Env.getCtx(), "Month"));
149
timePane.addChangeListener(this);
150         //
151
mainPanel.add(confirmPanel, BorderLayout.SOUTH);
152         //
153
this.getContentPane().add(statusBar, BorderLayout.SOUTH);
154     } // jbInit
155

156     /**
157      * Dynamic Init
158      * @param createNew if true, allows to create new assignments
159      */

160     private void dynInit (boolean createNew)
161     {
162         // Resource
163
fillResourceType();
164         fillResource();
165         fieldResourceType.addActionListener(this);
166         fieldResource.addActionListener(this);
167
168         // Date
169
fieldDate.setValue(m_dateFrom);
170         fieldDate.addActionListener(this);
171         bPrevious.addActionListener(this);
172         bNext.addActionListener(this);
173
174         // Set Init values
175
daySchedule.setCreateNew(createNew);
176         weekSchedule.setCreateNew(createNew);
177         monthSchedule.setCreateNew(createNew);
178         //
179
confirmPanel.addActionListener(this);
180         displayCalendar();
181     } // dynInit
182

183     /**
184      * Fill Resource Type (one time)
185      */

186     private void fillResourceType()
187     {
188         // Get ResourceType of selected Resource
189
int S_ResourceType_ID = 0;
190         if (m_mAssignment.getS_Resource_ID() != 0)
191         {
192             String JavaDoc sql = "SELECT S_ResourceType_ID FROM S_Resource WHERE S_Resource_ID=?";
193             try
194             {
195                 PreparedStatement pstmt = DB.prepareStatement(sql);
196                 pstmt.setInt(1, m_mAssignment.getS_Resource_ID());
197                 ResultSet rs = pstmt.executeQuery();
198                 if (rs.next())
199                     S_ResourceType_ID = rs.getInt(1);
200                 rs.close();
201                 pstmt.close();
202             }
203             catch (SQLException e)
204             {
205                 Log.error("InfoSchedule.fillResourceType-1", e);
206             }
207         }
208
209         // Get Resource Types
210
String JavaDoc sql = MRole.getDefault().addAccessSQL(
211             "SELECT S_ResourceType_ID, Name FROM S_ResourceType WHERE IsActive='Y' ORDER BY 2",
212             "S_ResourceType", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);
213         KeyNamePair defaultValue = null;
214         try
215         {
216             PreparedStatement pstmt = DB.prepareStatement(sql);
217             ResultSet rs = pstmt.executeQuery();
218             while (rs.next())
219             {
220                 KeyNamePair pp = new KeyNamePair(rs.getInt(1), rs.getString(2));
221                 if (S_ResourceType_ID == pp.getKey())
222                     defaultValue = pp;
223                 fieldResourceType.addItem(pp);
224             }
225             rs.close();
226             pstmt.close();
227         }
228         catch (SQLException e)
229         {
230             Log.error("InfoSchedule.fillResourceType-2", e);
231         }
232         if (defaultValue != null)
233             fieldResourceType.setSelectedItem(defaultValue);
234     } // fillResourceType
235

236     /**
237      * Fill Resource Pick from Resource Type
238      */

239     private void fillResource()
240     {
241         // Get Resource Type
242
KeyNamePair pp = (KeyNamePair)fieldResourceType.getSelectedItem();
243         if (pp == null)
244             return;
245         int S_ResourceType_ID = pp.getKey();
246
247         KeyNamePair defaultValue = null;
248
249         // Load Resources
250
m_loading = true;
251         fieldResource.removeAllItems();
252         String JavaDoc sql = "SELECT S_Resource_ID, Name FROM S_Resource WHERE S_ResourceType_ID=? ORDER BY 2";
253         try
254         {
255             PreparedStatement pstmt = DB.prepareStatement(sql);
256             pstmt.setInt(1, S_ResourceType_ID);
257             ResultSet rs = pstmt.executeQuery();
258             while (rs.next())
259             {
260                 pp = new KeyNamePair(rs.getInt(1), rs.getString(2));
261                 if (m_mAssignment.getS_Resource_ID() == pp.getKey())
262                     defaultValue = pp;
263                 fieldResource.addItem(pp);
264             }
265             rs.close();
266             pstmt.close();
267         }
268         catch (SQLException e)
269         {
270             Log.error("InfoSchedule.fillResource", e);
271         }
272         if (defaultValue != null)
273             fieldResource.setSelectedItem(defaultValue);
274
275         m_loading = false;
276     } // fillResource
277

278     /**
279      * Display Calendar for selected Resource, Time(day/week/month) and Date
280      */

281     private void displayCalendar ()
282     {
283         // Get Values
284
KeyNamePair pp = (KeyNamePair)fieldResource.getSelectedItem();
285         if (pp == null)
286             return;
287         int S_Resource_ID = pp.getKey();
288         m_mAssignment.setS_Resource_ID(S_Resource_ID);
289         Timestamp date = fieldDate.getTimestamp();
290         int index = timePane.getSelectedIndex();
291         Log.trace(Log.l3_Util, "InfoSchedule.displayCalendar " + index + ", ID=" + S_Resource_ID, date);
292
293         // Set Info
294
m_loading = true;
295         if (index == 0)
296             daySchedule.recreate (S_Resource_ID, date);
297         else if (index == 1)
298             weekSchedule.recreate (S_Resource_ID, date);
299         else
300             monthSchedule.recreate (S_Resource_ID, date);
301         m_loading = false;
302         repaint();
303     } // displayCalendar
304

305     /*************************************************************************/
306
307     /**
308      * Dispose.
309      */

310     public void dispose()
311     {
312         daySchedule.dispose();
313         weekSchedule.dispose();
314         monthSchedule.dispose();
315         this.removeAll();
316         super.dispose();
317     } // dispose
318

319     /**
320      * Action Listener
321      * @param e event
322      */

323     public void actionPerformed(ActionEvent e)
324     {
325         if (m_loading)
326             return;
327         this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
328
329         if (e.getActionCommand().equals(ConfirmPanel.A_OK))
330             dispose();
331         else if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL))
332             dispose();
333         //
334
else if (e.getSource() == fieldResourceType)
335             fillResource();
336         //
337
else if (e.getSource() == fieldResource || e.getSource() == fieldDate)
338             displayCalendar();
339         //
340
else if (e.getSource() == bPrevious)
341             adjustDate(-1);
342         else if (e.getSource() == bNext)
343             adjustDate(+1);
344         //
345
this.setCursor(Cursor.getDefaultCursor());
346     } // actionPerformed
347

348     /**
349      * Change Listener (Tab Pane)
350      * @param e event
351      */

352     public void stateChanged (ChangeEvent e)
353     {
354         displayCalendar();
355     } // stateChanged
356

357     /**
358      * Adjust Date
359      * @param diff difference
360      */

361     private void adjustDate (int diff)
362     {
363         Timestamp date = fieldDate.getTimestamp();
364         GregorianCalendar cal = new GregorianCalendar();
365         cal.setTime(date);
366         if (timePane.getSelectedIndex() == 0)
367             cal.add(java.util.Calendar.DAY_OF_YEAR, diff);
368         else if (timePane.getSelectedIndex() == 1)
369             cal.add(java.util.Calendar.WEEK_OF_YEAR, diff);
370         else
371             cal.add(java.util.Calendar.MONTH, diff);
372         //
373
fieldDate.setValue(new Timestamp(cal.getTimeInMillis()));
374         displayCalendar ();
375     } // adjustDate
376

377     /*************************************************************************/
378
379     /**
380      * Callback.
381      * Called from VSchedulePanel after VAssignmentDialog finished
382      * @param assignment New/Changed Assignment
383      */

384     public void mAssignmentCallback (MAssignment assignment)
385     {
386         m_mAssignment = assignment;
387         if (m_createNew)
388             dispose();
389         else
390             displayCalendar();
391     } // mAssignmentCallback
392

393     /**
394      * Get Assignment
395      * @return Assignment
396      */

397     public MAssignment getMAssignment()
398     {
399         return m_mAssignment;
400     } // getMAssignment
401

402
403
404     /**
405 SELECT o.DocumentNo, ol.Line, ol.Description
406 FROM C_OrderLine ol, C_Order o
407 WHERE ol.S_ResourceAssignment_ID=1
408   AND ol.C_Order_ID=o.C_Order_ID
409 UNION
410 SELECT i.DocumentNo, il.Line, il.Description
411 FROM C_InvoiceLine il, C_Invoice i
412 WHERE il.S_ResourceAssignment_ID=1
413   AND il.C_Invoice_ID=i.C_Invoice_ID
414 UNION
415 SELECT e.DocumentNo, el.Line, el.Description
416 FROM S_TimeExpenseLine el, S_TimeExpense e
417 WHERE el.S_ResourceAssignment_ID=1
418   AND el.S_TimeExpense_ID=el.S_TimeExpense_ID
419      */

420 } // InfoSchedule
421
Popular Tags