KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.text.*;
19 import javax.swing.*;
20 import java.math.*;
21 import java.beans.*;
22
23 import org.compiere.apps.*;
24 import org.compiere.util.*;
25 import org.compiere.model.*;
26 import org.compiere.plaf.*;
27 import org.compiere.swing.*;
28
29 /**
30  * Location Control (Address)
31  *
32  * @author Jorg Janke
33  * @version $Id: VLocation.java,v 1.17 2003/09/05 04:58:59 jjanke Exp $
34  */

35 public class VLocation extends JComponent
36     implements VEditor, ActionListener
37 {
38     /**
39      * Constructor
40      *
41      * @param columnName column name
42      * @param mandatory mandatory
43      * @param isReadOnly read only
44      * @param isUpdateable updateable
45      * @param mLocation location model
46      */

47     public VLocation(String JavaDoc columnName, boolean mandatory, boolean isReadOnly, boolean isUpdateable,
48         MLocation mLocation)
49     {
50         super();
51         super.setName(columnName);
52         m_columnName = columnName;
53         m_mLocation = mLocation;
54         //
55
LookAndFeel.installBorder(this, "TextField.border");
56         this.setLayout(new BorderLayout());
57         // Size
58
this.setPreferredSize(m_text.getPreferredSize()); // causes r/o to be the same length
59
int height = m_text.getPreferredSize().height;
60
61         // Button
62
m_button.setIcon(Env.getImageIcon("Location10.gif"));
63         m_button.setMargin(new Insets(0,0,0,0));
64         m_button.setPreferredSize(new Dimension(height, height));
65         m_button.addActionListener(this);
66         this.add(m_button, BorderLayout.EAST);
67         // *** Button & Text ***
68
m_text.setBorder(null);
69         m_text.setEditable(false);
70         m_text.setFocusable(false);
71         m_text.setFont(CompierePLAF.getFont_Field());
72         m_text.setForeground(CompierePLAF.getTextColor_Normal());
73         m_text.addMouseListener(new VLocation_mouseAdapter(this));
74         this.add(m_text, BorderLayout.CENTER);
75
76         // Editable
77
if (isReadOnly || !isUpdateable)
78             setReadWrite (false);
79         else
80             setReadWrite (true);
81         setMandatory (mandatory);
82         //
83
mDelete = new JMenuItem(Msg.getMsg(Env.getCtx(), "Delete"), Env.getImageIcon ("Delete16.gif"));
84         mDelete.addActionListener(this);
85         popupMenu.add(mDelete);
86
87     } // VLocation
88

89     /**
90      * Dispose
91      */

92     public void dispose()
93     {
94         m_text = null;
95         m_button = null;
96         m_mLocation = null;
97     } // dispose
98

99     /** The Text Field */
100     private JTextField m_text = new JTextField(VLookup.DISPLAY_LENGTH);
101     /** The Button */
102     private CButton m_button = new CButton();
103
104     private MLocation m_mLocation;
105     private Object JavaDoc m_value;
106
107     private String JavaDoc m_columnName;
108
109     // Popup
110
JPopupMenu popupMenu = new JPopupMenu();
111     private JMenuItem mDelete;
112
113     /**
114      * Enable/disable
115      * @param value true if ReadWrite
116      */

117     public void setReadWrite (boolean value)
118     {
119         m_button.setReadWrite (value);
120         if (m_button.isVisible() != value)
121             m_button.setVisible (value);
122         setBackground(false);
123     } // setReadWrite
124

125     /**
126      * IsReadWrite
127      * @return value true if ReadWrite
128      */

129     public boolean isReadWrite()
130     {
131         return m_button.isReadWrite();
132     } // isReadWrite
133

134     /**
135      * Set Mandatory (and back bolor)
136      * @param mandatory true if mandatory
137      */

138     public void setMandatory (boolean mandatory)
139     {
140         m_button.setMandatory(mandatory);
141         setBackground(false);
142     } // setMandatory
143

144     /**
145      * Is it mandatory
146      * @return true if mandatory
147      */

148     public boolean isMandatory()
149     {
150         return m_button.isMandatory();
151     } // isMandatory
152

153     /**
154      * Set Background
155      * @param color color
156      */

157     public void setBackground (Color color)
158     {
159         if (!color.equals(m_text.getBackground()))
160             m_text.setBackground(color);
161     } // setBackground
162

163     /**
164      * Set Background based on editable / mandatory / error
165      * @param error if true, set background to error color, otherwise mandatory/editable
166      */

167     public void setBackground (boolean error)
168     {
169         if (error)
170             setBackground(CompierePLAF.getFieldBackground_Error());
171         else if (!isReadWrite())
172             setBackground(CompierePLAF.getFieldBackground_Inactive());
173         else if (isMandatory())
174             setBackground(CompierePLAF.getFieldBackground_Mandatory());
175         else
176             setBackground(CompierePLAF.getFieldBackground_Normal());
177     } // setBackground
178

179     /**
180      * Set Foreground
181      * @param fg color
182      */

183     public void setForeground(Color fg)
184     {
185         m_text.setForeground(fg);
186     } // setForeground
187

188     /**
189      * Set Editor to value
190      * @param value value
191      */

192     public void setValue(Object JavaDoc value)
193     {
194         m_value = value;
195         m_text.setText(m_mLocation.getDisplay(value)); // loads value
196
} // setValue
197

198     /**
199      * Property Change Listener
200      * @param evt PropertyChangeEvent
201      */

202     public void propertyChange (PropertyChangeEvent evt)
203     {
204         if (evt.getPropertyName().equals(org.compiere.model.MField.PROPERTY))
205             setValue(evt.getNewValue());
206     } // propertyChange
207

208     /**
209      * Return Editor value
210      * @return value
211      */

212     public Object JavaDoc getValue()
213     {
214         return new Integer JavaDoc (m_mLocation.getC_Location_ID());
215     } // getValue
216

217     /**
218      * Return Display Value
219      * @return display value
220      */

221     public String JavaDoc getDisplay()
222     {
223         return m_text.getText();
224     } // getDisplay
225

226     /**
227      * ActionListener - Button - Start Dialog
228      * @param e ActionEvent
229      */

230     public void actionPerformed(ActionEvent e)
231     {
232         if (e.getSource() == mDelete)
233             m_mLocation.load(0); // create new
234
//
235
VLocationDialog ld = new VLocationDialog(Env.getFrame(this),
236             Msg.getMsg(Env.getCtx(), "Location"), m_mLocation);
237         ld.setVisible(true);
238         //
239
if (e.getSource() == mDelete)
240             ;
241         else if (!ld.isChanged())
242             return;
243
244         // Data Binding
245
try
246         {
247             int location = m_mLocation.getC_Location_ID();
248             Integer JavaDoc loc = new Integer JavaDoc(location);
249             // force Change - user does not realize that embedded object is already saved.
250
fireVetoableChange(m_columnName, null, null); // resets m_mLocation
251
if (location != 0)
252                 fireVetoableChange(m_columnName, null, loc);
253             setValue(loc);
254         }
255         catch (PropertyVetoException pve)
256         {
257             Log.error("VLocation.actionPerformed", pve);
258         }
259
260     } // actionPerformed
261

262     /**
263      * Action Listener Interface
264      * @param listener listener
265      */

266     public void addActionListener(ActionListener listener)
267     {
268         m_text.addActionListener(listener);
269     } // addActionListener
270

271     /**
272      * Set Field/WindowNo for ValuePreference (NOP)
273      * @param mField Model Field
274      */

275     public void setField (org.compiere.model.MField mField)
276     {
277     } // setField
278

279 } // VLocation
280

281 /*****************************************************************************/
282
283 /**
284  * Mouse Listener for Popup Menu
285  */

286 final class VLocation_mouseAdapter extends java.awt.event.MouseAdapter JavaDoc
287 {
288     /**
289      * Constructor
290      * @param adaptee adaptee
291      */

292     VLocation_mouseAdapter(VLocation adaptee)
293     {
294         this.adaptee = adaptee;
295     } // VLookup_mouseAdapter
296

297     private VLocation adaptee;
298
299     /**
300      * Mouse Listener
301      * @param e MouseEvent
302      */

303     public void mouseClicked(MouseEvent e)
304     {
305         // popup menu
306
if (SwingUtilities.isRightMouseButton(e))
307             adaptee.popupMenu.show((Component)e.getSource(), e.getX(), e.getY());
308     } // mouse Clicked
309

310 } // VLocation_mouseAdapter
311
Popular Tags