PageRenderTime 254ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/ui/beans/LightMPComboBox.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 343 lines | 190 code | 39 blank | 114 comment | 17 complexity | 34e055fb70e68fec5859b88e6f7dc996 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. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.ui.beans;
  18. import java.awt.Font;
  19. import java.awt.event.ActionEvent;
  20. import java.awt.event.ActionListener;
  21. import java.awt.event.KeyEvent;
  22. import java.awt.event.KeyListener;
  23. import java.awt.event.MouseEvent;
  24. import java.awt.event.MouseListener;
  25. import java.util.Arrays;
  26. import java.util.List;
  27. import java.util.Vector;
  28. import javax.swing.ComboBoxEditor;
  29. import javax.swing.DefaultComboBoxModel;
  30. import javax.swing.JComboBox;
  31. import javax.swing.JTable;
  32. import javax.swing.SwingUtilities;
  33. import mpv5.db.common.Context;
  34. import mpv5.db.common.DatabaseObject;
  35. import mpv5.db.common.DatabaseSearch;
  36. import mpv5.handler.MPEnum;
  37. import mpv5.logging.Log;
  38. import mpv5.ui.frames.MPView;
  39. import mpv5.utils.models.MPComboBoxModelItem;
  40. import mpv5.utils.models.MPComboboxModel;
  41. import mpv5.utils.renderer.ComboBoxRendererForTooltip;
  42. /**
  43. *
  44. */
  45. public class LightMPComboBox extends JComboBox {
  46. private static final long serialVersionUID = 1L;
  47. public boolean SEARCH_ON_ENTER = false;
  48. public static final String VALUE_SEARCHFIELDS = ".lightmpcomboboxsearchvalue";
  49. private Context context;
  50. private int sortmode = 0;
  51. private JTable table;
  52. /**
  53. * If this combobox is within a table cell, set the table here
  54. * @param table
  55. */
  56. public void setTable(JTable table) {
  57. this.table = table;
  58. }
  59. private boolean initiated;
  60. /** Creates new form LabeledTextField */
  61. public LightMPComboBox() {
  62. super();
  63. getEditor().getEditorComponent().addKeyListener(new KeyListener() {
  64. @Override
  65. public void keyTyped(KeyEvent e) {
  66. }
  67. @Override
  68. public void keyPressed(KeyEvent e) {
  69. if (SEARCH_ON_ENTER && (e.getKeyCode() == KeyEvent.VK_F2) && context != null) {
  70. search();
  71. }
  72. // if (SEARCH_ON_ENTER && (e.getKeyCode() == KeyEvent.VK_DOWN) && context != null) {
  73. // showPopup();
  74. // }
  75. }
  76. @Override
  77. public void keyReleased(KeyEvent e) {
  78. }
  79. });
  80. getComponent(0).addMouseListener(new MouseListener() {
  81. @Override
  82. public void mouseClicked(MouseEvent e) {
  83. }
  84. @Override
  85. public void mousePressed(MouseEvent e) {
  86. if (SEARCH_ON_ENTER && !initiated) {
  87. if (table == null) {
  88. setModel(new DefaultComboBoxModel(new String[]{""}));
  89. }
  90. search();
  91. // if (table != null) {
  92. // showPopup();
  93. //// }
  94. }
  95. }
  96. @Override
  97. public void mouseReleased(MouseEvent e) {
  98. }
  99. @Override
  100. public void mouseEntered(MouseEvent e) {
  101. }
  102. @Override
  103. public void mouseExited(MouseEvent e) {
  104. }
  105. });
  106. setRenderer(new ComboBoxRendererForTooltip());
  107. setModel();
  108. }
  109. /**
  110. * Creates a new ComboBox with {@link MPCombobox#setSearchOnEnterEnabled(boolean)} enabled and the given search {@link Context}
  111. *
  112. * @param c
  113. * @param table
  114. */
  115. public LightMPComboBox(Context c, JTable table) {
  116. this();
  117. setSearchOnEnterEnabled(true);
  118. setContext(c);
  119. setTable(table);
  120. putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
  121. addActionListener(new ActionListener() {
  122. @Override
  123. public void actionPerformed(ActionEvent e) {
  124. for (int i = 0; i < receiver.size(); i++) {
  125. MPCBSelectionChangeReceiver mPCBSelectionChangeReceiver = receiver.get(i);
  126. mPCBSelectionChangeReceiver.changeSelection((MPComboBoxModelItem) getItemAt(getSelectedIndex()), context);
  127. }
  128. }
  129. });
  130. }
  131. public JComboBox getComboBox() {
  132. return this;
  133. }
  134. /**
  135. * Triggers the search functionality
  136. */
  137. public void search() {
  138. initiated = true;
  139. Runnable runnable = new Runnable() {
  140. @Override
  141. public void run() {
  142. ComboBoxEditor cbField = getEditor();
  143. Object value = cbField.getItem();
  144. if (value == null) {
  145. value = "";
  146. }
  147. String params = "ids, cname";
  148. String vars = null;
  149. if (mpv5.db.objects.User.getCurrentUser().getProperties().hasProperty(context + VALUE_SEARCHFIELDS)) {
  150. params = "ids";
  151. vars = mpv5.db.objects.User.getCurrentUser().getProperties().getProperty(context + VALUE_SEARCHFIELDS);
  152. String[] vaars = vars.split("_\\$");
  153. for (int i = 0; i < vaars.length; i++) {
  154. try {
  155. if (vaars[i] != null && vaars[i].contains("$_")) {
  156. params += "," + vaars[i].split("\\$_")[0].replace(",", "").replace("'", "`");
  157. }
  158. } catch (Exception e) {
  159. Log.Debug(e);
  160. }
  161. }
  162. }
  163. Object[][] data = new DatabaseSearch(context, 200).getValuesFor2(params, String.valueOf(value), null, true, true);
  164. setModel(MPComboBoxModelItem.toModel(MPComboBoxModelItem.toItems(data, true, MPComboBoxModelItem.COMPARE_BY_VALUE, vars)));
  165. try {
  166. table.editCellAt(table.getSelectedRow(), 4);
  167. } catch (Exception e) {
  168. }
  169. // showPopup();//avoid flickering
  170. }
  171. };
  172. new Thread(runnable).start();
  173. }
  174. /**
  175. *
  176. * @return The model
  177. */
  178. public MPComboboxModel getMPModel() {
  179. return (MPComboboxModel) getModel();
  180. }
  181. /**
  182. *
  183. * @param values
  184. */
  185. public void setModel(MPEnum[] values) {
  186. setModel(MPComboBoxModelItem.toModel(values));
  187. }
  188. /**
  189. *
  190. * @param values
  191. * @param compareMode
  192. */
  193. public void setModel(MPEnum[] values, int compareMode) {
  194. setModel(MPComboBoxModelItem.toModel(values, compareMode, new java.util.Vector<Integer>()));
  195. }
  196. /**
  197. * Delegates to setModel(MPComboBoxModelItem.toModel(data));
  198. * {id (hidden), value (shown in the list)}
  199. * @param data
  200. */
  201. public void setModel(Object[][] data) {
  202. setModel(MPComboBoxModelItem.toModel(data));
  203. }
  204. /**
  205. * Set the model. Should contain only {@link MPComboBoxModelItem}s
  206. * @param model
  207. */
  208. public void setModel(MPComboboxModel model) {
  209. super.setModel(model);
  210. }
  211. /**
  212. * Convenience Method to set a single {@link DatabaseObject} as the model of the combobox.<br/>
  213. * Will set the DO as the selected item after adding.
  214. * @param obj
  215. */
  216. public void setModel(DatabaseObject obj) {
  217. setModel(new Vector<DatabaseObject>(Arrays.asList(new DatabaseObject[]{
  218. obj
  219. })));
  220. setSelectedIndex(0);
  221. }
  222. /**
  223. * Convenience Method to set a {@link List} of {@link DatabaseObject}s as the model of the combobox.<br/>
  224. * @param vector
  225. */
  226. public void setModel(List<DatabaseObject> vector) {
  227. setModel(new MPComboboxModel(MPComboBoxModelItem.toItems(vector)));
  228. }
  229. /**
  230. * Sets the item with the given value as selected item
  231. * @param valueOfItem
  232. */
  233. public void setSelectedItem(String valueOfItem) {
  234. setSelectedIndex(MPComboBoxModelItem.getItemIDfromValue(valueOfItem, getModel()));
  235. }
  236. @Override
  237. public void setSelectedIndex(int index) {
  238. super.setSelectedIndex(index);
  239. // for (int i = 0; i < receiver.size(); i++) {
  240. // MPCBSelectionChangeReceiver mPCBSelectionChangeReceiver = receiver.get(i);
  241. // mPCBSelectionChangeReceiver.changeSelection((MPComboBoxModelItem) getItemAt(index), context);
  242. // }
  243. }
  244. List<MPCBSelectionChangeReceiver> receiver = new Vector<MPCBSelectionChangeReceiver>();
  245. /**
  246. *
  247. * @param rec
  248. */
  249. public void addSelectionChangeReceiver(MPCBSelectionChangeReceiver rec) {
  250. receiver.add(rec);
  251. }
  252. // /**
  253. // * Sets the item with the given ID as selected item
  254. // * @param ID
  255. // */
  256. // public void setSelectedItem(Object ID) {
  257. // setSelectedIndex(MPComboBoxModelItem.getItemID(ID,getModel()));
  258. // }
  259. /**
  260. * If set to true, hitting "Enter" on the text field will trigger a search for the entered value and popup the results if any.
  261. * <br/>{@link LabeledCombobox#setContext(Context)} must be called before this can work.
  262. * @param enabled
  263. */
  264. public void setSearchOnEnterEnabled(boolean enabled) {
  265. SEARCH_ON_ENTER = enabled;
  266. setEditable(true);
  267. }
  268. /**
  269. * Set the context for database queries
  270. * @param c
  271. */
  272. public void setContext(Context c) {
  273. this.context = c;
  274. }
  275. /**
  276. * @return the _text
  277. */
  278. public MPComboBoxModelItem getValue() {
  279. return (MPComboBoxModelItem) getSelectedItem();
  280. }
  281. @Override
  282. public void setEnabled(boolean enabled) {
  283. setEnabled(enabled);
  284. }
  285. /**
  286. 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.
  287. 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.
  288. 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.
  289. ActionListeners added to the combo box will be notified with an ActionEvent when this method is called.
  290. Parameters:
  291. anObject - the list object to select; use null to clear the selection
  292. * @param text
  293. */
  294. public void setValue(String text) {
  295. setSelectedItem(text);
  296. setPopupVisible(false);
  297. }
  298. /**
  299. * Sets an empty model
  300. */
  301. public void setModel() {
  302. setModel(new Vector<DatabaseObject>());
  303. }
  304. }