Google Tag Manager

2008/10/20

aterai

Modal Internal Frame

Code

//menuItem.setMnemonic(KeyEvent.VK_3);
//Creating Modal Internal Frames -- Approach 1 and Approach 2
//http://java.sun.com/developer/JDCTechTips/2001/tt1220.html
class ModalInternalFrameAction3 extends AbstractAction {
  private final Component orgGlassPane;
  private final JPanel glass = new PrintGlassPane();
  public ModalInternalFrameAction3(String label) {
    super(label);
    orgGlassPane = frame.getGlassPane();
    glass.setVisible(false);
  }
  public void actionPerformed(ActionEvent e) {
    JOptionPane optionPane = new JOptionPane();
    optionPane.setMessage("Hello, World");
    optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
    JInternalFrame modal = optionPane.createInternalFrame(desktop, "modal3");
    removeSystemMenuListener(modal);
    modal.addInternalFrameListener(new InternalFrameAdapter() {
      public void internalFrameClosed(InternalFrameEvent e) {
        glass.setVisible(false);
        frame.setGlassPane(orgGlassPane);
      }
    });
    glass.add(modal);
    Rectangle screen = desktop.getBounds();
    modal.setLocation(screen.x + screen.width/2  - modal.getSize().width/2,
                      screen.y + screen.height/2 - modal.getSize().height/2);
    frame.setGlassPane(glass);
    glass.setVisible(true);
    modal.setVisible(true);
    try{
      modal.setSelected(true);
    }catch(java.beans.PropertyVetoException ex) {}
  }
}

References

2008/10/14

aterai

rubber band selection, drag and drop reordering

Code

JList list = new JList(model);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(0);
list.setFixedCellWidth(62);
list.setFixedCellHeight(62);
list.setCellRenderer(new IconListCellRenderer());
RubberBandingListener rbl = new RubberBandingListener();
list.addMouseMotionListener(rbl);
list.addMouseListener(rbl);
list.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.setTransferHandler(new ListItemTransferHandler());
list.setDropMode(DropMode.INSERT);

References

2008/10/09

aterai

using rubber band selection in JList

Code

class RubberBandingListener extends MouseInputAdapter {
  @Override public void mouseDragged(MouseEvent e) {
    setFocusable(true);
    if(srcPoint==null) srcPoint = e.getPoint();
    Point destPoint = e.getPoint();
    polygon.reset();
    polygon.addPoint(srcPoint.x,  srcPoint.y);
    polygon.addPoint(destPoint.x, srcPoint.y);
    polygon.addPoint(destPoint.x, destPoint.y);
    polygon.addPoint(srcPoint.x,  destPoint.y);
    //setSelectedIndices(getIntersectsIcons(polygon));
    if(srcPoint.getX()==destPoint.getX() || srcPoint.getY()==destPoint.getY()) {
      line.setLine(srcPoint.getX(),srcPoint.getY(),destPoint.getX(),destPoint.getY());
      setSelectedIndices(getIntersectsIcons(line));
    }else{
      setSelectedIndices(getIntersectsIcons(polygon));
    }
    repaint();
  }
  @Override
  public void mouseReleased(MouseEvent e) {
    setFocusable(true);
    srcPoint = null;
    repaint();
  }
  @Override
  public void mousePressed(MouseEvent e) {
    int index = locationToIndex(e.getPoint());
    Rectangle rect = getCellBounds(index,index);
    if(!rect.contains(e.getPoint())) {
      getSelectionModel().setLeadSelectionIndex(getModel().getSize());
      clearSelection();
      setFocusable(false);
    }else{
      setFocusable(true);
    }
  }
  private int[] getIntersectsIcons(Shape p) {
    ListModel model = getModel();
    Vector< Integer > list = new Vector< Integer >(model.getSize());
    for(int i=0;i < model.getSize();i++) {
      Rectangle r = getCellBounds(i,i);
      if(p.intersects(r)) {
        list.add(i);
      }
    }
    int[] il = new int[list.size()];
    for(int i=0;i < list.size();i++) {
      il[i] = list.get(i);
    }
    return il;
  }
}

References