basic functionality done
This commit is contained in:
parent
49cc348e13
commit
6031d69c8b
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -1,14 +1,33 @@
|
||||
package de.dhsn.oop;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import de.dhsn.oop.data.TodoList;
|
||||
import de.dhsn.oop.ui.MainWindow;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
//initialize the state empty
|
||||
List<TodoList> listen = new ArrayList<>();
|
||||
// try t ope nthe file and read json data if successful
|
||||
try(JsonReader reader = new JsonReader(new FileReader(new File("./test.json")))) {
|
||||
Type listType = new TypeToken<List<TodoList>>(){}.getType();
|
||||
Gson g = new Gson();
|
||||
List<TodoList> temp = g.fromJson(reader, listType);
|
||||
//insert all data from json int o state list
|
||||
listen.addAll(temp);
|
||||
} catch (FileNotFoundException ignored) {
|
||||
// ignore file not found --> probably first start and no file present
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} ;
|
||||
//open the window with the data
|
||||
MainWindow mw = new MainWindow(listen);
|
||||
}
|
||||
}
|
@ -3,22 +3,16 @@ package de.dhsn.oop.data;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public abstract class TodoItem extends JPanel {
|
||||
public abstract class TodoItem{
|
||||
public boolean done = false;
|
||||
public String title;
|
||||
|
||||
private JLabel t;
|
||||
private JCheckBox cbdone;
|
||||
|
||||
public TodoItem(String t){
|
||||
title = t;
|
||||
setLayout(new BorderLayout());
|
||||
this.t = new JLabel(t);
|
||||
cbdone = new JCheckBox();
|
||||
add(cbdone, BorderLayout.LINE_START);
|
||||
add(this.t, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
setVisible(true);
|
||||
public TodoItem(boolean done, String title) {
|
||||
this.done = done;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
@ -28,4 +22,12 @@ public abstract class TodoItem extends JPanel {
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public boolean isDone() {
|
||||
return done;
|
||||
}
|
||||
|
||||
public void setDone(boolean done) {
|
||||
this.done = done;
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TodoList {
|
||||
List<TodoItem> list;
|
||||
List<TextTodoItem> list;
|
||||
String listTitle;
|
||||
|
||||
public TodoList(String listTitle) {
|
||||
@ -22,11 +22,11 @@ public class TodoList {
|
||||
this.listTitle = listTitle;
|
||||
}
|
||||
|
||||
public void addTodo(TodoItem item){
|
||||
public void addTodo(TextTodoItem item){
|
||||
list.add(item);
|
||||
}
|
||||
|
||||
public List<TodoItem> getList(){
|
||||
public List<TextTodoItem> getList(){
|
||||
return list;
|
||||
}
|
||||
|
||||
|
@ -12,6 +12,10 @@ import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@ -23,6 +27,7 @@ public class MainWindow extends JFrame implements ActionListener, ListSelectionL
|
||||
/**
|
||||
* Main window constructor, takes a (potentially empty) list of TodoList
|
||||
* this list of lists is supposed to keep all application state for easy file save and open operations
|
||||
*
|
||||
* @param lists
|
||||
* @throws HeadlessException
|
||||
*/
|
||||
@ -33,7 +38,7 @@ public class MainWindow extends JFrame implements ActionListener, ListSelectionL
|
||||
cp.removeAll();
|
||||
setTitle("Todo List");
|
||||
setSize(new Dimension(800, 600));
|
||||
setMinimumSize(new Dimension(400, 250));
|
||||
setMinimumSize(new Dimension(200, 150));
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
list = new JList<>();
|
||||
@ -52,6 +57,12 @@ public class MainWindow extends JFrame implements ActionListener, ListSelectionL
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
System.out.println("exit test");
|
||||
//save to file
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File("./test.json")))) {
|
||||
writer.write(lists + "");
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
System.out.println(lists);
|
||||
System.exit(0);
|
||||
}
|
||||
@ -60,6 +71,7 @@ public class MainWindow extends JFrame implements ActionListener, ListSelectionL
|
||||
|
||||
/**
|
||||
* listener function for the add new list button on the main page
|
||||
*
|
||||
* @param e the event to be processed
|
||||
*/
|
||||
@Override
|
||||
@ -70,27 +82,38 @@ public class MainWindow extends JFrame implements ActionListener, ListSelectionL
|
||||
/**
|
||||
* callback function to be called with a title for a new list
|
||||
* this is called from NewListDialog when the user presses the save button
|
||||
*
|
||||
* @param title
|
||||
* @return
|
||||
*/
|
||||
public void nldCallback(String title){
|
||||
boolean result = lists.add(new TodoList(title));
|
||||
|
||||
//overwrite list contents
|
||||
public void nldCallback(String title) {
|
||||
lists.add(new TodoList(title));
|
||||
//overwrite listview contents
|
||||
list.setModel(new TodoListListModel(lists));
|
||||
}
|
||||
|
||||
/**
|
||||
* handle click events on the list of list titles
|
||||
* displays the list in a new window
|
||||
*
|
||||
* @param e the event that characterizes the change.
|
||||
*/
|
||||
@Override
|
||||
public void valueChanged(ListSelectionEvent e) {
|
||||
// two events are emitted - only execute the code once
|
||||
if (e.getValueIsAdjusting()){
|
||||
if (e.getValueIsAdjusting()) {
|
||||
//show new window for the selected list
|
||||
new TodoListView(lists.get(list.getSelectedIndex()));
|
||||
new TodoListView(lists.get(list.getSelectedIndex()), this);
|
||||
}
|
||||
}
|
||||
|
||||
// todo fühlt sich nach nem reudigen hack an
|
||||
public void restartList(TodoList l){
|
||||
new TodoListView(l, this);
|
||||
}
|
||||
|
||||
public void deleteList(TodoList l){
|
||||
lists.remove(l);
|
||||
list.setModel(new TodoListListModel(lists));
|
||||
}
|
||||
}
|
||||
|
35
src/main/java/de/dhsn/oop/ui/NewTodoItemView.java
Normal file
35
src/main/java/de/dhsn/oop/ui/NewTodoItemView.java
Normal file
@ -0,0 +1,35 @@
|
||||
package de.dhsn.oop.ui;
|
||||
|
||||
import de.dhsn.oop.data.TextTodoItem;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class NewTodoItemView extends JFrame implements ActionListener {
|
||||
JTextField input = new JTextField();
|
||||
JButton submit = new JButton("OK");
|
||||
TodoListView view;
|
||||
|
||||
public NewTodoItemView(TodoListView view){
|
||||
this.view = view;
|
||||
this.setMaximumSize(new Dimension(200,70));
|
||||
this.setSize(new Dimension(200,70));
|
||||
setLayout(new BorderLayout());
|
||||
add(new Label("Text: "), BorderLayout.LINE_START);
|
||||
add(input, BorderLayout.CENTER);
|
||||
submit.addActionListener(this);
|
||||
add(submit, BorderLayout.LINE_END);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
//hand data back to parent windwo
|
||||
view.addCallback(new TextTodoItem(input.getText().trim()));
|
||||
//close and gc this window
|
||||
setVisible(false);
|
||||
dispose();
|
||||
}
|
||||
}
|
45
src/main/java/de/dhsn/oop/ui/TodoItemPanel.java
Normal file
45
src/main/java/de/dhsn/oop/ui/TodoItemPanel.java
Normal file
@ -0,0 +1,45 @@
|
||||
package de.dhsn.oop.ui;
|
||||
|
||||
import de.dhsn.oop.data.TodoItem;
|
||||
import de.dhsn.oop.data.TodoList;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class TodoItemPanel extends JPanel implements ActionListener {
|
||||
private TodoItem item;
|
||||
private TodoList list;
|
||||
private TodoListView view;
|
||||
public TodoItemPanel(TodoItem i, TodoList l, TodoListView v){
|
||||
item = i;
|
||||
list = l;
|
||||
view = v;
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
JCheckBox cb = new JCheckBox();
|
||||
cb.setSelected(item.isDone());
|
||||
cb.addActionListener(this);
|
||||
add(cb, BorderLayout.LINE_START);
|
||||
|
||||
JLabel title = new JLabel(item.title);
|
||||
add(title, BorderLayout.CENTER);
|
||||
|
||||
JButton delete = new JButton("X");
|
||||
delete.addActionListener(this::deletePressed);
|
||||
add(delete, BorderLayout.LINE_END);
|
||||
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
item.setDone(((JCheckBox)(e.getSource())).isSelected());
|
||||
}
|
||||
|
||||
public void deletePressed(ActionEvent e) {
|
||||
list.getList().remove(item);
|
||||
view.forceRerender();
|
||||
}
|
||||
}
|
@ -6,46 +6,90 @@ import de.dhsn.oop.data.TodoList;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
|
||||
public class TodoListView extends JFrame {
|
||||
class TodoListItem extends JPanel {
|
||||
public TodoListItem(TodoItem item) {
|
||||
private JLabel title;
|
||||
private JButton addButton;
|
||||
private JButton deleteButton;
|
||||
private TodoList list;
|
||||
private JPanel scrollContent;
|
||||
private JScrollPane scrolPane;
|
||||
MainWindow mainWindow;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
JLabel title;
|
||||
TodoList list;
|
||||
|
||||
public TodoListView(TodoList list) {
|
||||
public TodoListView(TodoList list, MainWindow mw) {
|
||||
super();
|
||||
mainWindow = mw;
|
||||
this.list = list;
|
||||
|
||||
for(int i = 0; i < 32; i++)
|
||||
this.list.addTodo(new TextTodoItem("item " + i));
|
||||
|
||||
setMinimumSize(new Dimension(100, 100));
|
||||
setSize(new Dimension(100, 300));
|
||||
Container cp = getContentPane();
|
||||
setLayout(new BorderLayout());
|
||||
setLayout(new GridBagLayout());
|
||||
GridBagConstraints c = new GridBagConstraints();
|
||||
c.gridx = 0;
|
||||
c.gridy = 0;
|
||||
title = new JLabel(list.getListTitle());
|
||||
cp.add(title, BorderLayout.NORTH);
|
||||
cp.add(title, c);
|
||||
c.gridx++;
|
||||
|
||||
addButton = new JButton("+");
|
||||
addButton.addActionListener(this::addTodoClicked);
|
||||
cp.add(addButton, c);
|
||||
c.gridx++;
|
||||
|
||||
deleteButton = new JButton("\uD83D\uDEAE");
|
||||
deleteButton.addActionListener(this::deleteButtonClicked);
|
||||
cp.add(deleteButton, c);
|
||||
c.gridx = 0;
|
||||
c.gridy++;
|
||||
c.gridwidth = 3;
|
||||
c.weightx = 1;
|
||||
c.weighty = 1;
|
||||
c.fill = GridBagConstraints.BOTH;
|
||||
c.gridheight = 5;
|
||||
|
||||
// scrolling content panel
|
||||
JPanel scrollContent = new JPanel();
|
||||
//not setting a size results in immediate collapsing of the lsit upon resizing the window
|
||||
scrollContent.setMinimumSize(new Dimension(50,50));
|
||||
scrollContent.setLayout(new BoxLayout(scrollContent, BoxLayout.Y_AXIS));
|
||||
scrollContent = new JPanel();
|
||||
|
||||
//add all todoitems in the list to the scrolpane
|
||||
for (TodoItem t : list.getList())
|
||||
scrollContent.add(t);
|
||||
|
||||
JScrollPane scp = new JScrollPane(scrollContent);
|
||||
|
||||
cp.add(scp,BorderLayout.CENTER);
|
||||
buildScrollPanel();
|
||||
|
||||
cp.add(scrolPane, c);
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
public void buildScrollPanel(){
|
||||
scrollContent = new JPanel();
|
||||
scrollContent.removeAll();
|
||||
scrollContent.setMinimumSize(new Dimension(50,50));
|
||||
scrollContent.setLayout(new BoxLayout(scrollContent, BoxLayout.Y_AXIS));
|
||||
for (TodoItem t: list.getList())
|
||||
scrollContent.add(new TodoItemPanel(t, list, this));
|
||||
scrolPane = new JScrollPane(scrollContent);
|
||||
revalidate();
|
||||
repaint();
|
||||
}
|
||||
|
||||
public void forceRerender(){
|
||||
mainWindow.restartList(list);
|
||||
setVisible(false);
|
||||
dispose();
|
||||
}
|
||||
|
||||
public void addTodoClicked(ActionEvent e){
|
||||
new NewTodoItemView(this);
|
||||
}
|
||||
|
||||
public void deleteButtonClicked(ActionEvent e){
|
||||
mainWindow.deleteList(list);
|
||||
forceRerender();
|
||||
}
|
||||
|
||||
public void addCallback(TextTodoItem item){
|
||||
list.addTodo(item);
|
||||
// todo fühlt sich nach nem reudigen hack an
|
||||
forceRerender();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user