package dongfang.mkt.ui.offscreendisplay;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.Formatter;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSlider;
import javax.swing.border.Border;
import dongfang.mkt.frames.OSDDataResponseFrame;
public class VariousInfoPane
extends JPanel implements OSDDataConsumer
{
class PositionsPanel
extends JPanel implements OSDDataConsumer
{
class CurrentPositionPanel
extends JPanel {
JLabel lat
;
JLabel lon
;
void init
() {
lat =
new JLabel();
lon =
new JLabel();
}
int getNumRows
() {
return 2;
}
void layoutSubs
() {
add
(new JLabel("lat"));
add
(lat
);
add
(new JLabel("lon"));
add
(lon
);
}
CurrentPositionPanel
() {
setLayout
(new GridLayout(getNumRows
(),
2));
init
();
layoutSubs
();
}
}
class HomePositionPanel
extends CurrentPositionPanel
{
JLabel home
;
void init
() {
super.
init();
home =
new JLabel();
}
int getNumRows
() {
return 3;
}
void layoutSubs
() {
super.
layoutSubs();
add
(new JLabel("home"));
add
(home
);
}
}
CurrentPositionPanel currentPosition =
new CurrentPositionPanel
();
HomePositionPanel homePosition =
new HomePositionPanel
();
public PositionsPanel
() {
Border b =
BorderFactory.
createTitledBorder(
BorderFactory.
createEtchedBorder(),
"Curr pos");
currentPosition.
setBorder(b
);
b =
BorderFactory.
createTitledBorder(
BorderFactory.
createEtchedBorder(),
"Home pos");
homePosition.
setBorder(b
);
setLayout
(new GridLayout(2,
1));
add
(currentPosition
);
add
(homePosition
);
}
public void update
(OSDDataResponseFrame data,
long timestamp
) {
if (data ==
null)
return;
String coordinateFormatString =
"%+10.5f";
StringBuilder sb =
new StringBuilder();
Formatter formatter =
new Formatter(sb,
Locale.
US);
formatter.
format(coordinateFormatString, data.
getCurrentPosition()
.
getLatitude());
currentPosition.
lat.
setText(sb.
toString());
sb.
setLength(0);
formatter.
format(coordinateFormatString, data.
getCurrentPosition()
.
getLongitude());
currentPosition.
lon.
setText(sb.
toString());
sb.
setLength(0);
formatter.
format(coordinateFormatString, data.
getCurrentPosition()
.
getLongitude());
currentPosition.
lon.
setText(sb.
toString());
sb.
setLength(0);
formatter.
format(coordinateFormatString, data.
getHomePosition()
.
getLatitude());
homePosition.
lat.
setText(sb.
toString());
sb.
setLength(0);
formatter.
format(coordinateFormatString, data.
getHomePosition()
.
getLongitude());
homePosition.
lon.
setText(sb.
toString());
homePosition.
home.
setText(data.
getCurrentToHome().
toString());
}
}
class HeightsPanel
extends JPanel implements OSDDataConsumer
{
class HeightPanel
extends JPanel implements OSDDataConsumer
{
JLabel baroHeight
;
JLabel GPSHeight
;
JLabel baroVVI
;
JLabel GPSVVI
;
void init
() {
baroHeight =
new JLabel();
GPSHeight =
new JLabel();
baroVVI =
new JLabel();
GPSVVI =
new JLabel();
}
int getNumRows
() {
return 4;
}
void layoutSubs
() {
add
(new JLabel("BaroHeight"));
add
(baroHeight
);
add
(new JLabel("baroVVI"));
add
(baroVVI
);
add
(new JLabel("GPSHeight"));
add
(GPSHeight
);
add
(new JLabel("GPSVVI"));
add
(GPSVVI
);
}
HeightPanel
() {
setLayout
(new GridLayout(getNumRows
(),
2));
}
public void update
(OSDDataResponseFrame data,
long timestamp
) {
if (data ==
null)
return;
String heightFormatString =
"%+10.1f";
StringBuilder sb =
new StringBuilder();
Formatter formatter =
new Formatter(sb,
Locale.
US);
formatter
.
format(heightFormatString, data.
getHeightByPressure());
baroHeight.
setText(sb.
toString());
sb.
setLength(0);
double vviText = data.
getVerticalVelocityByPressure();
formatter.
format(heightFormatString, vviText
);
baroVVI.
setText(sb.
toString());
sb.
setLength(0);
double height = data.
getCurrentPosition().
getAltitude()
- data.
getHomePosition().
getAltitude();
formatter.
format(heightFormatString, height
);
GPSHeight.
setText(sb.
toString());
sb.
setLength(0);
vviText = data.
getVerticalVelocityByGPS();
formatter.
format(heightFormatString, vviText
);
GPSVVI.
setText(sb.
toString());
}
}
HeightPanel height
;
JSlider vvi
;
HeightsPanel
() {
setLayout
(new BorderLayout());
vvi =
new JSlider(-
100,
100,
0);
vvi.
setOrientation(JSlider.
VERTICAL);
vvi.
setEnabled(false);
add
(vvi,
BorderLayout.
WEST);
height =
new HeightPanel
();
height.
init();
height.
layoutSubs();
add
(height,
BorderLayout.
CENTER);
setBorder
(BorderFactory.
createTitledBorder(
BorderFactory.
createEtchedBorder(),
"Altitude"));
}
public void update
(OSDDataResponseFrame data,
long timestamp
) {
if (data ==
null)
return;
height.
update(data, timestamp
);
vvi.
setValue((int) (data.
getVerticalVelocityByPressure() * 50));
}
}
class SpeedHeadingPanel
extends JPanel implements OSDDataConsumer
{
JLabel noseDirection
;
JLabel flightDirection
;
JLabel speed
;
void init
() {
noseDirection =
new JLabel();
flightDirection =
new JLabel();
speed =
new JLabel();
Border b =
BorderFactory.
createTitledBorder(
BorderFactory.
createEtchedBorder(),
"Speed and heading");
setBorder
(b
);
}
int getNumRows
() {
return 3;
}
void layoutSubs
() {
setLayout
(new GridLayout(getNumRows
(),
2));
add
(new JLabel("Nose"));
add
(noseDirection
);
add
(new JLabel("Flight"));
add
(flightDirection
);
add
(new JLabel("Speed"));
add
(speed
);
}
SpeedHeadingPanel
() {
init
();
layoutSubs
();
}
public void update
(OSDDataResponseFrame data,
long timestamp
) {
if (data ==
null)
return;
String speedFormatString =
"%10.1f";
StringBuilder sb =
new StringBuilder();
Formatter formatter =
new Formatter(sb,
Locale.
US);
noseDirection.
setText("" + data.
getCompassHeading());
flightDirection.
setText("" + data.
getDirectionOfFlight());
formatter.
format(speedFormatString, data.
getGroundSpeed());
speed.
setText(sb.
toString());
}
}
class StatusPanel
extends JPanel implements OSDDataConsumer
{
JProgressBar dataAgeProgress
;
JProgressBar battery
;
long dataTime =
0;
void init
() {
dataAgeProgress =
new JProgressBar(0,
5000);
battery =
new JProgressBar(100,
130);
//dataAgeProgress.setEnabled(false);
Border b =
BorderFactory.
createTitledBorder(
BorderFactory.
createEtchedBorder(),
"Status");
setBorder
(b
);
TimerTask dataAgeTask =
new TimerTask() {
@
Override
public void run
() {
long age =
System.
currentTimeMillis() - dataTime
;
if (age
> 5000) age =
5000;
// System.out.println(age);
dataAgeProgress.
setValue((int)age
);
StatusPanel.
this.
repaint();
}
};
Timer t =
new Timer();
t.
schedule(dataAgeTask,
0,
200);
}
int getNumRows
() {
return 2;
}
void layoutSubs
() {
add
(new JLabel("Data age"));
add
(dataAgeProgress
);
add
(new JLabel("Battery"));
add
(battery
);
}
StatusPanel
() {
setLayout
(new GridLayout(getNumRows
(),
2));
init
();
layoutSubs
();
}
public void update
(OSDDataResponseFrame data,
long timestamp
) {
if (data ==
null)
return;
dataTime =
System.
currentTimeMillis();
battery.
setValue(data.
getBatteryVoltage());
}
}
StatusPanel status =
new StatusPanel
();
PositionsPanel positions =
new PositionsPanel
();
SpeedHeadingPanel speedHeading =
new SpeedHeadingPanel
();
HeightsPanel altitudes =
new HeightsPanel
();
public VariousInfoPane
() {
}
public void init
() {
setLayout
(new GridLayout(0,
1));
add
(status
);
add
(positions
);
add
(speedHeading
);
add
(altitudes
);
}
public void update
(OSDDataResponseFrame data,
long timestamp
) {
status.
update(data, timestamp
);
positions.
update(data, timestamp
);
speedHeading.
update(data, timestamp
);
altitudes.
update(data, timestamp
);
}
public void setWidth
(int width
) {
positions.
setSize(width,
(int) positions.
getSize().
getHeight());
altitudes.
setSize(width,
(int) altitudes.
getSize().
getHeight());
}
}