From 9489518eb7d8469ba1e13ba04241b18e9870d45f Mon Sep 17 00:00:00 2001 From: shubhamr837 Date: Wed, 24 Mar 2021 00:01:11 +0530 Subject: [PATCH 1/5] Disable all other buttons while any operation is being performed. --- .../org/opendatakit/suitcase/ui/IOPanel.java | 16 ++++++-- .../opendatakit/suitcase/ui/PullPanel.java | 25 ++++++----- .../opendatakit/suitcase/ui/PushPanel.java | 41 ++++++++++--------- 3 files changed, 50 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/opendatakit/suitcase/ui/IOPanel.java b/src/main/java/org/opendatakit/suitcase/ui/IOPanel.java index b06cc10..91146a4 100644 --- a/src/main/java/org/opendatakit/suitcase/ui/IOPanel.java +++ b/src/main/java/org/opendatakit/suitcase/ui/IOPanel.java @@ -10,6 +10,8 @@ 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()); @@ -17,11 +19,13 @@ public IOPanel(MainPanel parent) { 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); @@ -29,6 +33,12 @@ public IOPanel(MainPanel parent) { this.add(splitPane, LayoutDefault.getDefaultGbc()); } + public void setButtonState(boolean state) + { + pushPanel.setButtonState(state); + pullPanel.setsPullButtonState(state); + } + public CloudEndpointInfo getCloudEndpointInfo() { return parent.getCloudEndpointInfo(); } diff --git a/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java b/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java index 9c8af8b..f3a0f57 100644 --- a/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java +++ b/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java @@ -47,7 +47,7 @@ public PullPanel(IOPanel parent) { this.sPullButton = new JButton(); this.sTableIdText = new JTextField(1); this.savePathChooser = new PathChooserPanel( - SAVE_PATH_LABEL, FileUtils.getDefaultSavePath().toString() + SAVE_PATH_LABEL, FileUtils.getDefaultSavePath().toString() ); GridBagConstraints gbc = LayoutDefault.getDefaultGbc(); @@ -55,16 +55,16 @@ public PullPanel(IOPanel parent) { gbc.gridy = GridBagConstraints.RELATIVE; JPanel pullInputPanel = new InputPanel( - new String[] {"Table ID"}, - new JTextField[] {sTableIdText}, - new String[] {"table_id"} + 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 + new String[] {"Download attachments?", "Apply Scan formatting?", "Extra metadata columns?"}, + new JCheckBox[] {sDownloadAttachment, sApplyScanFmt, sExtraMetadata}, 3, 1 ); gbc.weighty = 5; this.add(pullPrefPanel, gbc); @@ -87,13 +87,13 @@ public void actionPerformed(ActionEvent e) { sTableIdText.setText(sTableIdText.getText().trim()); String error = FieldsValidatorUtils.checkDownloadFields( - sTableIdText.getText(), savePathChooser.getPath(), parent.getCloudEndpointInfo()); + sTableIdText.getText(), savePathChooser.getPath(), parent.getCloudEndpointInfo()); if (error != null) { DialogUtils.showError(error, true); } else { // disable download button - sPullButton.setEnabled(false); + parent.setButtonState(false); sPullButton.setText(DOWNLOADING_LABEL); @@ -101,7 +101,7 @@ public void actionPerformed(ActionEvent e) { if (attachMngr == null) { attachMngr = new AttachmentManager(parent.getCloudEndpointInfo(), sTableIdText.getText(), - savePathChooser.getPath()); + savePathChooser.getPath()); } else { attachMngr.setSavePath(savePathChooser.getPath()); } @@ -124,12 +124,17 @@ public void actionPerformed(ActionEvent e) { 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); - sPullButton.setEnabled(true); + parent.setButtonState(true); } } } diff --git a/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java b/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java index 21b81f1..081abd2 100644 --- a/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java +++ b/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java @@ -36,7 +36,7 @@ public PushPanel(IOPanel parent) { this.sPushButton = new JButton(); this.sResetButton = new JButton(); this.dataPathChooser = new PathChooserPanel( - DATA_PATH_LABEL, FileUtils.getDefaultUploadPath().toString() + DATA_PATH_LABEL, FileUtils.getDefaultUploadPath().toString() ); GridBagConstraints gbc = LayoutDefault.getDefaultGbc(); @@ -44,9 +44,9 @@ public PushPanel(IOPanel parent) { gbc.gridy = GridBagConstraints.RELATIVE; JPanel pushInputPanel = new InputPanel( - new String[] {"Version"}, - new JTextField[] {sVersionPushText}, - new String[] {"2"} + new String[] {"Version"}, + new JTextField[] {sVersionPushText}, + new String[] {"2"} ); gbc.weighty = 2; this.add(pushInputPanel, gbc); @@ -57,9 +57,9 @@ public PushPanel(IOPanel parent) { JCheckBox placeholderCheckbox2 = new JCheckBox(); placeholderCheckbox2.setVisible(false); JPanel pushPrefPanel = new CheckboxPanel( - new String[] {"Option Placeholder", "Option Placeholder 2"}, - new JCheckBox[] {placeholderCheckbox, placeholderCheckbox2}, - 2, 1 + new String[] {"Option Placeholder", "Option Placeholder 2"}, + new JCheckBox[] {placeholderCheckbox, placeholderCheckbox2}, + 2, 1 ); gbc.weighty = 5; this.add(pushPrefPanel, gbc); @@ -86,16 +86,16 @@ public void actionPerformed(ActionEvent e) { sVersionPushText.setText(sVersionPushText.getText().trim()); String error = FieldsValidatorUtils.checkUploadFields(sVersionPushText.getText(), - dataPathChooser.getPath(), UploadTask.RESET_APP_OP); + dataPathChooser.getPath(), UploadTask.RESET_APP_OP); if (error != null) { DialogUtils.showError(error, true); } else { sPushButton.setText(PUSHING_LABEL); - setButtonState(false); + parent.setButtonState(false); UploadTask worker = new UploadTask(parent.getCloudEndpointInfo(), dataPathChooser.getPath(), - sVersionPushText.getText(), true, null, null); + sVersionPushText.getText(), true, null, null); worker.addPropertyChangeListener(parent.getProgressBar()); worker.addPropertyChangeListener(PushPanel.this); worker.execute(); @@ -114,13 +114,16 @@ public void actionPerformed(ActionEvent e) { if (error != null) { DialogUtils.showError(error, true); } else { - sResetButton.setText(RESETTING_LABEL); - setButtonState(false); - - ResetTask worker = new ResetTask(sVersionPushText.getText(), true); - worker.addPropertyChangeListener(parent.getProgressBar()); - worker.addPropertyChangeListener(PushPanel.this); - worker.execute(); + 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.setButtonState(false); + + ResetTask worker = new ResetTask(sVersionPushText.getText(), true); + worker.addPropertyChangeListener(parent.getProgressBar()); + worker.addPropertyChangeListener(PushPanel.this); + worker.execute(); + } } } }); @@ -129,7 +132,7 @@ public void actionPerformed(ActionEvent e) { pushButtonPanel.add(sPushButton, gbc); } - private void setButtonState(boolean state) { + public void setButtonState(boolean state) { sPushButton.setEnabled(state); sResetButton.setEnabled(state); } @@ -137,7 +140,7 @@ private void setButtonState(boolean state) { @Override public void propertyChange(PropertyChangeEvent evt) { if (evt.getNewValue() != null && evt.getPropertyName().equals(SuitcaseSwingWorker.DONE_PROPERTY)) { - setButtonState(true); + parent.setButtonState(true); sPushButton.setText(PUSH_LABEL); sResetButton.setText(RESET_LABEL); } From f2c8da80f0a829ab91cbb323efca0a8d1553b708 Mon Sep 17 00:00:00 2001 From: shubhamr837 Date: Fri, 26 Mar 2021 11:42:23 +0530 Subject: [PATCH 2/5] Make function definition more descriptive --- .../java/org/opendatakit/suitcase/ui/IOPanel.java | 6 +++--- .../java/org/opendatakit/suitcase/ui/PullPanel.java | 4 ++-- .../java/org/opendatakit/suitcase/ui/PushPanel.java | 12 ++++++------ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/opendatakit/suitcase/ui/IOPanel.java b/src/main/java/org/opendatakit/suitcase/ui/IOPanel.java index 91146a4..a15b23e 100644 --- a/src/main/java/org/opendatakit/suitcase/ui/IOPanel.java +++ b/src/main/java/org/opendatakit/suitcase/ui/IOPanel.java @@ -33,10 +33,10 @@ public IOPanel(MainPanel parent) { this.add(splitPane, LayoutDefault.getDefaultGbc()); } - public void setButtonState(boolean state) + public void setButtonState(boolean pushButtonState,boolean pullButtonState,boolean resetButtonState) { - pushPanel.setButtonState(state); - pullPanel.setsPullButtonState(state); + pushPanel.setButtonsState(pushButtonState,resetButtonState); + pullPanel.setsPullButtonState(pullButtonState); } public CloudEndpointInfo getCloudEndpointInfo() { diff --git a/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java b/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java index f3a0f57..d33df58 100644 --- a/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java +++ b/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java @@ -93,7 +93,7 @@ public void actionPerformed(ActionEvent e) { DialogUtils.showError(error, true); } else { // disable download button - parent.setButtonState(false); + parent.setButtonState(false,false,false); sPullButton.setText(DOWNLOADING_LABEL); @@ -134,7 +134,7 @@ 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.setButtonState(true); + parent.setButtonState(true,true,true); } } } diff --git a/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java b/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java index 081abd2..b759b42 100644 --- a/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java +++ b/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java @@ -92,7 +92,7 @@ public void actionPerformed(ActionEvent e) { DialogUtils.showError(error, true); } else { sPushButton.setText(PUSHING_LABEL); - parent.setButtonState(false); + parent.setButtonState(false,false,false); UploadTask worker = new UploadTask(parent.getCloudEndpointInfo(), dataPathChooser.getPath(), sVersionPushText.getText(), true, null, null); @@ -117,7 +117,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.setButtonState(false); + parent.setButtonState(false,false , false); ResetTask worker = new ResetTask(sVersionPushText.getText(), true); worker.addPropertyChangeListener(parent.getProgressBar()); @@ -132,15 +132,15 @@ public void actionPerformed(ActionEvent e) { pushButtonPanel.add(sPushButton, gbc); } - public void setButtonState(boolean state) { - sPushButton.setEnabled(state); - sResetButton.setEnabled(state); + public void setButtonsState(boolean pushButtonState , boolean resetButtonState) { + sPushButton.setEnabled(pushButtonState); + sResetButton.setEnabled(resetButtonState); } @Override public void propertyChange(PropertyChangeEvent evt) { if (evt.getNewValue() != null && evt.getPropertyName().equals(SuitcaseSwingWorker.DONE_PROPERTY)) { - parent.setButtonState(true); + parent.setButtonState(true,true,true); sPushButton.setText(PUSH_LABEL); sResetButton.setText(RESET_LABEL); } From d1fa2d1526ed3b41e8b1521706f7c033a325354b Mon Sep 17 00:00:00 2001 From: shubhamr837 Date: Fri, 26 Mar 2021 11:45:04 +0530 Subject: [PATCH 3/5] Correct function name --- src/main/java/org/opendatakit/suitcase/ui/IOPanel.java | 2 +- src/main/java/org/opendatakit/suitcase/ui/PullPanel.java | 4 ++-- src/main/java/org/opendatakit/suitcase/ui/PushPanel.java | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/opendatakit/suitcase/ui/IOPanel.java b/src/main/java/org/opendatakit/suitcase/ui/IOPanel.java index a15b23e..072d4ad 100644 --- a/src/main/java/org/opendatakit/suitcase/ui/IOPanel.java +++ b/src/main/java/org/opendatakit/suitcase/ui/IOPanel.java @@ -33,7 +33,7 @@ public IOPanel(MainPanel parent) { this.add(splitPane, LayoutDefault.getDefaultGbc()); } - public void setButtonState(boolean pushButtonState,boolean pullButtonState,boolean resetButtonState) + public void setButtonsState(boolean pushButtonState,boolean pullButtonState,boolean resetButtonState) { pushPanel.setButtonsState(pushButtonState,resetButtonState); pullPanel.setsPullButtonState(pullButtonState); diff --git a/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java b/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java index d33df58..71ba350 100644 --- a/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java +++ b/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java @@ -93,7 +93,7 @@ public void actionPerformed(ActionEvent e) { DialogUtils.showError(error, true); } else { // disable download button - parent.setButtonState(false,false,false); + parent.setButtonsState(false,false,false); sPullButton.setText(DOWNLOADING_LABEL); @@ -134,7 +134,7 @@ 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.setButtonState(true,true,true); + parent.setButtonsState(true,true,true); } } } diff --git a/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java b/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java index 8f3555c..17925d5 100644 --- a/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java +++ b/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java @@ -92,7 +92,7 @@ public void actionPerformed(ActionEvent e) { DialogUtils.showError(error, true); } else { sPushButton.setText(PUSHING_LABEL); - parent.setButtonState(false,false,false); + parent.setButtonsState(false,false,false); UploadTask worker = new UploadTask(parent.getCloudEndpointInfo(), dataPathChooser.getPath(), sVersionPushText.getText(), true, null, null); @@ -118,7 +118,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.setButtonState(false,false , false); + parent.setButtonsState(false,false , false); ResetTask worker = new ResetTask(sVersionPushText.getText(), true); worker.addPropertyChangeListener(parent.getProgressBar()); @@ -142,7 +142,7 @@ public void setButtonsState(boolean pushButtonState , boolean resetButtonState) @Override public void propertyChange(PropertyChangeEvent evt) { if (evt.getNewValue() != null && evt.getPropertyName().equals(SuitcaseSwingWorker.DONE_PROPERTY)) { - parent.setButtonState(true,true,true); + parent.setButtonsState(true,true,true); sPushButton.setText(PUSH_LABEL); sResetButton.setText(RESET_LABEL); } From efa0e49d3599cfe37a55f6a09e31fbf6b0610ff8 Mon Sep 17 00:00:00 2001 From: shubhamr837 Date: Tue, 30 Mar 2021 12:51:04 +0530 Subject: [PATCH 4/5] Add ButtonState enum --- .../org/opendatakit/suitcase/ui/IOPanel.java | 3 +- .../opendatakit/suitcase/ui/PullPanel.java | 240 +++++++++--------- .../opendatakit/suitcase/ui/PushPanel.java | 25 +- .../suitcase/utils/ButtonState.java | 6 + 4 files changed, 149 insertions(+), 125 deletions(-) create mode 100644 src/main/java/org/opendatakit/suitcase/utils/ButtonState.java diff --git a/src/main/java/org/opendatakit/suitcase/ui/IOPanel.java b/src/main/java/org/opendatakit/suitcase/ui/IOPanel.java index 072d4ad..7c6a55f 100644 --- a/src/main/java/org/opendatakit/suitcase/ui/IOPanel.java +++ b/src/main/java/org/opendatakit/suitcase/ui/IOPanel.java @@ -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.*; @@ -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); diff --git a/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java b/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java index 71ba350..32083a2 100644 --- a/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java +++ b/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java @@ -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; @@ -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); } - } } diff --git a/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java b/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java index 17925d5..cff0bb9 100644 --- a/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java +++ b/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java @@ -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; @@ -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); @@ -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()); @@ -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); } diff --git a/src/main/java/org/opendatakit/suitcase/utils/ButtonState.java b/src/main/java/org/opendatakit/suitcase/utils/ButtonState.java new file mode 100644 index 0000000..908b666 --- /dev/null +++ b/src/main/java/org/opendatakit/suitcase/utils/ButtonState.java @@ -0,0 +1,6 @@ +package org.opendatakit.suitcase.utils; + +public enum ButtonState { + ENABLED, + DISABLED +} From e3f43c68edb209a7d6f5cb954f45ba794d5adc7d Mon Sep 17 00:00:00 2001 From: shubhamr837 Date: Wed, 31 Mar 2021 12:13:44 +0530 Subject: [PATCH 5/5] Add method in enum to get boolean value of button state --- .../org/opendatakit/suitcase/ui/PullPanel.java | 6 +----- .../org/opendatakit/suitcase/ui/PushPanel.java | 16 ++-------------- .../opendatakit/suitcase/utils/ButtonState.java | 14 ++++++++++++-- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java b/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java index 32083a2..ba7d00b 100644 --- a/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java +++ b/src/main/java/org/opendatakit/suitcase/ui/PullPanel.java @@ -126,11 +126,7 @@ public void actionPerformed(ActionEvent e) { } public void setsPullButtonState(ButtonState pullButtonState) { - if (pullButtonState == ButtonState.ENABLED) { - sPullButton.setEnabled(true); - } else if (pullButtonState == ButtonState.DISABLED) { - sPullButton.setEnabled(false); - } + sPullButton.setEnabled(pullButtonState.getButtonStateBooleanValue()); } @Override diff --git a/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java b/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java index cff0bb9..4cab106 100644 --- a/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java +++ b/src/main/java/org/opendatakit/suitcase/ui/PushPanel.java @@ -136,20 +136,8 @@ public void actionPerformed(ActionEvent e) { } 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); - } + sPushButton.setEnabled(pushButtonState.getButtonStateBooleanValue()); + sResetButton.setEnabled(resetButtonState.getButtonStateBooleanValue()); } @Override diff --git a/src/main/java/org/opendatakit/suitcase/utils/ButtonState.java b/src/main/java/org/opendatakit/suitcase/utils/ButtonState.java index 908b666..633090b 100644 --- a/src/main/java/org/opendatakit/suitcase/utils/ButtonState.java +++ b/src/main/java/org/opendatakit/suitcase/utils/ButtonState.java @@ -1,6 +1,16 @@ package org.opendatakit.suitcase.utils; public enum ButtonState { - ENABLED, - DISABLED + ENABLED(true), + DISABLED(false); + + boolean state; + + public boolean getButtonStateBooleanValue() { + return this.state; + } + + ButtonState(boolean b) { + this.state = b; + } }