Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package fi.todoordelay.gui; import java.awt.Toolkit; import fi.todoordelay.logic.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import static java.lang.Double.NaN; import java.time.LocalDateTime; import java.util.List; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; import org.apache.commons.lang3.math.NumberUtils; /** * * @author henri */ public class GuiMainFrame extends javax.swing.JFrame { public GuiMainFrame(LogicMain mainLogic) { this.mainLogic = mainLogic; } /** * Creates new form GuiMainFrame */ public GuiMainFrame() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { menuBar = new javax.swing.JMenuBar(); projectMenu = new javax.swing.JMenu(); addprojectMenuItem = new javax.swing.JMenuItem(); saveMenuItem = new javax.swing.JMenuItem(); saveAsMenuItem = new javax.swing.JMenuItem(); exitMenuItem = new javax.swing.JMenuItem(); taskMenu = new javax.swing.JMenu(); addtaskMenuItem = new javax.swing.JMenuItem(); copyMenuItem = new javax.swing.JMenuItem(); pasteMenuItem = new javax.swing.JMenuItem(); deleteMenuItem = new javax.swing.JMenuItem(); ownlistMenu = new javax.swing.JMenu(); addownlistMenuItem = new javax.swing.JMenuItem(); aboutMenuItem = new javax.swing.JMenuItem(); projectListModel = new javax.swing.DefaultListModel(); projectsList = new javax.swing.JList(projectListModel); projectsPane = new javax.swing.JScrollPane(projectsList); taskListModel = new javax.swing.DefaultListModel(); tasksList = new javax.swing.JList(taskListModel); tasksPane = new javax.swing.JScrollPane(tasksList); informationTextArea = new javax.swing.JTextArea(); informationPane = new javax.swing.JScrollPane(informationTextArea); addItemDialog = new javax.swing.JOptionPane(); editItemDialog = new javax.swing.JOptionPane(); mainLogic = new fi.todoordelay.logic.LogicMain(); projectsListMouseListener = new MouseListener() { public void mouseClicked(MouseEvent evt) { int index = projectsList.locationToIndex(evt.getPoint()); selectedProject = index; selectionGroup = 0; if (evt.getButton() == 3) { int d = editItemDialog.showConfirmDialog(null, "ARE YOU SURE YOU WANT TO DELETE CHOSEN PROJECT?", "Delete project dialog", editItemDialog.YES_NO_OPTION, editItemDialog.WARNING_MESSAGE); if (d == 0) { mainLogic.removeProject(mainLogic.getProjects().get(selectedProject)); } selectedProject = -1; updateProjectListModel(); } if (evt.getClickCount() == 2 && selectedProject > -1) { List pl = mainLogic.getProjects(); Project p = (Project) pl.get(index); editItemDialog.setWantsInput(true); String name = editItemDialog.showInputDialog(null, "New project name (required):", "Edit project dialog", editItemDialog.INFORMATION_MESSAGE); String description = editItemDialog.showInputDialog(null, "New project description:", "Edit project dialog", editItemDialog.INFORMATION_MESSAGE); String dueString = editItemDialog.showInputDialog(null, "New due date in x day(s):", "Edit project dialog", editItemDialog.INFORMATION_MESSAGE); try { if (name.equals("")) { JOptionPane.showMessageDialog(editItemDialog, "Project name cannot be empty (exiting without changes)"); } else if (Long.parseLong(dueString) < 0) { JOptionPane.showMessageDialog(editItemDialog, "Due date must be zero or higher"); } else { p.setName(name); p.setDescription(description); p.setDuedate(LocalDateTime.now().plusDays(Long.parseLong(dueString))); } } catch (Exception e) { JOptionPane.showMessageDialog(editItemDialog, "Due date must be valid number"); } } updateProjectListModel(); mouseClickedOnProjectActionPerformed(evt); } public void mouseEntered(MouseEvent evt) { } public void mouseExited(MouseEvent evt) { } public void mouseReleased(MouseEvent evt) { } public void mousePressed(MouseEvent evt) { } }; projectsList.addMouseListener(projectsListMouseListener); tasksListMouseListener = new MouseListener() { public void mouseClicked(MouseEvent evt) { int index = tasksList.locationToIndex(evt.getPoint()); selectedTask = index; selectionGroup = 1; if (evt.getButton() == 3) { int d = editItemDialog.showConfirmDialog(null, "ARE YOU SURE YOU WANT TO DELETE CHOSEN TASK?", "Delete task dialog", editItemDialog.YES_NO_OPTION, editItemDialog.WARNING_MESSAGE); if (d == 0) { mainLogic.getProjects().get(selectedProject).removeTask( mainLogic.getProjects().get(selectedProject).getTasks().get(selectedTask)); } updateProjectListModel(); } if (evt.getClickCount() == 2 && selectedTask > -1) { List tl = mainLogic.getProjects().get(selectedProject).getTasks(); Task t = (Task) tl.get(index); editItemDialog.setWantsInput(true); String name = editItemDialog.showInputDialog(null, "New task name (required):", "Edit task dialog", editItemDialog.INFORMATION_MESSAGE); String description = editItemDialog.showInputDialog(null, "New task description:", "Edit task dialog", editItemDialog.INFORMATION_MESSAGE); String dueString = editItemDialog.showInputDialog(null, "New due date in x day(s):", "Edit task dialog", editItemDialog.INFORMATION_MESSAGE); try { if (name.equals("")) { JOptionPane.showMessageDialog(editItemDialog, "Task name cannot be empty (exiting without changes)"); } else if (Long.parseLong(dueString) < 0) { JOptionPane.showMessageDialog(editItemDialog, "Due date must be zero or higher"); } else { t.setName(name); t.setDescription(description); t.setDuedate(LocalDateTime.now().plusDays(Long.parseLong(dueString))); } } catch (Exception e) { JOptionPane.showMessageDialog(editItemDialog, "Due date must be valid number"); } } updateProjectListModel(); mouseClickedOnProjectActionPerformed(evt); } public void mouseEntered(MouseEvent evt) { } public void mouseExited(MouseEvent evt) { } public void mouseReleased(MouseEvent evt) { } public void mousePressed(MouseEvent evt) { } }; tasksList.addMouseListener(tasksListMouseListener); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); projectsPane.setBorder(javax.swing.BorderFactory.createTitledBorder("Projects")); projectsPane.setViewportBorder(null); tasksPane.setBorder(javax.swing.BorderFactory.createTitledBorder("Tasks")); tasksPane.setViewportBorder(null); informationPane.setBorder(javax.swing.BorderFactory.createTitledBorder("Description")); projectsPane.setViewportBorder(null); projectMenu.setText("Project"); addprojectMenuItem.setMnemonic('p'); addprojectMenuItem.setText("Add Project"); projectMenu.add(addprojectMenuItem); addprojectMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addItemDialog.setWantsInput(true); String name = addItemDialog.showInputDialog(null, "Project name (required):", "Add project dialog", addItemDialog.INFORMATION_MESSAGE); String description = addItemDialog.showInputDialog(null, "Project description:", "Add project dialog", addItemDialog.INFORMATION_MESSAGE); String due_at = addItemDialog.showInputDialog(null, "Due in x day(s):", "Add project dialog", addItemDialog.INFORMATION_MESSAGE); Project newProject = new Project(name, description, Long.parseLong(due_at)); mainLogic.addProject(newProject); projectListModel.addElement(name); updateProjectListModel(); addprojectMenuItemActionPerformed(evt); } }); menuBar.add(projectMenu); taskMenu.setText("Task"); addtaskMenuItem.setMnemonic('t'); addtaskMenuItem.setText("Add Task"); taskMenu.add(addtaskMenuItem); addtaskMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addItemDialog.setWantsInput(true); String name = addItemDialog.showInputDialog(null, "Task name (required):", "Add task dialog", addItemDialog.INFORMATION_MESSAGE); String description = addItemDialog.showInputDialog(null, "Task description:", "Add task dialog", addItemDialog.INFORMATION_MESSAGE); String due_at = addItemDialog.showInputDialog(null, "Due in x day(s):", "Add task dialog", addItemDialog.INFORMATION_MESSAGE); Task newTask = new Task(name, description, Long.parseLong(due_at)); Project s = mainLogic.getProjects().get(selectedProject); s.addTask(newTask); taskListModel.addElement(name); updateProjectListModel(); addprojectMenuItemActionPerformed(evt); } }); menuBar.add(taskMenu); setJMenuBar(menuBar); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent( projectsPane, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup( layout.createSequentialGroup().addGap(312, 312, 312).addComponent(tasksPane, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(624, 624, 624).addComponent( informationPane, javax.swing.GroupLayout.PREFERRED_SIZE, 300, Short.MAX_VALUE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(informationPane, javax.swing.GroupLayout.DEFAULT_SIZE, 860, Short.MAX_VALUE) .addComponent(tasksPane, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(projectsPane, javax.swing.GroupLayout.Alignment.LEADING)) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void updateProjectListModel() { // selectionGroup = 0 ; projects // selectionGroup = 1 ; tasks // selectionGroup = 2 ; ownlists if (mainLogic.getProjects().size() > 0) { projectListModel = new javax.swing.DefaultListModel(); for (Project p : mainLogic.getProjects()) { projectListModel.addElement(p.getName()); } projectsList.setModel(projectListModel); //projectsList.setSelectedIndex(selectedProject); } else { projectListModel = new javax.swing.DefaultListModel(); projectsList.setModel(projectListModel); } if (selectedProject > -1) { taskListModel = new javax.swing.DefaultListModel(); for (Task t : mainLogic.getProjects().get(selectedProject).getTasks()) { taskListModel.addElement(t.getName()); } tasksList.setModel(taskListModel); //tasksList.setSelectedIndex(0); } else { taskListModel = new javax.swing.DefaultListModel(); tasksList.setModel(taskListModel); } informationTextArea.setText(""); if (selectionGroup == 0) { String nameUnderline = ""; int nameLength = mainLogic.getProjects().get(selectedProject).getName().length(); for (int i = 0; i < nameLength; i++) { nameUnderline += "="; } informationTextArea.setText(mainLogic.getProjects().get(selectedProject).getName() + "\n" + nameUnderline + "\n\n" + mainLogic.getProjects().get(selectedProject).getDescription()); } else if (selectionGroup == 1) { String nameUnderline = ""; int nameLength = mainLogic.getProjects().get(selectedProject).getTasks().get(selectedTask).getName() .length(); for (int i = 0; i < nameLength; i++) { nameUnderline += "="; } informationTextArea.setText(mainLogic.getProjects().get(selectedProject).getTasks().get(selectedTask) .getName() + "\n" + nameUnderline + "\n\n" + mainLogic.getProjects().get(selectedProject).getTasks().get(selectedTask).getDescription()); } // } else if (selectionGroup == 2) { // String nameUnderline = ""; // int nameLength = mainLogic.getProjects().get(selectedProject).getOwnListsList().get(selectedOwnList).getName().length(); // for (int i = 0; i < nameLength; i++) { // nameUnderline += "="; // } // informationTextArea.setText(mainLogic.getProjects().get(selectedProject).getTasks().get(selectedTask).getName() + "\n" + nameUnderline + "\n\n" + mainLogic.getProjects().get(selectedProject).getTasks().get(selectedTask).getDescription()); // } } private void mouseClickedOnProjectActionPerformed(java.awt.event.MouseEvent evt) { } private void addprojectMenuItemActionPerformed(java.awt.event.ActionEvent evt) { } private void projectListSelectionActionPerformed(javax.swing.event.ListSelectionEvent evt) { } private void editItemDialogctionPerformed(java.awt.event.ActionEvent evt) { } private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed System.exit(0); }//GEN-LAST:event_exitMenuItemActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(GuiMainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(GuiMainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(GuiMainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(GuiMainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { GuiMainFrame gui = new GuiMainFrame(); gui.setSize(Toolkit.getDefaultToolkit().getScreenSize()); gui.setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JMenuItem aboutMenuItem; private javax.swing.JMenuItem addownlistMenuItem; private javax.swing.JMenuItem copyMenuItem; private javax.swing.JMenuItem addtaskMenuItem; private javax.swing.JMenuItem deleteMenuItem; private javax.swing.JMenu taskMenu; private javax.swing.JMenuItem exitMenuItem; private javax.swing.JMenu projectMenu; private javax.swing.JMenu ownlistMenu; private javax.swing.JMenuBar menuBar; private javax.swing.JMenuItem addprojectMenuItem; private javax.swing.JMenuItem pasteMenuItem; private javax.swing.JMenuItem saveAsMenuItem; private javax.swing.JMenuItem saveMenuItem; private javax.swing.JScrollPane tasksPane; private javax.swing.JList tasksList; private javax.swing.DefaultListModel taskListModel; private javax.swing.JScrollPane informationPane; private javax.swing.JTextArea informationTextArea; //private javax.swing.DefaultListModel informationListModel; private javax.swing.JScrollPane projectsPane; private javax.swing.JList projectsList; private javax.swing.DefaultListModel projectListModel; private javax.swing.JOptionPane addItemDialog; private javax.swing.JOptionPane editItemDialog; private fi.todoordelay.logic.LogicMain mainLogic; private javax.swing.event.ListSelectionEvent projectsListSelectionListenerEvent; private javax.swing.event.ListSelectionListener projectsListSelectionListener; private java.awt.event.MouseListener projectsListMouseListener; private java.awt.event.MouseListener tasksListMouseListener; private int selectedProject = -1; private int selectedTask = -1; private int selectionGroup = -1; // End of variables declaration//GEN-END:variables }