KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > db > CConnectionEditor


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.db;
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import javax.swing.*;
19 import java.util.*;
20
21 import org.compiere.plaf.*;
22 import org.compiere.swing.*;
23
24 /**
25  * Connection Editor.
26  * A combo box and a button
27  *
28  * @author Jorg Janke
29  * @version $Id: CConnectionEditor.java,v 1.4 2002/10/07 01:19:47 jjanke Exp $
30  */

31 public class CConnectionEditor extends JComponent
32     implements CEditor
33 {
34     /**
35      * Connection Editor creating new Connection
36      */

37     public CConnectionEditor()
38     {
39         super();
40
41         CConnectionExitor_MouseListener ml = new CConnectionExitor_MouseListener();
42         // Layout
43
m_text.setEditable(false);
44         m_text.setBorder(null);
45         m_text.addMouseListener(ml);
46         m_server.setIcon(new ImageIcon(getClass().getResource("Server16.gif")));
47         m_server.setFocusable(false);
48         m_server.setBorder(null);
49         m_server.setOpaque(true);
50         m_server.addMouseListener(ml);
51         m_db.setIcon(new ImageIcon(getClass().getResource("Database16.gif")));
52         m_db.setFocusable(false);
53         m_db.setBorder(null);
54         m_db.setOpaque(true);
55         m_db.addMouseListener(ml);
56         LookAndFeel.installBorder(this, "TextField.border");
57         //
58
setLayout(new BorderLayout(0,0));
59         add(m_server, BorderLayout.WEST);
60         add(m_text, BorderLayout.CENTER);
61         add(m_db, BorderLayout.EAST);
62     } // CConnectionEditor
63

64     /** Text Element */
65     private JTextField m_text = new JTextField(10);
66     /** DB Button Element */
67     private JLabel m_db = new JLabel ();
68     /** Host Button Element */
69     private JLabel m_server = new JLabel();
70     /** The Value */
71     private CConnection m_value = null;
72     /** ReadWrite */
73     private boolean m_rw = true;
74     /** Mandatory */
75     private boolean m_mandatory = false;
76     /** Action Listeners */
77     transient private Vector m_actionListeners;
78
79     /**
80      * Enable Editor
81      * @param rw true, if you can enter/select data
82      */

83     public void setReadWrite (boolean rw)
84     {
85         m_rw = rw;
86         setBackground(false);
87     } // setReadWrite
88

89     /**
90      * Is it possible to edit
91      * @return true, if editable
92      */

93     public boolean isReadWrite()
94     {
95         return m_rw;
96     } // isReadWrite
97

98     /**
99      * Set Editor Mandatory
100      * @param mandatory true, if you have to enter data
101      */

102     public void setMandatory (boolean mandatory)
103     {
104         m_mandatory = mandatory;
105     } // setMandatory
106

107     /**
108      * Is Field mandatory
109      * @return true, if mandatory
110      */

111     public boolean isMandatory()
112     {
113         return m_mandatory;
114     } // isMandatory
115

116     /**
117      * Set Background based on editable / mandatory / error
118      * @param error if true, set background to error color, otherwise mandatory/editable
119      */

120     public void setBackground (boolean error)
121     {
122         Color c = null;
123         if (error)
124             c = CompierePLAF.getFieldBackground_Error();
125         else if (!m_rw)
126             c = CompierePLAF.getFieldBackground_Inactive();
127         else if (m_mandatory)
128             c = CompierePLAF.getFieldBackground_Mandatory();
129         else
130             c = CompierePLAF.getFieldBackground_Normal();
131         setBackground(c);
132     } // setBackground
133

134     /**
135      * Set Background color
136      * @param color
137      */

138     public void setBackground (Color color)
139     {
140         m_server.setBackground(color);
141         m_text.setBackground(color);
142         m_db.setBackground(color);
143     } // setBackground
144

145     /**
146      * Set Visible
147      * @param visible true if field is to be shown
148      */

149     public void setVisible (boolean visible)
150     {
151         this.setVisible(visible);
152     }
153
154     /**
155      * Set Editor to value
156      * @param value value of the editor
157      */

158     public void setValue (Object JavaDoc value)
159     {
160         if (value != null && value instanceof CConnection)
161             m_value = (CConnection)value;
162         setDisplay();
163     } // setValue
164

165     /**
166      * Return Editor value
167      * @return current value
168      */

169     public Object JavaDoc getValue()
170     {
171         return m_value;
172     } // getValue
173

174     /**
175      * Return Display Value
176      * @return displayed String value
177      */

178     public String JavaDoc getDisplay()
179     {
180         if (m_value == null)
181             return "";
182         return m_value.getName();
183     } // getDisplay
184

185     /**
186      * Update Display with Connection info
187      */

188     public void setDisplay()
189     {
190         m_text.setText(getDisplay());
191         if (m_value == null)
192             return;
193         // Text
194
if (m_value.isAppsServerOK(false) || m_value.isDatabaseOK())
195         {
196             m_text.setForeground(CompierePLAF.getTextColor_OK());
197             setBackground(false);
198             if (!m_value.isAppsServerOK(false))
199                 m_server.setBackground(CompierePLAF.getFieldBackground_Error());
200             if (!m_value.isDatabaseOK())
201                 m_db.setBackground(CompierePLAF.getFieldBackground_Error());
202         }
203         else
204         {
205             m_text.setForeground(CompierePLAF.getTextColor_Issue());
206             setBackground(true);
207         }
208     } // setDisplay
209

210     /*************************************************************************/
211
212     /**
213      * Remove Action Listener
214      * @param l
215      */

216     public synchronized void removeActionListener(ActionListener l)
217     {
218         if (m_actionListeners != null && m_actionListeners.contains(l))
219         {
220             Vector v = (Vector) m_actionListeners.clone();
221             v.removeElement(l);
222             m_actionListeners = v;
223         }
224     } // removeActionListener
225

226     /**
227      * Add Action Listener
228      * @param l
229      */

230     public synchronized void addActionListener(ActionListener l)
231     {
232         Vector v = m_actionListeners == null ? new Vector(2) : (Vector) m_actionListeners.clone();
233         if (!v.contains(l))
234         {
235             v.addElement(l);
236             m_actionListeners = v;
237         }
238     } // addActionListener
239

240     /**
241      * Fire Action Performed
242      */

243     private void fireActionPerformed()
244     {
245         if (m_actionListeners == null || m_actionListeners.size() == 0)
246             return;
247         ActionEvent e = new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
248                 "actionPerformed");
249         for (int i = 0; i < m_actionListeners.size(); i++)
250             ((ActionListener)m_actionListeners.get(i)).actionPerformed(e);
251     } // fireActionPerformed
252

253     /*************************************************************************/
254
255     /**
256      * Test Method
257      * @param args
258      */

259     public static void main(String JavaDoc[] args)
260     {
261     // System.out.println("CConnectionEditor");
262
JFrame frame = new JFrame("CConnectionEditor");
263         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
264         frame.getRootPane().getContentPane().add(new CConnectionEditor());
265         CompierePLAF.showCenterScreen(frame);
266     } // main
267

268
269     /**
270      * MouseListener
271      */

272     public class CConnectionExitor_MouseListener extends MouseAdapter
273     {
274         /**
275          * Mouse Clicked - Open Dialog
276          * @param e
277          */

278         public void mouseClicked(MouseEvent e)
279         {
280             if (!isEnabled() || !m_rw || m_active)
281                 return;
282             m_active = true;
283             setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
284             //
285
if (m_value == null)
286                 m_value = new CConnection();
287             CConnectionDialog cd = new CConnectionDialog(m_value);
288             setValue(cd.getConnection());
289             fireActionPerformed();
290             //
291
setCursor(Cursor.getDefaultCursor());
292             m_active = false;
293         } // mouseClicked
294

295         private boolean m_active = false;
296     } // CConnectionExitor_MouseListener
297

298 } // CConnectionEditor
299
Popular Tags