1 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 35 public class RecordAccessDialog extends CDialog 36 { 37 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 e) 55 { 56 log.error ("RecordAccessDialog", e); 57 } 58 AEnv.showCenterWindow(owner, this); 59 } 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 89 private void dynInit() 90 { 91 String 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 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 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 e) 125 { 126 pstmt = null; 127 } 128 log.debug("dynInit - RecordAccess #" + m_recordAccesss.size()); 129 setLine(0, false); 130 } 132 136 private void jbInit() throws Exception 137 { 138 this.getContentPane().setLayout(mainLayout); 139 this.getContentPane().add(centerPanel, BorderLayout.CENTER); 140 this.getContentPane().add(confirmPanel, BorderLayout.SOUTH); 141 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 Dimension size = centerPanel.getPreferredSize(); 155 size.width = 600; 156 centerPanel.setPreferredSize(size); 157 bUp.addActionListener(this); 159 bDown.addActionListener(this); 160 bDelete.addActionListener(this); 161 bNew.addActionListener(this); 162 confirmPanel.addActionListener(this); 163 } 165 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 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 MRecordAccess ra = (MRecordAccess)m_recordAccesss.get(m_currentRow); 198 setLine(ra); 199 } 200 StringBuffer txt = new StringBuffer (); 202 if (newRecord) 203 txt.append("+"); 204 txt.append(m_currentRow+1).append("/").append(maxIndex+1); 205 rowNoLabel.setText(txt.toString()); 206 bUp.setEnabled(m_currentRow > 0); 208 bDown.setEnabled(m_currentRow < maxIndex); 209 } 211 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 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 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 } 254 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 } 279 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 boolean isActive = cbActive.isSelected(); 292 boolean isExclude = cbExclude.isSelected(); 293 boolean isReadOnly = cbReadOnly.isSelected(); 294 boolean isDependentEntities = cbDependent.isSelected(); 295 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 log.debug("cmd_save - Success=" + success); 309 return success; 310 } 312 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 } 331 } | Popular Tags |