PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/ui/beans/LabeledCombobox.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 360 lines | 187 code | 44 blank | 129 comment | 13 complexity | df7ca48caf50fb30c066f2f7c8231be1 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.Font;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import java.util.Arrays;
  15. import java.util.List;
  16. import java.util.Vector;
  17. import javax.swing.JComboBox;
  18. import javax.swing.JTextArea;
  19. import mpv5.db.common.Context;
  20. import mpv5.db.common.DatabaseObject;
  21. import mpv5.handler.MPEnum;
  22. import mpv5.logging.Log;
  23. import mpv5.ui.dialogs.subcomponents.DatabaseObejctReceiver;
  24. import mpv5.ui.panels.DataPanel;
  25. import mpv5.utils.models.*;
  26. /**
  27. *
  28. *
  29. */
  30. public class LabeledCombobox extends javax.swing.JPanel {
  31. private static final long serialVersionUID = 1L;
  32. private String _text;
  33. private String _label;
  34. /** Creates new form LabeledTextField */
  35. public LabeledCombobox() {
  36. initComponents();
  37. }
  38. public JComboBox getComboBox() {
  39. return mPCombobox1.getComboBox();
  40. }
  41. /**
  42. *
  43. * @return The model
  44. */
  45. public MPComboboxModel getModel() {
  46. return mPCombobox1.getModel();
  47. }
  48. /**
  49. *
  50. * @param values
  51. */
  52. public void setModel(MPEnum[] values) {
  53. mPCombobox1.setModel(MPComboBoxModelItem.toModel(values));
  54. }
  55. /**
  56. * Sets an empty model
  57. */
  58. public void setModel() {
  59. mPCombobox1.setModel();
  60. }
  61. public void setModel(List<Context> values, boolean bloedsinn) {
  62. mPCombobox1.setModel(new MPComboboxModel(MPComboBoxModelItem.toItems(values, bloedsinn)));
  63. }
  64. /**
  65. *
  66. * @param values
  67. * @param compareMode
  68. * @param skip
  69. */
  70. public void setModel(MPEnum[] values, int compareMode, List<Integer> skip) {
  71. mPCombobox1.setModel(MPComboBoxModelItem.toModel(values, compareMode, skip));
  72. }
  73. /**
  74. * Uses enum.name() as ID, and enum.toString() as value.
  75. * @param values
  76. */
  77. public void setModel(Enum[] values) {
  78. String[][] val = new String[values.length][2];
  79. for (int i = 0; i < values.length; i++) {
  80. Enum enum1 = values[i];
  81. val[i][0] = enum1.name();
  82. val[i][1] = enum1.toString();
  83. }
  84. setModel(val);
  85. }
  86. /**
  87. * Delegates to setModel(MPComboBoxModelItem.toModel(data));
  88. * {id (hidden), value (shown in the list)}
  89. * @param data
  90. */
  91. public void setModel(Object[][] data) {
  92. mPCombobox1.setModel(MPComboBoxModelItem.toModel(data));
  93. }
  94. /**
  95. *
  96. * @return An {@link MPComboBoxModelItem} or null if nothing is selected. Attention - may return an item with ID -1 if a default item was set.
  97. */
  98. public MPComboBoxModelItem getSelectedItem() {
  99. if (mPCombobox1.getSelectedItem() instanceof MPComboBoxModelItem) {
  100. return mPCombobox1.getSelectedItem();
  101. } else {
  102. return null;
  103. }
  104. }
  105. /**
  106. * Set the model. Should contain only {@link MPComboBoxModelItem}s
  107. * @param model
  108. */
  109. public void setModel(MPComboboxModel model) {
  110. mPCombobox1.setModel(model);
  111. }
  112. /**
  113. * Convenience Method to set a single {@link DatabaseObject} as the model of the combobox.<br/>
  114. * Will set the DO as the selected item after adding.
  115. * @param obj
  116. */
  117. public void setModel(DatabaseObject obj) {
  118. setModel(new Vector<DatabaseObject>(Arrays.asList(new DatabaseObject[]{
  119. obj
  120. })));
  121. setSelectedIndex(0);
  122. }
  123. /**
  124. * Convenience Method to set a {@link List} of {@link DatabaseObject}s as the model of the combobox.<br/>
  125. * @param vector
  126. */
  127. public void setModel(List<DatabaseObject> vector) {
  128. setModel(new MPComboboxModel(MPComboBoxModelItem.toItems(vector)));
  129. }
  130. /**
  131. * Delegates to getComboBox().setSelectedIndex(itemID);
  132. * @param itemindex
  133. */
  134. public void setSelectedIndex(int itemindex) {
  135. if (getComboBox().getItemCount() > 0) {
  136. if (itemindex < getComboBox().getItemCount()) {
  137. getComboBox().setSelectedIndex(itemindex);
  138. } else {
  139. throw new IndexOutOfBoundsException(itemindex + " must be lower than " + getComboBox().getItemCount());
  140. }
  141. }
  142. }
  143. /**
  144. * Sets the item with the given value as selected item
  145. * @param valueOfItem
  146. */
  147. public void setSelectedItem(String valueOfItem) {
  148. mPCombobox1.setSelectedIndex(MPComboBoxModelItem.getItemIDfromValue(valueOfItem, mPCombobox1.getModel()));
  149. }
  150. /**
  151. * Sets the item with the given ID object as selected item
  152. * @param ID
  153. */
  154. public void setSelectedItem(Object ID) {
  155. mPCombobox1.setSelectedIndex(MPComboBoxModelItem.getItemID(ID, mPCombobox1.getModel()));
  156. }
  157. /**
  158. * If set to true, hitting "Enter" on the text field will trigger a search for the entered value and popup the results if any.
  159. * <br/>{@link LabeledCombobox#setContext(Context)} must be called before this can work.
  160. * @param enabled
  161. */
  162. public void setSearchEnabled(boolean enabled) {
  163. mPCombobox1.setSearchEnabled(enabled);
  164. }
  165. /**
  166. * Set the context for database queries
  167. * @param c
  168. */
  169. public void setContext(Context c) {
  170. mPCombobox1.setContext(c);
  171. }
  172. /** This method is called from within the constructor to
  173. * initialize the form.
  174. * WARNING: Do NOT modify this code. The content of this method is
  175. * always regenerated by the Form Editor.
  176. */
  177. @SuppressWarnings("unchecked")
  178. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  179. private void initComponents() {
  180. jLabel1 = new javax.swing.JLabel();
  181. mPCombobox1 = new mpv5.ui.beans.MPCombobox();
  182. setOpaque(false);
  183. setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));
  184. jLabel1.setText("text");
  185. jLabel1.setMaximumSize(new java.awt.Dimension(333, 333));
  186. jLabel1.setPreferredSize(new java.awt.Dimension(100, 18));
  187. add(jLabel1);
  188. mPCombobox1.setBorder(null);
  189. mPCombobox1.setMinimumSize(new java.awt.Dimension(33, 18));
  190. add(mPCombobox1);
  191. }// </editor-fold>//GEN-END:initComponents
  192. // Variables declaration - do not modify//GEN-BEGIN:variables
  193. private javax.swing.JLabel jLabel1;
  194. private mpv5.ui.beans.MPCombobox mPCombobox1;
  195. // End of variables declaration//GEN-END:variables
  196. /**
  197. * Gives a {@link MPComboBoxModelItem} or null if nothing is selected
  198. * @return the combobox item
  199. * @throws ClassCastException
  200. */
  201. public MPComboBoxModelItem getValue() {
  202. return mPCombobox1.getSelectedItem();
  203. }
  204. /**
  205. * The textual representation of the seleceted item or null if nothing is selected
  206. * @return the text of the combobox
  207. */
  208. public String getText() {
  209. String t = mPCombobox1.getComboBox().getEditor().getItem().toString();
  210. if (t.equals("null")) {
  211. return null;
  212. } else {
  213. return t;
  214. }
  215. }
  216. /**
  217. * @return the _label
  218. */
  219. public String get_Label() {
  220. return jLabel1.getText();
  221. }
  222. /**
  223. * @param label the _label to set
  224. */
  225. public void set_Label(String label) {
  226. this._label = label;
  227. jLabel1.setText(_label);
  228. this.setToolTipText(_text);
  229. jLabel1.setToolTipText(_text);
  230. }
  231. @Deprecated
  232. public void set_LabelFont(Font font) {
  233. }
  234. @Override
  235. public void setEnabled(boolean enabled) {
  236. jLabel1.setEnabled(enabled);
  237. mPCombobox1.setEnabled(enabled);
  238. }
  239. /**
  240. Sets the selected item in the combo box display area to the object in the argument. If anObject is in the list, the display area shows anObject selected.
  241. If anObject is not in the list and the combo box is uneditable, it will not change the current selection. For editable combo boxes, the selection will change to anObject.
  242. If this constitutes a change in the selected item, ItemListeners added to the combo box will be notified with one or two ItemEvents. If there is a current selected item, an ItemEvent will be fired and the state change will be ItemEvent.DESELECTED. If anObject is in the list and is not currently selected then an ItemEvent will be fired and the state change will be ItemEvent.SELECTED.
  243. ActionListeners added to the combo box will be notified with an ActionEvent when this method is called.
  244. Parameters:
  245. anObject - the list object to select; use null to clear the selection
  246. * @param text
  247. */
  248. public void setValue(String text) {
  249. mPCombobox1.setValue(text);
  250. }
  251. /**
  252. * Trigger a search on the combo box
  253. */
  254. public void triggerSearch() {
  255. mPCombobox1.search(true);
  256. }
  257. /**
  258. * Define a textfield wich displays selection changes
  259. * @param receiver
  260. */
  261. public void setReceiver(final LabeledTextField receiver) {
  262. getComboBox().addActionListener(new ActionListener() {
  263. public void actionPerformed(ActionEvent e) {
  264. receiver.setDisplayingObject(getSelectedItem());
  265. }
  266. });
  267. }
  268. /**
  269. *
  270. * @param panel
  271. */
  272. public void setReceiver(DataPanel panel) {
  273. mPCombobox1.setReceiver(panel);
  274. }
  275. /**
  276. *
  277. * @param b
  278. */
  279. public void setSearchOnEnterEnabled(boolean b) {
  280. setSearchEnabled(b);
  281. }
  282. /**
  283. * Enable/disable editing of the combobox
  284. * @param b
  285. */
  286. public void setEditable(boolean b) {
  287. getComboBox().setEditable(b);
  288. }
  289. public void setReceiver(final JTextArea receiver) {
  290. getComboBox().addActionListener(new ActionListener() {
  291. public void actionPerformed(ActionEvent e) {
  292. try {
  293. receiver.setText(getSelectedItem().toString());
  294. } catch (Exception eg) {
  295. }
  296. }
  297. });
  298. }
  299. public void setReceiver(final DatabaseObejctReceiver obj) {
  300. getComboBox().addActionListener(new ActionListener() {
  301. public void actionPerformed(ActionEvent e) {
  302. try {
  303. if (getSelectedItem().getSelectedItem() instanceof DatabaseObject) {
  304. obj.receive((DatabaseObject) getSelectedItem().getSelectedItem());
  305. } else if (mPCombobox1.getContext() != null) {
  306. obj.receive(DatabaseObject.getObject(mPCombobox1.getContext(), (Integer) getSelectedItem().getIdObject()));
  307. } else {
  308. Log.Debug(this, "No dbos in model or nor context set!");
  309. }
  310. } catch (Exception ex) {
  311. Log.Debug(this, ex.getMessage());
  312. }
  313. }
  314. });
  315. }
  316. }