Skip to content

Commit

Permalink
Merge pull request #51 from Shubhamr837/master
Browse files Browse the repository at this point in the history
Disable Reset button while download is in progress and vice versa.
  • Loading branch information
wbrunette authored Apr 1, 2021
2 parents 1d0f232 + e3f43c6 commit a9b9083
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 140 deletions.
17 changes: 14 additions & 3 deletions 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 All @@ -10,25 +11,35 @@ public class IOPanel extends JPanel {
public static final String PUSH_TAB_LABEL = "Upload";

private MainPanel parent;
private PullPanel pullPanel;
private PushPanel pushPanel;

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

this.parent = parent;

JTabbedPane tabs = new JTabbedPane();
tabs.addTab(PULL_TAB_LABEL, new PullPanel(this));
tabs.addTab(PUSH_TAB_LABEL, new PushPanel(this));
pullPanel = new PullPanel(this);
pushPanel = new PushPanel(this);
tabs.addTab(PULL_TAB_LABEL, pullPanel);
tabs.addTab(PUSH_TAB_LABEL, pushPanel);

JSplitPane splitPane =
new JSplitPane(JSplitPane.VERTICAL_SPLIT, tabs, parent.getProgressBar());
new JSplitPane(JSplitPane.VERTICAL_SPLIT, tabs, parent.getProgressBar());
splitPane.setResizeWeight(0.8);
splitPane.setDividerSize(0);
splitPane.setEnabled(false);

this.add(splitPane, LayoutDefault.getDefaultGbc());
}

public void setButtonsState(ButtonState pushButtonState, ButtonState pullButtonState, ButtonState resetButtonState)
{
pushPanel.setButtonsState(pushButtonState,resetButtonState);
pullPanel.setsPullButtonState(pullButtonState);
}

public CloudEndpointInfo getCloudEndpointInfo() {
return parent.getCloudEndpointInfo();
}
Expand Down
231 changes: 118 additions & 113 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,119 +18,123 @@
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
sPullButton.setEnabled(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) {
sPullButton.setEnabled(pullButtonState.getButtonStateBooleanValue());
}

@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);
}

@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);
sPullButton.setEnabled(true);
}
}
}
Loading

0 comments on commit a9b9083

Please sign in to comment.