1 14 package org.compiere.grid.ed; 15 16 import java.awt.*; 17 import java.awt.event.*; 18 import javax.swing.*; 19 import javax.swing.border.*; 20 import java.util.*; 21 import java.sql.*; 22 23 import org.compiere.util.*; 24 import org.compiere.apps.*; 25 import org.compiere.model.*; 26 import org.compiere.swing.*; 27 import org.compiere.plaf.*; 28 29 36 public class ValuePreference extends JDialog implements ActionListener 37 { 38 44 public static ValuePreference start (MField mField, Object aValue) 45 { 46 return start (mField, aValue, null); 47 } 49 56 public static ValuePreference start (MField mField, Object aValue, String aDisplayValue) 57 { 58 String Value = null; 60 String DisplayValue = null; 61 if (aValue != null) 62 { 63 Value = aValue.toString(); 64 DisplayValue = (aDisplayValue == null) ? Value : aDisplayValue; 65 } 66 67 int AD_Window_ID = mField.getAD_Window_ID(); 70 String Attribute = mField.getColumnName(); 71 String DisplayAttribute = mField.getHeader(); 72 int displayType = mField.getDisplayType(); 73 int AD_Reference_ID = 0; 74 int WindowNo = mField.getWindowNo(); 75 76 int AD_Client_ID = Env.getContextAsInt(Env.getCtx(), WindowNo, "AD_Client_ID"); 79 int AD_Org_ID = Env.getContextAsInt(Env.getCtx(), WindowNo, "AD_Org_ID"); 80 int AD_User_ID = Env.getContextAsInt(Env.getCtx(), "#AD_User_ID");; 81 Frame frame = Env.getWindow(WindowNo); 82 83 ValuePreference vp = new ValuePreference (frame, WindowNo, 85 AD_Client_ID, AD_Org_ID, AD_User_ID, AD_Window_ID, 86 Attribute, DisplayAttribute, Value, DisplayValue, 87 displayType, AD_Reference_ID); 88 return vp; 89 } 91 113 public static JMenuItem addMenu (ActionListener l, JPopupMenu popupMenu) 114 { 115 JMenuItem mi = new JMenuItem (Msg.getMsg(Env.getCtx(), NAME), s_icon); 116 mi.setActionCommand(NAME); 117 mi.addActionListener(l); 118 popupMenu.add(mi); 119 return mi; 120 } 122 123 public static final String NAME = "ValuePreference"; 124 125 private static Icon s_icon = new ImageIcon(org.compiere.Compiere.class.getResource("images/VPreference16.gif")); 126 127 143 public ValuePreference (Frame frame, int WindowNo, 144 int AD_Client_ID, int AD_Org_ID, int AD_User_ID, int AD_Window_ID, 145 String Attribute, String DisplayAttribute, String Value, String DisplayValue, 146 int displayType, int AD_Reference_ID) 147 { 148 super(frame, Msg.getMsg(Env.getCtx(), NAME) + " " + DisplayAttribute, true); 149 Log.trace(Log.l3_Util, NAME, "WindowNo=" + WindowNo 150 + ", Client_ID=" + AD_Client_ID + ", Org_ID=" + AD_Org_ID + ", User_ID=" + AD_User_ID + ", Window_ID=" + AD_Window_ID 151 + ", Attribute=" + Attribute + "/" + DisplayAttribute + ", Value=" + Value + "/" + DisplayValue 152 + ", DisplayType=" + displayType + ", Reference_ID=" + AD_Reference_ID); 153 m_ctx = Env.getCtx(); 154 m_WindowNo = WindowNo; 155 m_AD_Client_ID = AD_Client_ID; 156 m_AD_Org_ID = AD_Org_ID; 157 m_AD_User_ID = AD_User_ID; 158 m_AD_Window_ID = AD_Window_ID; 159 m_Attribute = Attribute; 160 m_DisplayAttribute = DisplayAttribute; 161 m_Value = Value; 162 m_DisplayValue = DisplayValue; 163 m_DisplayType = displayType; 164 m_AD_Reference_ID = AD_Reference_ID; 165 try 166 { 167 jbInit(); 168 dynInit(); 169 } 170 catch(Exception ex) 171 { 172 Log.error("ValuePreference", ex); 173 } 174 AEnv.showCenterScreen(this); 175 } 177 private Properties m_ctx; 178 private int m_WindowNo; 179 private int m_AD_Client_ID; 180 private int m_AD_Org_ID; 181 private int m_AD_User_ID; 182 private int m_AD_Window_ID; 183 private String m_Attribute; 184 private String m_DisplayAttribute; 185 private String m_Value; 186 private String m_DisplayValue; 187 private int m_DisplayType; 188 private int m_AD_Reference_ID; 189 190 private CPanel setPanel = new CPanel(); 192 private GridBagLayout setLayout = new GridBagLayout(); 193 private CLabel lAttribute = new CLabel(); 194 private CTextField fAttribute = new CTextField(); 195 private CLabel lAttributeValue = new CLabel(); 196 private CLabel lValue = new CLabel(); 197 private CLabel lValueValue = new CLabel(); 198 private CTextField fValue = new CTextField(); 199 private CLabel lSetFor = new CLabel(); 200 private VCheckBox cbClient = new VCheckBox(); 201 private VCheckBox cbOrg = new VCheckBox(); 202 private VCheckBox cbUser = new VCheckBox(); 203 private VCheckBox cbWindow = new VCheckBox(); 204 private CLabel lExplanation = new CLabel(); 205 private CPanel currentPanel = new CPanel(); 206 private TitledBorder titledBorder; 207 private JScrollPane scrollPane = new JScrollPane(); 208 private BorderLayout currentLayout = new BorderLayout(); 209 private JTable table = new JTable(); 210 211 private ConfirmPanel confirmPanel = new ConfirmPanel(true); 212 private JButton bDelete; 213 214 218 void jbInit() throws Exception 219 { 220 CompiereColor.setBackground(this); 221 setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 222 titledBorder = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(148, 145, 140)), 223 Msg.getMsg(m_ctx, "CurrentSettings")); 224 lAttribute.setText(Msg.translate(m_ctx, "Attribute")); 226 lValue.setText(Msg.translate(m_ctx, "Value")); 227 lSetFor.setText(Msg.getMsg(m_ctx, "ValuePreferenceSetFor")); 228 cbClient.setText(Msg.translate(m_ctx, "AD_Client_ID")); 229 cbClient.setEnabled(false); 230 cbClient.setSelected(true); 231 cbOrg.setText(Msg.translate(m_ctx, "AD_Org_ID")); 232 cbUser.setText(Msg.translate(m_ctx, "AD_User_ID")); 233 cbWindow.setText(Msg.translate(m_ctx, "AD_Window_ID")); 234 cbWindow.setSelected(true); 235 setPanel.setLayout(setLayout); 237 fAttribute.setEditable(false); 238 fValue.setEditable(false); 239 this.getContentPane().add(setPanel, BorderLayout.NORTH); 240 setPanel.add(lAttribute, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 241 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); 242 setPanel.add(fAttribute, new GridBagConstraints(1, 0, 4, 1, 0.0, 0.0 243 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); 244 setPanel.add(lValue, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0 245 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); 246 setPanel.add(fValue, new GridBagConstraints(1, 1, 4, 1, 0.0, 0.0 247 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); 248 setPanel.add(lSetFor, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0 249 ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); 250 setPanel.add(cbClient, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0 251 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); 252 setPanel.add(cbOrg, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0 253 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); 254 setPanel.add(cbUser, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0 255 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); 256 setPanel.add(cbWindow, new GridBagConstraints(4, 2, 1, 1, 0.0, 0.0 257 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); 258 setPanel.add(lAttributeValue, new GridBagConstraints(5, 0, 1, 1, 0.0, 0.0 259 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 260 setPanel.add(lValueValue, new GridBagConstraints(5, 1, 1, 1, 0.0, 0.0 261 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); 262 setPanel.add(lExplanation, new GridBagConstraints(1, 3, 4, 1, 0.0, 0.0 263 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); 264 currentPanel.setBorder(titledBorder); 266 currentPanel.setLayout(currentLayout); 267 currentPanel.add(scrollPane, BorderLayout.CENTER); 269 scrollPane.getViewport().add(table, null); 270 this.getContentPane().add(confirmPanel, BorderLayout.SOUTH); 271 } 273 276 private void dynInit() 277 { 278 fAttribute.setText(m_DisplayAttribute); 280 lAttributeValue.setText(m_Attribute); 281 fValue.setText(m_DisplayValue); 282 lValueValue.setText(m_Value); 283 if (Log.getTraceLevel() < Log.l6_Database) 284 { 285 lAttributeValue.setVisible(false); 286 lValueValue.setVisible(false); 287 } 288 289 cbClient.addActionListener(this); 291 cbOrg.addActionListener(this); 292 cbUser.addActionListener(this); 293 cbWindow.addActionListener(this); 294 295 confirmPanel.addActionListener(this); 297 bDelete = confirmPanel.addButton("Delete", Msg.getMsg(Env.getCtx(), "Delete"), Env.getImageIcon("Delete24.gif"), 0); 298 bDelete.addActionListener(this); 299 setExplanation(); 300 } 302 306 public void actionPerformed (ActionEvent e) 307 { 308 if (e.getActionCommand().equals(ConfirmPanel.A_CANCEL)) 309 { 310 dispose(); 311 } 312 else if (e.getActionCommand().equals(ConfirmPanel.A_OK)) 313 { 314 insert(); 315 dispose(); 316 } 317 else if (e.getSource() == bDelete) 318 { 319 int no = delete(); 320 if (no == 0) 321 ADialog.warn(m_WindowNo, this, "ValuePreferenceNotFound"); 322 else 323 ADialog.info(m_WindowNo, this, "ValuePreferenceDeleted", String.valueOf(no)); 324 dispose(); 325 } 326 else 327 setExplanation(); 328 } 330 333 private void setExplanation() 334 { 335 336 StringBuffer expl = new StringBuffer ("For "); 337 if (cbClient.isSelected() && cbOrg.isSelected()) 338 expl.append("this specific Client and Organization"); 339 else if (cbClient.isSelected() && !cbOrg.isSelected()) 340 expl.append("all Organizations of this Client"); 341 else if (!cbClient.isSelected() && cbOrg.isSelected()) 342 cbOrg.setSelected(false); 343 else 344 expl.append("entire System"); 345 if (cbUser.isSelected()) 346 expl.append(" and this User"); 347 else 348 expl.append(" and all Users"); 349 if (cbWindow.isSelected()) 350 expl.append(" and this Window"); 351 else 352 expl.append(" and all Windows"); 353 if (Env.getLanguage(Env.getCtx()).isBaseLanguage()) 355 { 356 lExplanation.setText (expl.toString ()); 357 this.pack (); 358 } 359 } 361 365 public int delete() 366 { 367 Log.trace(Log.l1_User, "ValuePreference.delete"); 368 369 StringBuffer sql = new StringBuffer ("DELETE FROM AD_Preference WHERE "); 370 sql.append("AD_Client_ID=").append(cbClient.isSelected() ? m_AD_Client_ID : 0); 371 sql.append(" AND AD_Org_ID=").append(cbOrg.isSelected() ? m_AD_Org_ID : 0); 372 if (cbUser.isSelected()) 373 sql.append(" AND AD_User_ID=").append(m_AD_User_ID); 374 else 375 sql.append(" AND AD_User_ID IS NULL"); 376 if (cbWindow.isSelected()) 377 sql.append(" AND AD_Window_ID=").append(m_AD_Window_ID); 378 else 379 sql.append(" AND AD_Window_ID IS NULL"); 380 sql.append(" AND Attribute='").append(m_Attribute).append("'"); 381 Log.trace(Log.l6_Database, sql.toString()); 383 int no = DB.executeUpdate(sql.toString()); 384 if (no > 0) 385 Env.setContext(m_ctx, getContextKey(), null); 386 return no; 387 } 389 393 private String getContextKey() 394 { 395 if (cbWindow.isSelected()) 396 return "P" + m_AD_Window_ID + "|" + m_Attribute; 397 else 398 return "P|" + m_Attribute; 399 } 401 404 public void insert() 405 { 406 Log.trace(Log.l1_User, "ValuePreference.insert"); 407 408 int no = delete(); 410 411 if (m_Value == null || m_Value.length() == 0) 413 { 414 if (no == 0) 415 ADialog.warn(m_WindowNo, this, "ValuePreferenceNotFound"); 416 else 417 ADialog.info(m_WindowNo, this, "ValuePreferenceDeleted", String.valueOf(no)); 418 return; 419 } 420 421 int Client_ID = cbClient.isSelected() ? m_AD_Client_ID : 0; 423 int Org_ID = cbOrg.isSelected() ? m_AD_Org_ID : 0; 424 int AD_Preference_ID = DB.getKeyNextNo(m_ctx, m_WindowNo, "AD_Preference"); 425 StringBuffer sql = new StringBuffer ("INSERT INTO AD_Preference (" 427 + "AD_Preference_ID, AD_Client_ID, AD_Org_ID, IsActive, Created,CreatedBy,Updated,UpdatedBy," 428 + "AD_Window_ID, AD_User_ID, Attribute, Value) VALUES ("); 429 sql.append(AD_Preference_ID).append(",").append(Client_ID).append(",").append(Org_ID) 430 .append(", 'Y',SysDate,").append(m_AD_User_ID).append(",SysDate,").append(m_AD_User_ID).append(", "); 431 if (cbWindow.isSelected()) 432 sql.append(m_AD_Window_ID).append(","); 433 else 434 sql.append("NULL,") ; 435 if (cbUser.isSelected()) 436 sql.append(m_AD_User_ID).append(","); 437 else 438 sql.append("NULL,"); 439 sql.append("'").append(m_Attribute).append("','").append(m_Value).append("')"); 440 Log.trace(Log.l6_Database, sql.toString()); 442 no = DB.executeUpdate(sql.toString()); 443 if (no == 1) 444 { 445 Env.setContext(m_ctx, getContextKey(), m_Value); 446 ADialog.info(m_WindowNo, this, "ValuePreferenceInserted"); 447 } 448 else 449 ADialog.warn(m_WindowNo, this, "ValuePreferenceNotInserted"); 450 451 } 453 } | Popular Tags |