Search in sources :

Example 56 with KeyListener

use of java.awt.event.KeyListener in project mafscaling by vimsh.

the class MafCompare method initialize.

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    try {
        ImageIcon tableImage = new ImageIcon(getClass().getResource("/table.jpg"));
        setTitle(Title);
        setIconImage(tableImage.getImage());
        setBounds(100, 100, 621, 372);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setSize(Config.getCompWindowSize());
        setLocation(Config.getCompWindowLocation());
        setLocationRelativeTo(null);
        setVisible(false);
        addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                Utils.clearTable(origMafTable);
                Utils.clearTable(newMafTable);
                Utils.clearTable(compMafTable);
                Config.setCompWindowSize(getSize());
                Config.setCompWindowLocation(getLocation());
                origMafData.clear();
                newMafData.clear();
            }
        });
        JPanel dataPanel = new JPanel();
        GridBagLayout gbl_dataPanel = new GridBagLayout();
        gbl_dataPanel.columnWidths = new int[] { 0, 0, 0 };
        gbl_dataPanel.rowHeights = new int[] { RowHeight, RowHeight, RowHeight, RowHeight, RowHeight, 0 };
        gbl_dataPanel.columnWeights = new double[] { 0.0, 0.0, 0.0 };
        gbl_dataPanel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 };
        dataPanel.setLayout(gbl_dataPanel);
        getContentPane().add(dataPanel);
        JLabel origLabel = new JLabel(origMaf);
        GridBagConstraints gbc_origLabel = new GridBagConstraints();
        gbc_origLabel.anchor = GridBagConstraints.PAGE_START;
        gbc_origLabel.insets = new Insets(1, 1, 1, 5);
        gbc_origLabel.weightx = 0;
        gbc_origLabel.weighty = 0;
        gbc_origLabel.gridx = 0;
        gbc_origLabel.gridy = 0;
        gbc_origLabel.gridheight = 2;
        dataPanel.add(origLabel, gbc_origLabel);
        JLabel newLabel = new JLabel(newMaf);
        GridBagConstraints gbc_newLabel = new GridBagConstraints();
        gbc_newLabel.anchor = GridBagConstraints.PAGE_START;
        gbc_newLabel.insets = new Insets(1, 1, 1, 5);
        gbc_newLabel.weightx = 0;
        gbc_newLabel.weighty = 0;
        gbc_newLabel.gridx = 0;
        gbc_newLabel.gridy = 2;
        gbc_newLabel.gridheight = 2;
        dataPanel.add(newLabel, gbc_newLabel);
        JLabel compLabel = new JLabel("Change");
        GridBagConstraints gbc_compLabel = new GridBagConstraints();
        gbc_compLabel.anchor = GridBagConstraints.PAGE_START;
        gbc_compLabel.insets = new Insets(1, 1, 1, 5);
        gbc_compLabel.weightx = 0;
        gbc_compLabel.weighty = 0;
        gbc_compLabel.gridx = 0;
        gbc_compLabel.gridy = 4;
        dataPanel.add(compLabel, gbc_compLabel);
        JLabel origVoltLabel = new JLabel("volt");
        GridBagConstraints gbc_origVoltLabel = new GridBagConstraints();
        gbc_origVoltLabel.anchor = GridBagConstraints.PAGE_START;
        gbc_origVoltLabel.insets = new Insets(1, 1, 1, 5);
        gbc_origVoltLabel.weightx = 0;
        gbc_origVoltLabel.weighty = 0;
        gbc_origVoltLabel.gridx = 1;
        gbc_origVoltLabel.gridy = 0;
        dataPanel.add(origVoltLabel, gbc_origVoltLabel);
        JLabel origGsLabel = new JLabel(" g/s");
        GridBagConstraints gbc_origGsLabel = new GridBagConstraints();
        gbc_origGsLabel.anchor = GridBagConstraints.PAGE_START;
        gbc_origGsLabel.insets = new Insets(1, 1, 1, 5);
        gbc_origGsLabel.weightx = 0;
        gbc_origGsLabel.weighty = 0;
        gbc_origGsLabel.gridx = 1;
        gbc_origGsLabel.gridy = 1;
        dataPanel.add(origGsLabel, gbc_origGsLabel);
        JLabel newVoltLabel = new JLabel("volt");
        GridBagConstraints gbc_newVoltLabel = new GridBagConstraints();
        gbc_newVoltLabel.anchor = GridBagConstraints.PAGE_START;
        gbc_newVoltLabel.insets = new Insets(1, 1, 1, 5);
        gbc_newVoltLabel.weightx = 0;
        gbc_newVoltLabel.weighty = 0;
        gbc_newVoltLabel.gridx = 1;
        gbc_newVoltLabel.gridy = 2;
        dataPanel.add(newVoltLabel, gbc_newVoltLabel);
        JLabel newGsLabel = new JLabel(" g/s");
        GridBagConstraints gbc_newGsLabel = new GridBagConstraints();
        gbc_newGsLabel.anchor = GridBagConstraints.PAGE_START;
        gbc_newGsLabel.insets = new Insets(1, 1, 1, 5);
        gbc_newGsLabel.weightx = 0;
        gbc_newGsLabel.weighty = 0;
        gbc_newGsLabel.gridx = 1;
        gbc_newGsLabel.gridy = 3;
        dataPanel.add(newGsLabel, gbc_newGsLabel);
        JLabel compPctLabel = new JLabel(" %  ");
        GridBagConstraints gbc_compPctLabel = new GridBagConstraints();
        gbc_compPctLabel.anchor = GridBagConstraints.PAGE_START;
        gbc_compPctLabel.insets = new Insets(1, 1, 1, 5);
        gbc_compPctLabel.weightx = 0;
        gbc_compPctLabel.weighty = 0;
        gbc_compPctLabel.gridx = 1;
        gbc_compPctLabel.gridy = 4;
        dataPanel.add(compPctLabel, gbc_compPctLabel);
        JPanel tablesPanel = new JPanel();
        GridBagLayout gbl_tablesPanel = new GridBagLayout();
        gbl_tablesPanel.columnWidths = new int[] { 0 };
        gbl_tablesPanel.rowHeights = new int[] { 0, 0, 0 };
        gbl_tablesPanel.columnWeights = new double[] { 0.0 };
        gbl_tablesPanel.rowWeights = new double[] { 0.0, 0.0, 1.0 };
        tablesPanel.setLayout(gbl_tablesPanel);
        JScrollPane mafScrollPane = new JScrollPane(tablesPanel);
        mafScrollPane.setMinimumSize(new Dimension(1600, 107));
        mafScrollPane.getHorizontalScrollBar().setMaximumSize(new Dimension(20, 20));
        mafScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        mafScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        GridBagConstraints gbc_mafScrollPane = new GridBagConstraints();
        gbc_mafScrollPane.weightx = 1.0;
        gbc_mafScrollPane.anchor = GridBagConstraints.PAGE_START;
        gbc_mafScrollPane.fill = GridBagConstraints.HORIZONTAL;
        gbc_mafScrollPane.gridx = 2;
        gbc_mafScrollPane.gridy = 0;
        gbc_mafScrollPane.gridheight = 5;
        dataPanel.add(mafScrollPane, gbc_mafScrollPane);
        origMafTable = new JTable();
        origMafTable.setColumnSelectionAllowed(true);
        origMafTable.setCellSelectionEnabled(true);
        origMafTable.setBorder(new LineBorder(new Color(0, 0, 0)));
        origMafTable.setRowHeight(RowHeight);
        origMafTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        origMafTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        origMafTable.setModel(new DefaultTableModel(2, MafTableColumnCount));
        origMafTable.setTableHeader(null);
        origMafTable.putClientProperty("terminateEditOnFocusLost", true);
        Utils.initializeTable(origMafTable, ColumnWidth);
        GridBagConstraints gbc_origMafTable = new GridBagConstraints();
        gbc_origMafTable.anchor = GridBagConstraints.PAGE_START;
        gbc_origMafTable.insets = new Insets(0, 0, 0, 0);
        gbc_origMafTable.fill = GridBagConstraints.HORIZONTAL;
        gbc_origMafTable.weightx = 1.0;
        gbc_origMafTable.weighty = 0;
        gbc_origMafTable.gridx = 0;
        gbc_origMafTable.gridy = 0;
        tablesPanel.add(origMafTable, gbc_origMafTable);
        excelAdapter.addTable(origMafTable, false, false, false, false, true, false, true, false, true);
        newMafTable = new JTable();
        newMafTable.setColumnSelectionAllowed(true);
        newMafTable.setCellSelectionEnabled(true);
        newMafTable.setBorder(new LineBorder(new Color(0, 0, 0)));
        newMafTable.setRowHeight(RowHeight);
        newMafTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        newMafTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        newMafTable.setModel(new DefaultTableModel(2, MafTableColumnCount));
        newMafTable.setTableHeader(null);
        newMafTable.putClientProperty("terminateEditOnFocusLost", true);
        Utils.initializeTable(newMafTable, ColumnWidth);
        GridBagConstraints gbc_newMafTable = new GridBagConstraints();
        gbc_newMafTable.anchor = GridBagConstraints.PAGE_START;
        gbc_newMafTable.insets = new Insets(0, 0, 0, 0);
        gbc_newMafTable.fill = GridBagConstraints.HORIZONTAL;
        gbc_newMafTable.weightx = 1.0;
        gbc_newMafTable.weighty = 0;
        gbc_newMafTable.gridx = 0;
        gbc_newMafTable.gridy = 1;
        tablesPanel.add(newMafTable, gbc_newMafTable);
        excelAdapter.addTable(newMafTable, false, false, false, false, false, false, false, false, true);
        compMafTable = new JTable();
        compMafTable.setColumnSelectionAllowed(true);
        compMafTable.setCellSelectionEnabled(true);
        compMafTable.setBorder(new LineBorder(new Color(0, 0, 0)));
        compMafTable.setRowHeight(RowHeight);
        compMafTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        compMafTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        compMafTable.setModel(new DefaultTableModel(1, MafTableColumnCount));
        compMafTable.setTableHeader(null);
        compMafTable.putClientProperty("terminateEditOnFocusLost", true);
        Utils.initializeTable(compMafTable, ColumnWidth);
        NumberFormatRenderer numericRenderer = new NumberFormatRenderer();
        numericRenderer.setFormatter(new DecimalFormat("0.000"));
        compMafTable.setDefaultRenderer(Object.class, numericRenderer);
        GridBagConstraints gbc_compMafTable = new GridBagConstraints();
        gbc_compMafTable.anchor = GridBagConstraints.PAGE_START;
        gbc_compMafTable.insets = new Insets(0, 0, 0, 0);
        gbc_compMafTable.fill = GridBagConstraints.HORIZONTAL;
        gbc_compMafTable.weightx = 1.0;
        gbc_compMafTable.weighty = 0;
        gbc_compMafTable.gridx = 0;
        gbc_compMafTable.gridy = 2;
        tablesPanel.add(compMafTable, gbc_compMafTable);
        compExcelAdapter.addTable(compMafTable, false, true, false, false, false, true, true, false, true);
        TableModelListener origTableListener = new TableModelListener() {

            public void tableChanged(TableModelEvent tme) {
                if (tme.getType() == TableModelEvent.UPDATE) {
                    int colCount = origMafTable.getColumnCount();
                    Utils.ensureColumnCount(colCount, newMafTable);
                    Utils.ensureColumnCount(colCount, compMafTable);
                    origMafData.clear();
                    String origY, origX, newY;
                    for (int i = 0; i < colCount; ++i) {
                        origY = origMafTable.getValueAt(1, i).toString();
                        if (Pattern.matches(Utils.fpRegex, origY)) {
                            origX = origMafTable.getValueAt(0, i).toString();
                            if (Pattern.matches(Utils.fpRegex, origX))
                                origMafData.add(Double.valueOf(origX), Double.valueOf(origY), false);
                            newY = newMafTable.getValueAt(1, i).toString();
                            if (Pattern.matches(Utils.fpRegex, newY))
                                compMafTable.setValueAt(((Double.valueOf(newY) / Double.valueOf(origY)) - 1.0) * 100.0, 0, i);
                        } else
                            break;
                    }
                    origMafData.fireSeriesChanged();
                }
            }
        };
        TableModelListener newTableListener = new TableModelListener() {

            public void tableChanged(TableModelEvent tme) {
                if (tme.getType() == TableModelEvent.UPDATE) {
                    int colCount = newMafTable.getColumnCount();
                    Utils.ensureColumnCount(colCount, origMafTable);
                    Utils.ensureColumnCount(colCount, compMafTable);
                    newMafData.clear();
                    String newY, newX, origY;
                    for (int i = 0; i < colCount; ++i) {
                        newY = newMafTable.getValueAt(1, i).toString();
                        if (Pattern.matches(Utils.fpRegex, newY)) {
                            newX = newMafTable.getValueAt(0, i).toString();
                            if (Pattern.matches(Utils.fpRegex, newX))
                                newMafData.add(Double.valueOf(newX), Double.valueOf(newY), false);
                            origY = origMafTable.getValueAt(1, i).toString();
                            if (Pattern.matches(Utils.fpRegex, origY))
                                compMafTable.setValueAt(((Double.valueOf(newY) / Double.valueOf(origY)) - 1.0) * 100.0, 0, i);
                        } else
                            break;
                    }
                    newMafData.fireSeriesChanged();
                }
            }
        };
        origMafTable.getModel().addTableModelListener(origTableListener);
        newMafTable.getModel().addTableModelListener(newTableListener);
        Action action = new AbstractAction() {

            private static final long serialVersionUID = 8148393537657380215L;

            public void actionPerformed(ActionEvent e) {
                TableCellListener tcl = (TableCellListener) e.getSource();
                if (Pattern.matches(Utils.fpRegex, compMafTable.getValueAt(0, tcl.getColumn()).toString())) {
                    if (Pattern.matches(Utils.fpRegex, origMafTable.getValueAt(1, tcl.getColumn()).toString())) {
                        double corr = Double.valueOf(compMafTable.getValueAt(0, tcl.getColumn()).toString()) / 100.0 + 1.0;
                        newMafTable.setValueAt(Double.valueOf(origMafTable.getValueAt(1, tcl.getColumn()).toString()) * corr, 1, tcl.getColumn());
                    }
                } else
                    compMafTable.setValueAt(tcl.getOldValue(), 0, tcl.getColumn());
            }
        };
        setCompMafCellListener(new TableCellListener(compMafTable, action));
        // CHART
        JFreeChart chart = ChartFactory.createXYLineChart(null, null, null, null, PlotOrientation.VERTICAL, false, true, false);
        chart.setBorderVisible(true);
        chartPanel = new ChartPanel(chart, true, true, true, true, true);
        chartPanel.setFocusable(true);
        chartPanel.setAutoscrolls(true);
        GridBagConstraints gbl_chartPanel = new GridBagConstraints();
        gbl_chartPanel.anchor = GridBagConstraints.PAGE_START;
        gbl_chartPanel.fill = GridBagConstraints.BOTH;
        gbl_chartPanel.insets = new Insets(1, 1, 1, 1);
        gbl_chartPanel.weightx = 1.0;
        gbl_chartPanel.weighty = 1.0;
        gbl_chartPanel.gridx = 0;
        gbl_chartPanel.gridy = 5;
        gbl_chartPanel.gridheight = 1;
        gbl_chartPanel.gridwidth = 3;
        dataPanel.add(chartPanel, gbl_chartPanel);
        XYSplineRenderer lineRenderer = new XYSplineRenderer(3);
        lineRenderer.setUseFillPaint(true);
        lineRenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, new DecimalFormat("0.00"), new DecimalFormat("0.00")));
        Stroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f, null, 0.0f);
        lineRenderer.setSeriesStroke(0, stroke);
        lineRenderer.setSeriesStroke(1, stroke);
        lineRenderer.setSeriesPaint(0, new Color(201, 0, 0));
        lineRenderer.setSeriesPaint(1, new Color(0, 0, 255));
        lineRenderer.setSeriesShape(0, ShapeUtilities.createDiamond((float) 2.5));
        lineRenderer.setSeriesShape(1, ShapeUtilities.createDownTriangle((float) 2.5));
        lineRenderer.setLegendItemLabelGenerator(new StandardXYSeriesLabelGenerator() {

            private static final long serialVersionUID = -4045338273187150888L;

            public String generateLabel(XYDataset dataset, int series) {
                XYSeries xys = ((XYSeriesCollection) dataset).getSeries(series);
                return xys.getDescription();
            }
        });
        NumberAxis mafvDomain = new NumberAxis(XAxisName);
        mafvDomain.setAutoRangeIncludesZero(false);
        mafvDomain.setAutoRange(true);
        mafvDomain.setAutoRangeStickyZero(false);
        NumberAxis mafgsRange = new NumberAxis(YAxisName);
        mafgsRange.setAutoRangeIncludesZero(false);
        mafgsRange.setAutoRange(true);
        mafgsRange.setAutoRangeStickyZero(false);
        XYSeriesCollection lineDataset = new XYSeriesCollection();
        origMafData.setDescription(origMaf);
        newMafData.setDescription(newMaf);
        lineDataset.addSeries(origMafData);
        lineDataset.addSeries(newMafData);
        XYPlot plot = chart.getXYPlot();
        plot.setRangePannable(true);
        plot.setDomainPannable(true);
        plot.setDomainGridlinePaint(Color.DARK_GRAY);
        plot.setRangeGridlinePaint(Color.DARK_GRAY);
        plot.setBackgroundPaint(new Color(224, 224, 224));
        plot.setDataset(0, lineDataset);
        plot.setRenderer(0, lineRenderer);
        plot.setDomainAxis(0, mafvDomain);
        plot.setRangeAxis(0, mafgsRange);
        plot.mapDatasetToDomainAxis(0, 0);
        plot.mapDatasetToRangeAxis(0, 0);
        LegendTitle legend = new LegendTitle(plot.getRenderer());
        legend.setItemFont(new Font("Arial", 0, 10));
        legend.setPosition(RectangleEdge.TOP);
        chart.addLegend(legend);
        chartPanel.addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent e) {
                chartPanel.requestFocusInWindow();
            }
        });
        chartPanel.addKeyListener(new KeyListener() {

            public void keyPressed(KeyEvent e) {
                if (!chartPanel.hasFocus())
                    return;
                int keyCode = e.getKeyCode();
                if (keyCode < KeyEvent.VK_LEFT || keyCode > KeyEvent.VK_DOWN)
                    return;
                ValueAxis axis = null;
                if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_RIGHT)
                    axis = ((XYPlot) chartPanel.getChart().getXYPlot()).getDomainAxis();
                else
                    axis = ((XYPlot) chartPanel.getChart().getXYPlot()).getRangeAxis();
                if (axis != null) {
                    double delta = (axis.getUpperBound() - axis.getLowerBound()) / 100.0;
                    if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_DOWN)
                        axis.setRange(axis.getLowerBound() - delta, axis.getUpperBound() - delta);
                    else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_RIGHT)
                        axis.setRange(axis.getLowerBound() + delta, axis.getUpperBound() + delta);
                }
            }

            public void keyReleased(KeyEvent arg0) {
            }

            public void keyTyped(KeyEvent arg0) {
            }
        });
    } catch (Exception e) {
        logger.error(e);
    }
}
Also used : BasicStroke(java.awt.BasicStroke) XYSeries(org.jfree.data.xy.XYSeries) ImageIcon(javax.swing.ImageIcon) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) AbstractAction(javax.swing.AbstractAction) Action(javax.swing.Action) Insets(java.awt.Insets) ChartPanel(org.jfree.chart.ChartPanel) NumberAxis(org.jfree.chart.axis.NumberAxis) GridBagLayout(java.awt.GridBagLayout) XYSplineRenderer(org.jfree.chart.renderer.xy.XYSplineRenderer) TableModelEvent(javax.swing.event.TableModelEvent) ActionEvent(java.awt.event.ActionEvent) LineBorder(javax.swing.border.LineBorder) DefaultTableModel(javax.swing.table.DefaultTableModel) DecimalFormat(java.text.DecimalFormat) StandardXYSeriesLabelGenerator(org.jfree.chart.labels.StandardXYSeriesLabelGenerator) WindowAdapter(java.awt.event.WindowAdapter) LegendTitle(org.jfree.chart.title.LegendTitle) Font(java.awt.Font) KeyEvent(java.awt.event.KeyEvent) StandardXYToolTipGenerator(org.jfree.chart.labels.StandardXYToolTipGenerator) ValueAxis(org.jfree.chart.axis.ValueAxis) AbstractAction(javax.swing.AbstractAction) JScrollPane(javax.swing.JScrollPane) BasicStroke(java.awt.BasicStroke) Stroke(java.awt.Stroke) MouseEvent(java.awt.event.MouseEvent) Color(java.awt.Color) MouseAdapter(java.awt.event.MouseAdapter) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) Point(java.awt.Point) JFreeChart(org.jfree.chart.JFreeChart) XYPlot(org.jfree.chart.plot.XYPlot) WindowEvent(java.awt.event.WindowEvent) JTable(javax.swing.JTable) TableModelListener(javax.swing.event.TableModelListener) XYDataset(org.jfree.data.xy.XYDataset) KeyListener(java.awt.event.KeyListener) XYSeriesCollection(org.jfree.data.xy.XYSeriesCollection)

Example 57 with KeyListener

use of java.awt.event.KeyListener in project mafscaling by vimsh.

the class LogView method createWotChart.

protected void createWotChart() {
    wotMarker = new XYDomainMutilineAnnotation();
    wotMarker.setDefaultPaint(Color.WHITE);
    JFreeChart chart = ChartFactory.createXYLineChart(null, null, null, null, PlotOrientation.VERTICAL, false, true, false);
    wotChartPanel = new ChartPanel(chart, true, true, true, true, true);
    wotChartPanel.setFocusable(true);
    wotChartPanel.setAutoscrolls(true);
    chart.setBackgroundPaint(chartColor);
    wotPlot = chart.getXYPlot();
    wotPlot.setRangePannable(true);
    wotPlot.setDomainPannable(true);
    wotPlot.setDomainGridlinePaint(Color.LIGHT_GRAY);
    wotPlot.setRangeGridlinePaint(Color.LIGHT_GRAY);
    wotPlot.setBackgroundPaint(chartBgColor);
    wotPlot.addAnnotation(wotMarker);
    wotPlot.setRangeAxis(null);
    NumberAxis xAxis = new NumberAxis();
    xAxis.setAutoRangeIncludesZero(false);
    xAxis.setTickLabelPaint(Color.WHITE);
    xAxis.setLabelPaint(Color.LIGHT_GRAY);
    wotPlot.setDomainAxis(0, xAxis);
    wotPlot.setDataset(0, new XYSeriesCollection());
    wotPlot.mapDatasetToDomainAxis(0, 0);
    wotPlot.mapDatasetToRangeAxis(0, 0);
    LegendTitle legend = new LegendTitle(wotPlot);
    legend.setItemFont(new Font("Arial", 0, 10));
    legend.setPosition(RectangleEdge.TOP);
    legend.setItemPaint(Color.WHITE);
    chart.addLegend(legend);
    wotChartPanel.addChartMouseListener(new ChartMouseListener() {

        @Override
        public void chartMouseMoved(ChartMouseEvent event) {
            try {
                wotPlot.clearRangeMarkers();
                wotMarker.clearLabels(false);
                Rectangle2D dataArea = wotChartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
                Point2D p = wotChartPanel.translateScreenToJava2D(event.getTrigger().getPoint());
                double x = wotPlot.getDomainAxis().java2DToValue(p.getX(), dataArea, wotPlot.getDomainAxisEdge());
                XYSeriesCollection dataset;
                XYSeries series;
                for (int i = 0; i < wotPlot.getDatasetCount(); ++i) {
                    dataset = (XYSeriesCollection) wotPlot.getDataset(i);
                    if (dataset != null && dataset.getSeriesCount() > 0) {
                        for (int j = 0; j < dataset.getSeriesCount(); ++j) {
                            series = dataset.getSeries(j);
                            int idx = 0;
                            double closest;
                            double pclosest = Math.abs(series.getX(0).doubleValue() - x);
                            for (int k = 1; k < series.getItemCount(); ++k) {
                                closest = Math.abs(series.getX(k).doubleValue() - x);
                                if (closest < pclosest) {
                                    idx = k;
                                    pclosest = closest;
                                }
                            }
                            double x0 = series.getX(0).doubleValue();
                            double x1 = series.getX(series.getItemCount() - 1).doubleValue();
                            double xerr = x0 * 0.01;
                            if (x >= (x0 - xerr) && x <= (x1 + xerr))
                                wotMarker.addLabel(series.getDescription() + ": ", wotPlot.getRenderer(i).getSeriesPaint(j), series.getY(idx).toString(), false);
                        }
                    }
                }
                if (wotMarker.count() > 0) {
                    boolean isLeft = (p.getX() < (dataArea.getMaxX() - dataArea.getMinX()) / 2) ? true : false;
                    if (isLeft) {
                        wotMarker.setLabelAnchor(RectangleAnchor.TOP_RIGHT);
                        wotMarker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
                    } else {
                        wotMarker.setLabelAnchor(RectangleAnchor.TOP_LEFT);
                        wotMarker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
                    }
                    wotMarker.setValue(x);
                }
                wotChartPanel.repaint();
            } catch (Exception ex) {
                ex.printStackTrace();
                logger.error(ex);
            }
        }

        @Override
        public void chartMouseClicked(ChartMouseEvent event) {
            wotChartPanel.requestFocusInWindow();
            ChartEntity entity = event.getEntity();
            if (entity == null || !(entity instanceof LegendItemEntity))
                return;
            LegendItemEntity itemEntity = (LegendItemEntity) entity;
            int seriesIndex = ((XYDataset) itemEntity.getDataset()).indexOf(itemEntity.getSeriesKey());
            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) wotChartPanel.getChart().getXYPlot().getRenderer();
            Paint p = renderer.getSeriesPaint(seriesIndex);
            colorChooser.setColor((Color) p);
            final ColorPreviewPanel preview = new ColorPreviewPanel(colorChooser);
            colorChooser.setPreviewPanel(preview);
            AbstractColorChooserPanel[] panels = colorChooser.getChooserPanels();
            for (AbstractColorChooserPanel accp : panels) {
                if (!accp.getDisplayName().equals("Swatches")) {
                    colorChooser.removeChooserPanel(accp);
                }
            }
            ColorSelectionModel model = colorChooser.getSelectionModel();
            model.addChangeListener(new ChangeListener() {

                @Override
                public void stateChanged(ChangeEvent event) {
                    ColorSelectionModel model = (ColorSelectionModel) event.getSource();
                    preview.setColor(model.getSelectedColor());
                }
            });
            if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(null, colorChooser, "Select new color for " + itemEntity.getSeriesKey(), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE))
                renderer.setSeriesPaint(seriesIndex, colorChooser.getColor());
        }
    });
    wotChartPanel.addKeyListener(new KeyListener() {

        public void keyPressed(KeyEvent e) {
            if (!wotChartPanel.hasFocus())
                return;
            int keyCode = e.getKeyCode();
            if (keyCode < KeyEvent.VK_LEFT || keyCode > KeyEvent.VK_DOWN)
                return;
            ValueAxis axis = null;
            if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_RIGHT)
                axis = ((XYPlot) wotChartPanel.getChart().getXYPlot()).getDomainAxis();
            else
                axis = ((XYPlot) wotChartPanel.getChart().getXYPlot()).getRangeAxis();
            if (axis != null) {
                double delta = (axis.getUpperBound() - axis.getLowerBound()) / 100.0;
                if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_DOWN)
                    axis.setRange(axis.getLowerBound() - delta, axis.getUpperBound() - delta);
                else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_RIGHT)
                    axis.setRange(axis.getLowerBound() + delta, axis.getUpperBound() + delta);
            }
        }

        public void keyReleased(KeyEvent arg0) {
        }

        public void keyTyped(KeyEvent arg0) {
        }
    });
}
Also used : XYSeries(org.jfree.data.xy.XYSeries) ChartPanel(org.jfree.chart.ChartPanel) NumberAxis(org.jfree.chart.axis.NumberAxis) LegendTitle(org.jfree.chart.title.LegendTitle) ChartMouseEvent(org.jfree.chart.ChartMouseEvent) Font(java.awt.Font) KeyEvent(java.awt.event.KeyEvent) AbstractColorChooserPanel(javax.swing.colorchooser.AbstractColorChooserPanel) Point2D(java.awt.geom.Point2D) ValueAxis(org.jfree.chart.axis.ValueAxis) ChangeListener(javax.swing.event.ChangeListener) ColorSelectionModel(javax.swing.colorchooser.ColorSelectionModel) XYLineAndShapeRenderer(org.jfree.chart.renderer.xy.XYLineAndShapeRenderer) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) LegendItemEntity(org.jfree.chart.entity.LegendItemEntity) Paint(java.awt.Paint) JFreeChart(org.jfree.chart.JFreeChart) IOException(java.io.IOException) ChartMouseListener(org.jfree.chart.ChartMouseListener) ChangeEvent(javax.swing.event.ChangeEvent) KeyListener(java.awt.event.KeyListener) ChartEntity(org.jfree.chart.entity.ChartEntity) XYSeriesCollection(org.jfree.data.xy.XYSeriesCollection)

Example 58 with KeyListener

use of java.awt.event.KeyListener in project triplea by triplea-game.

the class TripleAFrame method getArrowKeyListener.

private KeyListener getArrowKeyListener() {
    return new KeyListener() {

        @Override
        public void keyPressed(final KeyEvent e) {
            isCtrlPressed = e.isControlDown();
            // scroll map according to wasd/arrowkeys
            final int diffPixel = computeScrollSpeed();
            final int x = mapPanel.getXOffset();
            final int y = mapPanel.getYOffset();
            final int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_RIGHT) {
                getMapPanel().setTopLeft(x + diffPixel, y);
            } else if (keyCode == KeyEvent.VK_LEFT) {
                getMapPanel().setTopLeft(x - diffPixel, y);
            } else if (keyCode == KeyEvent.VK_DOWN) {
                getMapPanel().setTopLeft(x, y + diffPixel);
            } else if (keyCode == KeyEvent.VK_UP) {
                getMapPanel().setTopLeft(x, y - diffPixel);
            }
            // I for info
            if (keyCode == KeyEvent.VK_I || keyCode == KeyEvent.VK_V) {
                String unitInfo = "";
                if (unitsBeingMousedOver != null && !unitsBeingMousedOver.isEmpty()) {
                    final Unit unit = unitsBeingMousedOver.get(0);
                    final UnitAttachment ua = UnitAttachment.get(unit.getType());
                    if (ua != null) {
                        unitInfo = "<b>Unit:</b><br>" + unit.getType().getName() + ": " + ua.toStringShortAndOnlyImportantDifferences(unit.getOwner(), true, false);
                    }
                }
                String terrInfo = "";
                if (territoryLastEntered != null) {
                    final TerritoryAttachment ta = TerritoryAttachment.get(territoryLastEntered);
                    if (ta != null) {
                        terrInfo = "<b>Territory:</b><br>" + ta.toStringForInfo(true, true) + "<br>";
                    } else {
                        terrInfo = "<b>Territory:</b><br>" + territoryLastEntered.getName() + "<br>Water Territory";
                    }
                }
                String tipText = unitInfo;
                if (unitInfo.length() > 0 && terrInfo.length() > 0) {
                    tipText = tipText + "<br><br><br><br><br>";
                }
                tipText = tipText + terrInfo;
                if (tipText.length() > 0) {
                    final Point currentPoint = MouseInfo.getPointerInfo().getLocation();
                    final PopupFactory popupFactory = PopupFactory.getSharedInstance();
                    final JToolTip info = new JToolTip();
                    info.setTipText("<html>" + tipText + "</html>");
                    final Popup popup = popupFactory.getPopup(mapPanel, info, currentPoint.x, currentPoint.y);
                    popup.show();
                    new Thread(() -> {
                        Interruptibles.sleep(5000);
                        popup.hide();
                    }, "popup waiter").start();
                }
            }
        }

        @Override
        public void keyTyped(final KeyEvent e) {
        }

        @Override
        public void keyReleased(final KeyEvent e) {
            isCtrlPressed = e.isControlDown();
        }
    };
}
Also used : KeyEvent(java.awt.event.KeyEvent) UnitAttachment(games.strategy.triplea.attachments.UnitAttachment) PopupFactory(javax.swing.PopupFactory) JToolTip(javax.swing.JToolTip) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) Popup(javax.swing.Popup) KeyListener(java.awt.event.KeyListener) Point(java.awt.Point) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit) Point(java.awt.Point)

Example 59 with KeyListener

use of java.awt.event.KeyListener in project jmulticard by ctt-gob-es.

the class CustomDialogSmartcard method createMainButtonsPanel.

/**
 * Panel que contiene los botones principales de las alerta.
 */
void createMainButtonsPanel() {
    this.buttonsPanel = new JPanel(new GridBagLayout());
    // Restricciones para el panel de botones
    final GridBagConstraints consButtons = new GridBagConstraints();
    consButtons.fill = GridBagConstraints.NONE;
    consButtons.gridx = 0;
    consButtons.gridy = 0;
    // right padding
    consButtons.insets = new Insets(0, 10, 0, 10);
    consButtons.anchor = GridBagConstraints.CENTER;
    // OK button
    final JPanel okPanel = new JPanel();
    // $NON-NLS-1$
    CustomDialogSmartcard.okButton = getButton(Messages.getString("PrincipalGUI.aceptar"), KeyEvent.VK_A);
    okButton.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(final KeyEvent arg0) {
        /* No necesario */
        }

        @Override
        public void keyReleased(final KeyEvent arg0) {
        /* No necesario */
        }

        @Override
        public void keyPressed(final KeyEvent ke) {
            if (10 == ke.getKeyCode()) {
                getOkButton().doClick();
            }
        }
    });
    okPanel.add(CustomDialogSmartcard.okButton);
    this.buttonsPanel.add(okPanel, consButtons);
    CustomDialogSmartcard.okButton.addActionListener(this);
}
Also used : KeyEvent(java.awt.event.KeyEvent) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) KeyListener(java.awt.event.KeyListener)

Example 60 with KeyListener

use of java.awt.event.KeyListener in project jmulticard by ctt-gob-es.

the class CustomDialogSmartcard method createAccessibilityButtonsPanel.

/**
 * Se crea el panel de botones de accesibilidad.
 */
private void createAccessibilityButtonsPanel() {
    this.accessibilityButtonsPanel = new JPanel(new GridBagLayout());
    // Para el tooltip
    final JWindow tip = new JWindow();
    final JLabel tipText = new JLabel();
    // Panel que va a contener los botones de accesibilidad
    final JPanel panel = new JPanel(new GridBagLayout());
    // Restricciones para los botones
    final GridBagConstraints consButtons = new GridBagConstraints();
    consButtons.fill = GridBagConstraints.BOTH;
    consButtons.gridx = 0;
    consButtons.gridy = 0;
    consButtons.weightx = 1.0;
    consButtons.weighty = 1.0;
    // right padding
    consButtons.insets = new Insets(0, 0, 0, 0);
    // Restore button
    final JPanel restorePanel = new JPanel();
    // $NON-NLS-1$
    final ImageIcon imageIconRestore = new ImageIcon(CustomDialogSmartcard.class.getResource("/images/restore.png"));
    this.restoreButton = new JButton(imageIconRestore);
    this.restoreButton.setMnemonic(KeyEvent.VK_R);
    // $NON-NLS-1$
    this.restoreButton.setToolTipText(Messages.getString("Wizard.restaurar.description"));
    this.restoreButton.getAccessibleContext().setAccessibleName(this.restoreButton.getToolTipText());
    this.restoreButton.addFocusListener(new FocusListener() {

        /**
         * Evento que se produce cuando el componente pierde el foco.
         */
        @Override
        public void focusLost(final FocusEvent e) {
            Utils.showToolTip(false, tip, CustomDialogSmartcard.this.getRestoreButton(), tipText);
        }

        /**
         * Evento que se produce cuando el componente tiene el foco.
         */
        @Override
        public void focusGained(final FocusEvent e) {
            Utils.showToolTip(true, tip, CustomDialogSmartcard.this.getRestoreButton(), tipText);
        }
    });
    final Dimension dimension = new Dimension(20, 20);
    this.restoreButton.setPreferredSize(dimension);
    this.restoreButton.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(final KeyEvent arg0) {
        /* No necesario */
        }

        @Override
        public void keyReleased(final KeyEvent arg0) {
        /* No necesario */
        }

        @Override
        public void keyPressed(final KeyEvent ke) {
            if (10 == ke.getKeyCode()) {
                getRestoreButton().doClick();
            }
        }
    });
    // $NON-NLS-1$
    this.restoreButton.setName("restaurar");
    restorePanel.add(this.restoreButton);
    this.restoreButton.addActionListener(new ActionListener() {

        /**
         * Accion del boton.
         */
        @Override
        public void actionPerformed(final ActionEvent e) {
            restaurarActionPerformed();
        }
    });
    Utils.remarcar(this.restoreButton);
    panel.add(restorePanel, consButtons);
    consButtons.gridx = 1;
    // right padding
    consButtons.insets = new Insets(0, 0, 0, 0);
    // Maximize button
    final JPanel maximizePanel = new JPanel();
    // $NON-NLS-1$
    final ImageIcon imageIconMaximize = new ImageIcon(CustomDialogSmartcard.class.getResource("/images/maximize.png"));
    this.maximizeButton = new JButton(imageIconMaximize);
    this.maximizeButton.setMnemonic(KeyEvent.VK_M);
    // $NON-NLS-1$
    this.maximizeButton.setToolTipText(Messages.getString("Wizard.maximizar.description"));
    this.maximizeButton.getAccessibleContext().setAccessibleName(this.maximizeButton.getToolTipText());
    this.maximizeButton.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(final KeyEvent arg0) {
        /* No necesario */
        }

        @Override
        public void keyReleased(final KeyEvent arg0) {
        /* No necesario */
        }

        @Override
        public void keyPressed(final KeyEvent ke) {
            if (10 == ke.getKeyCode()) {
                getMaximizeButton().doClick();
            }
        }
    });
    // $NON-NLS-1$
    this.maximizeButton.setName("maximizar");
    // Se asigna una dimension por defecto
    this.maximizeButton.setPreferredSize(dimension);
    Utils.remarcar(this.maximizeButton);
    maximizePanel.add(this.maximizeButton);
    this.maximizeButton.addFocusListener(new FocusListener() {

        /**
         * Evento que se produce cuando el componente pierde el foco.
         */
        @Override
        public void focusLost(final FocusEvent e) {
            Utils.showToolTip(false, tip, CustomDialogSmartcard.this.getMaximizeButton(), tipText);
        }

        /**
         * Evento que se produce cuando el componente tiene el foco.
         */
        @Override
        public void focusGained(final FocusEvent e) {
            Utils.showToolTip(true, tip, CustomDialogSmartcard.this.getMaximizeButton(), tipText);
        }
    });
    this.maximizeButton.addActionListener(new ActionListener() {

        /**
         * Accion del boton.
         */
        @Override
        public void actionPerformed(final ActionEvent e) {
            maximizarActionPerformed();
        }
    });
    panel.add(maximizePanel, consButtons);
    // Se anade al panel general
    // Restricciones para el panel de botones
    final GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.NONE;
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.insets = new Insets(0, 0, 0, 0);
    c.anchor = GridBagConstraints.SOUTH;
    this.accessibilityButtonsPanel.add(panel, c);
    // Habilitado/Deshabilitado de botones restaurar/maximizar
    if (GeneralConfig.isMaximized()) {
        // Se deshabilita el boton de maximizado
        this.maximizeButton.setEnabled(false);
        // Se habilita el boton de restaurar
        this.restoreButton.setEnabled(true);
    } else {
        // Se habilita el boton de maximizado
        this.maximizeButton.setEnabled(true);
        // Se deshabilita el boton de restaurar
        this.restoreButton.setEnabled(false);
    }
}
Also used : JPanel(javax.swing.JPanel) ImageIcon(javax.swing.ImageIcon) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) ActionEvent(java.awt.event.ActionEvent) JWindow(javax.swing.JWindow) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) FocusEvent(java.awt.event.FocusEvent) KeyEvent(java.awt.event.KeyEvent) ActionListener(java.awt.event.ActionListener) KeyListener(java.awt.event.KeyListener) FocusListener(java.awt.event.FocusListener)

Aggregations

KeyListener (java.awt.event.KeyListener)64 KeyEvent (java.awt.event.KeyEvent)53 JPanel (javax.swing.JPanel)20 ActionEvent (java.awt.event.ActionEvent)18 ActionListener (java.awt.event.ActionListener)17 GridBagConstraints (java.awt.GridBagConstraints)15 JLabel (javax.swing.JLabel)14 JTextField (javax.swing.JTextField)13 GridBagLayout (java.awt.GridBagLayout)12 Insets (java.awt.Insets)11 BorderLayout (java.awt.BorderLayout)8 Dimension (java.awt.Dimension)8 FocusEvent (java.awt.event.FocusEvent)8 FocusListener (java.awt.event.FocusListener)8 JButton (javax.swing.JButton)8 Point (java.awt.Point)7 MouseAdapter (java.awt.event.MouseAdapter)6 MouseEvent (java.awt.event.MouseEvent)6 JComponent (javax.swing.JComponent)5 Color (java.awt.Color)4