algorithms-for-computing-li.../src/main/java/View/MainFrame.java

416 lines
11 KiB
Java

package View;
import Presenter.Presenter;
import View.Panels.LMSPanel;
import View.Panels.MenuPanel;
import View.Panels.OutputPanel;
import View.Panels.RMPanel;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
* Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
*
* @Author: Armin Wolf
* @Email: a_wolf28@uni-muenster.de
* @Date: 28.05.2017.
*/
public class MainFrame extends JFrame {
private Presenter presenter;
private Boolean lmsIsComplete = false;
private Boolean rmIsComplete = false;
private Boolean tsIsComplete = false;
private JButton arrangementButton;
private JButton importButton;
private OutputPanel output;
private MenuPanel menupanel;
private LMSPanel lmsPanel;
private RMPanel rmPanel;
private JPanel pane;
private ArrangementDialog arrangement;
private JDialog arrangementDialog;
private PlotDialog plotLMS;
private PlotDialog plotRM;
private PlotDialog plotTS;
private JDialog progressDialog;
private Container progressContent;
private JProgressBar progressBar;
private JSplitPane splitpane;
private JScrollPane scrollPane;
private JTabbedPane tabbedPane;
public MainFrame() {
initializeComponents();
setDimensions();
setLayouts();
setTitles();
addComponents();
setCloseOperations();
setActionListeners();
disableFunctionality();
this.setVisible(true);
}
/*******************************************************************************************************************
* visualisierungs methoden
******************************************************************************************************************/
public void createArrangement() {
if (arrangement == null) {
arrangement = new ArrangementDialog();
arrangement.setPrameters(getPresenter().calcArrangementLines(),
getPresenter().getModel().getNodes());
arrangement.createArrangement();
SwingUtilities.invokeLater(() -> {
arrangementDialog.add(arrangement, BorderLayout.CENTER);
arrangementDialog.setVisible(true);
});
} else {
arrangementDialog.setVisible(true);
}
}
public void visualizeLMS(double m, double b) {
plotLMS = new PlotDialog();
lmsPanel.setPlotDialog(plotLMS);
createPlot(m,b,plotLMS,lmsPanel);
}
public void visualizeRM(double m, double b) {
plotRM = new PlotDialog();
rmPanel.setPlotDialog(plotRM);
createPlot(m,b,plotRM,rmPanel);
}
public void createPlot(double m, double b, PlotDialog plot, JPanel panel){
SwingUtilities.invokeLater(() -> {
plot.clear();
plot.createPlot(getPresenter().getLines());
plot.addLineToPlot(m, b);
panel.revalidate();
});
}
public void showImportProgress(Integer progress){
progressBar.setValue(progress);
progressBar.setStringPainted(true);
progressDialog.setVisible(true);
}
/*******************************************************************************************************************
* init GUI
******************************************************************************************************************/
private void setTitles() {
this.setTitle("MainFrame");
arrangementDialog.setTitle("Dual Representation - Dialog");
importButton.setText("Import");
arrangementButton.setText("Dualraum");
}
private void setupTabbedPane() {
tabbedPane.add("Least Median of Squares", lmsPanel);
tabbedPane.add("Repeated Median", rmPanel);
tabbedPane.add("Theil-Sen", new JPanel());
}
private void addComponents() {
pane.add(arrangementButton);
pane.add(importButton);
setupSplitPane();
setupTabbedPane();
progressContent.add(progressBar, BorderLayout.NORTH);
progressBar.setBorder(BorderFactory.createTitledBorder("Import..."));
this.add(pane, BorderLayout.SOUTH);
this.add(splitpane, BorderLayout.CENTER);
this.add(menupanel, BorderLayout.NORTH);
}
private void setupSplitPane() {
splitpane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
splitpane.setResizeWeight(.5d);
splitpane.setContinuousLayout(true);
splitpane.setLeftComponent(output);
splitpane.setRightComponent(tabbedPane);
}
private void setCloseOperations() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
arrangementDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
progressDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}
private void setDimensions() {
this.setMinimumSize(new Dimension(1900,1000));
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
lmsPanel.setMinimumSize(new Dimension(400, 500));
rmPanel.setMinimumSize(new Dimension(400, 500));
arrangementDialog.setSize(new Dimension(800, 800));
output.setMinimumSize(new Dimension(400, 500));
progressDialog.setSize(300, 100);
}
private void setLayouts() {
this.setLayout(new BorderLayout());
pane.setLayout(new FlowLayout());
}
private void initializeComponents() {
//Panels
pane = new JPanel();
lmsPanel = new LMSPanel();
rmPanel = new RMPanel();
menupanel = new MenuPanel();
//Dialogs
arrangementDialog = new JDialog();
progressDialog = new JDialog();
progressDialog.setLocationRelativeTo(null);
progressContent = progressDialog.getContentPane();
progressBar = new JProgressBar();
//Panes
tabbedPane = new JTabbedPane();
output = new OutputPanel();
splitpane = new JSplitPane();
//Buttons
arrangementButton = new JButton();
importButton = new JButton();
}
private void setActionListeners() {
arrangementButton.addActionListener((ActionEvent e) -> {
Thread t = new Thread(() -> getPresenter().visualizeDualLines());
t.start();
});
lmsPanel.getStartButton().addActionListener((ActionEvent e) -> {
Thread t = new Thread(
() -> this.getPresenter().calculateLMS(lmsPanel.getInput()));
t.start();
});
rmPanel.getStartButton().addActionListener((ActionEvent e) -> {
Thread t = new Thread(
() -> this.getPresenter().calculateRM(rmPanel.getInput()));
t.start();
});
importButton.addActionListener((ActionEvent e) -> {
SwingUtilities.invokeLater(() -> {
File file = null;
JFileChooser chooser = new JFileChooser();
chooser.setPreferredSize(new Dimension(800,700));
chooser.setFileFilter(new FileNameExtensionFilter("Comma-Separated Value", "csv", "text"));
chooser.setMultiSelectionEnabled(false);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
System.out.println ("Datei "+chooser.getSelectedFile()+
" ausgewählt.");
file = chooser.getSelectedFile();
final File input = file;
Thread t = new Thread(() -> this.getPresenter().startImport(input));
t.start();
}
});
});
}
public void enableFunctionality(){
this.getLmsPanel().getStartButton().setEnabled(true);
this.getRmPanel().getStartButton().setEnabled(true);
this.getArrangementButton().setEnabled(true);
}
public void disableFunctionality(){
this.getLmsPanel().getStartButton().setEnabled(false);
this.getRmPanel().getStartButton().setEnabled(false);
this.getArrangementButton().setEnabled(false);
}
/*******************************************************************************************************************
* log Methode
******************************************************************************************************************/
public void log(String s) {
SwingUtilities.invokeLater(() -> output.appendParagraph(s));
}
public void logError(String s) {
SwingUtilities.invokeLater(() -> output.appendParagraphRed(s));
}
public void logSuccess(String s) {
SwingUtilities.invokeLater(() -> output.appendParagraphGreen(s));
}
public void logHeading(String s) {
SwingUtilities.invokeLater(() -> output.appendParagraphWithHeading(s));
}
public void createTable(List<String> heading, List<List<String>> rows) {
SwingUtilities.invokeLater(() -> output.logTable(heading, rows));
}
/*******************************************************************************************************************
* Getter und Setter Methoden
******************************************************************************************************************/
public Boolean getLmsIsComplete() {
return lmsIsComplete;
}
public void setLmsIsComplete(Boolean lmsIsComplete) {
this.lmsIsComplete = lmsIsComplete;
}
public Boolean getRmIsComplete() {
return rmIsComplete;
}
public void setRmIsComplete(Boolean rmIsComplete) {
this.rmIsComplete = rmIsComplete;
}
public Boolean getTsIsComplete() {
return tsIsComplete;
}
public void setTsIsComplete(Boolean tsIsComplete) {
this.tsIsComplete = tsIsComplete;
}
public JButton getArrangementButton() {
return arrangementButton;
}
public void setArrangementButton(JButton arrangementButton) {
this.arrangementButton = arrangementButton;
}
public JButton getImportButton() {
return importButton;
}
public void setImportButton(JButton importButton) {
this.importButton = importButton;
}
public JPanel getPane() {
return pane;
}
public void setPane(JPanel pane) {
this.pane = pane;
}
public JDialog getArrangementDialog() {
return arrangementDialog;
}
public void setArrangementDialog(JDialog arrangementDialog) {
this.arrangementDialog = arrangementDialog;
}
public OutputPanel getOutput() {
return output;
}
public void setOutput(OutputPanel output) {
this.output = output;
}
public MenuPanel getMenupanel() {
return menupanel;
}
public LMSPanel getLmsPanel() {
return lmsPanel;
}
public JSplitPane getSplitpane() {
return splitpane;
}
public void setSplitpane(JSplitPane splitpane) {
this.splitpane = splitpane;
}
public JScrollPane getScrollPane() {
return scrollPane;
}
public void setScrollPane(JScrollPane scrollPane) {
this.scrollPane = scrollPane;
}
public RMPanel getRmPanel() {
return rmPanel;
}
public void setRmPanel(RMPanel rmPanel) {
this.rmPanel = rmPanel;
}
public PlotDialog getPlotLMS() {
return plotLMS;
}
public void setPlotLMS(PlotDialog plotLMS) {
this.plotLMS = plotLMS;
}
public Presenter getPresenter() {
return presenter;
}
public void setPresenter(Presenter presenter) {
this.presenter = presenter;
}
public JDialog getProgressDialog() {
return progressDialog;
}
public void setProgressDialog(JDialog progressDialog) {
this.progressDialog = progressDialog;
}
}