PageRenderTime 194ms CodeModel.GetById 84ms app.highlight 98ms RepoModel.GetById 1ms app.codeStats 0ms

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