Explorar el Código

Add act support.

Gilian Joosen hace 10 años
padre
commit
1f6fa3fbe8

+ 4 - 0
src/agenda/Agenda.java

@@ -70,6 +70,10 @@ public class Agenda {
         this.artists.remove(artist);
     }
     
+    public void removeAct(Act act){
+        this.acts.remove(act);
+    }
+    
     @Override
     public String toString(){
         String string = "";

+ 5 - 0
src/agenda/Artist.java

@@ -19,4 +19,9 @@ public class Artist {
     public String getGenre() {
         return genre;
     }
+    
+    @Override
+    public String toString(){
+        return this.name;
+    }
 }

+ 5 - 0
src/agenda/Stage.java

@@ -14,4 +14,9 @@ public class Stage {
     public String getName(){
         return this.name;
     }
+    
+    @Override
+    public String toString(){
+        return this.name;
+    }
 }

+ 1 - 1
src/gui/frames/MainFrame.java

@@ -19,7 +19,7 @@ public class MainFrame extends JFrame{
     
     public MainFrame(){
         this.agenda = new Agenda();
-        this.currentPanel = new MainPanel(this.agenda);
+        this.currentPanel = new EditPane(this.agenda);
         this.add(this.currentPanel);
 		this.setResizable(true);
 		this.setBounds(100,100,1440,900);

+ 47 - 9
src/gui/panels/edit/ActsPane.java

@@ -3,9 +3,12 @@ package gui.panels.edit;
 import agenda.Act;
 import agenda.Agenda;
 import agenda.Stage;
+import gui.panels.edit.dialogs.AddActDialogPanel;
 
 import javax.swing.*;
 import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.util.ArrayList;
 
 /**
@@ -15,25 +18,23 @@ public class ActsPane extends JPanel {
 
     private JList actsList;
     private Agenda agenda;
+    private DefaultListModel model;
 
     public ActsPane(Agenda agenda){
         this.agenda = agenda;
+        this.model = new DefaultListModel();
         super.setLayout(new BorderLayout());
         super.add(new Label("Acts"), BorderLayout.NORTH);
         super.add(new JPanel(), BorderLayout.EAST);
         super.add(new JPanel(), BorderLayout.WEST);
         super.add(this.buttonRow(), BorderLayout.SOUTH);
 
-        //the JList needs an array, so you have to go from the List to an array.
-        Act[] acts = new Act[this.agenda.getActs().size()];
-        int i = 0;
         for(Act act: this.agenda.getActs()){
-            acts[i] = act;
-            i++;
+            this.model.addElement(act);
         }
 
-        //initialize the JList with the artist objects.
-        this.actsList = new JList(acts);
+        this.actsList = new JList();
+        this.actsList.setModel(this.model);
 
         //the cell renderer.
         this.actsList.setCellRenderer(new ActCellRenderer());
@@ -47,10 +48,47 @@ public class ActsPane extends JPanel {
     private JPanel buttonRow(){
         JPanel buttonPane = new JPanel();
         buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
-        buttonPane.add(new Button("add"));
-        buttonPane.add(new Button("remove"));
+        
+        //add button
+        JButton addButton = new JButton("add");
+        addButton.addActionListener(new ActionListener() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                JDialog dialog = new AddActDialogPanel(agenda, model);
+                dialog.pack();
+                dialog.setLocation(getCenterOfScreen(dialog));
+                dialog.setVisible(true);
+            }
+        });
+        buttonPane.add(addButton);
+        
+        //remove button
+        JButton removeButton = new JButton("remove");
+        removeButton.addActionListener(new ActionListener() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                Object object = model.remove(actsList.getSelectedIndex());
+                agenda.removeAct((Act) object);
+                
+                System.out.println(model.size());
+                System.out.println(agenda.getActs().size());
+            }
+        });
+        buttonPane.add(removeButton);
         return buttonPane;
     }
+
+    private Point getCenterOfScreen(JDialog dialog){
+        Toolkit toolkit = Toolkit.getDefaultToolkit();
+        Dimension screenSize = toolkit.getScreenSize();
+
+        int x = (int) ((screenSize.getWidth() - dialog.getWidth()) / 2);
+        int y = (int) ((screenSize.getHeight() - dialog.getHeight()) / 2);
+
+        Point center = new Point(x, y);
+        System.out.println(center.getX() + " - " + center.getY());
+        return center;
+    }
     
 }
 

+ 13 - 0
src/gui/panels/edit/ArtistPane.java

@@ -58,6 +58,7 @@ public class ArtistPane extends JPanel {
             @Override
             public void actionPerformed(ActionEvent e) {
                 JDialog dialog = new AddArtistDialogPanel(agenda, model);
+                dialog.setLocation(getCenterOfScreen(dialog));
                 dialog.pack();
                 dialog.setVisible(true);
             }
@@ -80,6 +81,18 @@ public class ArtistPane extends JPanel {
         
         return buttonPane;
     }
+
+    private Point getCenterOfScreen(JDialog dialog){
+        Toolkit toolkit = Toolkit.getDefaultToolkit();
+        Dimension screenSize = toolkit.getScreenSize();
+
+        int x = (int) ((screenSize.getWidth() - dialog.getWidth()) / 2);
+        int y = (int) ((screenSize.getHeight() - dialog.getHeight()) / 2);
+
+        Point center = new Point(x, y);
+        System.out.println(center.getX() + " - " + center.getY());
+        return center;
+    }
 }
 
 class ArtistCellRenderer extends JLabel implements ListCellRenderer {

+ 16 - 4
src/gui/panels/edit/StagesPane.java

@@ -56,9 +56,10 @@ public class StagesPane extends JPanel {
         addButton.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
-                JDialog dialog = new JDialog();
-                dialog.setContentPane(new AddStageDialogPanel(agenda, model));
+                JDialog dialog = new AddStageDialogPanel(agenda, model);
+                dialog.setLocationRelativeTo(null);
                 dialog.pack();
+                dialog.setLocation(getCenterOfScreen(dialog));
                 dialog.setVisible(true);
             }
         });
@@ -79,7 +80,18 @@ public class StagesPane extends JPanel {
         buttonPane.add(remove);
         return buttonPane;
     }
-    
+
+    private Point getCenterOfScreen(JDialog dialog){
+        Toolkit toolkit = Toolkit.getDefaultToolkit();
+        Dimension screenSize = toolkit.getScreenSize();
+
+        int x = (int) ((screenSize.getWidth() - dialog.getWidth()) / 2);
+        int y = (int) ((screenSize.getHeight() - dialog.getHeight()) / 2);
+
+        Point center = new Point(x, y);
+        System.out.println(center.getX() + " - " + center.getY());
+        return center;
+    }
 }
 
 class StageCellRenderer extends JLabel implements ListCellRenderer {
@@ -103,5 +115,5 @@ class StageCellRenderer extends JLabel implements ListCellRenderer {
             setForeground(Color.black);
         }
           return this;
-    }
+    }   
 }

+ 214 - 0
src/gui/panels/edit/dialogs/AddActDialogPanel.java

@@ -0,0 +1,214 @@
+package gui.panels.edit.dialogs;
+
+import agenda.*;
+import com.sun.codemodel.internal.JOp;
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.ArrayList;
+
+/**
+ * Created by gjoosen on 20/02/15.
+ */
+public class AddActDialogPanel extends JDialog{
+
+    private Agenda agenda;
+    private JTextField name, genre;
+    private JComboBox stageComboBox;
+
+    private DefaultListModel model;
+    private final DefaultListModel  artistModel = new DefaultListModel();
+    
+    public AddActDialogPanel(Agenda agenda, DefaultListModel model){
+        this.agenda = agenda;
+        this.model = model;
+
+        JPanel main = new JPanel();
+        main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
+        main.add(new Label("new act"));
+        main.add(this.namePanel());
+        //add
+        main.add(this.stageChooser());
+        main.add(this.genreChooser());
+        //todo dates
+        main.add(this.dates());
+        main.add(this.artistsChooser());
+        
+        main.add(this.buttons());
+        super.add(main);
+        super.setVisible(true);
+        super.pack();
+    }
+    
+    private JPanel namePanel(){
+        JPanel name = new JPanel();
+        name.setLayout(new BoxLayout(name, BoxLayout.X_AXIS));
+        name.add(new JLabel("Name"));
+
+        this.name = new JTextField();
+
+        name.add(this.name);
+        return name;
+    }
+
+    private JPanel stageChooser(){
+        JPanel panel = new JPanel();
+        this.stageComboBox = new JComboBox();
+        for(Stage stage: this.agenda.getStages()){
+            this.stageComboBox.addItem(stage);
+        }
+        
+        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
+        panel.add(new JLabel("stage:"));
+        panel.add(this.stageComboBox);        
+        
+        return panel;
+    }
+    
+    private JPanel genreChooser(){
+        JPanel genrePanel = new JPanel();
+        genrePanel.setLayout(new BoxLayout(genrePanel, BoxLayout.X_AXIS));
+        genrePanel.add(new JLabel("Genre"));
+
+        this.genre = new JTextField();
+
+        genrePanel.add(this.genre);
+        return genrePanel;
+    }
+    
+    private JPanel dates(){
+        JPanel dates = new JPanel();   
+        return dates;
+    }
+    
+    private JPanel artistsChooser(){
+        final JPanel artists = new JPanel();
+        artists.setLayout(new BoxLayout(artists, BoxLayout.X_AXIS));
+        
+        //list
+        JList<Act> list = new JList();
+        list.setCellRenderer(new ArtistCellRenderer());
+        
+        list.setModel(artistModel);
+        
+        JScrollPane scrollPane = new JScrollPane(list);
+        artists.add(scrollPane);
+        
+        JButton addArtists = new JButton("+");
+        addArtists.addActionListener(new ActionListener() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                boolean chosen = false;
+                
+                //artists 
+                ArrayList listArtists = new ArrayList<Artist>();
+                //for every artist in the agenda
+                for(Artist artistAgenda: agenda.getArtists()){
+                    
+                    System.out.println("artist: " + artistAgenda);
+                    
+                    //for every artist in the model.
+                    for(Object artistObject: artistModel.toArray()){
+                        Artist artistModel = (Artist) artistObject;
+                        
+                        System.out.println("vergelijk: " + artistModel + " - " + artistAgenda);
+                        
+                        if(artistModel == artistAgenda) {
+                            System.out.println("true: " + artistModel + " - " + artistAgenda);
+                            chosen = true;
+                        }                    
+                    }
+                    if(!chosen){
+                        System.out.println(artistAgenda);
+                        listArtists.add(artistAgenda);
+                    }
+                    chosen  = false;
+                }
+
+                Artist[] artist = new Artist[listArtists.size()];
+                
+                for(int i = 0; i<listArtists.size(); i++){
+                    artist[i] = (Artist) listArtists.get(i);
+                }
+                Artist chosenArtist = (Artist) JOptionPane.showInputDialog(null, "add artist", "add artist", JOptionPane.PLAIN_MESSAGE, null, artist ,null);
+                artistModel.addElement(chosenArtist);
+            }
+        });
+        
+        artists.add(addArtists);
+        return artists;
+    }
+    
+    private JPanel buttons(){
+        JPanel buttons = new JPanel();
+
+        JButton save = new JButton("save");
+        save.addActionListener(new ActionListener() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                save();
+            }
+        });
+        buttons.add(save);
+
+        JButton cancel = new JButton("cancel");
+        cancel.addActionListener(new ActionListener() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                cancel();
+            }
+        });
+
+        buttons.add(cancel);
+
+        return buttons;
+    }
+    
+    private void save(){
+
+        Artist[] artists = new Artist[this.artistModel.size()];
+        for(int i = 0; i < artistModel.size(); i++){
+            artists[i] = (Artist) artistModel.get(i);
+        }
+        
+        Act act = new Act(this.name.getText(), (Stage) this.stageComboBox.getSelectedItem(), this.genre.getText(), new ActTime(2015,02,11,21,00  ,2015,02,11,23,00), artists);
+        System.out.println(act);
+        
+        this.model.addElement(act);
+        this.agenda.addAct(act);
+        
+        dispose();
+
+    }
+
+    private void cancel(){
+        dispose();
+    }
+}
+
+class ArtistCellRenderer extends JLabel implements ListCellRenderer {
+
+    private static final Color HIGHLIGHT_COLOR = new Color(0, 0, 128);
+
+    public ArtistCellRenderer() {
+        setOpaque(true);
+        setIconTextGap(12);
+    }
+
+    public Component getListCellRendererComponent(JList list, Object value,
+                                                  int index, boolean isSelected, boolean cellHasFocus) {
+        Artist stage = (Artist) value;
+        setText(stage.getName());
+        if (isSelected) {
+            setBackground(HIGHLIGHT_COLOR);
+            setForeground(Color.white);
+        } else {
+            setBackground(Color.white);
+            setForeground(Color.black);
+        }
+        return this;
+    }
+}
+