Google Tag Manager

2008/12/25

JTable FishEye Row

Code

public void mouseMoved(MouseEvent e) {
  int row = rowAtPoint(e.getPoint());
  if(prev_row==row) return;
  initRowHeigth(prev_height, row);
  prev_row = row;
}
public void initRowHeigth(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 {
  public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2 = (Graphics2D)g;
    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.translate(-x,-y);
  }
  public int getIconWidth()  { return 9; }
  public int getIconHeight() { return 9; }
}
class MenuToggleButton extends JToggleButton {
  private static final Icon i = new MenuArrowIcon();
  public MenuToggleButton() {
    this("", null);
  }
  public MenuToggleButton(Icon icon) {
    this("", icon);
  }
  public MenuToggleButton(String text) {
    this(text, null);
  }
  public MenuToggleButton(String text, Icon icon) {
    super();
    Action a = new AbstractAction(text) {
      public void actionPerformed(ActionEvent ae) {
        MenuToggleButton b = (MenuToggleButton)ae.getSource();
        if(pop!=null) pop.show(b, 0, b.getHeight());
      }
    };
    a.putValue(Action.SMALL_ICON, icon);
    setAction(a);
    setFocusable(false);
    setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4+i.getIconWidth()));
  }
  protected JPopupMenu pop;
  public void setPopupMenu(final JPopupMenu pop) {
    this.pop = pop;
    pop.addPopupMenuListener(new PopupMenuListener() {
      public void popupMenuCanceled(PopupMenuEvent e) {}
      public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}
      public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        setSelected(false);
      }
    });
  }
  public 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-i.getIconHeight())/2;
    i.paintIcon(this, g, x, y);
  }
}

References

2008/12/02

JLabel Star Rating Bar

Code

class LevelBar extends JPanel implements MouseListener, MouseMotionListener{
  private final int gap;
  protected final List< ImageIcon > iconList;
  protected final List< JLabel > labelList = Arrays.asList(
    new JLabel(),new JLabel(),new JLabel(),new JLabel(),new JLabel()
  );
  protected final ImageIcon defaultIcon;
  private int clicked = -1;
  public LevelBar(ImageIcon defaultIcon, List< ImageIcon > list, int gap) {
    super(new GridLayout(1, 5, gap*2, gap*2));
    this.defaultIcon = defaultIcon;
    this.iconList = list;
    this.gap = gap;
    for(JLabel l:labelList) {
      l.setIcon(defaultIcon);
      add(l);
    }
    addMouseListener(this);
    addMouseMotionListener(this);
  }
  public void clear() {
    clicked = -1;
    repaintIcon(clicked);
  }
  public int getLevel() {
    return clicked;
  }
  public void setLevel(int l) {
    clicked = l;
    repaintIcon(clicked);
  }
  private int getSelectedIconIndex(Point p) {
    for(int i=0;i < labelList.size();i++) {
      Rectangle r = labelList.get(i).getBounds();
      r.grow(gap, gap);
      if(r.contains(p)) return i;
    }
    return -1;
  }
  protected void repaintIcon(int index) {
    for(int i=0;i < labelList.size();i++) {
      labelList.get(i).setIcon(i <= index?iconList.get(i):defaultIcon);
    }
    repaint();
  }
  public void mouseMoved(MouseEvent e) {
    repaintIcon(getSelectedIconIndex(e.getPoint()));
  }
  public void mouseEntered(MouseEvent e) {
    repaintIcon(getSelectedIconIndex(e.getPoint()));
  }
  public void mouseClicked(MouseEvent e) {
    clicked = getSelectedIconIndex(e.getPoint());
  }
  public void mouseExited(MouseEvent e) {
    repaintIcon(clicked);
  }
  public void mouseDragged(MouseEvent e) {}
  public void mousePressed(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
}
class SelectedImageFilter extends RGBImageFilter {
  private final float[] filter;
  public SelectedImageFilter(float[] filter) {
    this.filter = filter;
    canFilterIndexColorModel = true;
  }
  public int filterRGB(int x, int y, int argb) {
    Color color = new Color(argb, true);
    float[] array = new float[4];
    color.getComponents(array);
    return new Color(array[0]*filter[0],
                     array[1]*filter[1],
                     array[2]*filter[2],
                     array[3]).getRGB();
  }
}

References