Graph and data storage in the same singleton, it is an error ??

E

Etantonio

I need to create a GUI to show data coming from a satellite receiver
through the usb ( used as a virtual com port with library rxtx)

I already created an architecture like this :

package it.imt.edusat.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class JTabbedTest {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
(new JTabbedTest()).create();
}
});
}

private void create() {
JTabbedPane telemetryGroupsTabs = new JTabbedPane();
telemetryGroupsTabs.addTab("One", new TelemetryGroup());
telemetryGroupsTabs.addTab("Two", new TelemetryGroup());

JFrame f = new JFrame("JTabbedPaneTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(telemetryGroupsTabs);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}

class TelemetryGroup extends JPanel {
public TelemetryGroup() {
this.setLayout(new BorderLayout());
View view = new View();
this.add(new View(), BorderLayout.CENTER);
this.add(new Control(view), BorderLayout.WEST);
}
}

class Control extends JPanel {

private static final Random random = new Random();

public Control(ActionListener listener) {
JButton change = new JButton("Change");
add(change);
change.addActionListener(listener);
}

}

class View extends JPanel implements ActionListener {

private static final Random random = new Random();

public View() {
setPreferredSize(new Dimension(100, 100));
setBackground(new Color(random.nextInt()));
}

public void actionPerformed(ActionEvent e) {
setBackground(new Color(random.nextInt()));
repaint();
}
}




where View ia a Singleton Class, it stores data coming from the serial
and also shows them using JfreeChart, I choose the singleton because I
think it is not necessary to create a graph for any tab of the
JTabbedPane, I could reuse the same graph, it seems to be not so, the
result is that I can see just the graph for the latest created View
and I cannot see it in the other tabs.

DO you think this is an architecture error or what ??

Thanks

Antonio
ww.etantonio.it/en
 
M

Mark Space

Etantonio said:
View view = new View();
this.add(new View(), BorderLayout.CENTER);
this.add(new Control(view), BorderLayout.WEST);

where View ia a Singleton Class, it stores data coming from the serial
DO you think this is an architecture error or what ??

I think a singleton pattern would be a mistake, at least for most types
of programs. Depending on your requirements, a singleton view might be
correct, but if anything changes you'd likely have to refactor that.

However what you did doesn't look at all like a singleton to me. It
looks fine, the correct way to do it. The rest of your code looks good
btw, very nice.
 
E

Etantonio

The class View was an example, the real implementation is the
following singleton class :
Any Idea about where's my mistake ??

Antonio



package it.imt.edusat.telemetry;

import it.imt.edusat.telemetry.enumeration.GenericTelemetryType;
import it.imt.edusat.telemetry.enumeration.ObcTelemetryType;
import it.imt.edusat.telemetry.enumeration.TelemetryGroup;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.EventListener;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

public class TelemetryStorageAndGraph extends JPanel{

private static TelemetryStorageAndGraph istanza;
private static ArrayList<BitSetAndTime> telemetryStorage;

private TimeSeries series;
private ChartPanel chartPanel = null;
TelemetryGroup telemetryGroup;
GenericTelemetryType genericTelemetryType;
private String title = "";

private TelemetryStorageAndGraph() {
}

public static TelemetryStorageAndGraph getInstance()
{
if (istanza == null)
{
istanza = new TelemetryStorageAndGraph();
telemetryStorage = new ArrayList<BitSetAndTime>();
}
return istanza;
}

public ArrayList<BitSetAndTime> getTelemetryStorage() {
return telemetryStorage;
}

public JPanel createTelemetryGraph(Telemetry telemetry){
// creazione del grafico
this.title= telemetry.getTelemetryName();
this.telemetryGroup = telemetry.getTLM_GROUP();
this.genericTelemetryType = telemetry.getSelectedTelemetryType();
this.series = new TimeSeries(genericTelemetryType.toString() ,
Millisecond.class);

final TimeSeriesCollection dataset = new TimeSeriesCollection
(this.series);
final JFreeChart chart = createChart(dataset);
chartPanel = new ChartPanel(chart);
BorderLayout borderLayout = new BorderLayout();
this.setLayout(borderLayout);
this.add(chartPanel);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
return this;
}

public void setTelemetry(BitSet bitSet){
BitSetAndTime bitSetAndTime = new BitSetAndTime(bitSet, new
Millisecond());
telemetryStorage.add(bitSetAndTime);
Object oResult = TelemetryExtractor.getTelemetryValue(bitSet,
this.telemetryGroup, this.genericTelemetryType);
if (oResult instanceof Integer) {
series.addOrUpdate(bitSetAndTime.getMS(), ((Integer)
oResult).intValue());
} else if (oResult instanceof Double){
series.addOrUpdate(bitSetAndTime.getMS(), ((Double)
oResult).doubleValue());
}
}

private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createTimeSeriesChart(
this.title,
"Time",
"Value",
dataset,
true,
true,
false
);
final XYPlot plot = result.getXYPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setAutoRange(true);
axis.setFixedAutoRange(60000.0); // 60 seconds
axis = plot.getRangeAxis();
axis.setRange(0.0, 200.0);
return result;
}

}
 
R

RedGrittyBrick

Etantonio said:
I need to create a GUI to show data coming from a satellite receiver
through the usb ( used as a virtual com port with library rxtx)

I already created an architecture like this :
[...]


where View ia a Singleton Class, it stores data coming from the serial
and also shows them using JfreeChart, I choose the singleton because I
think it is not necessary to create a graph for any tab of the
JTabbedPane, I could reuse the same graph, it seems to be not so, the
result is that I can see just the graph for the latest created View
and I cannot see it in the other tabs.

DO you think this is an architecture error or what ??

Using Swing, in general, you can't add a component to more than one
container.

If you are really showing the same chart data in several chart panels
I'd create separate instances and feed them the same data.

If the charting classes can provide a bitmap then maybe you could re-use
that as a source of display data for multiple views.

Note that one Observable may have several Observers. I'd maybe have a
Model class for the satellite feed, have the Model extend or encapsulate
Observable and have several View classes observe the model.


I don't like showing the same chart in several tabbed panes. Could you
reorganise the GUI so the common chart is shown outside of and next to
the JTabbedPane?
 
E

Etantonio

It is not the same chart,
Radiobuttons on COntrol are different for any tab, and associated at
any of them there is a different graph,
all the graph use the same storage ( ... containing bitset of about
400 bits) in any graph I show data corresponding to some of that bits.

So you suggest me that the main EDUSAT starts the class USBReceiver ,
this one is made observable, in it I've also the storage of alla data
received,
the observer are the four different graph, each in a different tab.

What do you think of this architecture ??

Thanks,

Antonio
www.etantonio.it/Forums/Varie/
 
R

RedGrittyBrick

Etantonio said:
It is not the same chart,
Radiobuttons on COntrol are different for any tab, and associated at
any of them there is a different graph,
all the graph use the same storage ( ... containing bitset of about
400 bits) in any graph I show data corresponding to some of that bits.

So you suggest me that the main EDUSAT starts the class USBReceiver ,
this one is made observable, in it I've also the storage of alla data
received,
the observer are the four different graph, each in a different tab.

What do you think of this architecture ??

I think it is very sensible. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top