multiple windows working with state
This commit is contained in:
commit
49cc348e13
38
.gitignore
vendored
Normal file
38
.gitignore
vendored
Normal file
@ -0,0 +1,38 @@
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea/modules.xml
|
||||
.idea/jarRepositories.xml
|
||||
.idea/compiler.xml
|
||||
.idea/libraries/
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### Eclipse ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
3
.idea/.gitignore
vendored
Normal file
3
.idea/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
7
.idea/encodings.xml
Normal file
7
.idea/encodings.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
14
.idea/misc.xml
Normal file
14
.idea/misc.xml
Normal file
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="jbr-21" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
26
pom.xml
Normal file
26
pom.xml
Normal file
@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>de.dhsn.oop</groupId>
|
||||
<artifactId>todo</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.13.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
14
src/main/java/de/dhsn/oop/Main.java
Normal file
14
src/main/java/de/dhsn/oop/Main.java
Normal file
@ -0,0 +1,14 @@
|
||||
package de.dhsn.oop;
|
||||
|
||||
import de.dhsn.oop.data.TodoList;
|
||||
import de.dhsn.oop.ui.MainWindow;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
List<TodoList> listen = new ArrayList<>();
|
||||
MainWindow mw = new MainWindow(listen);
|
||||
}
|
||||
}
|
7
src/main/java/de/dhsn/oop/data/TextTodoItem.java
Normal file
7
src/main/java/de/dhsn/oop/data/TextTodoItem.java
Normal file
@ -0,0 +1,7 @@
|
||||
package de.dhsn.oop.data;
|
||||
|
||||
public class TextTodoItem extends TodoItem{
|
||||
public TextTodoItem(String t) {
|
||||
super(t);
|
||||
}
|
||||
}
|
31
src/main/java/de/dhsn/oop/data/TodoItem.java
Normal file
31
src/main/java/de/dhsn/oop/data/TodoItem.java
Normal file
@ -0,0 +1,31 @@
|
||||
package de.dhsn.oop.data;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public abstract class TodoItem extends JPanel {
|
||||
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 String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
}
|
41
src/main/java/de/dhsn/oop/data/TodoList.java
Normal file
41
src/main/java/de/dhsn/oop/data/TodoList.java
Normal file
@ -0,0 +1,41 @@
|
||||
package de.dhsn.oop.data;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TodoList {
|
||||
List<TodoItem> list;
|
||||
String listTitle;
|
||||
|
||||
public TodoList(String listTitle) {
|
||||
this.listTitle = listTitle;
|
||||
this.list = new ArrayList<>();
|
||||
}
|
||||
|
||||
public String getListTitle() {
|
||||
return listTitle;
|
||||
}
|
||||
|
||||
public void setListTitle(String listTitle) {
|
||||
this.listTitle = listTitle;
|
||||
}
|
||||
|
||||
public void addTodo(TodoItem item){
|
||||
list.add(item);
|
||||
}
|
||||
|
||||
public List<TodoItem> getList(){
|
||||
return list;
|
||||
}
|
||||
|
||||
public int length(){
|
||||
return list.toArray().length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new Gson().toJson(this);
|
||||
}
|
||||
}
|
44
src/main/java/de/dhsn/oop/ui/ListOverviewItem.java
Normal file
44
src/main/java/de/dhsn/oop/ui/ListOverviewItem.java
Normal file
@ -0,0 +1,44 @@
|
||||
package de.dhsn.oop.ui;
|
||||
|
||||
import de.dhsn.oop.data.TodoList;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* Overview List Item to show Todo Lists on the home screen
|
||||
*/
|
||||
public class ListOverviewItem extends JPanel {
|
||||
private JLabel titleText = new JLabel();
|
||||
private JLabel title = new JLabel();
|
||||
private JLabel length = new JLabel();
|
||||
private JLabel lenghtText = new JLabel();
|
||||
|
||||
public ListOverviewItem(TodoList list) {
|
||||
this.setLayout(new GridBagLayout());
|
||||
GridBagConstraints c = new GridBagConstraints();
|
||||
c.gridx = 0;
|
||||
c.gridy = 0;
|
||||
|
||||
c.insets = new Insets(2,2,2,2);
|
||||
|
||||
title.setText(list.getListTitle());
|
||||
title.setFont(title.getFont().deriveFont(Font.BOLD, 15f));
|
||||
titleText.setText("Titel: ");
|
||||
lenghtText.setText("Anz.: ");
|
||||
length.setText(Integer.toString(list.length()));
|
||||
|
||||
this.add(titleText, c);
|
||||
c.gridx++;
|
||||
|
||||
this.add(title, c);
|
||||
c.gridx++;
|
||||
|
||||
this.add(lenghtText, c);
|
||||
c.gridx++;
|
||||
|
||||
this.add(length, c);
|
||||
|
||||
this.setVisible(true);
|
||||
}
|
||||
}
|
25
src/main/java/de/dhsn/oop/ui/ListsOverview.java
Normal file
25
src/main/java/de/dhsn/oop/ui/ListsOverview.java
Normal file
@ -0,0 +1,25 @@
|
||||
package de.dhsn.oop.ui;
|
||||
|
||||
import de.dhsn.oop.data.TodoList;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
|
||||
public class ListsOverview extends JPanel {
|
||||
|
||||
public ListsOverview(List<TodoList> lists){
|
||||
super();
|
||||
this.setLayout(new GridBagLayout());
|
||||
GridBagConstraints c = new GridBagConstraints();
|
||||
c.insets = new Insets(2,2,2,2);
|
||||
c.gridx=0;
|
||||
c.gridy=0;
|
||||
for(TodoList list: lists){
|
||||
ListOverviewItem listOverviewItem = new ListOverviewItem(list);
|
||||
this.add(listOverviewItem, c);
|
||||
c.gridy++;
|
||||
}
|
||||
this.setVisible(true);
|
||||
}
|
||||
}
|
96
src/main/java/de/dhsn/oop/ui/MainWindow.java
Normal file
96
src/main/java/de/dhsn/oop/ui/MainWindow.java
Normal file
@ -0,0 +1,96 @@
|
||||
package de.dhsn.oop.ui;
|
||||
|
||||
import de.dhsn.oop.data.TodoList;
|
||||
import de.dhsn.oop.ui.helpers.TodoListListModel;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.ListDataListener;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
import java.awt.*;
|
||||
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.Objects;
|
||||
|
||||
public class MainWindow extends JFrame implements ActionListener, ListSelectionListener {
|
||||
private JButton addNewList;
|
||||
private List<TodoList> lists;
|
||||
private JList<String> list;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public MainWindow(List<TodoList> lists) throws HeadlessException {
|
||||
super();
|
||||
this.lists = lists;
|
||||
Container cp = getContentPane();
|
||||
cp.removeAll();
|
||||
setTitle("Todo List");
|
||||
setSize(new Dimension(800, 600));
|
||||
setMinimumSize(new Dimension(400, 250));
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
list = new JList<>();
|
||||
list.setModel(new TodoListListModel(lists));
|
||||
list.addListSelectionListener(this);
|
||||
cp.add(list, BorderLayout.NORTH);
|
||||
|
||||
addNewList = new JButton("Neue Liste Anlegen");
|
||||
addNewList.addActionListener(this);
|
||||
cp.add(addNewList, BorderLayout.SOUTH);
|
||||
|
||||
pack();
|
||||
setVisible(true);
|
||||
|
||||
this.addWindowListener(new WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
System.out.println("exit test");
|
||||
System.out.println(lists);
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* listener function for the add new list button on the main page
|
||||
* @param e the event to be processed
|
||||
*/
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
new NewListDialog(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
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()){
|
||||
//show new window for the selected list
|
||||
new TodoListView(lists.get(list.getSelectedIndex()));
|
||||
}
|
||||
}
|
||||
}
|
37
src/main/java/de/dhsn/oop/ui/NewListDialog.java
Normal file
37
src/main/java/de/dhsn/oop/ui/NewListDialog.java
Normal file
@ -0,0 +1,37 @@
|
||||
package de.dhsn.oop.ui;
|
||||
|
||||
import de.dhsn.oop.data.TodoList;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.List;
|
||||
|
||||
public class NewListDialog extends JFrame implements ActionListener {
|
||||
private JTextField input = new JTextField("TODO");
|
||||
private JButton confirm = new JButton("Anlegen");
|
||||
private MainWindow mw;
|
||||
|
||||
public NewListDialog(MainWindow mw){
|
||||
this.mw = mw;
|
||||
Container cp = getContentPane();
|
||||
setLayout(new BorderLayout());
|
||||
JLabel l = new JLabel("Name der Liste: ");
|
||||
cp.add(l, BorderLayout.LINE_START);
|
||||
|
||||
input.setColumns(15);
|
||||
cp.add(input, BorderLayout.LINE_END);
|
||||
confirm.addActionListener(this);
|
||||
cp.add(confirm, BorderLayout.SOUTH);
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
mw.nldCallback(input.getText().strip());
|
||||
setVisible(false);
|
||||
dispose();
|
||||
}
|
||||
}
|
51
src/main/java/de/dhsn/oop/ui/TodoListView.java
Normal file
51
src/main/java/de/dhsn/oop/ui/TodoListView.java
Normal file
@ -0,0 +1,51 @@
|
||||
package de.dhsn.oop.ui;
|
||||
|
||||
import de.dhsn.oop.data.TextTodoItem;
|
||||
import de.dhsn.oop.data.TodoItem;
|
||||
import de.dhsn.oop.data.TodoList;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class TodoListView extends JFrame {
|
||||
class TodoListItem extends JPanel {
|
||||
public TodoListItem(TodoItem item) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
JLabel title;
|
||||
TodoList list;
|
||||
|
||||
public TodoListView(TodoList list) {
|
||||
super();
|
||||
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());
|
||||
title = new JLabel(list.getListTitle());
|
||||
cp.add(title, BorderLayout.NORTH);
|
||||
|
||||
// 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));
|
||||
|
||||
//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);
|
||||
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
32
src/main/java/de/dhsn/oop/ui/helpers/TodoListListModel.java
Normal file
32
src/main/java/de/dhsn/oop/ui/helpers/TodoListListModel.java
Normal file
@ -0,0 +1,32 @@
|
||||
package de.dhsn.oop.ui.helpers;
|
||||
|
||||
import de.dhsn.oop.data.TodoList;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.ListDataListener;
|
||||
import java.util.List;
|
||||
|
||||
public class TodoListListModel implements ListModel<String> {
|
||||
List<TodoList> global;
|
||||
public TodoListListModel(List<TodoList> lists){
|
||||
global = lists;
|
||||
}
|
||||
@Override
|
||||
public int getSize() {
|
||||
return global.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getElementAt(int index) {
|
||||
return ((TodoList) global.get(index)).getListTitle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListDataListener(ListDataListener l) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListDataListener(ListDataListener l) {
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user