KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > grid > ed > ValuePreference


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.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 /**
30  * Maintain Value Preferences.
31  * To delete a preference, select a null value and save.
32  *
33  * @author Jorg Janke
34  * @version $Id: ValuePreference.java,v 1.8 2003/02/18 06:12:44 jjanke Exp $
35  */

36 public class ValuePreference extends JDialog implements ActionListener
37 {
38     /**
39      * Factory
40      * @param mField field
41      * @param aValue value
42      * @return ValuePreference
43      */

44     public static ValuePreference start (MField mField, Object JavaDoc aValue)
45     {
46         return start (mField, aValue, null);
47     } // start
48

49     /**
50      * Factory
51      * @param mField field
52      * @param aValue value
53      * @param aDisplayValue display value
54      * @return ValuePreference
55      */

56     public static ValuePreference start (MField mField, Object JavaDoc aValue, String JavaDoc aDisplayValue)
57     {
58         // Set Value/DisplayValue
59
String JavaDoc Value = null;
60         String JavaDoc DisplayValue = null;
61         if (aValue != null)
62         {
63             Value = aValue.toString();
64             DisplayValue = (aDisplayValue == null) ? Value : aDisplayValue;
65         }
66
67         // Get from mField
68
// AD_Window_ID, DisplayAttribute, Attribute, DisplayType, AD_Referenece_ID
69
int AD_Window_ID = mField.getAD_Window_ID();
70         String JavaDoc Attribute = mField.getColumnName();
71         String JavaDoc DisplayAttribute = mField.getHeader();
72         int displayType = mField.getDisplayType();
73         int AD_Reference_ID = 0;
74         int WindowNo = mField.getWindowNo();
75
76         // Get from Environment (WindowNo)
77
// AD_Client_ID, AD_Org_ID, AD_User_ID, Frame
78
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         // Create Editor
84
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     } // create
90

91     /**
92      * Create the popup menu item to start the ValuePreference editor.
93      * <code>
94      * .. add method
95      * public void setField (MField mField)
96      * {
97      * m_mField = mField;
98      * if (m_mField != null)
99      * ValuePreference.addMenu (this, m_popupMenu);
100      * } // setField
101      *
102      * .. in actionPerformed add ..
103      * if (e.getActionCommand().equals(ValuePreference.NAME))
104      * {
105      * ValuePreference.start (m_mField, getValue(), DisplayValue);
106      * return;
107      * }
108      * </code>
109      * @param l listener
110      * @param popupMenu menu
111      * @return JMenuItem
112      */

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     } // addMenu
121

122     /** The Name of the Editor */
123     public static final String JavaDoc NAME = "ValuePreference";
124     /** The Menu Icon */
125     private static Icon s_icon = new ImageIcon(org.compiere.Compiere.class.getResource("images/VPreference16.gif"));
126
127     /**
128      * Constructor
129      *
130      * @param frame parent
131      * @param WindowNo window no
132      * @param AD_Client_ID client
133      * @param AD_Org_ID org
134      * @param AD_User_ID user
135      * @param AD_Window_ID window id
136      * @param Attribute attribute
137      * @param DisplayAttribute attribute display
138      * @param Value value
139      * @param DisplayValue calue display
140      * @param displayType display type
141      * @param AD_Reference_ID reference
142      */

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 JavaDoc Attribute, String JavaDoc DisplayAttribute, String JavaDoc Value, String JavaDoc 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 JavaDoc ex)
171         {
172             Log.error("ValuePreference", ex);
173         }
174         AEnv.showCenterScreen(this);
175     } // ValuePreference
176

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 JavaDoc m_Attribute;
184     private String JavaDoc m_DisplayAttribute;
185     private String JavaDoc m_Value;
186     private String JavaDoc m_DisplayValue;
187     private int m_DisplayType;
188     private int m_AD_Reference_ID;
189
190     // Display
191
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     /**
215      * Static Layout
216      * @throws Exception
217      */

218     void jbInit() throws Exception JavaDoc
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         //
225
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         //
236
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         //
265
currentPanel.setBorder(titledBorder);
266         currentPanel.setLayout(currentLayout);
267     // this.getContentPane().add(currentPanel, BorderLayout.CENTER);
268
currentPanel.add(scrollPane, BorderLayout.CENTER);
269         scrollPane.getViewport().add(table, null);
270         this.getContentPane().add(confirmPanel, BorderLayout.SOUTH);
271     } // jbInit
272

273     /**
274      * Dynamic Init
275      */

276     private void dynInit()
277     {
278         // Set Attribute/Value
279
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         // ActionListener
290
cbClient.addActionListener(this);
291         cbOrg.addActionListener(this);
292         cbUser.addActionListener(this);
293         cbWindow.addActionListener(this);
294
295         // Other
296
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     } // dynInit
301

302     /**
303      * Action Listener
304      * @param e event
305      */

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     } // actionPerformed
329

330     /**
331      * Set Explanation
332      */

333     private void setExplanation()
334     {
335         /** @todo translation */
336         StringBuffer JavaDoc expl = new StringBuffer JavaDoc("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         //
354
if (Env.getLanguage(Env.getCtx()).isBaseLanguage())
355         {
356             lExplanation.setText (expl.toString ());
357             this.pack ();
358         }
359     } // setExplanation
360

361     /**
362      * Delete Preference
363      * @return number of rows deleted
364      */

365     public int delete()
366     {
367         Log.trace(Log.l1_User, "ValuePreference.delete");
368
369         StringBuffer JavaDoc sql = new StringBuffer JavaDoc ("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         //
382
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     } // delete
388

389     /**
390      * Get Context Key
391      * @return Context Key
392      */

393     private String JavaDoc getContextKey()
394     {
395         if (cbWindow.isSelected())
396             return "P" + m_AD_Window_ID + "|" + m_Attribute;
397         else
398             return "P|" + m_Attribute;
399     } // getContextKey
400

401     /**
402      * Save to Disk
403      */

404     public void insert()
405     {
406         Log.trace(Log.l1_User, "ValuePreference.insert");
407
408         // --- Delete first
409
int no = delete();
410
411         // Only Delete
412
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         // --- Inserting
422
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         //
426
StringBuffer JavaDoc sql = new StringBuffer JavaDoc ("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         //
441
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     } // insert
452

453 } // ValuePreference
454
Popular Tags