PageRenderTime 233ms CodeModel.GetById 43ms RepoModel.GetById 0ms app.codeStats 1ms

/src/mpv5/utils/arrays/ArrayUtilities.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 1113 lines | 652 code | 159 blank | 302 comment | 170 complexity | a7b931b70693913ad4d8faa9e580b256 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. *
  3. This file is part of YaBS.
  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. YaBS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. */
  16. package mpv5.utils.arrays;
  17. import java.awt.Color;
  18. import java.awt.Component;
  19. import javax.swing.tree.TreeNode;
  20. import mpv5.utils.numbers.Ip;
  21. import java.util.Hashtable;
  22. import mpv5.utils.tables.*;
  23. import java.io.File;
  24. import java.lang.reflect.Array;
  25. import java.util.ArrayList;
  26. import java.util.Arrays;
  27. import java.util.Collection;
  28. import java.util.Date;
  29. import java.util.Enumeration;
  30. import java.util.HashMap;
  31. import java.util.HashSet;
  32. import java.util.Iterator;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.Set;
  36. import java.util.Vector;
  37. import javax.swing.JTable;
  38. import javax.swing.JTree;
  39. import javax.swing.table.DefaultTableCellRenderer;
  40. import javax.swing.table.DefaultTableModel;
  41. import javax.swing.table.TableColumn;
  42. import javax.swing.table.TableModel;
  43. import javax.swing.tree.DefaultMutableTreeNode;
  44. import javax.swing.tree.DefaultTreeModel;
  45. import javax.swing.tree.TreeModel;
  46. import javax.swing.tree.TreePath;
  47. import mpv5.db.common.DatabaseObject;
  48. import mpv5.db.objects.Group;
  49. import mpv5.logging.Log;
  50. import mpv5.ui.frames.MPView;
  51. import mpv5.utils.date.DateConverter;
  52. import mpv5.utils.files.FileDirectoryHandler;
  53. import mpv5.utils.files.FileReaderWriter;
  54. import mpv5.utils.models.MPTableModel;
  55. /**
  56. *
  57. *
  58. */
  59. public class ArrayUtilities {
  60. public static void addToTable(JTable table, Object[] row) {
  61. MPTableModel model = (MPTableModel) table.getModel();
  62. Object[][] data = new Object[model.getRowCount() + 1][row.length];
  63. Object[] columnNames = new Object[model.getColumnCount()];
  64. for (int idx = 0; idx < columnNames.length; idx++) {
  65. columnNames[idx] = model.getColumnName(idx);
  66. }
  67. for (int idx = 0; idx < model.getRowCount(); idx++) {
  68. for (int i = 0; i < row.length; i++) {
  69. data[idx][i] = model.getValueAt(idx, i);
  70. }
  71. }
  72. data[model.getRowCount()] = row;
  73. table.setModel(new MPTableModel(model.getTypes(), model.getCanEdits(), data, columnNames));
  74. }
  75. public static void addToTable(JTable table, Object[][] rows) {
  76. TableModel model = table.getModel();
  77. Object[][] data = new Object[model.getRowCount() + rows.length][rows[0].length];
  78. Object[] columnNames = new Object[model.getColumnCount()];
  79. for (int idx = 0; idx < columnNames.length; idx++) {
  80. columnNames[idx] = model.getColumnName(idx);
  81. }
  82. for (int idx = 0; idx < model.getRowCount(); idx++) {
  83. for (int i = 0; i < rows[0].length; i++) {
  84. data[idx][i] = model.getValueAt(idx, i);
  85. }
  86. }
  87. for (int i = 0; i < rows.length; i++) {
  88. data[model.getRowCount() + i] = rows[i];
  89. }
  90. table.setModel(new DefaultTableModel(data, columnNames));
  91. }
  92. /**
  93. * Supports
  94. * <li>Date
  95. * <li>Integer
  96. * <li>Double
  97. * <li>String
  98. *
  99. * @param d
  100. * @param column
  101. * @param aClass
  102. * @return
  103. */
  104. public static Object[][] changeToClassValue(Object[][] d, int column, Class aClass) {
  105. for (int i = 0; i < d.length; i++) {
  106. if (aClass.isInstance(new Integer(0))) {
  107. d[i][column] = Integer.valueOf(String.valueOf(d[i][column]));
  108. } else if (aClass.isInstance(new Double(0d))) {
  109. d[i][column] = Double.valueOf(String.valueOf(d[i][column]));
  110. } else if (aClass.isInstance(new Date())) {
  111. d[i][column] = DateConverter.getDate(String.valueOf(d[i][column]));
  112. } else if (aClass.isInstance(new String())) {
  113. d[i][column] = String.valueOf(d[i][column]);
  114. }
  115. }
  116. return d;
  117. }
  118. //
  119. // /**
  120. // *
  121. // * @param table
  122. // * @param countcol
  123. // * @param nettocol
  124. // * @param taxcol
  125. // * @return
  126. // */
  127. // public static CalculatedTableValues calculateTableCols(JTable table, int countcol, int nettocol, int taxcol) {
  128. //
  129. // TableModel m = table.getModel();
  130. // Double bruttobetrag = 0d;
  131. // Double nettobetrag = 0d;
  132. // Double allovertax = 0d;
  133. // boolean error = false;
  134. //
  135. // for (int i = 0; i < m.getRowCount(); i++) {
  136. // if (m.getValueAt(i, nettocol) != null && m.getValueAt(i, countcol) != null && m.getValueAt(i, taxcol) != null) {
  137. // try {
  138. // bruttobetrag = bruttobetrag + (FormatNumber.parseDezimal(m.getValueAt(i, nettocol).toString()) * (Double.valueOf((FormatNumber.parseDezimal(m.getValueAt(i, taxcol).toString()) / 100) + 1)));
  139. // nettobetrag = nettobetrag + (FormatNumber.parseDezimal(m.getValueAt(i, nettocol).toString()));
  140. // allovertax = allovertax + ((FormatNumber.parseDezimal(m.getValueAt(i, taxcol).toString())) + 100);
  141. // } catch (Exception exception) {
  142. // Log.Debug(this,exception);
  143. // error = true;
  144. // }
  145. // }
  146. // }
  147. //
  148. // if (nettobetrag > 0) {
  149. // allovertax = (allovertax / nettobetrag);
  150. // } else {
  151. // allovertax = 0d;
  152. // }
  153. //
  154. // if (!error) {
  155. // return new CalculatedTableValues(bruttobetrag, nettobetrag, allovertax);
  156. // } else {
  157. // return null;
  158. // }
  159. // }
  160. /**
  161. * Returns a table column's data as array
  162. * @param table
  163. * @param column
  164. * @return
  165. */
  166. public static Object[] getColumnAsArray(JTable table, int column) {
  167. MPTableModel model = (MPTableModel) table.getModel();
  168. Object[] data = new Object[model.getRowCount()];
  169. for (int idx = 0; idx < model.getRowCount(); idx++) {
  170. for (int i = 0; i < model.getColumnCount(); i++) {
  171. if (i == column) {
  172. data[idx] = model.getValueAt(idx, i);
  173. }
  174. }
  175. }
  176. return data;
  177. }
  178. /**
  179. * Converts a Hashtable to an array of keys and values
  180. * @param table
  181. * @return
  182. */
  183. public static Object[][] hashTableToArray(Hashtable<String, Object> table) {
  184. Object[][] n = new Object[table.size()][2];
  185. String[] keys = hashTableKeysToArray(table);
  186. int i = 0;
  187. for (int j = 0; j < keys.length; j++) {
  188. String k = keys[j];
  189. n[i][1] = table.get(k);
  190. n[i][0] = k;
  191. i++;
  192. }
  193. //
  194. // Log.PrintArray(n);
  195. return n;
  196. }
  197. // public static MPTableModel getModelCopy(JTable data) {
  198. //
  199. // }
  200. public static void removeColumn(JTable table, int vColIndex) {
  201. MPTableModel model = (MPTableModel) table.getModel();
  202. TableColumn col = table.getColumnModel().getColumn(vColIndex);
  203. int columnModelIndex = col.getModelIndex();
  204. Vector data = model.getDataVector();
  205. Vector colIds = model.getColumnIdentifiers();
  206. // Remove the column from the table
  207. table.removeColumn(col);
  208. // Remove the column header from the table model
  209. colIds.removeElementAt(columnModelIndex);
  210. // Remove the column data
  211. for (int r = 0; r < data.size(); r++) {
  212. Vector row = (Vector) data.get(r);
  213. row.removeElementAt(columnModelIndex);
  214. }
  215. model.setDataVector(data, colIds);
  216. // Correct the model indices in the TableColumn objects
  217. // by decrementing those indices that follow the deleted column
  218. Enumeration enumd = table.getColumnModel().getColumns();
  219. for (; enumd.hasMoreElements();) {
  220. TableColumn c = (TableColumn) enumd.nextElement();
  221. if (c.getModelIndex() >= columnModelIndex) {
  222. c.setModelIndex(c.getModelIndex() - 1);
  223. }
  224. }
  225. model.fireTableStructureChanged();
  226. }
  227. /**
  228. * Replaces a columns data in a table
  229. * @param data
  230. * @param column
  231. * @param columndata
  232. */
  233. public static Object[][] replaceColumn(Object[][] data, int column, Object[] columndata) {
  234. for (int idx = 0; idx < data.length; idx++) {
  235. for (int i = 0; i < data[idx].length; i++) {
  236. if (i == column) {
  237. if (columndata != null) {
  238. data[idx][i] = columndata[idx];
  239. } else {
  240. data[idx][i] = null;
  241. }
  242. }
  243. }
  244. }
  245. return data;
  246. }
  247. /**
  248. * Replaces a columns data in a table
  249. * @param table
  250. * @param column
  251. * @param columndata
  252. */
  253. public static void replaceColumn(JTable table, int column, Object[] columndata) {
  254. TableModel model = table.getModel();
  255. Object[][] data = new Object[model.getRowCount()][model.getColumnCount()];
  256. String[] columnNames = new String[model.getColumnCount()];
  257. for (int idx = 0; idx < columnNames.length; idx++) {
  258. columnNames[idx] = model.getColumnName(idx);
  259. }
  260. for (int idx = 0; idx < model.getRowCount(); idx++) {
  261. for (int i = 0; i < model.getColumnCount(); i++) {
  262. if (i != column) {
  263. data[idx][i] = model.getValueAt(idx, i);
  264. } else {
  265. if (columndata != null) {
  266. data[idx][i] = columndata[idx];
  267. } else {
  268. data[idx][i] = null;
  269. }
  270. }
  271. }
  272. }
  273. table.setModel(new MPTableModel(data, columnNames));
  274. }
  275. /**
  276. *
  277. * @param table
  278. * @param columns
  279. * @param separator
  280. * @param filename
  281. * @param suffix
  282. * @return
  283. */
  284. public static File tableModelToFile(JTable table, int[] columns, String separator, String filename, String suffix) {
  285. File file = FileDirectoryHandler.getTempFile(filename, suffix);
  286. FileReaderWriter rw = new FileReaderWriter(file, "UTF8");
  287. TableModel model = table.getModel();
  288. String[] data = new String[model.getRowCount()];
  289. String line = "";
  290. for (int idx = 0; idx < model.getRowCount(); idx++) {
  291. line = "";
  292. for (int i = 0; i < model.getColumnCount(); i++) {
  293. for (int j = 0; j < columns.length; j++) {
  294. if (i == columns[j]) {
  295. if (model.getValueAt(idx, i) != null) {
  296. line += model.getValueAt(idx, i) + separator;
  297. } else {
  298. line += separator;
  299. }
  300. }
  301. }
  302. }
  303. data[idx] = line.substring(0, line.length() - separator.length());
  304. }
  305. rw.writeWCharset(data);
  306. Log.Debug(ArrayUtilities.class, file);
  307. return file;
  308. }
  309. public static DefaultTableModel toTableModel(HashMap data) {
  310. Object[] array = data.keySet().toArray();
  311. Object[][] table = new Object[array.length][1];
  312. for (int i = 0; i < array.length; i++) {
  313. table[i][0] = array[i];
  314. }
  315. return new DefaultTableModel(table, new Object[]{"Devices"});
  316. }
  317. @SuppressWarnings("unchecked")
  318. public static List removeDuplicates(List arlList) {
  319. Set set = new HashSet();
  320. for (Iterator<Ip> iter = arlList.iterator(); iter.hasNext();) {
  321. Object element = iter.next();
  322. set.add(element);
  323. }
  324. arlList.clear();
  325. arlList.addAll(set);
  326. return arlList;
  327. }
  328. // /**
  329. // *
  330. // * @param data
  331. // * @return
  332. // */
  333. // @SuppressWarnings("unchecked")
  334. // public static DefaultTreeModel toTreeModel(HashMap<String, Ip> data) {
  335. ////
  336. //// data = new HashMap<String, Ip>();
  337. //// data.put("f", new Ip("192.168.8.1"));
  338. //// data.put("fg", new Ip("192.168.8.2"));
  339. //// data.put("g", new Ip("192.168.8.3"));
  340. //// data.put("d", new Ip("192.168.8.4"));
  341. //// data.put("gdfg", new Ip("192.168.8.5"));
  342. ////
  343. //// data.put("dfgfdg", new Ip("192.168.9.1"));
  344. //// data.put("yrtytry", new Ip("192.168.9.2"));
  345. //// data.put("tryty", new Ip("192.168.9.4"));
  346. ////
  347. //// data.put("ertertrt", new Ip("191.168.9.1"));
  348. //// data.put("56g", new Ip("191.168.9.2"));
  349. //// data.put("fghtyi", new Ip("191.168.9.4"));
  350. ////
  351. //// data.put("pop", new Ip("192.162.9.1"));
  352. //// data.put("ijlkghh", new Ip("192.162.9.2"));
  353. //// data.put("uyiouy", new Ip("192.167.9.4"));
  354. //
  355. //
  356. // DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Devices");
  357. // System.out.println("Converting to tree: " + data.keySet().size() + " values");
  358. //
  359. // DefaultMutableTreeNode subnode1 = null;
  360. //
  361. // try {
  362. // Collection<Ip> ips = data.values();
  363. // Iterator<Ip> ipIterator = ips.iterator();
  364. //
  365. // ArrayList<Ip> allIps = new ArrayList<Ip>(data.values());
  366. // ArrayList<Ip> subnet1 = new ArrayList<Ip>();
  367. // ArrayList<Ip> subnet2 = new ArrayList<Ip>();
  368. // ArrayList<Ip> subnet3 = new ArrayList<Ip>();
  369. //
  370. // subnode1 = new DefaultMutableTreeNode("IP");
  371. //
  372. // while (ipIterator.hasNext()) {
  373. // Ip ip = ipIterator.next();
  374. // subnet1.add(new Ip(String.valueOf(ip.getPart0()) + ".0.0.0"));
  375. // subnet2.add(new Ip(String.valueOf(ip.getPart0()) + "." + String.valueOf(ip.getPart1()) + ".0.0"));
  376. // subnet3.add(new Ip(String.valueOf(ip.getPart0()) + "." + String.valueOf(ip.getPart1()) + "." + String.valueOf(ip.getPart2()) + ".0"));
  377. //
  378. // }
  379. //
  380. // subnet1 = removeDuplicates(subnet1);
  381. // subnet2 = removeDuplicates(subnet2);
  382. // subnet3 = removeDuplicates(subnet3);
  383. //
  384. //
  385. ////*********************SUBNET 1 xxx.0.0.0 ******************
  386. // for (int i = 0; i < subnet1.size(); i++) {
  387. // Ip subn1 = subnet1.get(i);
  388. // DefaultMutableTreeNode nodex = new DefaultMutableTreeNode(subn1);
  389. // subnode1.add(nodex);
  390. //
  391. //
  392. ////*********************SUBNET 2 0.xxx.0.0 ******************
  393. // for (int ii = 0; ii < subnet2.size(); ii++) {
  394. // Ip subn2 = subnet2.get(ii);
  395. // if (subn2.getPart0() == subn1.getPart0()) {
  396. // DefaultMutableTreeNode nodey = new DefaultMutableTreeNode(subn2);
  397. // nodex.add(nodey);
  398. //
  399. //
  400. ////*********************SUBNET 3 0.0.xxx.0 ******************
  401. // for (int iii = 0; iii < subnet3.size(); iii++) {
  402. // Ip subn3 = subnet3.get(iii);
  403. // if (subn3.getPart0() == subn2.getPart0() &&
  404. // subn3.getPart1() == subn2.getPart1()) {
  405. // DefaultMutableTreeNode nodez = new DefaultMutableTreeNode(subn3);
  406. // nodey.add(nodez);
  407. //
  408. //
  409. //
  410. ////*********************SUBNET 4 0.0.0.xxx ******************
  411. // for (int iiii = 0; iiii < allIps.size(); iiii++) {
  412. // Ip ip = allIps.get(iiii);
  413. //
  414. // if (!new Ip("0.0.0.0").equals(ip) && ip.getPart0() == subn3.getPart0() &&
  415. // ip.getPart1() == subn3.getPart1() &&
  416. // ip.getPart2() == subn3.getPart2()) {
  417. // nodez.add(new DefaultMutableTreeNode(ip));
  418. // }
  419. // }
  420. // }
  421. // }
  422. // }
  423. // }
  424. // }
  425. // } catch (Exception e) {
  426. // e.printStackTrace();
  427. // }
  428. // node1.add(subnode1);
  429. // DefaultTreeModel model = new DefaultTreeModel(node1);
  430. // return model;
  431. // }
  432. public static ArrayList<String> getSelectionFromTree(JTree tree) {
  433. ArrayList<String> list = new ArrayList<String>();
  434. TreePath[] nodes = tree.getSelectionPaths();
  435. for (int i = 0; i < nodes.length; i++) {
  436. TreePath temp = nodes[i];
  437. Object tempObj = temp.getLastPathComponent();
  438. DefaultMutableTreeNode treNode = (DefaultMutableTreeNode) tempObj;
  439. String device = String.valueOf(treNode.getUserObject());
  440. list.add(device);
  441. }
  442. return list;
  443. }
  444. public static Object[][] tableModelToArray(JTable table) {
  445. return tableModelToArray(table, false);
  446. }
  447. public static Object[][] tableModelToArray(JTable table, boolean onlyTheSelectedRows) {
  448. DefaultTableModel model = (DefaultTableModel) table.getModel();
  449. Object[][] data;
  450. int[] selections = table.getSelectedRows();
  451. if (onlyTheSelectedRows) {
  452. data = new Object[selections.length][model.getColumnCount()];
  453. } else {
  454. data = new Object[model.getRowCount()][model.getColumnCount()];
  455. }
  456. if (!onlyTheSelectedRows) {
  457. for (int idx = 0; idx < model.getRowCount(); idx++) {
  458. for (int i = 0; i < model.getColumnCount(); i++) {
  459. data[idx][i] = model.getValueAt(idx, i);
  460. }
  461. }
  462. } else {
  463. for (int idx = 0; idx < selections.length; idx++) {
  464. int row = selections[idx];
  465. for (int i = 0; i < model.getColumnCount(); i++) {
  466. data[idx][i] = model.getValueAt(row, i);
  467. }
  468. }
  469. }
  470. return data;
  471. }
  472. public static void removeSelectedRowFromTable(JTable table) {
  473. TableModel model = table.getModel();
  474. Object[][] data = new Object[model.getRowCount()][model.getColumnCount()];
  475. Object[] columnNames = new Object[model.getColumnCount()];
  476. for (int idx = 0; idx < columnNames.length; idx++) {
  477. columnNames[idx] = model.getColumnName(idx);
  478. }
  479. for (int idx = 0; idx < model.getRowCount(); idx++) {
  480. for (int i = 0; i < model.getColumnCount(); i++) {
  481. if (idx != table.getSelectedRow()) {
  482. data[idx][i] = model.getValueAt(idx, i);
  483. }
  484. }
  485. }
  486. table.setModel(new DefaultTableModel(data, columnNames));
  487. }
  488. public static Object[][] reverseArray(Object[][] array) {
  489. //Reverse order
  490. int i = 0, j = array.length - 1;
  491. while (i < j) {
  492. Object[] h = array[i];
  493. array[i] = array[j];
  494. array[j] = h;
  495. i++;
  496. j--;
  497. }
  498. return array;
  499. }
  500. public static void addRowToTable(JTable table) {
  501. Object[] o = new Object[table.getModel().getColumnCount()];
  502. for (int idx = 0; idx < o.length; idx++) {
  503. o[idx] = null;
  504. }
  505. DefaultTableModel m = (DefaultTableModel) table.getModel();
  506. m.addRow(o);
  507. }
  508. /**
  509. *
  510. * @param original_array
  511. * @param value
  512. * @param place If smaller than< 0, value will be placed before all others
  513. * @return
  514. */
  515. public static Object[][] inserValue(Object[][] original_array, Object value, Integer place) {
  516. Object[][] array_formatiert = new Object[0][0];
  517. if (original_array.length > 0) {
  518. array_formatiert = new Object[original_array.length][original_array[0].length + 1];
  519. for (int zeile = 0; zeile < array_formatiert.length; zeile++) {
  520. int merker = 0;
  521. if (place != null) {
  522. if (place >= 0) {
  523. for (int spalte = 0; spalte < array_formatiert[zeile].length; spalte++, merker++) {
  524. if (spalte == place) {
  525. array_formatiert[zeile][place] = value;
  526. merker--;
  527. } else {
  528. array_formatiert[zeile][spalte] = original_array[zeile][merker];
  529. }
  530. }
  531. } else {
  532. array_formatiert[zeile][0] = value;
  533. for (int spalte = 0; spalte < original_array[zeile].length; spalte++) {
  534. array_formatiert[zeile][spalte + 1] = original_array[zeile][spalte];
  535. }
  536. }
  537. } else {
  538. for (int spalte = 0; spalte < original_array[zeile].length - 1; spalte++) {
  539. array_formatiert[zeile][spalte] = original_array[zeile][spalte];
  540. if (spalte == original_array[zeile].length - 2) {
  541. array_formatiert[zeile][spalte + 1] = value;
  542. }
  543. }
  544. }
  545. }
  546. }
  547. return array_formatiert;
  548. }
  549. /**
  550. *
  551. * @param table
  552. * @param columns
  553. * @return
  554. */
  555. public static String[][] formatTableArrayYesNo(String[][] table, int[] columns) {
  556. for (int i = 0; i < table.length; i++) {
  557. for (int z = 0; z < table[i].length; z++) {
  558. for (int h = 0; h < columns.length; h++) {
  559. if (z == columns[h]) {
  560. if (table[i][z].equals("0")) {
  561. table[i][z] = "Nein";
  562. } else {
  563. table[i][z] = "Ja";
  564. }
  565. }
  566. }
  567. }
  568. }
  569. return table;
  570. }
  571. /**
  572. * Copies an array to a new one
  573. * @param originalarray
  574. * @return
  575. */
  576. public static Object[][] toObjectArray(String[][] originalarray) {
  577. Object[][] data = new Object[originalarray.length][];
  578. for (int idx = 0; idx < originalarray.length; idx++) {
  579. for (int i = 0; i < originalarray[idx].length; i++) {
  580. data[idx][i] = String.valueOf(originalarray[idx][i]);
  581. }
  582. }
  583. return data;
  584. }
  585. public static String[] hashTableKeysToArray(Hashtable<String, Object> data) {
  586. Object[] array = data.keySet().toArray();
  587. String[] keyz = new String[array.length];
  588. for (int i = 0; i < array.length; i++) {
  589. keyz[i] = (array[i]).toString();
  590. }
  591. return keyz;
  592. }
  593. /**
  594. * Remove certain rows out of an array
  595. * @param array
  596. * @param column
  597. * @param removeTrigger
  598. * @return
  599. */
  600. public static Object[][] removeRows(Object[][] array, int column, Object removeTrigger) {
  601. List<Object[]> list = new Vector<Object[]>();
  602. for (int i = 0; i < array.length; i++) {
  603. Object[] objects = array[i];
  604. if (objects[column] != removeTrigger && !removeTrigger.equals(objects[column])) {
  605. list.add(objects);
  606. }
  607. }
  608. return list.toArray(new Object[][]{});
  609. }
  610. /**
  611. * Converts a HashMap to a 2-column array {key, value}
  612. * @param map
  613. * @return
  614. */
  615. public static Object[][] hashMapToArray(HashMap<String, Object> map) {
  616. return mapToArray(map);
  617. }
  618. /**
  619. * Converts a HashMap to a 2-column array {key, value}
  620. * @param map
  621. * @return
  622. */
  623. public static Object[][] mapToArray(Map<String, Object> map) {
  624. Object[][] data = new Object[map.size()][2];
  625. String[] arr = map.keySet().toArray(new String[]{});
  626. for (int idx = 0; idx < arr.length; idx++) {
  627. data[idx][0] = arr[idx];
  628. data[idx][1] = map.get(arr[idx]);
  629. }
  630. return data;
  631. }
  632. public static Object[][] merge(Object[][] array1, Object[][] array2) {
  633. if (array1 == null) {
  634. array1 = new Object[0][0];
  635. }
  636. if (array2 == null) {
  637. array2 = new Object[0][0];
  638. }
  639. int z = 0;
  640. if (array1 != null && array1.length > 0) {
  641. z = array1[0].length;
  642. }
  643. if (array2 != null && array2.length > 0) {
  644. if (z < array2[0].length) {
  645. z = array2[0].length;
  646. }
  647. }
  648. Object[][] mergedArray = new Object[array1.length + array2.length][z];
  649. int i = 0;
  650. for (i = 0; i < array1.length; i++) {
  651. for (int k = 0; k < array1[i].length; k++) {
  652. mergedArray[i][k] = array1[i][k];
  653. }
  654. }
  655. for (int l = 0; l < array2.length; l++) {
  656. for (int k = 0; k < array2[l].length; k++) {
  657. mergedArray[i + l][k] = array2[l][k];
  658. }
  659. }
  660. return mergedArray;
  661. }
  662. public static Object[][] merge(Object[] array1, Object[][] array2) {
  663. if (array1 == null) {
  664. array1 = new Object[0];
  665. }
  666. if (array2 == null) {
  667. array2 = new Object[0][0];
  668. }
  669. int z = 0;
  670. if (array1 != null && array1.length > 0) {
  671. z = array1.length;
  672. } else if (array2 != null && array2.length > 0) {
  673. z = array2[0].length;
  674. } else {
  675. z = 0;
  676. }
  677. Object[][] mergedArray = new Object[array1.length + array2.length][z];
  678. int i = 0;
  679. for (i = 0; i < array1.length; i++) {
  680. mergedArray[0][i] = array1[i];
  681. }
  682. for (int l = 0; l < array2.length; l++) {
  683. for (int k = 0; k < array2[l].length; k++) {
  684. mergedArray[i + l][k] = array2[l][k];
  685. }
  686. }
  687. return mergedArray;
  688. }
  689. public static Object[] merge(Object[] array1, Object[] array2) {
  690. if (array1 == null) {
  691. array1 = new Object[0];
  692. }
  693. if (array2 == null) {
  694. array2 = new Object[0];
  695. }
  696. Object[] mergedArray = new Object[array1.length + array2.length];
  697. int i = 0;
  698. for (i = 0; i < array1.length; i++) {
  699. mergedArray[i] = array1[i];
  700. }
  701. for (int l = 0; l < array2.length; l++) {
  702. mergedArray[i + l] = array2[l];
  703. }
  704. return mergedArray;
  705. }
  706. public static String[] reverseArray(String[] str) {
  707. int i = 0, j = str.length - 1;
  708. while (i < j) {
  709. String h = str[i];
  710. str[i] = str[j];
  711. str[j] = h;
  712. i++;
  713. j--;
  714. }
  715. return str;
  716. }
  717. public static String[][] reverseArray(String[][] str) {
  718. int i = 0, j = str.length - 1;
  719. while (i < j) {
  720. String[] h = str[i];
  721. str[i] = str[j];
  722. str[j] = h;
  723. i++;
  724. j--;
  725. }
  726. return str;
  727. }
  728. public static String[][] ObjectToStringArray(Object[][] array1) {
  729. if (array1 == null || array1.length < 1) {
  730. array1 = new Object[0][0];
  731. }
  732. String[][] mergedArray = null;
  733. try {
  734. mergedArray = new String[array1.length][array1[0].length];
  735. int i = 0;
  736. for (i = 0; i < array1.length; i++) {
  737. for (int k = 0; k < array1[i].length; k++) {
  738. mergedArray[i][k] = array1[i][k].toString();
  739. }
  740. }
  741. } catch (Exception e) {
  742. return new String[0][0];
  743. }
  744. return mergedArray;
  745. }
  746. public static String[] SmallObjectToStringArray(Object[] array1) {
  747. if (array1 == null) {
  748. array1 = new Object[0];
  749. }
  750. String[] mergedArray = new String[array1.length];
  751. int i = 0;
  752. for (i = 0; i < array1.length; i++) {
  753. mergedArray[i] = array1[i].toString();
  754. }
  755. return mergedArray;
  756. }
  757. public static Object[] ObjectToSingleColumnArray(Object[][] array1) {
  758. if (array1 == null) {
  759. array1 = new Object[0][0];
  760. }
  761. Object[] mergedArray = new Object[array1.length];
  762. int i = 0;
  763. for (i = 0; i < array1.length; i++) {
  764. mergedArray[i] = array1[i][0];
  765. }
  766. return mergedArray;
  767. }
  768. public static Object[] sort(Object[] items) {
  769. Arrays.sort(items);
  770. return items;
  771. }
  772. //
  773. // public static Object[][] sort(Object[][] items, int columnToSortBy) {
  774. //
  775. // for (int i = 0; i < items.length; i++) {
  776. // Object[] objects = items[i];
  777. //
  778. // }
  779. //
  780. // Arrays.sort(items);
  781. //
  782. // return items;
  783. // }
  784. public static ArrayList<String[]> StringArrayToList(String[][] array) {
  785. ArrayList<String[]> list = new ArrayList<String[]>();
  786. for (int i = 0; i < array.length; i++) {
  787. list.add(array[i]);
  788. }
  789. return list;
  790. }
  791. public static ArrayList TableModelToList(JTable mode) {
  792. ArrayList<String[]> list = new ArrayList<String[]>();
  793. DefaultTableModel model = (DefaultTableModel) mode.getModel();
  794. for (int i = 0; i < model.getRowCount(); i++) {
  795. String[] str = null;
  796. for (int j = 0; j < model.getColumnCount(); j++) {
  797. str = new String[model.getColumnCount()];
  798. str[j] = model.getValueAt(i, j).toString();
  799. }
  800. list.add(str);
  801. }
  802. return list;
  803. }
  804. /**
  805. *
  806. * @param list
  807. * @return
  808. */
  809. public static Integer[][][] listToIntegerArray(List list) {
  810. Integer[][][] str = new Integer[list.size()][][];
  811. ArrayList a, b;
  812. for (int i = 0; i < list.size(); i++) {
  813. a = (ArrayList) list.get(i);
  814. str[i] = new Integer[a.size()][];
  815. for (int m = 0; m < a.size(); m++) {
  816. b = (ArrayList) a.get(m);
  817. str[i][m] = new Integer[b.size()];
  818. for (int k = 0; k < a.size(); k++) {
  819. str[i][m][k] = (Integer) b.get(k);
  820. }
  821. }
  822. }
  823. return str;
  824. }
  825. /**
  826. *
  827. * @param list
  828. * @return
  829. */
  830. public static String[] listToStringArray(List list) {
  831. String[] str = new String[list.size()];
  832. for (int i = 0; i < list.size(); i++) {
  833. str[i] = (String) list.get(i);
  834. }
  835. return str;
  836. }
  837. public static String[][] listToStringArrayArray(List<String[]> list) {
  838. String[][] str = new String[list.size()][];
  839. for (int i = 0; i < list.size(); i++) {
  840. str[i] = list.get(i);
  841. }
  842. return str;
  843. }
  844. public static Object[][] listToTableArray(List<Object[]> list) {
  845. int width = 0;
  846. for (int i = 0; i < list.size(); i++) {
  847. Object[] objects = list.get(i);
  848. if (objects.length > width) {
  849. width = objects.length;
  850. }
  851. }
  852. Object[][] arr = new Object[list.size()][width];
  853. if (list.size() > 0) {
  854. for (int i = 0; i < list.size(); i++) {
  855. Object[] objects = list.get(i);
  856. for (int j = 0; j < objects.length; j++) {
  857. Object object = objects[j];
  858. arr[i][j] = object;
  859. }
  860. }
  861. }
  862. return arr;
  863. }
  864. public static String[][] StringListToTableArray(ArrayList list) {
  865. String[][] str = new String[list.size()][];
  866. for (int i = 0; i < list.size(); i++) {
  867. str[i] = (String[]) list.get(i);
  868. }
  869. return str;
  870. }
  871. @SuppressWarnings("unchecked")
  872. public static ArrayList merge(ArrayList list1, ArrayList list2) {
  873. Iterator it1 = list1.iterator();
  874. Iterator it2 = list2.iterator();
  875. ArrayList<Double> list3 = new ArrayList();
  876. while (it1.hasNext()) {
  877. list3.add((Double) it1.next());
  878. }
  879. while (it2.hasNext()) {
  880. list3.add((Double) it2.next());
  881. }
  882. return list3;
  883. }
  884. /**
  885. * list 1 +2 must have same element count!
  886. *
  887. * @param list1
  888. * @param list2
  889. * @return list1.values(n) - list2.values(n)
  890. * @throws Exception
  891. */
  892. @SuppressWarnings("unchecked")
  893. public static ArrayList substract(ArrayList<Double> list1, ArrayList<Double> list2) throws Exception {
  894. Iterator it1 = list1.iterator();
  895. Iterator it2 = list2.iterator();
  896. if (list1.size() != list2.size()) {
  897. throw new Exception("list 1 + 2 must have same element count!");
  898. }
  899. ArrayList<Double> list3 = new ArrayList();
  900. while (it1.hasNext() && it2.hasNext()) {
  901. Double value = (Double) it1.next() - (Double) it2.next();
  902. list3.add(value);
  903. }
  904. return list3;
  905. }
  906. /**
  907. * list 1 + 2 must have same element count!
  908. *
  909. * @param list1
  910. * @param list2
  911. * @return list1.values(n) + list2.values(n)
  912. * @throws Exception
  913. */
  914. public static ArrayList<Double> add(ArrayList<Double> list1, ArrayList<Double> list2) throws Exception {
  915. Iterator it1 = list1.iterator();
  916. Iterator it2 = list2.iterator();
  917. if (list1.size() != list2.size()) {
  918. throw new Exception("list 1 + 2 must have same element count!");
  919. }
  920. @SuppressWarnings("unchecked")
  921. ArrayList<Double> list3 = new ArrayList();
  922. while (it1.hasNext() && it2.hasNext()) {
  923. Double value = (Double) it1.next() + (Double) it2.next();
  924. list3.add(value);
  925. }
  926. return list3;
  927. }
  928. /**
  929. * Create an Enumeration from an Array
  930. * @param obj
  931. * @return
  932. */
  933. public static Enumeration arrayToEnumeration(final Object obj) {
  934. Class type = obj.getClass();
  935. if (!type.isArray()) {
  936. throw new IllegalArgumentException(obj.getClass().toString());
  937. } else {
  938. return (new Enumeration() {
  939. int size = Array.getLength(obj);
  940. int cursor;
  941. public boolean hasMoreElements() {
  942. return (cursor < size);
  943. }
  944. public Object nextElement() {
  945. return Array.get(obj, cursor++);
  946. }
  947. });
  948. }
  949. }
  950. }