mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-12 08:37:25 -05:00
* Misc minor performance improvements * Use more concise loop syntax * Use more concise Collection sort syntax * Use StringUtils pad methods * Optimize some loops and double-map-lookups * Optimize array instantiation There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]). In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation. * Cleanup string concatenation * Use EnumMap where possible * Use FastMath instead of Math * Initialize complex things only once * Reduce method size to aloow more JIT inlining * remove map of short->float * Move field to local variable * Cleanup Map lookup Co-authored-by: Rob Ekl <xwq876@zebra.com>
105 lines
2.6 KiB
Java
105 lines
2.6 KiB
Java
/*******************************************************************************
|
|
* SDR Trunk
|
|
* Copyright (C) 2014 Dennis Sheirer
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
******************************************************************************/
|
|
package io.github.dsheirer.map;
|
|
|
|
import org.apache.commons.math3.util.FastMath;
|
|
import org.jdesktop.swingx.JXMapViewer;
|
|
|
|
import java.awt.*;
|
|
import java.awt.event.MouseAdapter;
|
|
import java.awt.event.MouseEvent;
|
|
import java.awt.geom.Point2D;
|
|
|
|
/**
|
|
* Creates a selection rectangle based on mouse input
|
|
* Also triggers repaint events in the viewer
|
|
* @author Martin Steiger
|
|
*/
|
|
public class SelectionAdapter extends MouseAdapter
|
|
{
|
|
private boolean dragging;
|
|
private JXMapViewer viewer;
|
|
|
|
private Point2D startPos = new Point2D.Double();
|
|
private Point2D endPos = new Point2D.Double();
|
|
|
|
/**
|
|
* @param viewer the jxmapviewer
|
|
*/
|
|
public SelectionAdapter(JXMapViewer viewer)
|
|
{
|
|
this.viewer = viewer;
|
|
}
|
|
|
|
@Override
|
|
public void mousePressed(MouseEvent e)
|
|
{
|
|
if (e.getButton() != MouseEvent.BUTTON3)
|
|
return;
|
|
|
|
startPos.setLocation(e.getX(), e.getY());
|
|
endPos.setLocation(e.getX(), e.getY());
|
|
|
|
dragging = true;
|
|
}
|
|
|
|
@Override
|
|
public void mouseDragged(MouseEvent e)
|
|
{
|
|
if (!dragging)
|
|
return;
|
|
|
|
endPos.setLocation(e.getX(), e.getY());
|
|
|
|
viewer.repaint();
|
|
}
|
|
|
|
@Override
|
|
public void mouseReleased(MouseEvent e)
|
|
{
|
|
if (!dragging)
|
|
return;
|
|
|
|
if (e.getButton() != MouseEvent.BUTTON3)
|
|
return;
|
|
|
|
viewer.repaint();
|
|
|
|
dragging = false;
|
|
}
|
|
|
|
/**
|
|
* @return the selection rectangle
|
|
*/
|
|
public Rectangle getRectangle()
|
|
{
|
|
if (dragging)
|
|
{
|
|
int x1 = (int) FastMath.min(startPos.getX(), endPos.getX());
|
|
int y1 = (int) FastMath.min(startPos.getY(), endPos.getY());
|
|
int x2 = (int) FastMath.max(startPos.getX(), endPos.getX());
|
|
int y2 = (int) FastMath.max(startPos.getY(), endPos.getY());
|
|
|
|
return new Rectangle(x1, y1, x2-x1, y2-y1);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
}
|