Example usage for javax.swing JViewport setViewPosition

List of usage examples for javax.swing JViewport setViewPosition

Introduction

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

Prototype

public void setViewPosition(Point p) 

Source Link

Document

Sets the view coordinates that appear in the upper left hand corner of the viewport, does nothing if there's no view.

Usage

From source file:Main.java

public static void synchronizeView(final JViewport masterViewport, final JViewport slaveViewport,
        final int orientation) {
    final ChangeListener c1 = new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            if (masterViewport.getView() == null || slaveViewport.getView() == null) {
                return;
            }//from  w w w  .ja  v a  2s. c o m
            if (orientation == SwingConstants.HORIZONTAL) {
                Point v1 = masterViewport.getViewPosition();
                Point v2 = slaveViewport.getViewPosition();
                if (v1.x != v2.x) {
                    slaveViewport.setViewPosition(new Point(v1.x, v2.y));
                }
            } else if (orientation == SwingConstants.VERTICAL) {
                Point v1 = masterViewport.getViewPosition();
                Point v2 = slaveViewport.getViewPosition();
                if (v1.y != v2.y) {
                    slaveViewport.setViewPosition(new Point(v2.x, v1.y));
                }
            }
        }
    };

    masterViewport.addChangeListener(c1);
}

From source file:Main.java

public static void addMiddleButtonDragSupport(Component targetComponent) {
    MouseInputAdapter mia = new MouseInputAdapter() {
        int m_XDifference, m_YDifference;
        boolean m_dragging = false;

        public void mouseDragged(MouseEvent e) {
            if (!m_dragging)
                return;

            Component target = e.getComponent();
            Container c = target.getParent();
            if (c instanceof JViewport) {
                JViewport jv = (JViewport) c;
                Point p = jv.getViewPosition();
                int newX = p.x - (e.getX() - m_XDifference);
                int newY = p.y - (e.getY() - m_YDifference);

                int maxX = target.getWidth() - jv.getWidth();
                int maxY = target.getHeight() - jv.getHeight();
                if (newX < 0)
                    newX = 0;//from  w  w w  .j ava2 s.  com
                if (newX > maxX)
                    newX = maxX;
                if (newY < 0)
                    newY = 0;
                if (newY > maxY)
                    newY = maxY;

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

        Cursor oldCursor;

        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isMiddleMouseButton(e)) {
                m_dragging = true;
                oldCursor = e.getComponent().getCursor();
                e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
                m_XDifference = e.getX();
                m_YDifference = e.getY();
            }
        }

        public void mouseReleased(MouseEvent e) {
            if (m_dragging) {
                e.getComponent().setCursor(oldCursor);
                m_dragging = false;
            }
        }
    };
    targetComponent.addMouseMotionListener(mia);
    targetComponent.addMouseListener(mia);
}

From source file:Main.java

private void addComponents() {
    JEditorPane editorPane = new JEditorPane();
    editorPane.setContentType("text/html");
    editorPane.setEditable(false);//from ww w  .j  a  v  a  2  s  . co m
    editorPane.setText(TEXT);

    JScrollPane scrollpane = new JScrollPane(editorPane);

    editorPane.addHyperlinkListener(e -> {
        if (HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType())) {
            String description = e.getDescription();
            if (TOP.equals(description)) {
                JViewport viewport = scrollpane.getViewport();
                viewport.setViewPosition(new Point(0, 0));
            }
        }
    });
    super.add(scrollpane);
}

From source file:Main.java

public void setView(JScrollPane scroll, Component comp) {
    JViewport view = scroll.getViewport();
    Point p = comp.getLocation();
    view.setViewPosition(p);
}

From source file:de.tor.tribes.ui.wiz.ret.RetimerCalculationPanel.java

private void scroll() {
    Point point = new Point(0, (int) (jTextPane1.getSize().getHeight()));
    JViewport vp = jScrollPane1.getViewport();
    if ((vp == null) || (point == null)) {
        return;//w  w  w  .  java2  s .  c om
    }
    vp.setViewPosition(point);
}

From source file:de.tor.tribes.ui.wiz.ref.SupportRefillCalculationPanel.java

private void scroll() {
    Point point = new Point(0, (int) (jTextPane1.getSize().getHeight()));
    JViewport vp = jScrollPane1.getViewport();
    if (vp == null) {
        return;/*from w w w .j a v a 2 s .  c o  m*/
    }
    vp.setViewPosition(point);
}

From source file:Main.java

public GrabAndScrollLabel(ImageIcon i) {
    super(i);/*w w w .  j  ava  2s.c om*/

    MouseInputAdapter mia = new MouseInputAdapter() {
        int xDiff, yDiff;
        Container c;

        public void mouseDragged(MouseEvent e) {
            c = GrabAndScrollLabel.this.getParent();
            if (c instanceof JViewport) {
                JViewport jv = (JViewport) c;
                Point p = jv.getViewPosition();
                int newX = p.x - (e.getX() - xDiff);
                int newY = p.y - (e.getY() - yDiff);

                int maxX = GrabAndScrollLabel.this.getWidth() - jv.getWidth();
                int maxY = GrabAndScrollLabel.this.getHeight() - jv.getHeight();
                if (newX < 0)
                    newX = 0;
                if (newX > maxX)
                    newX = maxX;
                if (newY < 0)
                    newY = 0;
                if (newY > maxY)
                    newY = maxY;

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

        public void mousePressed(MouseEvent e) {
            setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
            xDiff = e.getX();
            yDiff = e.getY();
        }

        public void mouseReleased(MouseEvent e) {
            setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
    };
    addMouseMotionListener(mia);
    addMouseListener(mia);
}

From source file:GrabAndDragDemo.java

public GrabAndScrollLabel(ImageIcon i) {
    super(i);//  w ww. ja  va 2  s .  c om

    MouseInputAdapter mia = new MouseInputAdapter() {
        int xDiff, yDiff;

        boolean isDragging;

        Container c;

        public void mouseDragged(MouseEvent e) {
            c = GrabAndScrollLabel.this.getParent();
            if (c instanceof JViewport) {
                JViewport jv = (JViewport) c;
                Point p = jv.getViewPosition();
                int newX = p.x - (e.getX() - xDiff);
                int newY = p.y - (e.getY() - yDiff);

                int maxX = GrabAndScrollLabel.this.getWidth() - jv.getWidth();
                int maxY = GrabAndScrollLabel.this.getHeight() - jv.getHeight();
                if (newX < 0)
                    newX = 0;
                if (newX > maxX)
                    newX = maxX;
                if (newY < 0)
                    newY = 0;
                if (newY > maxY)
                    newY = maxY;

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

        public void mousePressed(MouseEvent e) {
            setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
            xDiff = e.getX();
            yDiff = e.getY();
        }

        public void mouseReleased(MouseEvent e) {
            setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
    };
    addMouseMotionListener(mia);
    addMouseListener(mia);
}

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  va  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.ucla.stat.SOCR.applications.demo.BinomialTradingApplication.java

void updateGraph() {
    //This method will later update/redraw/repaint the (Node, Edge)-Graph
    // For now, we just print out the results in JTextArea

    // Price of the Stock Price[k][l] is the price of the stock at level k (0<=k<=n)
    // and outcome l (0<=l<n+1)
    GraphModel model = new DefaultGraphModel();
    jgraph = new JGraph(model);
    cells = new DefaultGraphCell[((n + 1) * (n + 2) / 2) * 3];
    cellCount = 0;//from w w w  . ja  v  a2s  . c om

    int XSpace = 130;
    int YSpace = 30;
    int YMiddle = 20 + n * YSpace; //150

    for (int k = 0; k <= n; k++) {
        //System.out.println("============Start-of-level("+k+")==================");
        for (int l = 0; l <= k; l++) {
            // Print to GraphPanel's JTextArea
            int j = -k + l * 2;

            if (choice.charAt(9) == 'P') { //For put option
                if (k == n) { // For the last column, k=n, (in the money), 
                    // we need to color GREEN or BLUE the node-background 
                    // to indicate if the Call Price (C) is > 0 (green) or
                    // <= 0 (blue)
                    if (j < 0) {
                        if (Diff[k][l] > 0)
                            addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " P[" + k + ","
                                    + l + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace),
                                    (YMiddle + j * YSpace - 20), Color.green);
                        else
                            addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " P[" + k + ","
                                    + l + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace),
                                    (YMiddle + j * YSpace - 20), Color.blue);
                    } else if (j > 0) {
                        if (Diff[k][l] > 0)
                            addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " P[" + k + ","
                                    + l + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace),
                                    (YMiddle + j * YSpace + 20), Color.green);
                        else
                            addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " P[" + k + ","
                                    + l + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace),
                                    (YMiddle + j * YSpace + 20), Color.blue);
                    } else {
                        if (Diff[k][l] > 0)
                            addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " P[" + k + ","
                                    + l + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace), YMiddle,
                                    Color.green);
                        else
                            addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " P[" + k + ","
                                    + l + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace), YMiddle,
                                    Color.blue);
                    }
                } else { // For all columns before the last (in the money) column, k<n)!
                    if (j < 0)
                        addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " P[" + k + "," + l
                                + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace),
                                (YMiddle + j * YSpace - 20));
                    else if (j > 0)
                        addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " P[" + k + "," + l
                                + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace),
                                (YMiddle + j * YSpace + 20));
                    else
                        addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " P[" + k + "," + l
                                + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace), YMiddle);
                }
            } else { //For call option
                if (k == n) { // For the last column, k=n, (in the money), 
                    // we need to color GREEN or BLUE the node-background 
                    // to indicate if the Call Price (C) is > 0 (green) or
                    // <= 0 (blue)
                    if (j < 0) {
                        if (Diff[k][l] > 0)
                            addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " C[" + k + ","
                                    + l + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace),
                                    (YMiddle + j * YSpace - 20), Color.green);
                        else
                            addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " C[" + k + ","
                                    + l + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace),
                                    (YMiddle + j * YSpace - 20), Color.blue);
                    } else if (j > 0) {
                        if (Diff[k][l] > 0)
                            addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " C[" + k + ","
                                    + l + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace),
                                    (YMiddle + j * YSpace + 20), Color.green);
                        else
                            addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " C[" + k + ","
                                    + l + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace),
                                    (YMiddle + j * YSpace + 20), Color.blue);
                    } else {
                        if (Diff[k][l] > 0)
                            addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " C[" + k + ","
                                    + l + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace), YMiddle,
                                    Color.green);
                        else
                            addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " C[" + k + ","
                                    + l + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace), YMiddle,
                                    Color.blue);
                    }
                } else if (choice.charAt(0) == 'A' && Diff[k][l] == Math.abs(Price[k][l] - EP)) {
                    // For all columns before the last (in the money) column, k<n)! and possibly exercised early
                    if (j < 0)
                        addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " C[" + k + "," + l
                                + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace),
                                (YMiddle + j * YSpace - 20), Color.magenta);
                    else if (j > 0)
                        addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " C[" + k + "," + l
                                + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace),
                                (YMiddle + j * YSpace + 20), Color.magenta);
                    else
                        addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " C[" + k + "," + l
                                + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace), YMiddle,
                                Color.magenta);
                } else { // For all columns before the last (in the money) column, k<n)!
                    if (j < 0)
                        addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " C[" + k + "," + l
                                + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace),
                                (YMiddle + j * YSpace - 20));
                    else if (j > 0)
                        addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " C[" + k + "," + l
                                + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace),
                                (YMiddle + j * YSpace + 20));
                    else
                        addNode("S[" + k + "," + l + "]=" + formatter.format(Price[k][l]) + " C[" + k + "," + l
                                + "]=" + formatter.format(Diff[k][l]), (10 + k * XSpace), YMiddle);
                    // Also Print to STD_OUT
                    //   System.out.println("j="+j);
                    //   System.out.println("Price["+k+"]["+l+"]="+Price[k][l]);
                    //   System.out.println("Diff["+k+"]["+l+"]="+Diff[k][l]);
                }
            }
        }
        //System.out.println("============End-of-level("+k+")==================\n");
    }
    //System.out.println("cellCount="+cellCount);

    for (int k = 0; k < n; k++) {
        //System.out.println("============Start-of-level("+k+")==================");
        for (int l = 0; l <= k; l++) {
            //   System.out.println((k*(k+1)/2+l)+"->"+((k+1)*(k+2)/2+l) );

            addEdge((k * (k + 1) / 2 + l), ((k + 1) * (k + 2) / 2 + l));
            //System.out.println((k*(k+1)/2+l)+"->"+((k+1)*(k+2)/2+l+1) );
            addEdge((k * (k + 1) / 2 + l), ((k + 1) * (k + 2) / 2 + l + 1));
        }
        //   System.out.println("============End-of-level("+k+")==================\n");
    }

    //   System.out.println("cellCount="+cellCount);
    DefaultGraphCell[] cellsCache = new DefaultGraphCell[cellCount];

    for (int i = 0; i < cellCount; i++)
        cellsCache[i] = cells[i];

    jgraph.getGraphLayoutCache().insert(cellsCache);
    Dimension d = jgraph.getPreferredSize();
    //System.out.println(d.width +","+d.height);
    jgraph.setPreferredSize(new Dimension(d.width + 150, d.height));

    jgraphPanel.removeAll();
    JScrollPane jsp = new JScrollPane(jgraph);
    jgraphPanel.add(jsp);
    jgraphPanel.setPreferredSize(new Dimension(CHART_SIZE_X, CHART_SIZE_Y));
    JViewport jv = jsp.getViewport();
    jv.setViewPosition(new Point(0, YMiddle - 150));
    jgraphPanel.validate();
}