Example usage for javax.swing JViewport getViewPosition

List of usage examples for javax.swing JViewport getViewPosition

Introduction

In this page you can find the example usage for javax.swing JViewport getViewPosition.

Prototype

public Point getViewPosition() 

Source Link

Document

Returns the view coordinates that appear in the upper left hand corner of the viewport, or 0,0 if there's no view.

Usage

From source file:com.projity.pm.graphic.views.GanttView.java

public void init(ReferenceNodeModelCache cache, NodeModel model, CoordinatesConverter coord) {
    this.coord = coord;
    this.cache = NodeModelCacheFactory.getInstance().createFilteredCache((ReferenceNodeModelCache) cache,
            getViewName(), null);//  w  w w .  j  a  v  a 2 s. com

    fieldContext = new FieldContext();
    fieldContext.setLeftAssociation(true);
    /*cellStyle=new CellStyle(){
       CellFormat cellProperties=new CellFormat();
       public CellFormat getCellProperties(GraphicNode node){
    cellProperties.setBold(node.isSummary());
    cellProperties.setItalic(node.isAssignment());
    //cellProperties.setBackground((node.isAssignment())?"NORMAL_LIGHT_YELLOW":"NORMAL_YELLOW");
    cellProperties.setCompositeIcon(node.isComposite());
    return cellProperties;
       }
            
    };*/
    super.init();
    updateHeight(project);
    updateSize();

    //sync the height of spreadsheet and gantt
    leftScrollPane.getViewport().addChangeListener(new ChangeListener() {
        private Dimension olddl = null;

        public void stateChanged(ChangeEvent e) {
            Dimension dl = leftScrollPane.getViewport().getViewSize();
            if (dl.equals(olddl))
                return;
            olddl = dl;
            //            Dimension dr=rightScrollPane.getViewport().getViewSize();
            //            ((Gantt)rightScrollPane.getViewport().getView()).setPreferredSize(new Dimension((int)dr.getWidth(),(int)dl.getHeight()));
            //            rightScrollPane.getViewport().revalidate();
            ((Gantt) rightScrollPane.getViewport().getView()).setPreferredSize(
                    new Dimension(rightScrollPane.getViewport().getViewSize().width, dl.height));
        }
    });

    //TODO automatic scrolling to add as an option
    //      spreadSheet.getRowHeader().getSelectionModel().addListSelectionListener(new ListSelectionListener(){
    //         public void valueChanged(ListSelectionEvent e) {
    //            if (!e.getValueIsAdjusting()&&spreadSheet.getRowHeader().getSelectedRowCount()==1){
    //               List impls=spreadSheet.getSelectedNodesImpl();
    //               if (impls.size()!=1) return;
    //               Object impl=impls.get(0);
    //               if (!(impl instanceof HasStartAndEnd)) return;
    //               HasStartAndEnd interval=(HasStartAndEnd)impl;
    //               gantt.scrollToTask(interval, true);
    //            }
    //         }
    //      });

    MouseWheelListener scrollManager = new MouseWheelListener() {
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (0 != (e.getModifiers() & InputEvent.ALT_MASK)) {
                Component c = e.getComponent();
                while ((c != null) && !(c instanceof JViewport))
                    c = c.getParent();
                JViewport vp = (JViewport) c;
                Point p = vp.getViewPosition();
                int newX = p.x + e.getUnitsToScroll() * getWidth() / 20;
                if (newX > 0) {
                    p.x = newX;
                } else {
                    p.x = 0;
                }
                vp.setViewPosition(p);
            } else if (0 != (e.getModifiers() & InputEvent.CTRL_MASK)) {
                zoom(-e.getUnitsToScroll() / 3);
            } else {
                //Vertical scroll allways on gantt chart
                JViewport vp = (JViewport) gantt.getParent();
                Point p = vp.getViewPosition();

                int newY = p.y + e.getUnitsToScroll() * gantt.getRowHeight();
                if (newY > 0) {
                    p.y = newY;
                } else {
                    p.y = 0;
                }
                vp.setViewPosition(p);
            }
        }
    };

    leftScrollPane.getViewport().addMouseWheelListener(scrollManager);

    gantt.addMouseWheelListener(scrollManager);

    cache.update();

    //Call this last to be sure everything is initialized
    //gantt.insertCacheData(); //useless?

}

From source file:edu.purdue.cc.bionet.ui.HeatMap.java

/**
 * Scales to the given zoom level, 1.0 being 100%, centered on the 
 * given point./*from www.j a  va 2 s  .  com*/
 * 
 * @param level The level to zoom to.
 * @param center The center point for the scaling operation.
 * @return The new zoom level.
 */
public float scaleTo(float level, Point2D center) {
    float oldZoom = this.currentZoom;
    this.currentZoom = Math.max(level, 0.99f);
    Dimension viewSize;
    if (level < 1.0f) {
        viewSize = this.scrollPane.getSize();
    } else {
        viewSize = this.scrollPane.getViewport().getExtentSize();
    }
    Dimension newSize = new Dimension((int) (viewSize.width * currentZoom),
            (int) (viewSize.height * currentZoom));
    this.setPreferredSize(newSize);
    this.setSize(newSize);

    // translate the new view position so the mouse is in the same place
    // on the scaled view.
    JViewport vp = this.scrollPane.getViewport();
    double centerX = center.getX();
    double centerY = center.getY();
    double viewPortMouseX = centerX - vp.getViewPosition().getX();
    double viewPortMouseY = centerY - vp.getViewPosition().getY();
    centerX *= currentZoom / oldZoom;
    centerY *= currentZoom / oldZoom;
    viewPortMouseX = centerX - viewPortMouseX;
    viewPortMouseY = centerY - viewPortMouseY;
    vp.setViewPosition(new Point((int) viewPortMouseX, (int) viewPortMouseY));

    return this.currentZoom;
}

From source file:com.floreantpos.jasperreport.swing.JRViewerPanel.java

void pnlLinksMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnlLinksMouseDragged
    // Add your handling code here:

    Container container = pnlInScroll.getParent();
    if (container instanceof JViewport) {
        JViewport viewport = (JViewport) container;
        Point point = viewport.getViewPosition();
        int newX = point.x - (evt.getX() - downX);
        int newY = point.y - (evt.getY() - downY);

        int maxX = pnlInScroll.getWidth() - viewport.getWidth();
        int maxY = pnlInScroll.getHeight() - viewport.getHeight();

        if (newX < 0) {
            newX = 0;/*www .ja  v  a  2 s  .  c  om*/
        }
        if (newX > maxX) {
            newX = maxX;
        }
        if (newY < 0) {
            newY = 0;
        }
        if (newY > maxY) {
            newY = maxY;
        }

        viewport.setViewPosition(new Point(newX, newY));
    }
}

From source file:org.eurocarbdb.application.glycoworkbench.plugin.reporting.AnnotationReportCanvas.java

private void dragAndScroll(MouseEvent e) {
    // move view if near borders                
    Point point = e.getPoint();//from   w w  w .ja  v a2  s  . c o m
    JViewport view = theScrollPane.getViewport();
    Rectangle inner = view.getViewRect();
    inner.grow(-10, -10);

    if (!inner.contains(point)) {
        Point orig = view.getViewPosition();
        if (point.x < inner.x)
            orig.x -= 10;
        else if (point.x > (inner.x + inner.width))
            orig.x += 10;
        if (point.y < inner.y)
            orig.y -= 10;
        else if (point.y > (inner.y + inner.height))
            orig.y += 10;

        int maxx = getBounds().width - view.getViewRect().width;
        int maxy = getBounds().height - view.getViewRect().height;
        if (orig.x < 0)
            orig.x = 0;
        if (orig.x > maxx)
            orig.x = maxx;
        if (orig.y < 0)
            orig.y = 0;
        if (orig.y > maxy)
            orig.y = maxy;

        view.setViewPosition(orig);
    }
}

From source file:edu.purdue.cc.bionet.ui.GraphVisualizer.java

/**
 * Scales to the given zoom level, 1.0 being 100%, centered on the 
 * given point./*from w  w  w.  j  a  v a  2  s  . co  m*/
 * 
 * @param level The level to zoom to.
 * @param center The center point for the scaling operation.
 * @return The new zoom level.
 */
public float scaleTo(float level, Point2D center) {
    if (this.scrollPane == null)
        return 1.0f;
    float oldZoom = this.currentZoom;
    this.currentZoom = Math.max(minimumZoom, level);
    //      this.absoluteViewScaler.scale( this, level, center );
    Dimension viewSize;
    if (level < 1.0f) {
        viewSize = this.scrollPane.getSize();
    } else {
        viewSize = this.scrollPane.getViewport().getExtentSize();
    }
    Dimension newSize = new Dimension((int) (viewSize.width * currentZoom),
            (int) (viewSize.height * currentZoom));
    this.setPreferredSize(newSize);
    this.setSize(newSize);
    //      new LayoutScaler( this.getGraphLayout( )).setSize( newSize );
    if (Float.compare(level, 1.0f) <= 0)
        this.center();

    // translate the new view position so the mouse is in the same place
    // on the scaled view.
    JViewport vp = this.scrollPane.getViewport();
    double centerX = center.getX();
    double centerY = center.getY();
    double viewPortMouseX = centerX - vp.getViewPosition().getX();
    double viewPortMouseY = centerY - vp.getViewPosition().getY();
    centerX *= currentZoom / oldZoom;
    centerY *= currentZoom / oldZoom;
    viewPortMouseX = centerX - viewPortMouseX;
    viewPortMouseY = centerY - viewPortMouseY;
    vp.setViewPosition(new Point((int) viewPortMouseX, (int) viewPortMouseY));

    return this.currentZoom;
}

From source file:cn.pholance.datamanager.common.components.JRViewer.java

void pnlLinksMouseDragged(MouseEvent evt) {//GEN-FIRST:event_pnlLinksMouseDragged
    // Add your handling code here:

    Container container = pnlInScroll.getParent();
    if (container instanceof JViewport) {
        JViewport viewport = (JViewport) container;
        Point point = viewport.getViewPosition();
        int newX = point.x - (evt.getX() - downX);
        int newY = point.y - (evt.getY() - downY);

        int maxX = pnlInScroll.getWidth() - viewport.getWidth();
        int maxY = pnlInScroll.getHeight() - viewport.getHeight();

        if (newX < 0) {
            newX = 0;/*from w ww  . j a  v  a2  s  . c  o  m*/
        }
        if (newX > maxX) {
            newX = maxX;
        }
        if (newY < 0) {
            newY = 0;
        }
        if (newY > maxY) {
            newY = maxY;
        }

        viewport.setViewPosition(new Point(newX, newY));
    }
}

From source file:ireport_5_6_0.view.JRViewer.java

void pnlLinksMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pnlLinksMouseDragged
    // Add your handling code here:
    Container container = pnlInScroll.getParent();
    if (container instanceof JViewport) {
        JViewport viewport = (JViewport) container;
        Point point = viewport.getViewPosition();
        int newX = point.x - (evt.getX() - downX);
        int newY = point.y - (evt.getY() - downY);

        int maxX = pnlInScroll.getWidth() - viewport.getWidth();
        int maxY = pnlInScroll.getHeight() - viewport.getHeight();

        if (newX < 0) {
            newX = 0;/*w ww .  java2 s  .c o  m*/
        }
        if (newX > maxX) {
            newX = maxX;
        }
        if (newY < 0) {
            newY = 0;
        }
        if (newY > maxY) {
            newY = maxY;
        }

        viewport.setViewPosition(new Point(newX, newY));
    }
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopAbstractTable.java

protected void initChangeListener() {
    tableModel.addChangeListener(new AnyTableModelAdapter.DataChangeListener() {

        private boolean focused = false;
        private ThreadLocal<Set<E>> selectionBackup = new ThreadLocal<>();
        private int scrollRowIndex = -1;

        @Override/* w ww  .  j av  a  2  s.  c  o  m*/
        public void beforeChange(boolean structureChanged) {
            if (!structureChanged)
                return;

            isAdjusting = true;
            focused = impl.isFocusOwner();
            selectionBackup.set(selectedItems);

            JViewport viewport = (JViewport) impl.getParent();
            Point scrollPoint = viewport.getViewPosition();
            scrollRowIndex = impl.rowAtPoint(scrollPoint);
        }

        @Override
        public void afterChange(boolean structureChanged) {
            if (!structureChanged)
                return;

            isAdjusting = false;
            applySelection(filterSelection(selectionBackup.get()));
            selectionBackup.remove();

            if (focused) {
                impl.requestFocus();
            } else {
                if (impl.getCellEditor() != null) {
                    if (!impl.getCellEditor().stopCellEditing()) {
                        impl.getCellEditor().cancelCellEditing();
                    }
                }
            }

            TableFocusManager focusManager = ((FocusableTable) impl).getFocusManager();
            if (focusManager != null && scrollRowIndex >= 0) {
                focusManager.scrollToSelectedRow(scrollRowIndex);
            }

            // reassign identifiers for auto created columns
            setColumnIdentifiers();
        }

        @SuppressWarnings("unchecked")
        private Set<E> filterSelection(Set<E> selection) {
            if (selection == null)
                return Collections.emptySet();

            Set<E> newSelection = new HashSet<>(2 * selection.size());
            for (Entity item : selection) {
                if (datasource.containsItem(item.getId())) {
                    newSelection.add((E) datasource.getItem(item.getId()));
                }
            }
            return newSelection;
        }

        private void applySelection(Set<E> selection) {
            int minimalSelectionRowIndex = Integer.MAX_VALUE;
            if (!selection.isEmpty()) {
                for (Entity entity : selection) {
                    int rowIndex = tableModel.getRowIndex(entity);
                    if (rowIndex < minimalSelectionRowIndex && rowIndex >= 0) {
                        minimalSelectionRowIndex = rowIndex;
                    }
                }
            }

            setSelected(selection);

            if (!selection.isEmpty()) {
                if (focused) {
                    impl.requestFocus();
                } else {
                    if (impl.getCellEditor() != null) {
                        if (!impl.getCellEditor().stopCellEditing()) {
                            impl.getCellEditor().cancelCellEditing();
                        }
                    }
                }

                TableFocusManager focusManager = ((FocusableTable) impl).getFocusManager();
                if (focusManager != null) {
                    focusManager.scrollToSelectedRow(minimalSelectionRowIndex);
                }
            }
        }

        @Override
        public void dataSorted() {
            clearGeneratedColumnsCache();
            packRows();
        }
    });
}

From source file:org.nuclos.client.ui.collect.result.SearchResultStrategy.java

private void adjustVerticalScrollBarForSearch(boolean bRefreshOnly) {
    final CollectController<Clct> cc = getCollectController();
    final JViewport viewport = cc.getResultPanel().getResultTableScrollPane().getViewport();
    if (bRefreshOnly) {
        final Rectangle rect = cc.getResultTable().getCellRect(0, 0, true);
        final Rectangle viewRect = viewport.getViewRect();
        // There seem to be different opinions about what scrollRectToVisible has to do at SUN and everywhere else...
        rect.setLocation(viewRect.x, viewRect.y);//rect.x - viewRect.x, rect.y - viewRect.y);
        viewport.scrollRectToVisible(rect);
    } else {/*ww  w.j av a2 s  .  co  m*/
        Point viewPosition = viewport.getViewPosition();
        viewport.setViewPosition(new Point(viewPosition.x, 0));
    }
    final JScrollBar scrlbarVertical = cc.getResultPanel().getResultTableScrollPane().getVerticalScrollBar();
    scrlbarVertical.setValue(scrlbarVertical.getMinimum());
}

From source file:org.yccheok.jstock.gui.JTableUtilities.java

public static void scrollToVisible(JTable table, int rowIndex, int vColIndex) {
    if (!(table.getParent() instanceof JViewport)) {
        return;//from ww w  . j  a va  2  s . c o m
    }

    JViewport viewport = (JViewport) table.getParent();

    // This rectangle is relative to the table where the
    // northwest corner of cell (0,0) is always (0,0).
    Rectangle rect = table.getCellRect(rowIndex, vColIndex, true);

    // The location of the viewport relative to the table
    Point pt = viewport.getViewPosition();

    // Translate the cell location so that it is relative
    // to the view, assuming the northwest corner of the
    // view is (0,0)
    rect.setLocation(rect.x - pt.x, rect.y - pt.y);

    // Scroll the area into view
    viewport.scrollRectToVisible(rect);
}