Skip to content

Commit

Permalink
Add ButtonState enum
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubhamr837 committed Mar 30, 2021
1 parent d1fa2d1 commit efa0e49
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 125 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/opendatakit/suitcase/ui/IOPanel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.opendatakit.suitcase.ui;

import org.opendatakit.suitcase.model.CloudEndpointInfo;
import org.opendatakit.suitcase.utils.ButtonState;

import javax.swing.*;
import java.awt.*;
Expand Down Expand Up @@ -33,7 +34,7 @@ public IOPanel(MainPanel parent) {
this.add(splitPane, LayoutDefault.getDefaultGbc());
}

public void setButtonsState(boolean pushButtonState,boolean pullButtonState,boolean resetButtonState)
public void setButtonsState(ButtonState pushButtonState, ButtonState pullButtonState, ButtonState resetButtonState)
{
pushPanel.setButtonsState(pushButtonState,resetButtonState);
pullPanel.setsPullButtonState(pullButtonState);
Expand Down
240 changes: 122 additions & 118 deletions src/main/java/org/opendatakit/suitcase/ui/PullPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.opendatakit.suitcase.net.DownloadTask;
import org.opendatakit.suitcase.net.SuitcaseSwingWorker;
import org.apache.wink.json4j.JSONException;
import org.opendatakit.suitcase.utils.ButtonState;
import org.opendatakit.suitcase.utils.FieldsValidatorUtils;
import org.opendatakit.suitcase.utils.FileUtils;

Expand All @@ -17,124 +18,127 @@
import java.beans.PropertyChangeListener;

public class PullPanel extends JPanel implements PropertyChangeListener {
private static final String DOWNLOAD_LABEL = "Download";
private static final String DOWNLOADING_LABEL = "Downloading";
private static final String SAVE_PATH_LABEL = "Save to";

// ui components
private JCheckBox sDownloadAttachment;
private JCheckBox sApplyScanFmt;
private JCheckBox sExtraMetadata;
private JButton sPullButton;
private JTextField sTableIdText;
private PathChooserPanel savePathChooser;

// other instance vars
private IOPanel parent;
private AttachmentManager attachMngr;
private ODKCsv csv;

public PullPanel(IOPanel parent) {
super(new GridBagLayout());

this.parent = parent;
this.attachMngr = null;
this.csv = null;

this.sDownloadAttachment = new JCheckBox();
this.sApplyScanFmt = new JCheckBox();
this.sExtraMetadata = new JCheckBox();
this.sPullButton = new JButton();
this.sTableIdText = new JTextField(1);
this.savePathChooser = new PathChooserPanel(
SAVE_PATH_LABEL, FileUtils.getDefaultSavePath().toString()
);

GridBagConstraints gbc = LayoutDefault.getDefaultGbc();
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;

JPanel pullInputPanel = new InputPanel(
new String[] {"Table ID"},
new JTextField[] {sTableIdText},
new String[] {"table_id"}
);
gbc.weighty = 2;
this.add(pullInputPanel, gbc);

JPanel pullPrefPanel = new CheckboxPanel(
new String[] {"Download attachments?", "Apply Scan formatting?", "Extra metadata columns?"},
new JCheckBox[] {sDownloadAttachment, sApplyScanFmt, sExtraMetadata}, 3, 1
);
gbc.weighty = 5;
this.add(pullPrefPanel, gbc);

gbc.weighty = 1;
this.add(this.savePathChooser, gbc);

JPanel pullButtonPanel = new JPanel(new GridLayout(1, 1));
buildPullButtonArea(pullButtonPanel);
gbc.weighty = 2;
gbc.insets = new Insets(10, 0, 0, 0);
this.add(pullButtonPanel, gbc);
}

private void buildPullButtonArea(JPanel pullButtonPanel) {
sPullButton.setText(DOWNLOAD_LABEL);
sPullButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sTableIdText.setText(sTableIdText.getText().trim());

String error = FieldsValidatorUtils.checkDownloadFields(
sTableIdText.getText(), savePathChooser.getPath(), parent.getCloudEndpointInfo());

if (error != null) {
DialogUtils.showError(error, true);
} else {
// disable download button
parent.setButtonsState(false,false,false);

sPullButton.setText(DOWNLOADING_LABEL);

CsvConfig config = new CsvConfig(sDownloadAttachment.isSelected(), sApplyScanFmt.isSelected(), sExtraMetadata.isSelected());

if (attachMngr == null) {
attachMngr = new AttachmentManager(parent.getCloudEndpointInfo(), sTableIdText.getText(),
savePathChooser.getPath());
} else {
attachMngr.setSavePath(savePathChooser.getPath());
}

// create a new csv instance when csv == null or when table id changed
if (csv == null || !csv.getTableId().equals(sTableIdText.getText())) {
try {
csv = new ODKCsv(attachMngr, parent.getCloudEndpointInfo(), sTableIdText.getText());
} catch (JSONException e1) { /*should never happen*/ }
}

DownloadTask worker = new DownloadTask(parent.getCloudEndpointInfo(), csv, config, savePathChooser.getPath(), true);
worker.addPropertyChangeListener(parent.getProgressBar());
worker.addPropertyChangeListener(PullPanel.this);
worker.execute();
private static final String DOWNLOAD_LABEL = "Download";
private static final String DOWNLOADING_LABEL = "Downloading";
private static final String SAVE_PATH_LABEL = "Save to";

// ui components
private JCheckBox sDownloadAttachment;
private JCheckBox sApplyScanFmt;
private JCheckBox sExtraMetadata;
private JButton sPullButton;
private JTextField sTableIdText;
private PathChooserPanel savePathChooser;

// other instance vars
private IOPanel parent;
private AttachmentManager attachMngr;
private ODKCsv csv;

public PullPanel(IOPanel parent) {
super(new GridBagLayout());

this.parent = parent;
this.attachMngr = null;
this.csv = null;

this.sDownloadAttachment = new JCheckBox();
this.sApplyScanFmt = new JCheckBox();
this.sExtraMetadata = new JCheckBox();
this.sPullButton = new JButton();
this.sTableIdText = new JTextField(1);
this.savePathChooser = new PathChooserPanel(
SAVE_PATH_LABEL, FileUtils.getDefaultSavePath().toString()
);

GridBagConstraints gbc = LayoutDefault.getDefaultGbc();
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;

JPanel pullInputPanel = new InputPanel(
new String[]{"Table ID"},
new JTextField[]{sTableIdText},
new String[]{"table_id"}
);
gbc.weighty = 2;
this.add(pullInputPanel, gbc);

JPanel pullPrefPanel = new CheckboxPanel(
new String[]{"Download attachments?", "Apply Scan formatting?", "Extra metadata columns?"},
new JCheckBox[]{sDownloadAttachment, sApplyScanFmt, sExtraMetadata}, 3, 1
);
gbc.weighty = 5;
this.add(pullPrefPanel, gbc);

gbc.weighty = 1;
this.add(this.savePathChooser, gbc);

JPanel pullButtonPanel = new JPanel(new GridLayout(1, 1));
buildPullButtonArea(pullButtonPanel);
gbc.weighty = 2;
gbc.insets = new Insets(10, 0, 0, 0);
this.add(pullButtonPanel, gbc);
}

private void buildPullButtonArea(JPanel pullButtonPanel) {
sPullButton.setText(DOWNLOAD_LABEL);
sPullButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sTableIdText.setText(sTableIdText.getText().trim());

String error = FieldsValidatorUtils.checkDownloadFields(
sTableIdText.getText(), savePathChooser.getPath(), parent.getCloudEndpointInfo());

if (error != null) {
DialogUtils.showError(error, true);
} else {
// disable download button
parent.setButtonsState(ButtonState.DISABLED, ButtonState.DISABLED, ButtonState.DISABLED);

sPullButton.setText(DOWNLOADING_LABEL);

CsvConfig config = new CsvConfig(sDownloadAttachment.isSelected(), sApplyScanFmt.isSelected(), sExtraMetadata.isSelected());

if (attachMngr == null) {
attachMngr = new AttachmentManager(parent.getCloudEndpointInfo(), sTableIdText.getText(),
savePathChooser.getPath());
} else {
attachMngr.setSavePath(savePathChooser.getPath());
}

// create a new csv instance when csv == null or when table id changed
if (csv == null || !csv.getTableId().equals(sTableIdText.getText())) {
try {
csv = new ODKCsv(attachMngr, parent.getCloudEndpointInfo(), sTableIdText.getText());
} catch (JSONException e1) { /*should never happen*/ }
}

DownloadTask worker = new DownloadTask(parent.getCloudEndpointInfo(), csv, config, savePathChooser.getPath(), true);
worker.addPropertyChangeListener(parent.getProgressBar());
worker.addPropertyChangeListener(PullPanel.this);
worker.execute();
}
}
});

pullButtonPanel.add(sPullButton);
}

public void setsPullButtonState(ButtonState pullButtonState) {
if (pullButtonState == ButtonState.ENABLED) {
sPullButton.setEnabled(true);
} else if (pullButtonState == ButtonState.DISABLED) {
sPullButton.setEnabled(false);
}
}

@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getNewValue() != null && evt.getPropertyName().equals(SuitcaseSwingWorker.DONE_PROPERTY)) {
// re-enable download button and restore its label
sPullButton.setText(DOWNLOAD_LABEL);
parent.setButtonsState(ButtonState.ENABLED, ButtonState.ENABLED, ButtonState.ENABLED);
}
}
});

pullButtonPanel.add(sPullButton);
}

public void setsPullButtonState(boolean state)
{
sPullButton.setEnabled(state);
}

@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getNewValue() != null && evt.getPropertyName().equals(SuitcaseSwingWorker.DONE_PROPERTY)) {
// re-enable download button and restore its label
sPullButton.setText(DOWNLOAD_LABEL);
parent.setButtonsState(true,true,true);
}
}
}
25 changes: 19 additions & 6 deletions src/main/java/org/opendatakit/suitcase/ui/PushPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.opendatakit.suitcase.net.ResetTask;
import org.opendatakit.suitcase.net.SuitcaseSwingWorker;
import org.opendatakit.suitcase.net.UploadTask;
import org.opendatakit.suitcase.utils.ButtonState;
import org.opendatakit.suitcase.utils.FieldsValidatorUtils;
import org.opendatakit.suitcase.utils.FileUtils;

Expand Down Expand Up @@ -92,7 +93,7 @@ public void actionPerformed(ActionEvent e) {
DialogUtils.showError(error, true);
} else {
sPushButton.setText(PUSHING_LABEL);
parent.setButtonsState(false,false,false);
parent.setButtonsState(ButtonState.DISABLED, ButtonState.DISABLED, ButtonState.DISABLED);

UploadTask worker = new UploadTask(parent.getCloudEndpointInfo(), dataPathChooser.getPath(),
sVersionPushText.getText(), true, null, null);
Expand All @@ -118,7 +119,7 @@ public void actionPerformed(ActionEvent e) {
if(DialogUtils.promptConfirm("Are you sure you want to RESET? "
+ "This will delete ALL your data on the server?", true, false)) {
sResetButton.setText(RESETTING_LABEL);
parent.setButtonsState(false,false , false);
parent.setButtonsState(ButtonState.DISABLED, ButtonState.DISABLED , ButtonState.DISABLED);

ResetTask worker = new ResetTask(sVersionPushText.getText(), true);
worker.addPropertyChangeListener(parent.getProgressBar());
Expand All @@ -134,15 +135,27 @@ public void actionPerformed(ActionEvent e) {
pushButtonPanel.add(sPushButton, gbc);
}

public void setButtonsState(boolean pushButtonState , boolean resetButtonState) {
sPushButton.setEnabled(pushButtonState);
sResetButton.setEnabled(resetButtonState);
public void setButtonsState(ButtonState pushButtonState , ButtonState resetButtonState) {
if(pushButtonState== ButtonState.ENABLED) {
sPushButton.setEnabled(true);
}
else if(pushButtonState== ButtonState.DISABLED)
{
sPushButton.setEnabled(false);
}
if(resetButtonState== ButtonState.ENABLED) {
sResetButton.setEnabled(true);
}
else if(resetButtonState== ButtonState.DISABLED)
{
sResetButton.setEnabled(false);
}
}

@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getNewValue() != null && evt.getPropertyName().equals(SuitcaseSwingWorker.DONE_PROPERTY)) {
parent.setButtonsState(true,true,true);
parent.setButtonsState(ButtonState.ENABLED, ButtonState.ENABLED, ButtonState.ENABLED);
sPushButton.setText(PUSH_LABEL);
sResetButton.setText(RESET_LABEL);
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/opendatakit/suitcase/utils/ButtonState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.opendatakit.suitcase.utils;

public enum ButtonState {
ENABLED,
DISABLED
}

0 comments on commit efa0e49

Please sign in to comment.