PageRenderTime 161ms CodeModel.GetById 61ms app.highlight 40ms RepoModel.GetById 30ms app.codeStats 0ms

/src/mpv5/ui/beans/LabeledTextField.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 391 lines | 246 code | 40 blank | 105 comment | 46 complexity | cdb39ceb96485f220ada58a34dec859d MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5
  6/*
  7 * LabeledTextField.java
  8 *
  9 * Created on 20.11.2008, 19:26:39
 10 */
 11package mpv5.ui.beans;
 12
 13import java.awt.Color;
 14import java.awt.Font;
 15import java.math.BigDecimal;
 16import javax.swing.JLabel;
 17import javax.swing.JTextField;
 18import mpv5.db.common.Context;
 19import mpv5.db.common.DatabaseObject;
 20import mpv5.db.common.NodataFoundException;
 21import mpv5.globals.Messages;
 22import mpv5.logging.Log;
 23import mpv5.ui.dialogs.Popup;
 24import mpv5.ui.panels.DataPanel;
 25import mpv5.utils.numberformat.FormatNumber;
 26import mpv5.utils.ui.TextFieldUtils;
 27
 28/**
 29 *
 30 *  
 31 */
 32public class LabeledTextField extends javax.swing.JPanel {
 33
 34    private static final long serialVersionUID = 1L;
 35    private String _text;
 36    private String _label;
 37    private Class clazz; // = Double.class;//default now
 38    private DataPanel parent;
 39    private Context context;
 40    private String searchField;
 41    private boolean searchOnEnterEnabled;
 42    private Object displayingObject;
 43
 44    /** Creates new form LabeledTextField */
 45    public LabeledTextField() {
 46        initComponents();
 47
 48    }
 49
 50    /**
 51     *
 52     * @param d
 53     * @param clazz
 54     */
 55    public LabeledTextField(Object d, Class clazz) {
 56        initComponents();
 57        set_ValueClass(clazz);
 58        setText(d);
 59    }
 60
 61    /**
 62     *
 63     * @return
 64     */
 65    public JTextField getTextField() {
 66        return jTextField1;
 67    }
 68
 69    /**
 70     *
 71     * @return
 72     */
 73    public JLabel getLabelField() {
 74        return jLabel1;
 75    }
 76
 77    /**
 78     * Determines if the field has some text
 79     * @return
 80     */
 81    public boolean hasText() {
 82        if (jTextField1.getText() != null && jTextField1.getText().length() > 0) {
 83            return true;
 84        } else {
 85            return false;
 86        }
 87    }
 88
 89    public void setText(String text) {
 90        setText((Object) text);
 91    }
 92
 93//    public void setLabelFont(Font font) {
 94//        setFont(font);
 95//
 96//    }
 97    /** This method is called from within the constructor to
 98     * initialize the form.
 99     * WARNING: Do NOT modify this code. The content of this method is
100     * always regenerated by the Form Editor.
101     */
102    @SuppressWarnings("unchecked")
103    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
104    private void initComponents() {
105
106        jLabel1 = new javax.swing.JLabel();
107        jTextField1 = new javax.swing.JTextField();
108
109        setOpaque(false);
110        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));
111
112        jLabel1.setText("text");
113        jLabel1.setMaximumSize(new java.awt.Dimension(300, 50));
114        jLabel1.setPreferredSize(new java.awt.Dimension(100, 20));
115        add(jLabel1);
116
117        jTextField1.setDisabledTextColor(new java.awt.Color(0, 51, 51));
118        jTextField1.setPreferredSize(new java.awt.Dimension(100, 20));
119        jTextField1.addActionListener(new java.awt.event.ActionListener() {
120            public void actionPerformed(java.awt.event.ActionEvent evt) {
121                jTextField1ActionPerformed(evt);
122            }
123        });
124        add(jTextField1);
125    }// </editor-fold>//GEN-END:initComponents
126
127    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
128        DatabaseObject data;
129        if (searchOnEnterEnabled) {
130            try {
131                if (searchField != null) {
132                    data = DatabaseObject.getObject(context, searchField, jTextField1.getText());
133                } else {
134                    data = DatabaseObject.getObject(context, jTextField1.getText());
135                }
136                parent.setDataOwner(data, true);
137               // Log.Debug(this, "Data found: " + data);
138               // Log.PrintArray(data.getValues3().toArray(new Object[][]{}));
139            } catch (NodataFoundException ex) {
140                Log.Debug(this, "Data NOT found: " + jTextField1.getText());
141                TextFieldUtils.blinkerRed(jTextField1);
142            }
143        }
144    }//GEN-LAST:event_jTextField1ActionPerformed
145
146    // Variables declaration - do not modify//GEN-BEGIN:variables
147    private javax.swing.JLabel jLabel1;
148    private javax.swing.JTextField jTextField1;
149    // End of variables declaration//GEN-END:variables
150
151    /**
152     * @return the _text
153     */
154    public String get_Text() {
155        return getText();
156    }
157
158    /**
159     * @param text the _text to set
160     */
161    public void setText(Object text) {
162        if (text == null) {
163            text = "";
164        }
165        if (clazz != null && (clazz == Double.class || clazz == BigDecimal.class)) {
166            try {
167                this._text = FormatNumber.formatDezimal((Number) text);
168            } catch (Exception e) {
169                try {
170                    this._text = FormatNumber.formatDezimal(new BigDecimal(String.valueOf(text)));
171                } catch (Exception ex) {
172                    this._text = String.valueOf(text);
173                }
174            }
175        } else {
176            this._text = String.valueOf(text);
177        }
178        jTextField1.setText(_text);
179    }
180
181    /**
182     * @param text the _text to set
183     */
184    public void set_Text(String text) {
185        setText(text);
186    }
187
188    /**
189     * @return the _label
190     */
191    public String get_Label() {
192        return jLabel1.getText();
193    }
194
195    /**
196     * @param label the _label to set
197     */
198    public void set_Label(String label) {
199        this._label = label;
200        jLabel1.setText(_label);
201        this.setToolTipText(_text);
202        jLabel1.setToolTipText(_text);
203    }
204
205    /**
206     * @param label the _label to set
207     */
208    public void setLabel(String label) {
209        set_Label(label);
210    }
211
212    public void set_LabelFont(Font font) {
213//        if (font != null) {
214//            jLabel1.setFont(font);
215//        }
216    }
217
218    @Override
219    public void setEnabled(boolean enabled) {
220//        jLabel1.setEnabled(enabled);
221        jTextField1.setEnabled(enabled);
222    }
223
224    /**
225     * Set the class of possible entries. Currently supported:
226     * <li>Integer - invalid values will be replaced with 0
227     *<li>Double - invalid values will be replaced with 0.0
228     * @param clazz
229     */
230    public void set_ValueClass(Class clazz) {
231        this.clazz = clazz;
232    }
233
234    public String getText() {
235        if (clazz == Integer.class) {
236            try {
237                Integer.valueOf(jTextField1.getText());
238                jTextField1.setBackground(Color.WHITE);
239            } catch (NumberFormatException numberFormatException) {
240                Log.Debug(this, numberFormatException.getMessage());
241                TextFieldUtils.blinker(jTextField1, Color.gray);
242                jTextField1.setText("0");
243            }
244        } else if (clazz == Double.class) {
245            try {
246                FormatNumber.parseDezimal(jTextField1.getText());
247                jTextField1.setBackground(Color.WHITE);
248            } catch (NumberFormatException numberFormatException) {
249                Log.Debug(this, numberFormatException.getMessage());
250                TextFieldUtils.blinker(jTextField1, Color.gray);
251                jTextField1.setText("0.0");
252            }
253        } else if (clazz == BigDecimal.class) {
254            try {
255                FormatNumber.parseDezimal(jTextField1.getText());
256                jTextField1.setBackground(Color.WHITE);
257            } catch (NumberFormatException numberFormatException) {
258                Log.Debug(this, jTextField1.getText());
259                Log.Debug(this, numberFormatException.getMessage());
260
261                TextFieldUtils.blinker(jTextField1, Color.gray);
262                jTextField1.setText("0.0");
263            }
264        }
265        String txt = jTextField1.getText();
266        if (txt == null) {
267            txt = "";
268        }
269        return txt;
270    }
271
272    /**
273     * @param parent the parent to set
274     */
275    public void setParent(DataPanel parent) {
276        this.parent = parent;
277    }
278
279    /**
280     * @return the context
281     */
282    public Context getContext() {
283        return context;
284    }
285
286    /**
287     * @param context the context to set
288     */
289    public void setContext(Context context) {
290        this.context = context;
291    }
292
293    /**
294     * @return the searchOnEnterEnabled
295     */
296    public boolean isSearchOnEnterEnabled() {
297        return searchOnEnterEnabled;
298    }
299
300    /**
301     * @param searchOnEnterEnabled the searchOnEnterEnabled to set
302     */
303    public void setSearchOnEnterEnabled(boolean searchOnEnterEnabled) {
304        if (searchOnEnterEnabled) {
305            jTextField1.setToolTipText(Messages.SEARCHABLE.toString());
306        }
307        this.searchOnEnterEnabled = searchOnEnterEnabled;
308    }
309
310    /**
311     *
312     * @param string
313     */
314    public void setSearchField(String string) {
315        searchField = string;
316    }
317
318    /**
319     * Set the object displayed by this text field for later use
320     * @param selectedItem
321     */
322    public void setDisplayingObject(Object selectedItem) {
323        this.displayingObject = selectedItem;
324        setText(selectedItem);
325    }
326
327    /**
328     * @return the displayingObject
329     */
330    public Object getDisplayingObject() {
331        return displayingObject;
332    }
333
334    @SuppressWarnings("unchecked")
335    public <T extends Object> T getValue(T classtemplate) {
336        if (clazz != classtemplate.getClass()) {
337            throw new IllegalArgumentException("Classtemplate must match value class! " + classtemplate.getClass() + ", " + clazz);
338        }
339        if (clazz == Integer.class) {
340            try {
341                Integer.valueOf(jTextField1.getText());
342                jTextField1.setBackground(Color.WHITE);
343                return (T) Integer.valueOf(jTextField1.getText());
344            } catch (Exception numberFormatException) {
345                Log.Debug(this, numberFormatException.getMessage());
346                TextFieldUtils.blinker(jTextField1, Color.gray);
347                jTextField1.setText(classtemplate.toString());
348            }
349        } else if (clazz == Double.class) {
350            try {
351                FormatNumber.parseDezimal(jTextField1.getText());
352                jTextField1.setBackground(Color.WHITE);
353                return (T) new Double(FormatNumber.parseDezimal(jTextField1.getText()).doubleValue());
354            } catch (Exception numberFormatException) {
355                Log.Debug(this, numberFormatException.getMessage());
356                TextFieldUtils.blinker(jTextField1, Color.gray);
357                jTextField1.setText(FormatNumber.formatDezimal((Double)classtemplate));
358            }
359        } else if (clazz == BigDecimal.class) {
360            try {
361                FormatNumber.parseDezimal(jTextField1.getText());
362                jTextField1.setBackground(Color.WHITE);
363                return (T) FormatNumber.parseDezimal(jTextField1.getText());
364            } catch (Exception numberFormatException) {
365                Log.Debug(this, jTextField1.getText());
366                Log.Debug(this, numberFormatException.getMessage());
367
368                TextFieldUtils.blinker(jTextField1, Color.gray);
369                jTextField1.setText(FormatNumber.formatDezimal((BigDecimal)classtemplate));
370            }
371        }
372        return classtemplate;
373    }
374
375    /**
376     * Forces a user input
377     * @param nonNull
378     * @param value
379     * @return
380     */
381    public String getText(boolean nonNull, String value) {
382        String text = getText();
383        if (text == null || text.length() == 0) {
384            text = Popup.Enter_Value(Messages.ENTER_VALUE, value);
385        }
386        if (text == null || text.length() == 0) {
387            text = value;
388        }
389        return text;
390    }
391}