Exemplo n.º 1
0
  @SuppressWarnings("unchecked")
  public JPanel view(String src_n, String dst_n) {
    List<JPanel> panels = new LinkedList<>();
    LinkedList<String> sorted = new LinkedList<>(data.keySet());
    sorted.sort(String::compareTo);
    for (String k : sorted) {
      Set<Pair<Object, Object>> xxx = data.get(k);
      List<Pair<Object, Object>> table = new LinkedList<>(xxx);

      Object[][] arr = new Object[table.size()][2];
      int i = 0;
      for (Pair<Object, Object> p : table) {
        arr[i][0] = p.first;
        arr[i][1] = p.second;
        i++;
      }
      @SuppressWarnings("serial")
      JTable t =
          new JTable(arr, new Object[] {src_n, dst_n}) {
            @Override
            public Dimension getPreferredScrollableViewportSize() {
              Dimension d = getPreferredSize();
              return new Dimension(d.width, d.height);
            }
          };
      // //t.setRowSelectionAllowed(false);
      // t.setColumnSelectionAllowed(false);
      // MouseListener[] listeners = t.getMouseListeners();
      // for (MouseListener l : listeners) {
      // t.removeMouseListener(l);
      // }
      TableRowSorter<?> sorter = new MyTableRowSorter(t.getModel());

      t.setRowSorter(sorter);
      sorter.allRowsChanged();
      sorter.toggleSortOrder(0);
      t.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
      JPanel p = new JPanel(new GridLayout(1, 1));
      p.add(new JScrollPane(t));
      p.setBorder(
          BorderFactory.createTitledBorder(
              BorderFactory.createEmptyBorder(2, 2, 2, 2), k + "  (" + xxx.size() + " rows)"));
      panels.add(p);
      //	p.setPreferredSize(new Dimension(60, 60));
    }

    /*int x = (int) Math.ceil(Math.sqrt(panels.size()));
    if (x == 0) {
    	return new JPanel();
    }
    JPanel panel = new JPanel(new GridLayout(x, x));
    for (JPanel p : panels) {
    	panel.add(p);
    }
    panel.setBorder(BorderFactory.createEtchedBorder()); */
    return FqlUtil.makeGrid(((List<JComponent>) ((Object) panels)));
  }
Exemplo n.º 2
0
 private TableRowSorter<StockTableModel> createRowSorter(
     StockTableModel model, List<String> columns) {
   TableRowSorter<StockTableModel> rowSorter = new TableRowSorter<StockTableModel>(model);
   for (int i = 0; i < columns.size(); i++) {
     rowSorter.setComparator(i, DoubleAsStringComparator.instance);
   }
   rowSorter.toggleSortOrder(0);
   return rowSorter;
 }
  public NetworkTable(Properties display) {
    this.display = display;

    model =
        new GenericTableModel(
            new String[] {" ", "Address", "Label", "Description", "Pivot"}, "Address", 256);
    table = new ATable(model);
    TableRowSorter sorter = new TableRowSorter(model);
    sorter.toggleSortOrder(1);

    Comparator hostCompare =
        new Comparator() {
          public int compare(Object a, Object b) {
            long aa = Route.ipToLong(a + "");
            long bb = Route.ipToLong(b + "");

            if (aa > bb) {
              return 1;
            } else if (aa < bb) {
              return -1;
            } else {
              return 0;
            }
          }

          public boolean equals(Object a, Object b) {
            return (a + "").equals(b + "");
          }
        };

    sorter.setComparator(1, hostCompare);
    sorter.setComparator(4, hostCompare);

    table.setRowSorter(sorter);
    table.setColumnSelectionAllowed(false);

    table.getColumn("Address").setPreferredWidth(125);
    table.getColumn("Label").setPreferredWidth(125);
    table.getColumn("Pivot").setPreferredWidth(125);
    table.getColumn(" ").setPreferredWidth(32);
    table.getColumn(" ").setMaxWidth(32);
    table.getColumn("Description").setPreferredWidth(500);

    final TableCellRenderer parent = table.getDefaultRenderer(Object.class);
    table.setDefaultRenderer(
        Object.class,
        new TableCellRenderer() {
          public Component getTableCellRendererComponent(
              JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
            JLabel component =
                (JLabel)
                    parent.getTableCellRendererComponent(table, value, isSelected, false, row, col);

            if (col == 4 && Boolean.TRUE.equals(model.getValueAt(table, row, "Active"))) {
              component.setFont(component.getFont().deriveFont(Font.BOLD));
            } else if (col == 1 && !"".equals(model.getValueAt(table, row, "Description"))) {
              component.setFont(component.getFont().deriveFont(Font.BOLD));
            } else {
              component.setFont(component.getFont().deriveFont(Font.PLAIN));
            }

            String tip = model.getValueAt(table, row, "Tooltip") + "";

            if (tip.length() > 0) {
              component.setToolTipText(tip);
            }
            return component;
          }
        });

    table
        .getColumn(" ")
        .setCellRenderer(
            new TableCellRenderer() {
              public Component getTableCellRendererComponent(
                  JTable table,
                  Object value,
                  boolean isSelected,
                  boolean hasFocus,
                  int row,
                  int col) {
                JLabel component =
                    (JLabel)
                        parent.getTableCellRendererComponent(
                            table, value, isSelected, false, row, col);
                component.setIcon(new ImageIcon((Image) model.getValueAt(table, row, "Image")));
                component.setText("");

                String tip = model.getValueAt(table, row, "Tooltip") + "";

                if (tip.length() > 0) {
                  component.setToolTipText(tip);
                }

                return component;
              }
            });

    table.addMouseListener(
        new MouseAdapter() {
          public void all(MouseEvent ev) {
            if (ev.isPopupTrigger()) {
              popup.showGraphPopup(getSelectedHosts(), ev);
              ev.consume();
            }
          }

          public void mouseClicked(MouseEvent ev) {
            all(ev);
          }

          public void mousePressed(MouseEvent ev) {
            all(ev);
          }

          public void mouseReleased(MouseEvent ev) {
            all(ev);
          }
        });

    setLayout(new BorderLayout());
    scroller = new JScrollPane(table);
    add(scroller, BorderLayout.CENTER);
  }