リビジョン 37446fc4
| src/main/java/com/torutk/spectrum/view/SpectrumFileViewController.java | ||
|---|---|---|
|
package com.torutk.spectrum.view;
|
||
|
|
||
|
import javafx.beans.binding.Bindings;
|
||
|
import javafx.event.ActionEvent;
|
||
|
import javafx.fxml.FXML;
|
||
|
import javafx.fxml.Initializable;
|
||
|
import javafx.scene.Cursor;
|
||
|
import javafx.scene.chart.LineChart;
|
||
|
import javafx.scene.chart.NumberAxis;
|
||
|
import javafx.scene.control.Button;
|
||
|
import javafx.scene.control.TextField;
|
||
|
import javafx.stage.DirectoryChooser;
|
||
|
import javafx.stage.FileChooser;
|
||
|
|
||
|
import java.net.URL;
|
||
|
import java.util.ResourceBundle;
|
||
|
import java.util.logging.Logger;
|
||
|
|
||
|
public class SpectrumFileViewController implements Initializable {
|
||
|
private static final Logger logger = Logger.getLogger(SpectrumFileViewController.class.getName());
|
||
|
private FileChooser fileChooser = new FileChooser();
|
||
|
private DirectoryChooser directoryChooer = new DirectoryChooser();
|
||
|
private ResourceBundle resources;
|
||
|
private double chartDragPointX;
|
||
|
private final SpectrumFileViewModel model = SpectrumFileViewModel.INSTANCE;
|
||
|
|
||
|
@FXML private LineChart<Float, Float> chart;
|
||
|
@FXML private NumberAxis xAxis;
|
||
|
@FXML private NumberAxis yAxis;
|
||
|
@FXML private TextField startFrequencyField;
|
||
|
@FXML private TextField stopFrequencyField;
|
||
|
@FXML private TextField referenceLevelField;
|
||
|
@FXML private TextField scaleField;
|
||
|
@FXML private Button updateButton;
|
||
|
|
||
|
@FXML
|
||
|
private void open(ActionEvent ev) {
|
||
| ... | ... | |
|
@Override
|
||
|
public void initialize(URL url, ResourceBundle resourceBundle) {
|
||
|
resources = resourceBundle;
|
||
|
fileChooser.setTitle(resources.getString("spectrum.view.filechooser.title"));
|
||
|
fileChooser.initialDirectoryProperty().bindBidirectional(model.lastOpenDirectoryProperty());
|
||
|
fileChooser.getExtensionFilters().add(
|
||
|
new FileChooser.ExtensionFilter("Spectrum Files", "*.dat")
|
||
|
);
|
||
|
directoryChooer.setTitle(resources.getString("spectrum.view.directorychooser.title"));
|
||
|
directoryChooer.initialDirectoryProperty().bindBidirectional(model.lastOpenDirectoryProperty());
|
||
|
initializeChart();
|
||
|
initializeRightPane();
|
||
|
}
|
||
|
|
||
|
private void initializeChart() {
|
||
|
xAxis.lowerBoundProperty().bind(model.startFrequencyProperty());
|
||
|
xAxis.upperBoundProperty().bind(model.stopFrequencyProperty());
|
||
|
xAxis.tickUnitProperty().bind(Bindings.divide(
|
||
|
Bindings.subtract(model.stopFrequencyProperty(), model.startFrequencyProperty()), 10
|
||
|
));
|
||
|
yAxis.lowerBoundProperty().bind(Bindings.subtract(
|
||
|
model.referenceLevelProperty(), Bindings.multiply(model.scaleProperty(), 10)
|
||
|
));
|
||
|
yAxis.upperBoundProperty().bind(model.referenceLevelProperty());
|
||
|
yAxis.tickUnitProperty().bind(model.scaleProperty());
|
||
|
chart.dataProperty().bind(model.spectrumSeriesProperty());
|
||
|
chart.setOnMousePressed(event -> {
|
||
|
chart.setCursor(Cursor.CLOSED_HAND);
|
||
|
chartDragPointX = event.getX();
|
||
|
});
|
||
|
chart.setOnMouseDragged(event -> {
|
||
|
double mouseMoveX = event.getX() - chartDragPointX;
|
||
|
if (Math.abs(mouseMoveX) < 5) { // decimate number of refresh chart
|
||
|
return;
|
||
|
}
|
||
|
double diff = (model.getStopFrequency() - model.getStartFrequency()) * mouseMoveX / xAxis.getWidth();
|
||
|
diff = Math.floor(diff * 10) / 10;
|
||
|
model.setStartFrequency(model.getStartFrequency() - diff);
|
||
|
model.setStopFrequency(model.getStopFrequency() - diff);
|
||
|
chartDragPointX = event.getX();
|
||
|
refreshSettingFields();
|
||
|
});
|
||
|
chart.setOnMouseReleased(event -> chart.setCursor(Cursor.DEFAULT));
|
||
|
}
|
||
|
|
||
|
private void initializeRightPane() {
|
||
|
|
||
|
}
|
||
|
|
||
|
private void refreshSettingFields() {
|
||
|
startFrequencyField.setText(String.format("%10.4f", model.getStartFrequency()));
|
||
|
stopFrequencyField.setText(String.format("%10.4f", model.getStopFrequency()));
|
||
|
referenceLevelField.setText(String.format("%5.1f", model.getReferenceLevel()));
|
||
|
scaleField.setText(String.format("%4.1f", model.getScale()));
|
||
|
updateButton.setDisable(true);
|
||
|
}
|
||
|
}
|
||
| src/main/java/com/torutk/spectrum/view/SpectrumFileViewModel.java | ||
|---|---|---|
|
package com.torutk.spectrum.view;
|
||
|
|
||
|
import javafx.beans.property.DoubleProperty;
|
||
|
import javafx.beans.property.ObjectProperty;
|
||
|
import javafx.beans.property.SimpleDoubleProperty;
|
||
|
import javafx.beans.property.SimpleObjectProperty;
|
||
|
import javafx.collections.FXCollections;
|
||
|
import javafx.collections.ObservableList;
|
||
|
import javafx.scene.chart.XYChart;
|
||
|
|
||
|
import java.io.File;
|
||
|
import java.util.logging.Logger;
|
||
|
|
||
|
/**
|
||
|
* View Model of Spectrum Viewer Application.
|
||
|
*
|
||
|
* <ul>
|
||
|
* <li>Singleton implementation by enum.</li>
|
||
|
* <li>Holds all data to be displayed.</li>
|
||
|
* <li>Mediate between view and domain data(Model).</li>
|
||
|
* </ul>
|
||
|
*
|
||
|
*/
|
||
|
enum SpectrumFileViewModel {
|
||
|
INSTANCE;
|
||
|
private static final Logger logger = Logger.getLogger(SpectrumFileViewModel.class.getName());
|
||
|
|
||
|
private final DoubleProperty startFrequencyProperty = new SimpleDoubleProperty(950d);
|
||
|
private final DoubleProperty stopFrequencyProperty = new SimpleDoubleProperty(1450d);
|
||
|
private final DoubleProperty referenceLevelProperty = new SimpleDoubleProperty(0d);
|
||
|
private final DoubleProperty scaleProperty = new SimpleDoubleProperty(5d);
|
||
|
private final ObjectProperty<ObservableList<XYChart.Series<Float, Float>>> spectrumSeriesProperty =
|
||
|
new SimpleObjectProperty<>(FXCollections.observableArrayList());
|
||
|
private final ObjectProperty<File> lastOpenDirectoryProperty =
|
||
|
new SimpleObjectProperty<>(new File(System.getProperty("user.dir")));
|
||
|
|
||
|
/**
|
||
|
* @return start frequency of display in MHz.
|
||
|
*/
|
||
|
final double getStartFrequency() {
|
||
|
return startFrequencyProperty.get();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param value start frequency of display in MHz.
|
||
|
*/
|
||
|
final void setStartFrequency(double value) {
|
||
|
startFrequencyProperty.set(value);
|
||
|
}
|
||
|
|
||
|
DoubleProperty startFrequencyProperty() {
|
||
|
return startFrequencyProperty;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return stop frequency of display in MHz.
|
||
|
*/
|
||
|
final double getStopFrequency() {
|
||
|
return stopFrequencyProperty.get();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param value stop frequency of display in MHz.
|
||
|
*/
|
||
|
final void setStopFrequency(double value) {
|
||
|
stopFrequencyProperty.set(value);
|
||
|
}
|
||
|
|
||
|
DoubleProperty stopFrequencyProperty() {
|
||
|
return stopFrequencyProperty;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return reference level(top value of Y-axis) in dBm.
|
||
|
*/
|
||
|
final double getReferenceLevel() {
|
||
|
return referenceLevelProperty.get();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param value reference level in dBm.
|
||
|
*/
|
||
|
final void setReferenceLevel(double value) {
|
||
|
referenceLevelProperty.set(value);
|
||
|
}
|
||
|
|
||
|
DoubleProperty referenceLevelProperty() {
|
||
|
return referenceLevelProperty;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return scale of 1 div(1 of 10 division of Y-axis) in dBm.
|
||
|
*/
|
||
|
final double getScale() {
|
||
|
return scaleProperty.get();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param value scale of 1 div in dBm.
|
||
|
*/
|
||
|
final void setScale(double value) {
|
||
|
scaleProperty.set(value);
|
||
|
}
|
||
|
|
||
|
DoubleProperty scaleProperty() {
|
||
|
return scaleProperty;
|
||
|
}
|
||
|
|
||
|
ObjectProperty<ObservableList<XYChart.Series<Float, Float>>> spectrumSeriesProperty() {
|
||
|
return spectrumSeriesProperty;
|
||
|
}
|
||
|
|
||
|
ObjectProperty<File> lastOpenDirectoryProperty() {
|
||
|
return lastOpenDirectoryProperty;
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
Implement static behavior of UI