PageRenderTime 28ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/ui/beans/MPCombobox.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 460 lines | 264 code | 55 blank | 141 comment | 38 complexity | a3e6106fd36aa900d95a767d3e31f3a7 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.awt.event.KeyEvent;
  15. import java.awt.event.KeyListener;
  16. import java.awt.event.MouseEvent;
  17. import java.awt.event.MouseListener;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import java.util.Vector;
  21. import javax.swing.ComboBoxEditor;
  22. import javax.swing.DefaultComboBoxModel;
  23. import javax.swing.JComboBox;
  24. import javax.swing.JTable;
  25. import mpv5.db.common.ConnectionTypeHandler;
  26. import mpv5.db.common.Context;
  27. import mpv5.db.common.DatabaseObject;
  28. import mpv5.db.common.DatabaseSearch;
  29. import mpv5.db.common.NodataFoundException;
  30. import mpv5.globals.Messages;
  31. import mpv5.handler.MPEnum;
  32. import mpv5.logging.Log;
  33. import mpv5.ui.panels.DataPanel;
  34. import mpv5.utils.models.*;
  35. import mpv5.utils.renderer.ComboBoxRendererForTooltip;
  36. /**
  37. *
  38. *
  39. */
  40. public class MPCombobox extends javax.swing.JPanel {
  41. private static final long serialVersionUID = 1L;
  42. public boolean SEARCH_ON_ENTER = false;
  43. private Context context;
  44. private int sortmode = 0;
  45. private JTable table;
  46. private boolean instantiated;
  47. private DataPanel receiver;
  48. private String prevSearch = "";
  49. /**
  50. * If this combobox is within a table cell, set the table here
  51. * @param table
  52. */
  53. public void setTable(JTable table) {
  54. this.table = table;
  55. }
  56. /** Creates new form LabeledTextField */
  57. public MPCombobox() {
  58. initComponents();
  59. jComboBox1.getEditor().getEditorComponent().addKeyListener(new KeyListener() {
  60. @Override
  61. public void keyTyped(KeyEvent e) {
  62. }
  63. @Override
  64. public void keyPressed(KeyEvent e) {
  65. if (SEARCH_ON_ENTER && (e.getKeyCode() == KeyEvent.VK_ENTER) && getContext() != null) {
  66. search();
  67. }
  68. // if (table != null) {
  69. jComboBox1.showPopup();
  70. // }
  71. // if (table != null) {
  72. // JComboBox combobox = getComboBox();
  73. // if (!combobox.isPopupVisible()) {
  74. // combobox.hidePopup();
  75. // } else {
  76. // combobox.showPopup();
  77. // }
  78. // }
  79. }
  80. @Override
  81. public void keyReleased(KeyEvent e) {
  82. }
  83. });
  84. jComboBox1.getComponent(0).addMouseListener(new MouseListener() {
  85. @Override
  86. public void mouseClicked(MouseEvent e) {
  87. }
  88. @Override
  89. public void mousePressed(MouseEvent e) {
  90. if (SEARCH_ON_ENTER && !instantiated) {
  91. if (table == null) {
  92. jComboBox1.setModel(new DefaultComboBoxModel(new String[]{""}));
  93. }
  94. search();
  95. // if (table != null) {
  96. jComboBox1.showPopup();
  97. // }
  98. }
  99. }
  100. @Override
  101. public void mouseReleased(MouseEvent e) {
  102. }
  103. @Override
  104. public void mouseEntered(MouseEvent e) {
  105. }
  106. @Override
  107. public void mouseExited(MouseEvent e) {
  108. }
  109. });
  110. jComboBox1.setRenderer(new ComboBoxRendererForTooltip());
  111. // jComboBox1.setToolTipText(Messages.SEARCHABLE.toString());
  112. setModel();
  113. }
  114. /**
  115. * Creates a new ComboBox with {@link MPCombobox#setSearchOnEnterEnabled(boolean)} enabled and the given search {@link Context}
  116. *
  117. * @param c
  118. * @param table
  119. */
  120. public MPCombobox(Context c, JTable table) {
  121. this();
  122. setSearchEnabled(true);
  123. setContext(c);
  124. setTable(table);
  125. getComboBox().putClientProperty("JComboBox.isTableCellEditor", table != null);
  126. }
  127. public JComboBox getComboBox() {
  128. return jComboBox1;
  129. }
  130. /**
  131. * Triggers the search functionality
  132. */
  133. public void search() {
  134. search(false);
  135. }
  136. /**
  137. * Triggers the search functionality
  138. * @param hidePopup
  139. */
  140. public synchronized void search(final boolean hidePopup) {
  141. if (SEARCH_ON_ENTER) {
  142. instantiated = true;
  143. Runnable runnable = new Runnable() {
  144. @Override
  145. public void run() {
  146. ComboBoxEditor cbField = jComboBox1.getEditor();
  147. Object value = cbField.getItem();
  148. jComboBox1.setSelectedItem(new MPComboBoxModelItem(-1, value.toString()));
  149. if (prevSearch == null) {
  150. prevSearch = String.valueOf(value);
  151. } else {
  152. value = String.valueOf(value).equals(prevSearch) ? "" : String.valueOf(value);
  153. prevSearch = null;
  154. }
  155. Object[][] data = null;
  156. if (getComboBox().isEditable()) {
  157. if (getContext().equals(Context.getProduct())) {
  158. data = new DatabaseSearch(getContext(), 200).getValuesFor2("ids, " + ConnectionTypeHandler.concat("cname","' '", ConnectionTypeHandler.getToChar("externalnetvalue")), String.valueOf(value), null, true, true);
  159. } else if (((getContext().equals(Context.getCustomer()) || getContext().equals(Context.getManufacturer()) || getContext().equals(Context.getSupplier())) && mpv5.db.objects.User.getCurrentUser().getProperties().getProperty("org.openyabs.uiproperty", "companiesovernames")) || getContext().isCompany()==true) {
  160. data = new DatabaseSearch(getContext(), 200).getValuesFor("ids, company", "company", jComboBox1.getSelectedItem().toString(), true);
  161. } else {
  162. data = new DatabaseSearch(getContext(), 200).getValuesFor("ids, cname", "cname", jComboBox1.getSelectedItem().toString(), true);
  163. }
  164. } else {
  165. data = new DatabaseSearch(getContext(), 200).getValuesFor("ids, cname", "cname", "", true);
  166. }
  167. jComboBox1.setModel(MPComboBoxModelItem.toModel(MPComboBoxModelItem.toItems(data, true, true)));
  168. if (data.length > 1) {
  169. if (table != null) {
  170. table.editCellAt(table.getSelectedRow(), 4);
  171. }
  172. if (!hidePopup) {
  173. jComboBox1.showPopup();
  174. }
  175. }
  176. }
  177. };
  178. if (getContext() != null) {
  179. new Thread(runnable).start();
  180. }
  181. }
  182. }
  183. /**
  184. *
  185. * @return The model
  186. */
  187. public MPComboboxModel getModel() {
  188. return (MPComboboxModel) jComboBox1.getModel();
  189. }
  190. /**
  191. *
  192. * @param values
  193. */
  194. public void setModel(MPEnum[] values) {
  195. jComboBox1.setModel(MPComboBoxModelItem.toModel(values));
  196. }
  197. /**
  198. *
  199. * @param values
  200. * @param compareMode
  201. */
  202. public void setModel(MPEnum[] values, int compareMode) {
  203. jComboBox1.setModel(MPComboBoxModelItem.toModel(values, compareMode, new java.util.Vector<Integer>()));
  204. }
  205. /**
  206. * Uses enum.name() as ID, and enum.toString() as value.
  207. * @param values
  208. */
  209. public void setModel(Enum[] values) {
  210. String[][] val = new String[values.length][2];
  211. for (int i = 0; i < values.length; i++) {
  212. Enum enum1 = values[i];
  213. val[i][0] = enum1.name();
  214. val[i][1] = enum1.toString();
  215. }
  216. setModel(val);
  217. }
  218. /**
  219. * Delegates to setModel(MPComboBoxModelItem.toModel(data));
  220. * {id (hidden), value (shown in the list)}
  221. * @param data
  222. */
  223. public void setModel(Object[][] data) {
  224. jComboBox1.setModel(MPComboBoxModelItem.toModel(data));
  225. }
  226. /**
  227. *
  228. * @return An {@link MPComboBoxModelItem} or null if nothing is selected
  229. */
  230. public MPComboBoxModelItem getSelectedItem() {
  231. if (jComboBox1.getSelectedItem() instanceof MPComboBoxModelItem) {
  232. return (MPComboBoxModelItem) jComboBox1.getSelectedItem();
  233. } else {
  234. return null;
  235. }
  236. }
  237. /**
  238. * The textual representation of the seleceted item
  239. * @return
  240. */
  241. public String getSelectedValue() {
  242. return String.valueOf(jComboBox1.getSelectedItem());
  243. }
  244. /**
  245. * Set the model. Should contain only {@link MPComboBoxModelItem}s
  246. * @param model
  247. */
  248. public void setModel(MPComboboxModel model) {
  249. jComboBox1.setModel(model);
  250. }
  251. /**
  252. * Convenience Method to set a single {@link DatabaseObject} as the model of the combobox.<br/>
  253. * Will set the DO as the selected item after adding.
  254. * @param obj
  255. */
  256. public void setModel(DatabaseObject obj) {
  257. setModel(new Vector<DatabaseObject>(Arrays.asList(new DatabaseObject[]{
  258. obj
  259. })));
  260. setSelectedIndex(0);
  261. }
  262. /**
  263. * Convenience Method to set a {@link List} of {@link DatabaseObject}s as the model of the combobox.<br/>
  264. * @param vector
  265. */
  266. public void setModel(List<DatabaseObject> vector) {
  267. setModel(new MPComboboxModel(MPComboBoxModelItem.toItems(vector)));
  268. }
  269. /**
  270. * Delegates to getComboBox().setSelectedIndex(itemID);
  271. * @param itemID
  272. */
  273. public void setSelectedIndex(int itemID) {
  274. if (itemID >= 0 && itemID < getComboBox().getItemCount()) {
  275. getComboBox().setSelectedIndex(itemID);
  276. }
  277. }
  278. /**
  279. * Sets the item with the given value as selected item
  280. * @param valueOfItem
  281. */
  282. public void setSelectedItem(String valueOfItem) {
  283. jComboBox1.setSelectedIndex(MPComboBoxModelItem.getItemIDfromValue(valueOfItem, jComboBox1.getModel()));
  284. }
  285. /**
  286. * Sets the item with the given ID as selected item
  287. * @param ID
  288. */
  289. public void setSelectedItem(Object ID) {
  290. jComboBox1.setSelectedIndex(MPComboBoxModelItem.getItemID(ID, jComboBox1.getModel()));
  291. }
  292. /**
  293. * If set to true, hitting "Enter" on the text field will trigger a search for the entered value and popup the results if any.
  294. * <br/>{@link LabeledCombobox#setContext(Context)} must be called before this can work.
  295. * @param enabled
  296. */
  297. public void setSearchEnabled(boolean enabled) {
  298. SEARCH_ON_ENTER = enabled;
  299. jComboBox1.setEditable(enabled);
  300. if (enabled) {
  301. jComboBox1.setToolTipText(Messages.SEARCHABLE.toString());
  302. }
  303. }
  304. /**
  305. * Set the context for database queries
  306. * @param c
  307. */
  308. public void setContext(Context c) {
  309. this.context = c;
  310. }
  311. /** This method is called from within the constructor to
  312. * initialize the form.
  313. * WARNING: Do NOT modify this code. The content of this method is
  314. * always regenerated by the Form Editor.
  315. */
  316. @SuppressWarnings("unchecked")
  317. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  318. private void initComponents() {
  319. jComboBox1 = new javax.swing.JComboBox();
  320. setBorder(null);
  321. setOpaque(false);
  322. setLayout(new java.awt.BorderLayout());
  323. jComboBox1.setAutoscrolls(true);
  324. jComboBox1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(138, 138, 138), 1, true));
  325. jComboBox1.setMinimumSize(new java.awt.Dimension(33, 18));
  326. jComboBox1.setPreferredSize(new java.awt.Dimension(100, 20));
  327. jComboBox1.addMouseListener(new java.awt.event.MouseAdapter() {
  328. public void mouseClicked(java.awt.event.MouseEvent evt) {
  329. jComboBox1MouseClicked(evt);
  330. }
  331. });
  332. add(jComboBox1, java.awt.BorderLayout.CENTER);
  333. }// </editor-fold>//GEN-END:initComponents
  334. private void jComboBox1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jComboBox1MouseClicked
  335. search();
  336. }//GEN-LAST:event_jComboBox1MouseClicked
  337. // Variables declaration - do not modify//GEN-BEGIN:variables
  338. private javax.swing.JComboBox jComboBox1;
  339. // End of variables declaration//GEN-END:variables
  340. /**
  341. * @return the _text
  342. */
  343. public MPComboBoxModelItem getValue() {
  344. return (MPComboBoxModelItem) jComboBox1.getSelectedItem();
  345. }
  346. @Deprecated
  347. public void set_LabelFont(Font font) {
  348. }
  349. @Override
  350. public void setEnabled(boolean enabled) {
  351. jComboBox1.setEnabled(enabled);
  352. }
  353. /**
  354. 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.
  355. 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.
  356. 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.
  357. ActionListeners added to the combo box will be notified with an ActionEvent when this method is called.
  358. Parameters:
  359. anObject - the list object to select; use null to clear the selection
  360. * @param text
  361. */
  362. public void setValue(String text) {
  363. jComboBox1.setSelectedItem(text);
  364. jComboBox1.setPopupVisible(false);
  365. }
  366. /**
  367. * Sets an empty model
  368. */
  369. public void setModel() {
  370. setModel(new Vector<DatabaseObject>());
  371. }
  372. /**
  373. * Trigger a search on the combo box
  374. */
  375. public void triggerSearch() {
  376. search(true);
  377. }
  378. public void setReceiver(final DataPanel panel) {
  379. this.receiver = panel;
  380. jComboBox1.addActionListener(new ActionListener() {
  381. public void actionPerformed(ActionEvent e) {
  382. try {
  383. if (getSelectedItem() != null) {
  384. panel.setDataOwner(DatabaseObject.getObject(getContext(), Integer.valueOf(getSelectedItem().getId())), true);
  385. }
  386. } catch (NodataFoundException ex) {
  387. Log.Debug(this, ex.getMessage());
  388. }
  389. }
  390. });
  391. }
  392. /**
  393. *
  394. * @return The id obj of the selected entry or NULL if nothing is selected
  395. */
  396. public Object getSelectedItemId() {
  397. return getSelectedItem().getIdObject();
  398. }
  399. /**
  400. * @return the context
  401. */
  402. public Context getContext() {
  403. return context;
  404. }
  405. }