Skip to content

Commit

Permalink
Finished Functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
CP02A authored and BuildTools committed Oct 3, 2019
1 parent 96981a8 commit e6907cc
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# CSV-Viewer
A INFI program that reads CSV files
An INFI program that reads CSV files
10 changes: 9 additions & 1 deletion src/com/github/cp02a/csvviewer/control/MainControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ public class MainControl implements ActionListener {
public MainControl(){
model = new MainModel();
view = new MainView(this);
view.setGrid(model.getHeader(), model.getLine(0));
}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Make Buttons Work!
switch(e.getActionCommand()){
case "nex":
view.updateGrid(model.getLine(model.getCurrentLine()+1));
break;
case "prev":
view.updateGrid(model.getLine(model.getCurrentLine()-1));
break;
}
}
}
21 changes: 18 additions & 3 deletions src/com/github/cp02a/csvviewer/model/MainModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@
import java.io.IOException;

public class MainModel {
private String header;
private String[] lines;
private int currentLine;

public MainModel(){
lines = new String[0];
JFileChooser chooser = new JFileChooser();
int rueckgabeWert = chooser.showOpenDialog(null);
if(rueckgabeWert == JFileChooser.APPROVE_OPTION) {
BufferedReader objReader = null;
String strCurrentLine = "";
String strCurrentLine;
try {
String[] temp;
objReader = new BufferedReader(new FileReader(chooser.getSelectedFile()));
header = objReader.readLine();
while ((strCurrentLine = objReader.readLine()) != null) {
temp = lines.clone();
lines = new String[lines.length + 1];
for(int i = 0; i < temp.length; i++)
lines[i] = temp[i];
System.arraycopy(temp, 0, lines, 0, temp.length);
lines[lines.length-1] = strCurrentLine;
}
} catch (IOException e) {
Expand All @@ -37,4 +39,17 @@ public MainModel(){
}
}
}

public String getHeader(){
return header;
}

public String getLine(int i){
currentLine = i;
return lines[i];
}

public int getCurrentLine() {
return currentLine;
}
}
12 changes: 11 additions & 1 deletion src/com/github/cp02a/csvviewer/view/MainView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
import java.awt.event.ActionListener;

public class MainView extends JFrame {
private PanelView jp;

public MainView(ActionListener al){
super("CSV-Viewer");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(50, 50, 300, 165);
setVisible(true);

JPanel jp = new PanelView(al);
jp = new PanelView(al);
add(jp);
}

public void setGrid(String header, String line) {
jp.setGrid(header, line);
}

public void updateGrid(String line) {
jp.updateGrid(line);
}
}
40 changes: 39 additions & 1 deletion src/com/github/cp02a/csvviewer/view/PanelView.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
package com.github.cp02a.csvviewer.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class PanelView extends JPanel {
private JButton prev;
private JButton nex;
private JLabel[] info;

public PanelView(ActionListener al){
// TODO
BorderLayout layout = new BorderLayout();
this.setLayout(layout);
prev = new JButton("Previous");
nex = new JButton("Next");
prev.setActionCommand("prev");
nex.setActionCommand("nex");
prev.addActionListener(al);
nex.addActionListener(al);
this.add(prev, BorderLayout.LINE_START);
this.add(nex, BorderLayout.LINE_END);
}

public void setGrid(String header, String line) {
JPanel jp = new JPanel(new GridLayout(2, header.split(";").length, 0, 0));
for (String str : header.split(";")) {
JLabel jl = new JLabel(str);
jl.setHorizontalAlignment(SwingConstants.CENTER);
jp.add(jl);
}
info = new JLabel[header.split(";").length];
String[] temp = line.split(";");
for(int i = 0; i < info.length; i++){
JLabel jl = new JLabel(temp[i]);
jl.setHorizontalAlignment(SwingConstants.CENTER);
jp.add(jl);
info[i] = jl;
}
this.add(jp, BorderLayout.CENTER);
}

public void updateGrid(String line) {
String[] temp = line.split(";");
for(int i = 0; i < info.length; i++)
info[i].setText(temp[i]);
}
}

0 comments on commit e6907cc

Please sign in to comment.