Skip to content

Commit

Permalink
Trying to partially fix #61. See #61 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyat2 committed Nov 18, 2023
1 parent e82819f commit 4a2b507
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 23 deletions.
80 changes: 65 additions & 15 deletions src/org/aavso/tools/vstar/ui/dialog/model/HarmonicInfoDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
package org.aavso.tools.vstar.ui.dialog.model;

import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
Expand Down Expand Up @@ -62,8 +67,6 @@ public class HarmonicInfoDialog extends JDialog implements

private Map<String, Harmonic> harmonicMap;

private JButton dismissButton;

/**
* Constructor.
*
Expand Down Expand Up @@ -96,6 +99,13 @@ public HarmonicInfoDialog(HarmonicSearchResultMessage msg,

getContentPane().add(topPane);
pack();

this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dismiss();
}
});

setLocationRelativeTo(Mediator.getUI().getContentPane());
setVisible(true);
}
Expand Down Expand Up @@ -128,11 +138,16 @@ private JPanel createListPane() {
private JPanel createButtonPane() {
JPanel panel = new JPanel(new FlowLayout());

dismissButton = new JButton("Dismiss");
JButton dismissButton = new JButton("Dismiss");
dismissButton.addActionListener(createDismissButtonListener());
dismissButton.setEnabled(true);
panel.add(dismissButton);

JButton copyButton = new JButton("Copy");
copyButton.addActionListener(createCopyButtonListener());
copyButton.setEnabled(true);
panel.add(copyButton);

this.getRootPane().setDefaultButton(dismissButton);

return panel;
Expand All @@ -147,46 +162,81 @@ public void valueChanged(ListSelectionEvent e) {
String desc = (String) harmonicListModel
.get(selectedModelIndex);
Harmonic harmonic = harmonicMap.get(desc);
plotPane
.setCrossHair(harmonic.getFrequency(),
findNthRangeValueFromFrequency(harmonic
.getFrequency()));
double x = harmonic.getFrequency();
double y = findNthChartRangeValueFromFrequency(x);
// 'y' is not the exact value because the chart resolution is limited.
plotPane.setCrossHair(x, y);
}
}
}

// Return the range value corresponding to the specified frequency.
private Double findNthRangeValueFromFrequency(double frequency) {
private Double findNthChartRangeValueFromFrequency(double frequency) {
Double value = null;

List<Double> freqVals = plotPane.getModel().getDomainValues();
List<Double> rangeVals = plotPane.getModel().getRangeValues();

// We take points from the chart; the number of chart points (typically, 100)
// is less than the number of calculated frequencies among which
// the harmonic had been searched (see PeriodAnalysisDialogBase.findHarmonics()).
// So we cannot directly compare 'frequency' with chart values, and even
// Tolerance() will not help much.
// We assume, however, that the point of interest exists on the chart.
// So, we simply try to find the closest chart point.

int i = 0;
Double minDif = null;
while (i < freqVals.size()) {
if (freqVals.get(i) == frequency) {
double f = freqVals.get(i); // we assume get() returns non-null
double dif = Math.abs(f - frequency);
if (minDif != null) {
if (dif < minDif) {
minDif = dif;
value = rangeVals.get(i);
}
} else {
minDif = dif;
value = rangeVals.get(i);
break;
}
i++;
}

if (value == null) {
throw new IllegalArgumentException("Unknown frequency");
}

return value;
}

private void dismiss() {
setVisible(false);
dispose();
// Restore the plot's cross hair.
plotPane.setCrossHair(startX, startY);
}

// Return a listener for the "Dismiss" button.
private ActionListener createDismissButtonListener() {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
// Restore the plot's cross hair.
plotPane.setCrossHair(startX, startY);
dismiss();
}
};
}

// Return a listener for the "Dismiss" button.
private ActionListener createCopyButtonListener() {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = "";
for (int i = 0; i < harmonicListModel.size(); i++) {
s += harmonicListModel.get(i).toString() + "\n";
}
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(s) , null);
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ public void update(HarmonicSearchResultMessage info) {
if (!Mediator.isMsgForDialog(Mediator.getParentDialog(PeriodAnalysis2DChartPane.this), info))
return;
String id = PeriodAnalysis2DChartPane.this.getChartPaneID();
if (id != null && id.equals("PlotPane0")) {
String currentID = info.getIDstring();
if (currentID != null && currentID.equals(id)) {
new HarmonicInfoDialog(info, pane);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.aavso.tools.vstar.plugin.period.PeriodAnalysisComponentFactory;
import org.aavso.tools.vstar.plugin.period.PeriodAnalysisDialogBase;
import org.aavso.tools.vstar.ui.NamedComponent;
import org.aavso.tools.vstar.ui.dialog.MessageBox;
import org.aavso.tools.vstar.ui.mediator.Mediator;
import org.aavso.tools.vstar.ui.mediator.message.HarmonicSearchResultMessage;
import org.aavso.tools.vstar.ui.mediator.message.PeriodAnalysisSelectionMessage;
Expand All @@ -42,6 +43,7 @@
import org.aavso.tools.vstar.util.period.IPeriodAnalysisAlgorithm;
import org.aavso.tools.vstar.util.period.IPeriodAnalysisDatum;
import org.aavso.tools.vstar.util.period.PeriodAnalysisCoordinateType;
import org.aavso.tools.vstar.util.prefs.NumericPrecisionPrefs;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.DatasetRenderingOrder;

Expand Down Expand Up @@ -69,6 +71,10 @@ public class PeriodAnalysis2DResultDialog extends PeriodAnalysisDialogBase {
private IPeriodAnalysisDatum selectedDataPoint;

private Listener<PeriodAnalysisSelectionMessage> periodAnalysisListener;

private JTabbedPane tabbedPane;

private String findHarmonicsButtonText;

/**
* Constructor
Expand All @@ -95,6 +101,8 @@ public PeriodAnalysis2DResultDialog(String title, String seriesTitle,
assert searchType == PeriodAnalysisCoordinateType.PERIOD
|| searchType == PeriodAnalysisCoordinateType.FREQUENCY;

findHarmonicsButtonText = LocaleProps.get("FIND_HARMONICS_BUTTON");

selectedDataPoint = null;

resultDataMap = algorithm.getResultSeries();
Expand Down Expand Up @@ -147,7 +155,8 @@ public void windowClosing(WindowEvent we) {
}

protected Component createContent() {
return createTabs();
tabbedPane = createTabs();
return tabbedPane;
}

// Return the tabs containing table and plots of frequency vs one of the
Expand Down Expand Up @@ -229,13 +238,25 @@ protected void newPhasePlotButtonAction() {

@Override
protected void findHarmonicsButtonAction() {
String chartID = null;
Component c = tabbedPane.getSelectedComponent();
if (c instanceof PeriodAnalysis2DChartPane) {
chartID = ((PeriodAnalysis2DChartPane)c).getChartPaneID();
}

if (chartID == null) {
MessageBox.showMessageDialog("Find Harmonic", "Please select a tab with a chart");
return;
}

List<Double> data = algorithm.getResultSeries().get(
PeriodAnalysisCoordinateType.FREQUENCY);
List<Harmonic> harmonics = findHarmonics(
selectedDataPoint.getFrequency(), data);
HarmonicSearchResultMessage msg = new HarmonicSearchResultMessage(this,
harmonics, selectedDataPoint);
msg.setName(this.getName());
msg.setIDstring(chartID);
Mediator.getInstance().getHarmonicSearchNotifier().notifyListeners(msg);
}

Expand All @@ -249,6 +270,9 @@ public void update(PeriodAnalysisSelectionMessage info) {
setNewPhasePlotButtonState(true);
setFindHarmonicsButtonState(true);
selectedDataPoint = info.getDataPoint();
// Sometimes the cross-hair does not coincide with the real selection.
// Let's indicate the selected frequency on the button...
findHarmonicsButton.setText(findHarmonicsButtonText + " [" + NumericPrecisionPrefs.formatOther(selectedDataPoint.getFrequency()) + "]");
}

public boolean canBeRemoved() {
Expand Down
11 changes: 5 additions & 6 deletions src/org/aavso/tools/vstar/ui/mediator/Mediator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2363,13 +2363,12 @@ public static JDialog getParentDialog(Component c) {
}

public static String getParentDialogName(Component c) {
while (c != null) {
if (c instanceof JDialog) {
return c.getName();
}
c = c.getParent();
JDialog dlg = getParentDialog(c);
if (dlg != null) {
return dlg.getName();
} else {
return null;
}
return null;
}

public static boolean isMsgForDialog(JDialog dlg, MessageBase msg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class HarmonicSearchResultMessage extends MessageBase {

private List<Harmonic> harmonics;
private IPeriodAnalysisDatum dataPoint;
private String iDstring = null;

/**
* Constructor
Expand Down Expand Up @@ -60,4 +61,12 @@ public List<Harmonic> getHarmonics() {
public IPeriodAnalysisDatum getDataPoint() {
return dataPoint;
}

public void setIDstring(String s) {
iDstring = s;
}

public String getIDstring() {
return iDstring;
}
}

0 comments on commit 4a2b507

Please sign in to comment.