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 +}