I want to make sure this is a good implementation of MVC. I started earlier with this code found here and it was horrible compared to what it is now. If it is a good implementation of MVC what, are ways that I can improve the code? The application isn't completely done because I wanted to make sure I was on the right track versus writing bad code.
public class CreateAndShowUI {
public static void createUI() {
Model model = new Model();
new CSVFileController(model);
MainPanel mainPanel = new MainPanel(model);
JFrame frame = new JFrame();
frame.getContentPane().add(mainPanel.getMainPanel());
frame.setUndecorated(true);
frame.setPreferredSize(new Dimension(1100, 550));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static final Font getHeaderFont() {
return new Font("Consolas", Font.BOLD, 16);
}
public static final Font getFont() {
return new Font("Consolas", Font.BOLD, 14);
}
public static final Font getTableFont() {
return new Font("Consolas", Font.PLAIN, 16);
}
public static final Color getButtonColor() {
return new Color(230, 230, 230);
}
public static void main(String[] args) {
createUI();
}
}
This is the mainPanel
for the frame:
public class MainPanel {
private Model model;
private JPanel mainPanel;
private Dialog dialog;
private MenuBar menuBar;
private ButtonPanel buttonPanel;
private JScrollPane buttonScrollPane, listScrollPane;
private JTable table;
private JLabel search;
private JTextField searchTF;
private JButton xButton;
public MainPanel(Model model) {
this.model = model;
createMainPanel();
}
private void createMainPanel() {
mainPanel = new JPanel(new MigLayout("", "", "[]13[]"));
dialog = new Dialog();
table = new JTable(new ProductTableModel());
setTableColumnWidth((AbstractTableModel) table.getModel());
table.setFont(CreateAndShowUI.getTableFont());
DefaultCellEditor editor = (DefaultCellEditor) table
.getDefaultEditor(Object.class);
Component component = editor.getComponent();
component.setFont(table.getFont());
editor.setClickCountToStart(1);
table.getTableHeader()
.setPreferredSize(
new Dimension(table.getColumnModel()
.getTotalColumnWidth(), 48));
table.getTableHeader().setResizingAllowed(false);
table.getTableHeader().setReorderingAllowed(false);
table.getTableHeader().setFont(CreateAndShowUI.getFont());
menuBar = new MenuBar();
mainPanel.add(menuBar.getMenuBar());
search = new JLabel("Search");
mainPanel.add(search, "cell 0 0, gap 10px");
searchTF = new JTextField("", 30);
mainPanel.add(searchTF, "cell 0 0");
xButton = new JButton(new CloseAction("X"));
xButton.setFocusable(false);
xButton.setFont(CreateAndShowUI.getFont());
xButton.setBackground(new Color(158, 7, 7));
xButton.setForeground(Color.WHITE);
mainPanel.add(xButton, "cell 0 0, gap 283px, wrap");
buttonPanel = new ButtonPanel();
buttonScrollPane = new JScrollPane(buttonPanel.getButtonPanel(),
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
buttonScrollPane.setViewportView(buttonPanel.getButtonPanel());
buttonScrollPane.setPreferredSize(new Dimension(250, 990));
mainPanel.add(buttonScrollPane);
listScrollPane = new JScrollPane(table,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
listScrollPane.setPreferredSize(new Dimension(850, 990));
mainPanel.add(listScrollPane, "cell 0 1");
}
public JPanel getMainPanel() {
return mainPanel;
}
public JPanel getButtonPanel() {
return buttonPanel.getButtonPanel();
}
public JTable getTable() {
return table;
}
public JMenuBar getMenuBar() {
return menuBar.getMenuBar();
}
public void setTableColumnWidth(AbstractTableModel tableModel) {
final TableColumnModel columnModel = table.getColumnModel();
if (tableModel.getColumnCount() == 6) {
columnModel.getColumn(0).setPreferredWidth(200);
columnModel.getColumn(1).setPreferredWidth(300);
columnModel.getColumn(2).setPreferredWidth(80);
columnModel.getColumn(3).setPreferredWidth(75);
columnModel.getColumn(4).setPreferredWidth(80);
columnModel.getColumn(5).setPreferredWidth(75);
}
}
public class ButtonPanel {
private JPanel panel;
public ButtonPanel() {
panel = new JPanel (new MigLayout());
addButtons();
}
private void addButtons() {
List<Supplier> supplierList = model.getSuppliers();
for (Supplier supplier : supplierList) {
JButton button = new JButton(new SupplierButtonAction(
supplier.getName(), (ProductTableModel) table.getModel(), model));
button.setFocusable(false);
button.setFont(CreateAndShowUI.getFont());
button.setBackground(CreateAndShowUI.getButtonColor());
button.setPreferredSize(new Dimension(230, 30));
button.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(button, "wrap");
}
}
public JPanel getButtonPanel() {
return panel;
}
}
public class MenuBar {
private JMenuBar menuBar;
private JMenu application, suppliers, orders;
private JMenuItem viewOrders, newSupplier, addProduct, close;
public MenuBar() {
createMenuBar();
}
private void createMenuBar() {
menuBar = new JMenuBar();
close = new JMenuItem(new CloseAction("Close"));
close.setPreferredSize(new Dimension(125, 25));
close.setFont(CreateAndShowUI.getHeaderFont());
application = new JMenu("Application");
application.setFont(CreateAndShowUI.getHeaderFont());
application.setPreferredSize(new Dimension(125, 70));
application.add(close);
menuBar.add(application);
newSupplier = new JMenuItem(
new DialogActions.AddSupplierPanelAction(dialog, "New"));
newSupplier.setPreferredSize(new Dimension(125, 25));
newSupplier.setFont(CreateAndShowUI.getHeaderFont());
addProduct = new JMenuItem(new DialogActions.AddProductPanelAction(
dialog, "Add Products"));
addProduct.setPreferredSize(new Dimension(125, 25));
addProduct.setFont(CreateAndShowUI.getHeaderFont());
suppliers = new JMenu("Suppliers");
suppliers.setFont(CreateAndShowUI.getHeaderFont());
suppliers.setPreferredSize(new Dimension(125, 70));
suppliers.add(newSupplier);
suppliers.add(addProduct);
menuBar.add(suppliers);
viewOrders = new JMenuItem("View");
viewOrders.setFont(CreateAndShowUI.getHeaderFont());
viewOrders.setPreferredSize(new Dimension(125, 25));
orders = new JMenu("Orders");
orders.setFont(CreateAndShowUI.getHeaderFont());
orders.setPreferredSize(new Dimension(125, 70));
orders.add(viewOrders);
menuBar.add(orders);
}
public JMenuBar getMenuBar() {
return menuBar;
}
}
}
This is the controller for updating the JDialog
found in the MenuBar
class at the bottom of mainPanel
:
public class DialogActions {
public static class CloseDialogAction extends AbstractAction {
Dialog dialog;
public CloseDialogAction(Dialog dialog, String name) {
super(name);
this.dialog = dialog;
}
@Override
public void actionPerformed(ActionEvent e) {
dialog.getDialog().dispose();
}
}
public static class AddProductPanelAction extends AbstractAction {
Dialog dialog;
public AddProductPanelAction(Dialog dialog, String name) {
super(name);
this.dialog = dialog;
}
@Override
public void actionPerformed(ActionEvent e) {
dialog.addContainer(new AddProductPanel().getAddProductPanel());
}
}
public static class AddSupplierPanelAction extends AbstractAction {
Dialog dialog;
public AddSupplierPanelAction(Dialog dialog, String name) {
super(name);
this.dialog = dialog;
}
@Override
public void actionPerformed(ActionEvent e) {
dialog.addContainer(new AddSupplierPanel().getAddSupplierPanel());
}
}
public static class SelectSupplierPanelAction extends AbstractAction {
Dialog dialog;
public SelectSupplierPanelAction(Dialog dialog, String name) {
super(name);
this.dialog = dialog;
}
@Override
public void actionPerformed(ActionEvent e) {
dialog.getDialog().getContentPane().removeAll();
dialog.addContainer(new SelectSupplierScrollPane().getScrollPane());
}
}
}
This is a controller for updating the model, reading and as well writing of the CSV files within the application. The initialization of this class is in the createAndShowUI
class. I still have more methods to include into this class that involves writing to the CSV files and updating the model.
public class CSVFileController {
private Model model;
private BufferedReader reader;
private BufferedWriter writer;
private String line;
public CSVFileController(Model model) {
this.model = model;
this.reader = null;
this.writer = null;
this.line = "";
updateModel();
}
public void updateModel() {
List<Supplier> suppliers = new ArrayList<Supplier>();
try {
reader = new BufferedReader(new FileReader("Suppliers.csv"));
while ((line = reader.readLine()) != null) {
String[] dataArray = line.split(",");
suppliers.add(new Supplier(dataArray[0], Integer
.parseInt(dataArray[1])));
}
for (Supplier supplier : suppliers) {
List<Product> productList = new ArrayList<Product>();
reader = new BufferedReader(new FileReader(supplier.getName()
+ ".csv"));
while ((line = reader.readLine()) != null) {
String[] dataArray = line.split(",");
productList.add(new Product(dataArray[0], dataArray[1],
dataArray[2], Integer.parseInt(dataArray[3]),
Double.parseDouble(dataArray[4]), dataArray[5]));
}
model.addProductList(supplier, productList);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
closeReader(reader);
}
}
public void closeReader(BufferedReader br) {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is the model. Both Product
and Supplier
are POJOs with a bunch of setters so I won't include those.
public class Model {
private Map<Supplier, List<Product>> supplierProductList;
private List<Product> productList;
public Model() {
this.supplierProductList = new TreeMap<Supplier, List<Product>>();
}
public void addSupplier(Supplier supplier) {
if (!supplierProductList.containsKey(supplier)) {
this.supplierProductList.put(supplier, new ArrayList<Product>());
}
}
public List<Supplier> getSuppliers() {
return new ArrayList<Supplier>(supplierProductList.keySet());
}
public void addProduct(String name, Product product) {
for (Supplier supplier : supplierProductList.keySet()) {
if (supplier.getName().equals(name)
&& !supplierProductList.get(name).contains(product)) {
productList = supplierProductList.get(name);
productList.add(product);
supplierProductList.put(supplier, productList);
}
}
}
public void addProductList(Supplier supplier, List<Product> productList2) {
supplierProductList.put(supplier, productList2);
}
public List<Product> getProducts(String name) {
for (Supplier supplier : getSuppliers()) {
if (supplier.getName().equals(name))
return new ArrayList<Product>(supplierProductList.get(supplier));
}
return null;
}
}