Skip to content

Commit

Permalink
Removed 'new' functionality and updated open/save
Browse files Browse the repository at this point in the history
  • Loading branch information
PranavAmarnath committed Mar 3, 2021
1 parent cdbcdc9 commit 6a6c3d2
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 113 deletions.
8 changes: 2 additions & 6 deletions src/main/java/com/secres/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
* <P>
* Each instance of <code>Model</code> spawns a new {@link SwingWorker} for adding each new row to the current <code>JTable</code>'s <code>TableModel</code>
* <P>
* After starting and finishing ALL reads, the <code>Model</code> notifies {@link Main}.<br>
* 1. After <i>starting</i> the last read, {@link Main} creates a new instance of {@link View}.<br>
* 2. After <i>finishing</i> the last read, {@link Main} starts updating each <code>JFreeChart</code> for each <code>ChartPanel</code> that {@link View} created.
*
* The class also manages exporting table data to a CSV file.
*
* @author Pranav Amarnath
*
Expand Down Expand Up @@ -51,16 +50,13 @@ protected Void doInBackground() {
try {
reader = new CSVReader(new FileReader(path));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
header = (String[]) reader.readNext();
} catch (CsvValidationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//SwingUtilities.invokeAndWait(() -> model = new DefaultTableModel(header, 0)); // NOT invokeLater() because model HAS to be initialized immediately on EDT
Expand Down
179 changes: 73 additions & 106 deletions src/main/java/com/secres/View.java
Original file line number Diff line number Diff line change
@@ -1,178 +1,140 @@
package com.secres;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.function.BiConsumer;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;

public class View {

private static JFrame frame;
//private static File path;
private JButton openButton, saveButton, newButton;
private JButton openButton, saveButton;
private JToolBar toolBar;
private JTabbedPane tabbedPane;
private int tabNewIndex;
private static JTabbedPane tabbedPane;
private LinkedHashMap<TablePanel, File> newPanels = new LinkedHashMap<>();
private final int EMPTY_ROW_COUNT = 20;

public View() {
createAndShowGUI();
}

private void createAndShowGUI() {
frame = new JFrame("Secres GUI");
frame = new JFrame("Secres GUI") {
public Dimension getPreferredSize() {
return new Dimension(500, 400);
}
};
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel(new BorderLayout());

TablePanel tempPanel = new TablePanel();
DefaultTableModel tempModel = new DefaultTableModel(20, 20);
for(int i = 0; i < EMPTY_ROW_COUNT; i++) {
tempModel.addRow(new Object[] { });
}
tempPanel.getTable().setModel(tempModel);
JPanel tabsPanel = new JPanel(new BorderLayout());

openButton = new JButton(UIManager.getIcon("FileView.directoryIcon"));
openButton.setFocusable(false);
openButton.setToolTipText("Open");

tabbedPane = new JTabbedPane(SwingConstants.BOTTOM);
openButton.addActionListener(e -> {
FileNameExtensionFilter filter = new FileNameExtensionFilter("Comma Separated Value Files", "csv");
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(filter);
fileChooser.setAcceptAllFileFilterUsed(false);
Action details = fileChooser.getActionMap().get("viewTypeDetails");
details.actionPerformed(null);

if(newPanels.get(tabbedPane.getSelectedComponent()) != null) {
fileChooser.setCurrentDirectory(newPanels.get(tabbedPane.getSelectedComponent()));
}

int returnVal = fileChooser.showOpenDialog(frame);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File path = fileChooser.getSelectedFile();
TablePanel newPanel = new TablePanel();
newPanels.put(newPanel, path);
for(int i = 0; i < tabbedPane.getTabCount(); i++) {
if(tabbedPane.getTitleAt(i).equals(path.getName())) {
tabbedPane.setSelectedIndex(i);
break;
}
else {
tabbedPane.addTab(path.getName(), (Component) newPanels.keySet().toArray()[newPanels.size()-1]);
Main.createModel(path, ((TablePanel) newPanels.keySet().toArray()[newPanels.size()-1]).getTable());
if(tabbedPane.getTitleAt(0).contains("New")) {
tabbedPane.removeTabAt(0); // remove the initial tab
}
tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1);
break;
}
}
}
openOpenDialog();
});

saveButton = new JButton(UIManager.getIcon("FileView.floppyDriveIcon"));
saveButton.setFocusable(false);
saveButton.setToolTipText("Save");

saveButton.addActionListener(e -> {
int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to overwrite changes?", "Save", JOptionPane.YES_NO_OPTION);
if(result == JOptionPane.YES_OPTION) {
FileNameExtensionFilter filter = new FileNameExtensionFilter("Comma Separated Value Files", "csv");
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(filter);
fileChooser.setAcceptAllFileFilterUsed(false);
Action details = fileChooser.getActionMap().get("viewTypeDetails");
details.actionPerformed(null);

if(newPanels.get(tabbedPane.getSelectedComponent()) != null) {
fileChooser.setCurrentDirectory(newPanels.get(tabbedPane.getSelectedComponent()));
}

if(tabbedPane.getTitleAt(tabbedPane.getSelectedIndex()).contains("New")) {
int returnVal = fileChooser.showSaveDialog(frame);
if(returnVal == JFileChooser.APPROVE_OPTION) {
// **This saves the 'new file' but not the contents** -> Needs fix
File path = fileChooser.getSelectedFile();
Main.saveModel(path.getAbsolutePath(), ((TablePanel) tabbedPane.getSelectedComponent()).getTable());
}
}
else {
// This saves an opened file -> This works
Main.saveModel(newPanels.get(newPanels.keySet().toArray()[newPanels.size()-1]).getAbsolutePath(), ((TablePanel) tabbedPane.getSelectedComponent()).getTable());
}
}
});

newButton = new JButton(UIManager.getIcon("FileView.fileIcon"));
newButton.setFocusable(false);
newButton.setToolTipText("New");

newButton.addActionListener(e -> {
tabNewIndex++;
TablePanel temp = new TablePanel();
DefaultTableModel model = new DefaultTableModel(20, 20);
for(int i = 0; i < EMPTY_ROW_COUNT; i++) {
model.addRow(new Object[] { });
}
temp.getTable().setModel(model);
tabbedPane.addTab("New" + tabNewIndex, temp);
tabbedPane.setSelectedComponent(temp);
openSaveDialog();
});

tabNewIndex = 1;
tabbedPane.addTab("New" + tabNewIndex, tempPanel);
tabbedPane.putClientProperty("JTabbedPane.tabClosable", true);
tabbedPane.putClientProperty("JTabbedPane.showTabSeparators", true);
tabbedPane.putClientProperty("JTabbedPane.tabCloseCallback", (BiConsumer<JTabbedPane, Integer>) (tabbedPane, tabIndex) -> {
// close tab here
int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to close this file?", "Close", JOptionPane.YES_NO_OPTION);
if(result == JOptionPane.YES_OPTION) {
openSaveDialog();
tabbedPane.removeTabAt(tabIndex);
if(tabbedPane.getTabCount() == 0) {
tabNewIndex++;
TablePanel temp = new TablePanel();
DefaultTableModel model = new DefaultTableModel(20, 20);
for(int i = 0; i < EMPTY_ROW_COUNT; i++) {
model.addRow(new Object[] { });
}
temp.getTable().setModel(model);
tabbedPane.addTab("New" + tabNewIndex, temp);
}
}
});
tabbedPane.putClientProperty("JTabbedPane.tabCloseToolTipText", "Close");

toolBar = new JToolBar();
toolBar.add(newButton);
toolBar.add(openButton);
toolBar.add(saveButton);
frame.add(toolBar, BorderLayout.NORTH);

mainPanel.add(tabbedPane);
frame.add(mainPanel);
tabsPanel.add(tabbedPane);
frame.add(tabsPanel);

frame.pack();
frame.setVisible(true);
openOpenDialog();
}

private void openOpenDialog() {
FileNameExtensionFilter filter = new FileNameExtensionFilter("Comma Separated Value Files", "csv");
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(filter);
fileChooser.setAcceptAllFileFilterUsed(false);
Action details = fileChooser.getActionMap().get("viewTypeDetails");
details.actionPerformed(null);

if(newPanels.get(tabbedPane.getSelectedComponent()) != null) {
fileChooser.setCurrentDirectory(newPanels.get(tabbedPane.getSelectedComponent()));
}

int returnVal = fileChooser.showOpenDialog(frame);
if(returnVal == 0) {
File path = fileChooser.getSelectedFile();
TablePanel newPanel = new TablePanel();
newPanels.put(newPanel, path);
if(tabbedPane.getTabCount() > 0) {
for(int i = 0; i < tabbedPane.getTabCount(); i++) {
if(tabbedPane.getTitleAt(i).equals(path.getName())) {
tabbedPane.setSelectedIndex(i);
break;
}
else {
tabbedPane.addTab(path.getName(), (Component) newPanels.keySet().toArray()[newPanels.size()-1]);
Main.createModel(path, ((TablePanel) newPanels.keySet().toArray()[newPanels.size()-1]).getTable());
tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1);
break;
}
}
}
else {
tabbedPane.addTab(path.getName(), (Component) newPanels.keySet().toArray()[newPanels.size()-1]);
Main.createModel(path, ((TablePanel) newPanels.keySet().toArray()[newPanels.size()-1]).getTable());
tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1);
}
}
}

private void openSaveDialog() {
// Check here if the tab's first index contains '*' signifying it's been changed.
// The '*' should be added if the table has been changed.
int result = JOptionPane.showConfirmDialog(frame, "Do you want to overwrite changes?", "Save", JOptionPane.YES_NO_OPTION);
if(result == JOptionPane.YES_OPTION) {
Main.saveModel(newPanels.get(tabbedPane.getSelectedComponent()).getAbsolutePath(), ((TablePanel) tabbedPane.getSelectedComponent()).getTable());
}
}

class TablePanel extends JPanel {
Expand All @@ -184,6 +146,7 @@ public TablePanel() {
table.setShowGrid(true);
table.setAutoResizeMode(0);
table.setCellSelectionEnabled(true);
// Add TableModelListener

JScrollPane scrollPane = new JScrollPane(table);

Expand All @@ -200,6 +163,10 @@ public JTable getTable() {
}
}

public static JTabbedPane getTabbedPane() {
return tabbedPane;
}

public static JFrame getFrame() {
return frame;
}
Expand Down
Binary file modified target/classes/com/secres/Model$1.class
Binary file not shown.
Binary file modified target/classes/com/secres/Model$2.class
Binary file not shown.
Binary file modified target/classes/com/secres/Model.class
Binary file not shown.
Binary file added target/classes/com/secres/View$1.class
Binary file not shown.
Binary file modified target/classes/com/secres/View$TablePanel.class
Binary file not shown.
Binary file modified target/classes/com/secres/View.class
Binary file not shown.
2 changes: 1 addition & 1 deletion target/maven-archiver/pom.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven
#Tue Mar 02 21:51:03 PST 2021
#Wed Mar 03 13:04:51 PST 2021
groupId=com.secres
artifactId=secrescsv
version=1.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
com\secres\View$1.class
com\secres\Model$1.class
com\secres\Main.class
com\secres\View.class
Expand Down
Binary file modified target/original-secrescsv-1.0-SNAPSHOT.jar
Binary file not shown.
Binary file modified target/secrescsv-1.0-SNAPSHOT.jar
Binary file not shown.

0 comments on commit 6a6c3d2

Please sign in to comment.