Skip to content

Commit

Permalink
Add save button to toolbar, separate save dialog from Menu
Browse files Browse the repository at this point in the history
  • Loading branch information
dinasv committed Mar 12, 2019
1 parent d0c80e7 commit 3a2e367
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 31 deletions.
42 changes: 42 additions & 0 deletions src/Main/java/MVC/View/Components/Dialogs/SaveDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package MVC.View.Components.Dialogs;

import MVC.View.Events.SaveOutputEvent;
import MVC.View.Listeners.Listener;

import javax.swing.*;

/**
*/
public class SaveDialog {

private JFileChooser fileChooser;
private OutputTypeChooser outputTypeChooser;
private JFrame mainFrame;

private Listener<SaveOutputEvent> saveOutputListener;

private static final String SAVE_FILES_DIALOG_NAME = "Select Directory";

public SaveDialog(JFileChooser fileChooser, JFrame mainFrame){
this.fileChooser = fileChooser;
outputTypeChooser = new OutputTypeChooser();
this.mainFrame = mainFrame;
}

public void openDialog(){
initOutputFileChooser();
int action = fileChooser.showDialog(mainFrame, SAVE_FILES_DIALOG_NAME);
saveOutputListener.eventOccurred(new SaveOutputEvent(outputTypeChooser.getChosenOutput(),
outputTypeChooser.getDatasetName(), fileChooser.getSelectedFile().getAbsolutePath(), action));
}

private void initOutputFileChooser(){
fileChooser.resetChoosableFileFilters();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setAccessory(outputTypeChooser);
}

public void setSaveOutputListener(Listener<SaveOutputEvent> saveOutputListener) {
this.saveOutputListener = saveOutputListener;
}
}
36 changes: 30 additions & 6 deletions src/Main/java/MVC/View/Components/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import MVC.Common.CSBFinderRequest;
import MVC.Controller.CSBFinderController;
import MVC.View.Components.Dialogs.ClusterDialog;
import MVC.View.Components.Dialogs.FilterDialog;
import MVC.View.Components.Dialogs.InputParametersDialog;
import MVC.View.Components.Dialogs.ProgressBar;
import MVC.View.Components.Dialogs.*;
import MVC.View.Events.*;
import MVC.View.Images.Icon;
import MVC.View.Listeners.*;
Expand Down Expand Up @@ -39,6 +36,7 @@ public class MainFrame extends JFrame {
private InputParametersDialog inputParamsDialog;
private ClusterDialog clusterDialog;
private FilterDialog filterDialog;
private SaveDialog saveDialog;

private FamiliesFilter familiesFilter;

Expand Down Expand Up @@ -80,7 +78,11 @@ public void init() {
public void clearPanels() {
genomesPanel.clearPanel();
summaryPanel.clearPanel();

toolbar.disableClusterBtn();
toolbar.disableSaveBtn();
menuBar.disableSaveFileBtn();
summaryPanel.disableFilterBtn();
}

public void initComponents() {
Expand All @@ -90,8 +92,10 @@ public void initComponents() {
inputParamsDialog = new InputParametersDialog(fc);
clusterDialog = new ClusterDialog();
filterDialog = new FilterDialog();
saveDialog = new SaveDialog(fc, this);

toolbar = new Toolbar();
toolbar.disableSaveBtn();
genomesPanel = new GenomePanel(colorsUsed);

summaryPanel = new SummaryPanel(Icon.FILTER.getIcon());
Expand All @@ -117,6 +121,7 @@ private void setEventListeners() {
setRunCSBFinderListener();
setRunClusteringListener();
setSelectParamsListener();
setOpenSaveDialogListener();
setClusterListener();
setPatternRowClickedListener();
setFamilyRowClickedListener();
Expand All @@ -139,6 +144,7 @@ private void setMenuListeners() {
public void displayFamilyTable(List<Family> familyList) {
if (familyList.size() > 0) {
menuBar.enableSaveFileBtn();
toolbar.enableSaveBtn();
summaryPanel.enableFilterBtn();
toolbar.enableClusterBtn();

Expand All @@ -157,6 +163,7 @@ private void setGenomesData(String filePath){
} else {
toolbar.disableSelectParamsBtn();
menuBar.disableSaveFileBtn();
toolbar.disableSaveBtn();
}
}

Expand Down Expand Up @@ -295,6 +302,19 @@ public void eventOccurred(OpenDialogEvent e) {
});
}

private void setOpenSaveDialogListener(){
Listener<OpenDialogEvent> listener = new Listener<OpenDialogEvent>() {
@Override
public void eventOccurred(OpenDialogEvent e) {
saveDialog.openDialog();
}
};

menuBar.setSaveOutputListener(listener);
toolbar.setSaveListener(listener);
}


private void setNumOfNeighborsListener() {
toolbar.setSetNumOfNeighborsListener(e -> genomesPanel.setNumOfNeighbors(e.getNumOfNeighbors()));
}
Expand Down Expand Up @@ -438,8 +458,9 @@ protected void done() {
}



private void setSaveButtonListener() {
menuBar.setSaveOutputListener(new Listener<SaveOutputEvent>() {
Listener<SaveOutputEvent> listener = new Listener<SaveOutputEvent>() {
String msg = "";

@Override
Expand Down Expand Up @@ -473,7 +494,10 @@ protected void done() {
swingWorker.execute();
}
}
});
};

saveDialog.setSaveOutputListener(listener);

}

/**
Expand Down
23 changes: 8 additions & 15 deletions src/Main/java/MVC/View/Components/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import MVC.View.Components.Dialogs.InputFileChooser;
import MVC.View.Components.Dialogs.OutputTypeChooser;
import MVC.View.Components.Dialogs.SaveDialog;
import MVC.View.Events.LoadFileEvent;
import MVC.View.Events.OpenDialogEvent;
import MVC.View.Events.SaveOutputEvent;
import MVC.View.Listeners.Listener;

Expand All @@ -19,13 +21,12 @@ public class Menu implements ActionListener {
private static final String LOAD_GENOMES = "Genomes File";
private static final String LOAD_SESSION = "Session File";
private static final String LOAD_COG_INFO = "Orthology Information File";
private static final String SAVE_FILES_DIALOG_NAME = "Select Directory";
private static final String SAVE_FILES = "Save";

private Listener<LoadFileEvent> loadGenomesListener;
private Listener<LoadFileEvent> importSessionListener;
private Listener<LoadFileEvent> loadCogInfoListener;
private Listener<SaveOutputEvent> saveOutputListener;
private Listener<OpenDialogEvent> saveOutputListener;

private JMenuBar mainMenu;
private JMenu menu;
Expand All @@ -36,13 +37,11 @@ public class Menu implements ActionListener {
private JMenuItem saveItem;

private JFileChooser fileChooser;
private OutputTypeChooser outputTypeChooser;

private JFrame mainFrame;

public Menu(JFileChooser fileChooser, JFrame mainFrame){
this.fileChooser = fileChooser;
outputTypeChooser = new OutputTypeChooser();
mainMenu = new JMenuBar();
this.mainFrame = mainFrame;
this.mainFrame.setJMenuBar(mainMenu);
Expand Down Expand Up @@ -109,10 +108,8 @@ public void actionPerformed(ActionEvent e) {

break;
case SAVE_FILES:
initOutputFileChooser();
int action = fileChooser.showDialog(mainFrame, SAVE_FILES_DIALOG_NAME);
saveOutputListener.eventOccurred(new SaveOutputEvent(e, outputTypeChooser.getChosenOutput(),
outputTypeChooser.getDatasetName(), fileChooser.getSelectedFile().getAbsolutePath(), action));

saveOutputListener.eventOccurred(new OpenDialogEvent());

break;
}
Expand All @@ -137,12 +134,6 @@ private void loadEventOccured(ActionEvent e, Listener<LoadFileEvent> listener){
}
}

private void initOutputFileChooser(){
fileChooser.resetChoosableFileFilters();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setAccessory(outputTypeChooser);
}


public void setImportSessionListener(Listener<LoadFileEvent> importSessionListener) {
this.importSessionListener = importSessionListener;
Expand All @@ -156,8 +147,10 @@ public void setLoadCogInfoListener(Listener<LoadFileEvent> loadCogInfoListener)
this.loadCogInfoListener = loadCogInfoListener;
}

public void setSaveOutputListener(Listener<SaveOutputEvent> saveOutputListener) {
public void setSaveOutputListener(Listener<OpenDialogEvent> saveOutputListener) {
this.saveOutputListener = saveOutputListener;
}



}
39 changes: 30 additions & 9 deletions src/Main/java/MVC/View/Components/Toolbar.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package MVC.View.Components;

import MVC.View.Events.OpenDialogEvent;
import MVC.View.Events.SaveOutputEvent;
import MVC.View.Events.SetNumOfNeighborsEvent;
import MVC.View.Images.Icon;
import MVC.View.Listeners.Listener;
Expand All @@ -12,9 +13,11 @@ public class Toolbar extends JPanel{

private static final String SELECT_PARAMS_BTN_NAME = "Find CSBs";
private static final String CLUSTER_BTN_NAME = "Cluster to families";
private static final String SAVE_BTN_NAME = "Save";

private JButton selectParamsBtn;
private JButton clusterBtn;
private JButton saveBtn;

private final Integer[] NEIGHBORS_VALUES = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Expand All @@ -24,6 +27,7 @@ public class Toolbar extends JPanel{

private Listener<OpenDialogEvent> selectParamsListener;
private Listener<OpenDialogEvent> clusterListener;
private Listener<OpenDialogEvent> saveListener;
private Listener<SetNumOfNeighborsEvent> setNumOfNeighborsListener;

public Toolbar() {
Expand All @@ -32,18 +36,13 @@ public Toolbar() {

JPanel buttons = new JPanel(new FlowLayout());

selectParamsBtn = new JButton(Icon.RUN.getIcon());
selectParamsBtn.setBorder(BorderFactory.createEmptyBorder());
selectParamsBtn.setEnabled(false);
selectParamsBtn.setToolTipText(SELECT_PARAMS_BTN_NAME);

clusterBtn = new JButton(Icon.CLUSTER.getIcon());
clusterBtn.setBorder(BorderFactory.createEmptyBorder());
clusterBtn.setEnabled(false);
clusterBtn.setToolTipText(CLUSTER_BTN_NAME);
selectParamsBtn = createToolbarButton(Icon.RUN.getIcon(), SELECT_PARAMS_BTN_NAME);
clusterBtn = createToolbarButton(Icon.CLUSTER.getIcon(), CLUSTER_BTN_NAME);
saveBtn = createToolbarButton(Icon.SAVE.getIcon(), SAVE_BTN_NAME);

buttons.add(selectParamsBtn);
buttons.add(clusterBtn);
buttons.add(saveBtn);

selectNumOfNeighbors = new JComboBox<>(NEIGHBORS_VALUES);
selectNumOfNeighbors.setSelectedIndex(3);
Expand All @@ -60,10 +59,20 @@ public Toolbar() {

selectParamsBtn.addActionListener(e -> selectParamsListener.eventOccurred(new OpenDialogEvent()));
clusterBtn.addActionListener(e -> clusterListener.eventOccurred(new OpenDialogEvent()));
saveBtn.addActionListener(e -> saveListener.eventOccurred(new OpenDialogEvent()));
selectNumOfNeighbors.addActionListener(e -> setNumOfNeighborsListener.eventOccurred(
new SetNumOfNeighborsEvent(getNumOfNeighbors())));
}

private JButton createToolbarButton(ImageIcon icon, String btnName){
JButton btn = new JButton(icon);
btn.setBorder(BorderFactory.createEmptyBorder());
btn.setEnabled(false);
btn.setToolTipText(btnName);

return btn;
}


public void setSelectParamsListener(Listener<OpenDialogEvent> selectParamsListener) {
this.selectParamsListener = selectParamsListener;
Expand All @@ -73,6 +82,10 @@ public void setClusterListener(Listener<OpenDialogEvent> clusterListener) {
this.clusterListener = clusterListener;
}

public void setSaveListener(Listener<OpenDialogEvent> saveListener) {
this.saveListener = saveListener;
}

public void setSetNumOfNeighborsListener(Listener<SetNumOfNeighborsEvent> setNumOfNeighborsListener) {
this.setNumOfNeighborsListener = setNumOfNeighborsListener;
}
Expand All @@ -93,6 +106,14 @@ public void disableClusterBtn() {
clusterBtn.setEnabled(false);
}

public void enableSaveBtn() {
saveBtn.setEnabled(true);
}

public void disableSaveBtn() {
saveBtn.setEnabled(false);
}

public int getNumOfNeighbors(){
return selectNumOfNeighbors.getSelectedIndex();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Main/java/MVC/View/Events/SaveOutputEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class SaveOutputEvent implements Event {
private int action;


public SaveOutputEvent(Object source, OutputType outputType, String datasetName, String outputDirectory,
public SaveOutputEvent(OutputType outputType, String datasetName, String outputDirectory,
int action) {
this.outputType = outputType;
this.datasetName = datasetName;
Expand Down
1 change: 1 addition & 0 deletions src/Main/java/MVC/View/Images/Icon.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum Icon {
QUESTION_MARK("/question.png", "question mark icon"),
RUN("/right-arrow.png", "run icon"),
CLUSTER("/network.png", "cluster icon"),
SAVE("/save.png", "save icon"),
FILTER("/funnel.png", "include results icon");

private ImageIcon icon;
Expand Down

0 comments on commit 3a2e367

Please sign in to comment.