beholdmyglory avatar beholdmyglory committed 81862ef

Refactored the color chooser dialog to its own source file

Comments (0)

Files changed (2)

src/se/kth/csc/inda11/remains/gui/ColorChooserDialog.java

+package se.kth.csc.inda11.remains.gui;
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.BorderFactory;
+import javax.swing.Box;
+import javax.swing.BoxLayout;
+import javax.swing.JButton;
+import javax.swing.JColorChooser;
+import javax.swing.JDialog;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+
+import se.kth.csc.inda11.remains.simulator.Simulator;
+
+
+public class ColorChooserDialog extends JDialog {
+    private static final long serialVersionUID = -8200860968417399947L;
+    
+    private MainWindow mainWindow;
+    
+    class ColorPanel extends JPanel {
+        private static final long serialVersionUID = 7372710013211666766L;
+        public Color currColor;
+        
+        public ColorPanel(final String label, Color defaultColor) {
+            this.currColor = defaultColor;
+            this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
+            this.add(new JLabel(label));
+            this.add(Box.createHorizontalGlue());
+            final JButton colorButton = new JButton(ColorChooserDialog.this.mainWindow.buildCellIcon(defaultColor));
+            colorButton.addActionListener(new ActionListener() {
+                @Override
+                public void actionPerformed(ActionEvent e) {
+                    Color c = JColorChooser.showDialog(
+                            ColorChooserDialog.this,
+                            "Choose the " + label.toLowerCase() + " color",
+                            ColorPanel.this.currColor);
+                    if (c != null) {
+                        ColorPanel.this.currColor = c;
+                        colorButton.setIcon(ColorChooserDialog.this.mainWindow.buildCellIcon(c));
+                    }
+                }
+            });
+            this.add(colorButton);
+        }
+    }
+    
+    public ColorChooserDialog(MainWindow mainWindow) {
+        this.mainWindow = mainWindow;
+        
+
+        JPanel mainPanel = new JPanel();
+        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
+        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
+        
+        final ColorPanel emptyCellPanel = new ColorPanel("Empty cell", 
+                    this.mainWindow.cellGridPanel.getCellColor(Simulator.Cell.EMPTY));
+        final ColorPanel conductorPanel = new ColorPanel("Conductor", 
+                    this.mainWindow.cellGridPanel.getCellColor(Simulator.Cell.CONDUCTOR));
+        final ColorPanel headPanel = new ColorPanel("Electron head", 
+                    this.mainWindow.cellGridPanel.getCellColor(Simulator.Cell.HEAD));
+        final ColorPanel tailPanel = new ColorPanel("Electron tail", 
+                    this.mainWindow.cellGridPanel.getCellColor(Simulator.Cell.TAIL));
+        final ColorPanel gridPanel = new ColorPanel("Grid", 
+                    this.mainWindow.cellGridPanel.getGridColor());
+        
+        mainPanel.add(emptyCellPanel);
+        mainPanel.add(conductorPanel);
+        mainPanel.add(headPanel);
+        mainPanel.add(tailPanel);
+        mainPanel.add(gridPanel);
+        mainPanel.add(Box.createVerticalGlue());
+        
+        JPanel confirmPanel = new JPanel();
+        confirmPanel.setLayout(new BoxLayout(confirmPanel, BoxLayout.X_AXIS));
+        confirmPanel.add(Box.createHorizontalGlue());
+
+        JButton okButton = new JButton("OK");
+        okButton.addActionListener(new ActionListener() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                ColorChooserDialog.this.mainWindow.cellGridPanel.setCellColor(Simulator.Cell.EMPTY,
+                        emptyCellPanel.currColor);
+                ColorChooserDialog.this.mainWindow.cellGridPanel.setCellColor(Simulator.Cell.CONDUCTOR,
+                        conductorPanel.currColor);
+                ColorChooserDialog.this.mainWindow.cellGridPanel.setCellColor(Simulator.Cell.HEAD,
+                        headPanel.currColor);
+                ColorChooserDialog.this.mainWindow.cellGridPanel.setCellColor(Simulator.Cell.TAIL,
+                        tailPanel.currColor);
+                
+                ColorChooserDialog.this.mainWindow.cellGridPanel.setGridColor(gridPanel.currColor);
+                ColorChooserDialog.this.mainWindow.cellGridPanel.repaint();
+
+                ColorChooserDialog.this.mainWindow.cellChooserButtons[0].setIcon(
+                        ColorChooserDialog.this.mainWindow.buildCellIcon(emptyCellPanel.currColor));
+                ColorChooserDialog.this.mainWindow.cellChooserButtons[1].setIcon(
+                        ColorChooserDialog.this.mainWindow.buildCellIcon(conductorPanel.currColor));
+                ColorChooserDialog.this.mainWindow.cellChooserButtons[2].setIcon(
+                        ColorChooserDialog.this.mainWindow.buildCellIcon(headPanel.currColor));
+                ColorChooserDialog.this.mainWindow.cellChooserButtons[3].setIcon(
+                        ColorChooserDialog.this.mainWindow.buildCellIcon(tailPanel.currColor));
+                
+                ColorChooserDialog.this.mainWindow.colorChooserButton.setIcon(
+                        ColorChooserDialog.this.mainWindow.buildColorChooserIcon());
+                
+                ColorChooserDialog.this.setVisible(false);
+                ColorChooserDialog.this.dispose();
+            }
+        });
+        confirmPanel.add(okButton);
+        
+        confirmPanel.add(Box.createRigidArea(new Dimension(10, 0)));
+        
+        JButton cancelButton = new JButton("Cancel");
+        cancelButton.addActionListener(new ActionListener() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                ColorChooserDialog.this.setVisible(false);
+                ColorChooserDialog.this.dispose();
+            }
+        });
+        confirmPanel.add(cancelButton);
+        
+        mainPanel.add(confirmPanel);
+
+        this.add(mainPanel);
+        this.pack();
+        this.setSize(this.getWidth() + 20, this.getHeight() + 20);
+        this.setResizable(false);
+        this.setModalityType(ModalityType.APPLICATION_MODAL);
+    }
+    
+}

src/se/kth/csc/inda11/remains/gui/MainWindow.java

 
 import java.awt.BorderLayout;
 import java.awt.Color;
-import java.awt.Dialog.ModalityType;
-import java.awt.Dimension;
 import java.awt.Graphics;
 import java.awt.Point;
 import java.awt.event.ActionEvent;
 import java.util.Map;
 
 import javax.swing.AbstractAction;
-import javax.swing.BorderFactory;
-import javax.swing.Box;
-import javax.swing.BoxLayout;
 import javax.swing.ButtonGroup;
 import javax.swing.Icon;
 import javax.swing.ImageIcon;
 import javax.swing.JButton;
-import javax.swing.JColorChooser;
 import javax.swing.JComponent;
-import javax.swing.JDialog;
 import javax.swing.JFileChooser;
 import javax.swing.JFrame;
-import javax.swing.JLabel;
 import javax.swing.JOptionPane;
-import javax.swing.JPanel;
 import javax.swing.JSlider;
 import javax.swing.JToggleButton;
 import javax.swing.JToolBar;
     private final int      defaultTickLength = 200; // default tick length
     private final int      maxTickLength = 500;
     
-    private CellGrid cellGridPanel;
+    CellGrid cellGridPanel;
 
-    private CellChooserButton[] cellChooserButtons = new CellChooserButton[Simulator.Cell.values().length];
-    private JButton colorChooserButton;
+    CellChooserButton[] cellChooserButtons = new CellChooserButton[Simulator.Cell.values().length];
+    JButton colorChooserButton;
     
-    private class CellChooserButton extends JToggleButton {
+    class CellChooserButton extends JToggleButton {
         private static final long serialVersionUID = 2591164248760916981L;
         
         public Simulator.Cell cellType;
         this.colorChooserButton.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {;
-                final JDialog colorChooser = new JDialog(MainWindow.this);
-                JPanel mainPanel = new JPanel();
-                mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
-                mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
-                
-                class ColorPanel extends JPanel {
-                    private static final long serialVersionUID = -3317061748262543188L;
-                    public Color currColor;
-                    
-                    public ColorPanel(final String label, Color defaultColor) {
-                        this.currColor = defaultColor;
-                        this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
-                        this.add(new JLabel(label));
-                        this.add(Box.createHorizontalGlue());
-                        final JButton colorButton = new JButton(MainWindow.this.buildCellIcon(defaultColor));
-                        colorButton.addActionListener(new ActionListener() {
-                            @Override
-                            public void actionPerformed(ActionEvent e) {
-                                Color c = JColorChooser.showDialog(
-                                        colorChooser,
-                                        "Choose the " + label.toLowerCase() + " color",
-                                        ColorPanel.this.currColor);
-                                if (c != null) {
-                                    ColorPanel.this.currColor = c;
-                                    colorButton.setIcon(MainWindow.this.buildCellIcon(c));
-                                }
-                            }
-                        });
-                        this.add(colorButton);
-                    }
-                }
-                
-                final ColorPanel emptyCellPanel = new ColorPanel("Empty cell", 
-                            MainWindow.this.cellGridPanel.getCellColor(Simulator.Cell.EMPTY));
-                final ColorPanel conductorPanel = new ColorPanel("Conductor", 
-                            MainWindow.this.cellGridPanel.getCellColor(Simulator.Cell.CONDUCTOR));
-                final ColorPanel headPanel = new ColorPanel("Electron head", 
-                            MainWindow.this.cellGridPanel.getCellColor(Simulator.Cell.HEAD));
-                final ColorPanel tailPanel = new ColorPanel("Electron tail", 
-                            MainWindow.this.cellGridPanel.getCellColor(Simulator.Cell.TAIL));
-                final ColorPanel gridPanel = new ColorPanel("Grid", 
-                            MainWindow.this.cellGridPanel.getGridColor());
-                mainPanel.add(emptyCellPanel);
-                mainPanel.add(conductorPanel);
-                mainPanel.add(headPanel);
-                mainPanel.add(tailPanel);
-                mainPanel.add(gridPanel);
-                mainPanel.add(Box.createVerticalGlue());
-                
-                JPanel confirmPanel = new JPanel();
-                confirmPanel.setLayout(new BoxLayout(confirmPanel, BoxLayout.X_AXIS));
-                confirmPanel.add(Box.createHorizontalGlue());
-                
-                JButton okButton = new JButton("OK");
-                okButton.addActionListener(new ActionListener() {
-                    @Override
-                    public void actionPerformed(ActionEvent e) {
-                        MainWindow.this.cellGridPanel.setCellColor(Simulator.Cell.EMPTY,
-                                emptyCellPanel.currColor);
-                        MainWindow.this.cellGridPanel.setCellColor(Simulator.Cell.CONDUCTOR,
-                                conductorPanel.currColor);
-                        MainWindow.this.cellGridPanel.setCellColor(Simulator.Cell.HEAD,
-                                headPanel.currColor);
-                        MainWindow.this.cellGridPanel.setCellColor(Simulator.Cell.TAIL,
-                                tailPanel.currColor);
-                        MainWindow.this.cellGridPanel.setGridColor(gridPanel.currColor);
-                        MainWindow.this.cellGridPanel.repaint();
-
-                        MainWindow.this.cellChooserButtons[0].setIcon(
-                                MainWindow.this.buildCellIcon(emptyCellPanel.currColor));
-                        MainWindow.this.cellChooserButtons[1].setIcon(
-                                MainWindow.this.buildCellIcon(conductorPanel.currColor));
-                        MainWindow.this.cellChooserButtons[2].setIcon(
-                                MainWindow.this.buildCellIcon(headPanel.currColor));
-                        MainWindow.this.cellChooserButtons[3].setIcon(
-                                MainWindow.this.buildCellIcon(tailPanel.currColor));
-                        
-                        MainWindow.this.colorChooserButton.setIcon(
-                                MainWindow.this.buildColorChooserIcon());
-                        
-                        colorChooser.setVisible(false);
-                        colorChooser.dispose();
-                    }
-                });
-                confirmPanel.add(okButton);
-                confirmPanel.add(Box.createRigidArea(new Dimension(10, 0)));
-                JButton cancelButton = new JButton("Cancel");
-                cancelButton.addActionListener(new ActionListener() {
-                    @Override
-                    public void actionPerformed(ActionEvent e) {
-                        colorChooser.setVisible(false);
-                        colorChooser.dispose();
-                    }
-                });
-                confirmPanel.add(cancelButton);
-                mainPanel.add(confirmPanel);
-                
-                colorChooser.add(mainPanel);
-                colorChooser.pack();
-                colorChooser.setSize(colorChooser.getWidth() + 20, colorChooser.getHeight() + 20);
-                colorChooser.setResizable(false);
-                colorChooser.setModalityType(ModalityType.APPLICATION_MODAL);
+                ColorChooserDialog colorChooser = new ColorChooserDialog(MainWindow.this);
                 colorChooser.setVisible(true);
             }
         });
         }
     }
     
-    private Icon buildCellIcon(Color c) {
+    Icon buildCellIcon(Color c) {
         BufferedImage square = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
         Graphics g = square.getGraphics();
         g.setColor(c);
         return new ImageIcon(square);
     }
     
-    private Icon buildColorChooserIcon() {
+    Icon buildColorChooserIcon() {
         BufferedImage square = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
         Graphics g = square.getGraphics();
         g.setColor(this.cellGridPanel.getCellColor(Simulator.Cell.EMPTY));
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.