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