Google Tag Manager

2008/12/25

JTable FishEye Row

Code

@Override public void mouseMoved(MouseEvent e) {
  int row = rowAtPoint(e.getPoint());
  if (prev_row == row) {
    return;
  }
  initRowHeight(prev_height, row);
  prev_row = row;
}

public void initRowHeight(int height, int ccRow) {
  int rd2 = (fishEyeRowList.size() - 1) / 2;
  int rowCount = getModel().getRowCount();
  int view_rc = getViewableColoredRowCount(ccRow);
  int view_h = 0;
  for (int i = 0; i < view_rc; i++) {
    view_h += fishEyeRowHeightList.get(i);
  }
  int rest_rc = rowCount - view_rc;
  int rest_h = height - view_h;
  int rest_rh  = rest_h / rest_rc; rest_rh = rest_rh > 0 ? rest_rh : 1;
  int a = rest_h - rest_rh * rest_rc;
  int index = -1;
  for (int i = -rd2; i < rowCount; i++) {
    int crh;
    if (ccRow - rd2 <= i && i <= ccRow + rd2) {
      index++;
      if (i < 0) continue;
      crh = fishEyeRowHeightList.get(index);
    } else {
      if (i < 0) continue;
      crh = rest_rh + (a > 0 ? 1 : 0);
      a = a - 1;
    }
    setRowHeight(i, crh);
  }
}

References

2008/12/15

Adding JPopupMenu to JToolBar-Button

Code

class MenuArrowIcon implements Icon {
  @Override public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setPaint(Color.BLACK);
    g2.translate(x, y);
    g2.drawLine(2, 3, 6, 3);
    g2.drawLine(3, 4, 5, 4);
    g2.drawLine(4, 5, 4, 5);
    g2.dispose();
  }

  @Override public int getIconWidth()  {
    return 9;
  }

  @Override public int getIconHeight() {
    return 9;
  }
}

class MenuToggleButton extends JToggleButton {
  private static final Icon ARROW_ICON = new MenuArrowIcon();
  private JPopupMenu popup;

  protected MenuToggleButton() {
    this("", null);
  }

  protected MenuToggleButton(Icon icon) {
    this("", icon);
  }

  protected MenuToggleButton(String text) {
    this(text, null);
  }

  protected MenuToggleButton(String text, Icon icon) {
    super();
    Action action = new AbstractAction(text) {
      @Override public void actionPerformed(ActionEvent e) {
        Component b = (Component) e.getSource();
        Optional.ofNullable(getPopupMenu()).ifPresent(p -> p.show(b, 0, b.getHeight()));
      }
    };
    action.putValue(Action.SMALL_ICON, icon);
    setAction(action);
    setFocusable(false);
    setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4 + ARROW_ICON.getIconWidth()));
  }

  public JPopupMenu getPopupMenu() {
    return popup;
  }

  public void setPopupMenu(JPopupMenu pop) {
    this.popup = pop;
    pop.addPopupMenuListener(new PopupMenuListener() {
      @Override public void popupMenuCanceled(PopupMenuEvent e) {
        /* not needed */
      }

      @Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
        /* not needed */
      }

      @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        setSelected(false);
      }
    });
  }

  @Override protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Dimension dim = getSize();
    Insets ins = getInsets();
    int x = dim.width - ins.right;
    int y = ins.top + (dim.height - ins.top - ins.bottom - ARROW_ICON.getIconHeight()) / 2;
    ARROW_ICON.paintIcon(this, g, x, y);
  }
}

References

2008/12/02

JLabel Star Rating Bar

Code

private final ImageProducer ip = orgIcon.getImage().getSource();

private static ImageIcon makeStarImageIcon(
    ImageProducer ip, float rf, float gf, float bf) {
  return new ImageIcon(Toolkit.getDefaultToolkit().createImage(
    new FilteredImageSource(ip, new SelectedImageFilter(rf, gf, bf))));
}

class SelectedImageFilter extends RGBImageFilter {
  private final float rf;
  private final float gf;
  private final float bf;

  protected SelectedImageFilter(float rf, float gf, float bf) {
    super();
    this.rf = Math.min(1f, rf);
    this.gf = Math.min(1f, gf);
    this.bf = Math.min(1f, bf);
    canFilterIndexColorModel = false;
  }

  @Override public int filterRGB(int x, int y, int argb) {
    int r = (int) (((argb >> 16) & 0xFF) * rf);
    int g = (int) (((argb >> 8) & 0xFF) * gf);
    int b = (int) ((argb & 0xFF) * bf);
    return (argb & 0xFF_00_00_00) | (r << 16) | (g << 8) | (b);
  }
}

References