KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > grid > RecordAccessDialog


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

14 package org.compiere.grid;
15
16 import javax.swing.*;
17 import java.awt.*;
18 import java.awt.event.*;
19 import java.sql.*;
20 import java.util.*;
21
22 import org.compiere.swing.*;
23 import org.compiere.grid.ed.*;
24 import org.compiere.apps.*;
25 import org.compiere.model.*;
26 import org.compiere.util.*;
27
28
29 /**
30  * Record Access Dialog
31  *
32  * @author Jorg Janke
33  * @version $Id: RecordAccessDialog.java,v 1.2 2003/10/31 05:29:05 jjanke Exp $
34  */

35 public class RecordAccessDialog extends CDialog
36 {
37     /**
38      * Record Access Dialog
39      * @param owner owner
40      * @param AD_Table_ID table
41      * @param Record_ID record
42      */

43     public RecordAccessDialog(JFrame owner, int AD_Table_ID, int Record_ID)
44     {
45         super(owner, Msg.translate(Env.getCtx(), "RecordAccessDialog"));
46         log.info("AD_Table_ID=" + AD_Table_ID + ", Record_ID=" + Record_ID);
47         m_AD_Table_ID = AD_Table_ID;
48         m_Record_ID = Record_ID;
49         try
50         {
51             dynInit();
52             jbInit();
53         }
54         catch (Exception JavaDoc e)
55         {
56             log.error ("RecordAccessDialog", e);
57         }
58         AEnv.showCenterWindow(owner, this);
59     } // RecordAccessDialog
60

61     private int m_AD_Table_ID;
62     private int m_Record_ID;
63     private ArrayList m_recordAccesss = new ArrayList();
64     private int m_currentRow = 0;
65     private MRecordAccess m_currentData = null;
66     private Logger log = Logger.getCLogger(getClass());
67
68     private CPanel centerPanel = new CPanel(new ALayout());
69     private BorderLayout mainLayout = new BorderLayout();
70     
71     private CLabel roleLabel = new CLabel(Msg.translate(Env.getCtx(), "AD_Role_ID"));
72     private CComboBox roleField = null;
73     private CCheckBox cbActive = new CCheckBox(Msg.translate(Env.getCtx(), "IsActive"));
74     private CCheckBox cbExclude = new CCheckBox(Msg.translate(Env.getCtx(), "IsExclude"));
75     private CCheckBox cbReadOnly = new CCheckBox(Msg.translate(Env.getCtx(), "IsReadOnly"));
76     private CCheckBox cbDependent = new CCheckBox(Msg.translate(Env.getCtx(), "IsDependentEntities"));
77     private CButton bDelete = AEnv.getButton("Delete");
78     private CButton bNew = AEnv.getButton("New");
79     private JLabel rowNoLabel = new JLabel();
80
81     private CButton bUp = AEnv.getButton("Previous");
82     private CButton bDown = AEnv.getButton("Next");;
83
84     private ConfirmPanel confirmPanel = new ConfirmPanel(true);
85
86     /**
87      * Dynamic Init
88      */

89     private void dynInit()
90     {
91         // Load Roles
92
String JavaDoc sql = MRole.getDefault().addAccessSQL(
93             "SELECT AD_Role_ID, Name FROM AD_Role ORDER BY 2",
94             "AD_Role", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO);
95         roleField = new CComboBox(DB.getKeyNamePairs(sql));
96         
97         // Load Record Access for all roles
98
sql = "SELECT * FROM AD_Record_Access "
99             + "WHERE AD_Table_ID=? AND Record_ID=? AND AD_Client_ID=?";
100         PreparedStatement pstmt = null;
101         try
102         {
103             pstmt = DB.prepareCall(sql);
104             pstmt.setInt(1, m_AD_Table_ID);
105             pstmt.setInt(2, m_Record_ID);
106             pstmt.setInt(3, Env.getAD_Client_ID(Env.getCtx()));
107             ResultSet rs = pstmt.executeQuery();
108             while (rs.next())
109                 m_recordAccesss.add(new MRecordAccess(Env.getCtx(), rs));
110             rs.close();
111             pstmt.close();
112             pstmt = null;
113         }
114         catch (Exception JavaDoc e)
115         {
116             log.error("dynInit", e);
117         }
118         try
119         {
120             if (pstmt != null)
121                 pstmt.close();
122             pstmt = null;
123         }
124         catch (Exception JavaDoc e)
125         {
126             pstmt = null;
127         }
128         log.debug("dynInit - RecordAccess #" + m_recordAccesss.size());
129         setLine(0, false);
130     } // dynInit
131

132     /**
133      * Static Init
134      * @throws Exception
135      */

136     private void jbInit() throws Exception JavaDoc
137     {
138         this.getContentPane().setLayout(mainLayout);
139         this.getContentPane().add(centerPanel, BorderLayout.CENTER);
140         this.getContentPane().add(confirmPanel, BorderLayout.SOUTH);
141         //
142
centerPanel.add(bUp, new ALayoutConstraint(0,0));
143         centerPanel.add(bNew, new ALayoutConstraint(0,6));
144         centerPanel.add(roleLabel, new ALayoutConstraint(1,0));
145         centerPanel.add(roleField, null);
146         centerPanel.add(cbActive, null);
147         centerPanel.add(cbExclude, null);
148         centerPanel.add(cbReadOnly, null);
149         centerPanel.add(cbDependent, null);
150         centerPanel.add(bDelete, null);
151         centerPanel.add(bDown, new ALayoutConstraint(2,0));
152         centerPanel.add(rowNoLabel, new ALayoutConstraint(2,6));
153         //
154
Dimension size = centerPanel.getPreferredSize();
155         size.width = 600;
156         centerPanel.setPreferredSize(size);
157         //
158
bUp.addActionListener(this);
159         bDown.addActionListener(this);
160         bDelete.addActionListener(this);
161         bNew.addActionListener(this);
162         confirmPanel.addActionListener(this);
163     } // jbInit
164

165     /**
166      * Set Line
167      * @param rowDelta delta to current row
168      */

169     private void setLine (int rowDelta, boolean newRecord)
170     {
171         log.debug("setLine - delta=" + rowDelta + ", new=" + newRecord
172             + " - currentRow=" + m_currentRow + ", size=" + m_recordAccesss.size());
173         int maxIndex = 0;
174         // nothing defined
175
if (m_recordAccesss.size() == 0)
176         {
177             m_currentRow = 0;
178             maxIndex = 0;
179             newRecord = true;
180             setLine(null);
181         }
182         else if (newRecord)
183         {
184             m_currentRow = m_recordAccesss.size();
185             maxIndex = m_currentRow;
186             setLine(null);
187         }
188         else
189         {
190             m_currentRow += rowDelta;
191             maxIndex = m_recordAccesss.size() - 1;
192             if (m_currentRow < 0)
193                 m_currentRow = 0;
194             else if (m_currentRow > maxIndex)
195                 m_currentRow = maxIndex;
196             //
197
MRecordAccess ra = (MRecordAccess)m_recordAccesss.get(m_currentRow);
198             setLine(ra);
199         }
200         // Label
201
StringBuffer JavaDoc txt = new StringBuffer JavaDoc();
202         if (newRecord)
203             txt.append("+");
204         txt.append(m_currentRow+1).append("/").append(maxIndex+1);
205         rowNoLabel.setText(txt.toString());
206         // set up/down
207
bUp.setEnabled(m_currentRow > 0);
208         bDown.setEnabled(m_currentRow < maxIndex);
209     } // setLine
210

211     /**
212      * Set Selection
213      * @param ra record access
214      */

215     private void setLine (MRecordAccess ra)
216     {
217         int AD_Role_ID = 0;
218         boolean active = true;
219         boolean exclude = true;
220         boolean readonly = false;
221         boolean dependent = false;
222         //
223
if (ra != null)
224         {
225             AD_Role_ID = ra.getAD_Role_ID();
226             active = ra.isActive();
227             exclude = ra.isExclude();
228             readonly = ra.isReadOnly();
229             dependent = ra.isDependentEntities();
230         }
231         cbActive.setSelected(active);
232         cbExclude.setSelected(exclude);
233         cbReadOnly.setSelected(readonly);
234         cbDependent.setSelected(dependent);
235         bDelete.setEnabled(ra != null);
236         //
237
KeyNamePair selection = null;
238         for (int i = 0; i < roleField.getItemCount(); i++)
239         {
240             KeyNamePair pp = (KeyNamePair)roleField.getItemAt(i);
241             if (pp.getKey() == AD_Role_ID)
242                 selection = pp;
243         }
244         if (selection != null && ra != null)
245         {
246             roleField.setSelectedItem(selection);
247             m_currentData = ra;
248             log.debug("setLine - " + ra);
249         }
250         else
251             m_currentData = null;
252     } // setLine
253

254     /**
255      * Action Listener
256      * @param e event
257      */

258     public void actionPerformed(ActionEvent e)
259     {
260         if (e.getSource() == bUp)
261             setLine(-1, false);
262         else if (e.getSource() == bDown)
263             setLine(+1, false);
264         else if (e.getSource() == bNew)
265             setLine(0, true);
266         else
267         {
268             if (e.getSource() == bDelete)
269                 cmd_delete();
270             else if (e.getActionCommand().equals(ConfirmPanel.A_OK))
271             {
272                 if (!cmd_save())
273                     return;
274             }
275             dispose();
276         }
277     } // actionPerformed
278

279     /**
280      * Save Command
281      * @return true if saved
282      */

283     private boolean cmd_save()
284     {
285         KeyNamePair pp = (KeyNamePair)roleField.getSelectedItem();
286         roleField.setBackground(pp == null);
287         if (pp == null)
288             return false;
289         int AD_Role_ID = pp.getKey();
290         //
291
boolean isActive = cbActive.isSelected();
292         boolean isExclude = cbExclude.isSelected();
293         boolean isReadOnly = cbReadOnly.isSelected();
294         boolean isDependentEntities = cbDependent.isSelected();
295         //
296
if (m_currentData == null)
297         {
298             m_currentData = new MRecordAccess (Env.getCtx(), AD_Role_ID, m_AD_Table_ID, m_Record_ID);
299             m_recordAccesss.add(m_currentData);
300             m_currentRow = m_recordAccesss.size()-1;
301         }
302         m_currentData.setIsActive(isActive);
303         m_currentData.setIsExclude(isExclude);
304         m_currentData.setIsReadOnly(isReadOnly);
305         m_currentData.setIsDependentEntities(isDependentEntities);
306         boolean success = m_currentData.save();
307         //
308
log.debug("cmd_save - Success=" + success);
309         return success;
310     } // cmd_save
311

312     /**
313      * Delete Command
314      * @return true if deleted
315      */

316     private boolean cmd_delete()
317     {
318         boolean success = false;
319         if (m_currentData == null)
320             log.error("cmd_delete - no data");
321         else
322         {
323             success = m_currentData.delete();
324             m_currentData = null;
325             m_recordAccesss.remove(m_currentRow);
326             log.debug("cmd_delete - Success=" + success);
327         }
328         return success;
329     } // cmd_delete
330

331 } // RecordAccessDialog
332
Popular Tags