/src/mpv5/utils/models/MPComboBoxModelItem.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/ · Java · 602 lines · 297 code · 50 blank · 255 comment · 58 complexity · 20c865df8db0c1f8643030cd9e69f84b MD5 · raw file

  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.utils.models;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import java.util.Vector;
  21. import javax.swing.ComboBoxModel;
  22. import javax.swing.DefaultComboBoxModel;
  23. import mpv5.db.common.DatabaseObject;
  24. import mpv5.handler.MPEnum;
  25. import mpv5.logging.Log;
  26. /**
  27. * A MPComboBoxModelItem consists of a visible "value" part and an invisible "ID" part.
  28. *
  29. */
  30. public class MPComboBoxModelItem extends DefaultComboBoxModel implements Comparable<MPComboBoxModelItem> {
  31. private static final long serialVersionUID = 1L;
  32. public static int COMPARE_BY_ID = 0;
  33. /**
  34. * (default)
  35. */
  36. public static int COMPARE_BY_VALUE = 1;
  37. /**
  38. * The id field may have any class.
  39. */
  40. public Class ID_CLASS = String.class;
  41. /**
  42. * Convenience method for Integer IDs. Returns the index of the item with the given id
  43. * @param uid
  44. * @param model
  45. * @return
  46. */
  47. public static int getItemID(Integer uid, ComboBoxModel model) {
  48. return getItemID((Object) new Integer(uid), model);
  49. }
  50. /**
  51. * Returns the index of the item with the given id
  52. * @param uid
  53. * @param model
  54. * @return
  55. */
  56. public static int getItemID(Object uid, ComboBoxModel model) {
  57. for (int i = 0; i < model.getSize(); i++) {
  58. // Log.Debug(MPComboBoxModelItem.class, ((MPComboBoxModelItem) model.getElementAt(i)).id + " comparing with: " + uid);
  59. if (((MPComboBoxModelItem) model.getElementAt(i)).id.equals(uid)) {
  60. // Log.Debug(MPComboBoxModelItem.class, "Found at Index:" + i);
  61. return i;
  62. }
  63. }
  64. return -1;
  65. }
  66. /**
  67. * Convenience method for String IDs. Returns the index of the item with the given id
  68. * @param uid
  69. * @param model
  70. * @return
  71. */
  72. public static int getItemID(String uid, ComboBoxModel model) {
  73. for (int i = 0; i < model.getSize(); i++) {
  74. // Log.Debug(MPComboBoxModelItem.class, ((MPComboBoxModelItem) model.getElementAt(i)).id + " comparing with: " + uid);
  75. if (((MPComboBoxModelItem) model.getElementAt(i)).id.toString().equals(uid)) {
  76. // Log.Debug(MPComboBoxModelItem.class, "Found at Index:" + i);
  77. return i;
  78. }
  79. }
  80. return 0;
  81. }
  82. /**
  83. * Returns the index of the item with the given id
  84. * @param value
  85. * @param model
  86. * @return
  87. */
  88. public synchronized static int getItemIDfromValue(String value, ComboBoxModel model) {
  89. for (int i = 0; i < model.getSize(); i++) {
  90. // Log.Debug(MPComboBoxModelItem.class, ((MPComboBoxModelItem) model.getElementAt(i)).name + " comparing with: " + value);
  91. if (((MPComboBoxModelItem) model.getElementAt(i)).name.equals(value)) {
  92. // Log.Debug(MPComboBoxModelItem.class, "Found at Index:" + i);
  93. return i;
  94. }
  95. }
  96. return 0;
  97. }
  98. /**
  99. * Converts an array to mp combo box items
  100. * {id (hidden), value (shown in the list)}
  101. * @param items
  102. * @return
  103. */
  104. public static MPComboBoxModelItem[] toItems(Object[][] items) {
  105. return toItems(items, COMPARE_BY_VALUE);
  106. }
  107. /**
  108. * Converts an array to mp combo box items
  109. * {id (hidden), values (shown in the list)}
  110. * @param items
  111. * @param compareMode
  112. * @return
  113. */
  114. public static MPComboBoxModelItem[] toItems(Object[][] items, int compareMode) {
  115. return toItems(items, compareMode, false);
  116. }
  117. /**
  118. * Converts an array to mp combo box items
  119. * {id (hidden), values (shown in the list)}
  120. * @param items
  121. * @param compareMode
  122. * @param convertIndexToInteger
  123. * @return
  124. */
  125. public static MPComboBoxModelItem[] toItems(Object[][] items, int compareMode, boolean convertIndexToInteger) {
  126. MPComboBoxModelItem[] array = new MPComboBoxModelItem[items.length];
  127. for (int i = 0; i < array.length; i++) {
  128. String x = "";
  129. for (int j = 1; j < items[i].length; j++) {
  130. String k = String.valueOf(items[i][j]).trim();
  131. x += k + " ";
  132. }
  133. if (convertIndexToInteger) {
  134. array[i] = new MPComboBoxModelItem(Integer.valueOf(items[i][0].toString()), x.substring(0, x.length() - 1));
  135. } else {
  136. array[i] = new MPComboBoxModelItem((items[i][0]), x.substring(0, x.length() - 1));
  137. }
  138. array[i].setCompareMode(compareMode);
  139. }
  140. return array;
  141. }
  142. /**
  143. * Converts an array to mp combo box items
  144. * {id (hidden), values (shown in the list)}
  145. * @param items
  146. * @param compareMode
  147. * @param formatString format _$value1$_ xx _$value2$_ etc.
  148. * @return
  149. */
  150. public static MPComboBoxModelItem[] toItems(Object[][] items, int compareMode, String formatString) {
  151. MPComboBoxModelItem[] array = new MPComboBoxModelItem[items.length];
  152. String[] vaars = null;
  153. if (formatString != null) {
  154. vaars = formatString.split("_\\$");
  155. Log.Debug(MPComboBoxModelItem.class, "Length of var string: " + vaars.length);
  156. }
  157. for (int i = 0; i < items.length; i++) {
  158. String x = "";
  159. String format = formatString;
  160. for (int j = 1; j < items[i].length; j++) {
  161. String k = String.valueOf(items[i][j]);
  162. if (format == null) {
  163. x += k;
  164. } else {
  165. try {
  166. format = format.replaceFirst("_\\$(.*?)\\$_", k);
  167. } catch (Exception e) {
  168. Log.Debug(e);
  169. }
  170. x = format;
  171. }
  172. }
  173. array[i] = new MPComboBoxModelItem(items[i][0], x);
  174. array[i].setCompareMode(compareMode);
  175. }
  176. return array;
  177. }
  178. /**
  179. * Converts an array to mp combo box items
  180. * {id (hidden), value (shown in the list)}
  181. *
  182. * @param items
  183. * @param sortValuesNaturally If TRUE, sorts the Items into ascending order, according to the natural ordering of its values
  184. * @return
  185. */
  186. public static MPComboBoxModelItem[] toItems(Object[][] items, boolean sortValuesNaturally) {
  187. MPComboBoxModelItem[] array = toItems(items);
  188. if (sortValuesNaturally) {
  189. Arrays.sort(array);
  190. }
  191. return array;
  192. }
  193. /**
  194. * Converts an array to mp combo box items
  195. * {id (hidden), value (shown in the list)}
  196. *
  197. * @param items
  198. * @param sortValuesNaturally If TRUE, sorts the Items into ascending order, according to the natural ordering of its values
  199. * @param convertIndexToInteger
  200. * @return
  201. */
  202. public static MPComboBoxModelItem[] toItems(Object[][] items, boolean sortValuesNaturally, boolean convertIndexToInteger) {
  203. MPComboBoxModelItem[] array = toItems(items, COMPARE_BY_VALUE, convertIndexToInteger);
  204. if (sortValuesNaturally) {
  205. Arrays.sort(array);
  206. }
  207. return array;
  208. }
  209. /**
  210. * Converts an array to mp combo box items
  211. * {id (hidden), value (shown in the list)}
  212. *
  213. * @param items
  214. * @param sortValuesNaturally If TRUE, sorts the Items into ascending order, according to the natural ordering of its values
  215. * @param compareMode
  216. * @param format _$value1$_ xx _$value2$_ etc.
  217. * @return
  218. */
  219. public static MPComboBoxModelItem[] toItems(Object[][] items, boolean sortValuesNaturally, int compareMode, String format) {
  220. MPComboBoxModelItem[] array = toItems(items, compareMode, format);
  221. if (sortValuesNaturally) {
  222. Arrays.sort(array);
  223. }
  224. // Log.PrintArray(items);
  225. return array;
  226. }
  227. /**
  228. * Converts an array to mp combo box items
  229. * {Integer id (hidden), String value (shown in the list)}
  230. * @param items
  231. * @return
  232. */
  233. public static MPComboBoxModelItem[] toItems(List<DatabaseObject> items) {
  234. return toItems(items, COMPARE_BY_VALUE);
  235. }
  236. /**
  237. * Converts an array to mp combo box items
  238. * {Integer id (hidden), String value (shown in the list)}
  239. * @param items
  240. * @return
  241. */
  242. public static MPComboBoxModelItem[] toItems(List items, boolean simple) {
  243. MPComboBoxModelItem[] array = new MPComboBoxModelItem[items.size()];
  244. for (int i = 0; i < array.length; i++) {
  245. array[i] = new MPComboBoxModelItem(items.get(i), items.get(i).toString());
  246. array[i].setCompareMode(COMPARE_BY_VALUE);
  247. }
  248. return array;
  249. }
  250. /**
  251. * Converts an array to mp combo box items
  252. * {Integer id (hidden), String value (shown in the list)}
  253. * @param items
  254. * @param compareMode
  255. * @return
  256. */
  257. public static MPComboBoxModelItem[] toItems(List<DatabaseObject> items, int compareMode) {
  258. MPComboBoxModelItem[] array = new MPComboBoxModelItem[items.size()];
  259. for (int i = 0; i < array.length; i++) {
  260. array[i] = new MPComboBoxModelItem(new Integer(items.get(i).__getIDS()), items.get(i).__getCname());
  261. array[i].setCompareMode(compareMode);
  262. }
  263. return array;
  264. }
  265. /**
  266. * Converts an enum<id, name> to mp combo box items
  267. * {id (hidden), value (shown in the list)}
  268. * @param items
  269. * @return
  270. */
  271. public static MPComboBoxModelItem[] toItems(MPEnum[] items) {
  272. return toItems(items, COMPARE_BY_VALUE, new Vector<Integer>());
  273. }
  274. /**
  275. * Converts an enum<id, name> to mp combo box items
  276. * {id (hidden), value (shown in the list)}
  277. * @param items
  278. * @param compareMode
  279. * @param skip
  280. * @return
  281. */
  282. public static MPComboBoxModelItem[] toItems(MPEnum[] items, int compareMode, List<Integer> skip) {
  283. // Log.PrintArray(items);
  284. MPComboBoxModelItem[] array = new MPComboBoxModelItem[items.length - skip.size()];
  285. for (int i = 0, z = 0; i < items.length; i++) {
  286. if (!skip.contains(new Integer(i))) {
  287. array[z] = new MPComboBoxModelItem(items[i].getId(), items[i].getName());
  288. array[z].setCompareMode(compareMode);
  289. z++;
  290. }
  291. }
  292. return array;
  293. }
  294. /**
  295. * Creates a {@link MPComBoxModel} containing an array of {@link MPComboBoxModelItem}
  296. * {enum id (hidden), value (shown in the list)}
  297. * @param data
  298. * @return
  299. */
  300. public static MPComboboxModel toModel(MPEnum[] data) {
  301. return new MPComboboxModel(toItems(data));
  302. }
  303. /**
  304. * Creates a {@link MPComBoxModel} containing a {@link MPComboBoxModelItem}
  305. * {ids (hidden), cname (shown in the list)}
  306. * @param data
  307. * @return
  308. */
  309. public static MPComboboxModel toModel(DatabaseObject data) {
  310. return new MPComboboxModel(toItems(new Vector<DatabaseObject>(Arrays.asList(new DatabaseObject[]{data}))));
  311. }
  312. /**
  313. * Creates a {@link MPComBoxModel} containing a {@link MPComboBoxModelItem}
  314. * {entity (hidden), dbo (shown in the list)}
  315. * @param <T>
  316. * @param data
  317. * @return
  318. */
  319. public static <T extends DatabaseObject> MPComboboxModel toModel0(List<T> data) {
  320. Object[][] l = new Object[data.size()][2];
  321. for (int i = 0; i < data.size(); i++) {
  322. DatabaseObject databaseObject = data.get(i);
  323. l[i][0] = databaseObject.IDENTITY;
  324. l[i][0] = databaseObject;
  325. }
  326. return new MPComboboxModel(toItems(l));
  327. }
  328. /**
  329. * Delegates to new MPComboboxModel(data);
  330. * @param data
  331. * @return
  332. */
  333. public static MPComboboxModel toModel(MPComboBoxModelItem[] data) {
  334. return new MPComboboxModel(data);
  335. }
  336. /**
  337. * Creates a {@link DefaultComBoxModel} containing an array of {@link MPComboBoxModelItem}
  338. * @param list
  339. * @return
  340. */
  341. public static MPComboboxModel toModel(List<MPComboBoxModelItem> list) {
  342. return new MPComboboxModel(list.toArray(new MPComboBoxModelItem[]{}));
  343. }
  344. /**
  345. * Creates a {@link DefaultComBoxModel} containing an array of {@link MPComboBoxModelItem}
  346. * {enum id (hidden), value (shown in the list)}
  347. * @param data
  348. * @param compareMode
  349. * @param skip
  350. * @return
  351. */
  352. public static MPComboboxModel toModel(MPEnum[] data, int compareMode, List<Integer> skip) {
  353. return new MPComboboxModel(toItems(data, compareMode, skip));
  354. }
  355. /**
  356. * Creates a {@link DefaultComBoxModel} containing an array of {@link MPComboBoxModelItem}
  357. * {id (hidden), value (shown in the list)}
  358. * @param data
  359. * @return
  360. */
  361. public static MPComboboxModel toModel(Object[][] data) {
  362. return new MPComboboxModel(toItems(data));
  363. }
  364. private Object id;
  365. private String name;
  366. private int comparemode = COMPARE_BY_VALUE;
  367. /**
  368. * Creates a new item with the given id and value
  369. * @param id
  370. * @param name
  371. */
  372. public MPComboBoxModelItem(Integer id, String name) {
  373. this.id = id;
  374. this.name = name;
  375. this.ID_CLASS = id.getClass();
  376. }
  377. /**
  378. * Creates a new item with the given id and value
  379. * @param id
  380. * @param name
  381. */
  382. public MPComboBoxModelItem(Object id, String name) {
  383. this.id = id;
  384. this.name = name;
  385. this.ID_CLASS = id.getClass();
  386. }
  387. /**
  388. * To determine the class of the ID field object
  389. * @return A class
  390. */
  391. public Class getIDClass() {
  392. return ID_CLASS;
  393. }
  394. /**
  395. * Creates a new instance of the class represented by this items' ID Class object.
  396. * The class is instantiated as if by a new expression with an empty argument list.
  397. *
  398. * @return An Object or null if the ID class has no public empty constructor
  399. */
  400. public Object getInstanceOfIDClass() {
  401. try {
  402. return getIDClass().newInstance();
  403. } catch (InstantiationException ex) {
  404. mpv5.logging.Log.Debug(ex);//Logger.getLogger(MPComboBoxModelItem.class.getName()).log(Level.SEVERE, null, ex);
  405. } catch (IllegalAccessException ex) {
  406. mpv5.logging.Log.Debug(ex);//Logger.getLogger(MPComboBoxModelItem.class.getName()).log(Level.SEVERE, null, ex);
  407. }
  408. return null;
  409. }
  410. /**
  411. * The String value of the ID field
  412. * @return A {@link String} representation of this items' ID Object - <b>NEVER<b/> null
  413. */
  414. public String getId() {
  415. return String.valueOf(id);
  416. }
  417. /**
  418. * The ID field
  419. * @return the id Object
  420. */
  421. public Object getIdObject() {
  422. return id;
  423. }
  424. /**
  425. * If the id is greater than 0 or longer than 0 if a String index or non-null for other ID Objects
  426. * @return true if the ID appears to be valid as defined above.
  427. */
  428. public boolean isValid() {
  429. if (id != null) {
  430. try {
  431. if (Integer.valueOf(id.toString()).intValue() > 0) {
  432. return true;
  433. } else {
  434. return false;
  435. }
  436. } catch (NumberFormatException numberFormatException) {
  437. if (id instanceof String && ((String) id).length() > 0) {
  438. return true;
  439. } else {
  440. return false;
  441. }
  442. }
  443. } else {
  444. return false;
  445. }
  446. }
  447. /**
  448. * Define how the items compare to each other
  449. * @param mode
  450. */
  451. public void setCompareMode(int mode) {
  452. this.comparemode = mode;
  453. }
  454. /**
  455. * @param id the id to set
  456. */
  457. public void setId(Integer id) {
  458. this.id = id;
  459. }
  460. /**
  461. * Define the ID field of this item
  462. * @param id the id to set
  463. */
  464. public void setId(Object id) {
  465. this.id = id;
  466. }
  467. /**
  468. * @return the value
  469. */
  470. public String getValue() {
  471. return name;
  472. }
  473. /**
  474. * Delegates to {@link MPComboBoxModelItem#setValue(String)}
  475. * @param name the name to set
  476. */
  477. public void setName(String name) {
  478. this.name = name;
  479. }
  480. /**
  481. * Sets the displayed value of this item
  482. * @param value
  483. */
  484. public void setValue(String value) {
  485. this.name = value;
  486. }
  487. /**
  488. * Returns the value of the item
  489. */
  490. @Override
  491. public String toString() {
  492. return name != null && !"".equals(name) ? name : "";
  493. }
  494. // * <br/>The ID field is <b>NOT</b> part of the compare.
  495. // *
  496. // * Note: this class has a natural ordering that is inconsistent with equals.
  497. /**
  498. * MPComboBoxModelItems are compared to their ids and values!
  499. * @param to
  500. */
  501. @Override
  502. public int compareTo(MPComboBoxModelItem to) {
  503. final int EQUAL = 0;
  504. if (this == to) {
  505. return EQUAL;
  506. }
  507. if (to.getIdObject().equals(id) && to.getValue().equals(getValue())) {
  508. return EQUAL;
  509. }
  510. if (comparemode == COMPARE_BY_VALUE) {
  511. int comparison = this.getValue().compareTo(to.getValue());
  512. if (comparison != EQUAL) {
  513. return comparison;
  514. }
  515. comparison = this.getId().compareTo(to.getId());
  516. if (comparison != EQUAL) {
  517. return comparison;
  518. }
  519. } else {
  520. int comparison = this.getId().compareTo(to.getId());
  521. if (comparison != EQUAL) {
  522. return comparison;
  523. }
  524. comparison = this.getValue().compareTo(to.getValue());
  525. if (comparison != EQUAL) {
  526. return comparison;
  527. }
  528. }
  529. assert this.equals(to) : "compareTo inconsistent with equals.";
  530. return EQUAL;
  531. }
  532. @Override
  533. public boolean equals(Object anotherObject) {
  534. if (anotherObject == null || !(anotherObject instanceof MPComboBoxModelItem)) {
  535. return false;
  536. }
  537. MPComboBoxModelItem mPComboBoxModelItem = (MPComboBoxModelItem) anotherObject;
  538. if (mPComboBoxModelItem.getIdObject().equals(id) && mPComboBoxModelItem.getValue().equals(getValue())) {
  539. return true;
  540. } else {
  541. return false;
  542. }
  543. }
  544. @Override
  545. public int hashCode() {
  546. int hash = 7;
  547. hash = 29 * hash + (this.id != null ? this.id.hashCode() : 0);
  548. hash = 29 * hash + (this.name != null ? this.name.hashCode() : 0);
  549. return hash;
  550. }
  551. }