Example usage for javax.swing JScrollPane addMouseWheelListener

List of usage examples for javax.swing JScrollPane addMouseWheelListener

Introduction

In this page you can find the example usage for javax.swing JScrollPane addMouseWheelListener.

Prototype

public synchronized void addMouseWheelListener(MouseWheelListener l) 

Source Link

Document

Adds the specified mouse wheel listener to receive mouse wheel events from this component.

Usage

From source file:com.qspin.qtaste.ui.reporter.TestCaseReportTable.java

public void putEntry(TestResult tr, String campaignReportName) {
    boolean isInteractiveCommand = tr.getTestCaseDirectory().endsWith("QTaste_interactive");
    String testReportName = TestResultsReportManager.getInstance().getReportName();
    boolean isManualSUTStartOrStop = testReportName != null && testReportName.startsWith("Manual SUT");
    if (interactive) {
        if (!isInteractiveCommand && !isManualSUTStartOrStop) {
            return;
        }//from w  w  w . j  a va  2  s.c om
    } else {
        if (isInteractiveCommand) {
            return;
        }
        if (campaignReportName != null && !campaignReportName.equals(runName)) {
            return;
        }
        if (campaignReportName == null && !runName.equals("Run1")) {
            return;
        }
    }
    if (!testCases.containsKey(tr)) {
        Object[] cols = new Object[7];
        cols[TEST_CASE] = convertToUniqueTc(tr);
        cols[DETAILS] = tr.getExtraResultDetails();
        if (tr.getReturnValue() != null) {
            cols[RESULT] = tr.getReturnValue();
        }
        cols[STATUS] = getImage(tr.getStatus());
        if (tr.getStatus() != TestResult.Status.RUNNING) {
            cols[EXEC_TIME] = tr.getFormattedElapsedTime(false);
        }

        TestBedConfiguration testbed = TestBedConfiguration.getInstance();
        cols[TESTBED] = testbed.getFile().getName().replace("." + StaticConfiguration.CAMPAIGN_FILE_EXTENSION,
                "");

        cols[TC] = tr;
        //tcModel.addRow(cols);
        Integer rowNum = new Integer(tcModel.getRowCount());
        testCases.put(tr, rowNum);
        long currentScrollBarMax = 0;

        JScrollPane scrollPane = (JScrollPane) tcTable.getParent().getParent();
        JScrollBar scrollbar = scrollPane.getVerticalScrollBar();
        if (scrollbar != null) {
            if (scrollbar.getMouseListeners().length == 1) {
                scrollPane.addMouseWheelListener(new MouseWheelListener() {

                    public void mouseWheelMoved(MouseWheelEvent e) {
                        userScrollPosition = true;
                    }
                });
                scrollbar.addMouseListener(new MouseAdapter() {

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        userScrollPosition = true;
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        userScrollPosition = true;
                    }
                });
            }
            currentScrollBarMax = scrollbar.getMaximum() - scrollbar.getSize().height - tcTable.getRowHeight();
            tcModel.addRow(cols);
            if (!userScrollPosition) {
                tcTable.scrollRectToVisible(tcTable.getCellRect(tcModel.getRowCount() - 1, 0, true));
                return;
            } else if (scrollbar.getValue() >= currentScrollBarMax) {
                tcTable.scrollRectToVisible(tcTable.getCellRect(tcModel.getRowCount() - 1, 0, true));
                userScrollPosition = false;
                return;
            } else {
                System.out.println("Scrollbar pos=" + scrollbar.getValue() + "; max=" + currentScrollBarMax
                        + "height=" + scrollbar.getSize().height);
            }
        } else {
            tcModel.addRow(cols);
        }
    } else {
        updateTestCaseRow(tr);
    }

}

From source file:savant.view.swing.BookmarkSheet.java

public BookmarkSheet(Container c) {

    // set the layout of the data sheet
    c.setLayout(new BorderLayout());
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

    /**/*from   www . ja va 2s. c om*/
     * Create a toolbar. 
     */
    JMenuBar toolbar = new JMenuBar();
    toolbar.setLayout(new BoxLayout(toolbar, BoxLayout.X_AXIS));
    c.add(toolbar, BorderLayout.NORTH);

    JButton previousButton = new JButton();
    previousButton.setIcon(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.UP));
    previousButton.setToolTipText("Go to previous bookmark [ Ctrl+( ]");
    previousButton.putClientProperty("JButton.buttonType", "segmentedRoundRect");
    previousButton.putClientProperty("JButton.segmentPosition", "first");
    previousButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            goToPreviousBookmark();
        }

    });
    toolbar.add(previousButton);

    JButton nextButton = new JButton();
    nextButton.setIcon(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.DOWN));
    nextButton.setToolTipText("Go to next bookmark [ Ctrl+) ]");
    nextButton.putClientProperty("JButton.buttonType", "segmentedRoundRect");
    nextButton.putClientProperty("JButton.segmentPosition", "last");
    nextButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            goToNextBookmark();
        }

    });
    toolbar.add(nextButton);

    JButton goButton = new JButton("Go");
    goButton.setToolTipText("Go to selected bookmark");
    goButton.putClientProperty("JButton.buttonType", "segmentedRoundRect");
    goButton.putClientProperty("JButton.segmentPosition", "only");
    goButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            goToSelectedBookmark();
        }

    });
    toolbar.add(goButton);

    toolbar.add(Box.createGlue());

    addButton = new JButton();
    addButton.setIcon(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.BKMK_ADD));
    addButton.setToolTipText("Add bookmark for current range");
    addButton.putClientProperty("JButton.buttonType", "segmentedRoundRect");
    addButton.putClientProperty("JButton.segmentPosition", "first");
    addButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            BookmarkController fc = BookmarkController.getInstance();
            fc.addCurrentRangeToBookmarks();
        }
    });
    toolbar.add(addButton);

    JButton deleteButton = new JButton();
    deleteButton.setIcon(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.BKMK_RM));
    deleteButton.setToolTipText("Delete selected bookmarks");
    deleteButton.putClientProperty("JButton.buttonType", "segmentedRoundRect");
    deleteButton.putClientProperty("JButton.segmentPosition", "last");
    deleteButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            BookmarkController fc = BookmarkController.getInstance();
            int[] selectedRows = table.getSelectedRows();
            Arrays.sort(selectedRows);
            boolean delete = false;

            if (selectedRows.length > 0 && confirmDelete) {
                Object[] options = { "Yes", "No", "Yes, don't ask again" };
                JLabel message = new JLabel(
                        "Are you sure you want to delete " + selectedRows.length + " item(s)?");
                message.setPreferredSize(new Dimension(300, 20));
                int confirmDeleteDialog = JOptionPane.showOptionDialog(DialogUtils.getMainWindow(), message,
                        "Confirm Delete", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                        options, options[0]);

                if (confirmDeleteDialog == 0) {
                    delete = true;
                } else if (confirmDeleteDialog == 2) {
                    delete = true;
                    confirmDelete = false;
                }
            } else if (selectedRows.length > 0 && !confirmDelete) {
                delete = true;
            }

            if (delete) {
                for (int i = selectedRows.length - 1; i >= 0; i--) {
                    fc.removeBookmark(selectedRows[i]);
                }
            }
        }
    });
    toolbar.add(deleteButton);

    toolbar.add(Box.createGlue());

    JButton loadButton = new JButton();
    loadButton.setIcon(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.OPEN));
    loadButton.setToolTipText("Load bookmarks from file");
    loadButton.putClientProperty("JButton.buttonType", "segmentedRoundRect");
    loadButton.putClientProperty("JButton.segmentPosition", "first");
    loadButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            loadBookmarks(table);
        }
    });
    toolbar.add(loadButton);

    JButton saveButton = new JButton();
    saveButton.setIcon(SavantIconFactory.getInstance().getIcon(SavantIconFactory.StandardIcon.SAVE));
    saveButton.setToolTipText("Save bookmarks to file");
    saveButton.putClientProperty("JButton.buttonType", "segmentedRoundRect");
    saveButton.putClientProperty("JButton.segmentPosition", "last");
    saveButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            saveBookmarks(table);
        }
    });
    toolbar.add(saveButton);

    // create a table (the most important component)
    table = new JTable(new BookmarksTableModel());
    table.setAutoCreateRowSorter(true);
    table.setFillsViewportHeight(true);
    table.setShowGrid(true);
    table.setGridColor(Color.gray);
    //table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

    // add the table and its header to the subpanel
    c.add(table.getTableHeader());

    add(table);

    final JScrollPane sp = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    sp.setWheelScrollingEnabled(false);
    sp.addMouseWheelListener(new MouseWheelListener() {
        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            sp.getVerticalScrollBar().setValue(sp.getVerticalScrollBar().getValue() + e.getUnitsToScroll() * 2);
        }
    });

    c.add(sp);

    // add glue to fill the remaining space
    add(Box.createGlue());
}