diff options
author | Jakob Heher <jakob.heher@iaik.tugraz.at> | 2022-08-09 13:44:42 +0200 |
---|---|---|
committer | Jakob Heher <jakob.heher@iaik.tugraz.at> | 2022-08-09 13:44:42 +0200 |
commit | 2a971186dc2b19d469f69263fd5848d1cbf0f344 (patch) | |
tree | 1072bbcf6b1b3d2c03f2cda2545dd866eca177d3 | |
parent | 1489ade104320b959684c9112b47277e0cbe82bb (diff) | |
download | pdf-over-2a971186dc2b19d469f69263fd5848d1cbf0f344.tar.gz pdf-over-2a971186dc2b19d469f69263fd5848d1cbf0f344.tar.bz2 pdf-over-2a971186dc2b19d469f69263fd5848d1cbf0f344.zip |
deduplicate code in ConfigurationComposite
-rw-r--r-- | pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/ConfigurationComposite.java | 159 |
1 files changed, 49 insertions, 110 deletions
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/ConfigurationComposite.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/ConfigurationComposite.java index b5ae10cb..d88c66dc 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/ConfigurationComposite.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/ConfigurationComposite.java @@ -16,6 +16,8 @@ package at.asit.pdfover.gui.composites; import java.io.IOException; +import java.util.Iterator; +import java.util.NoSuchElementException; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.ScrolledComposite; @@ -77,6 +79,32 @@ public class ConfigurationComposite extends StateComposite { */ ConfigurationCompositeBase aboutConfigComposite; + private final Iterable<ConfigurationCompositeBase> _compositeIterable = new Iterable<>() { + @Override + public Iterator<ConfigurationCompositeBase> iterator() { + return new Iterator<>() { + int i = 0; + @Override + public boolean hasNext() { + return i < 4; + } + + @Override + public ConfigurationCompositeBase next() { + if ((i == 2) && (keystoreConfigComposite == null)) ++i; + switch (++i) { + case 1: return simpleConfigComposite; + case 2: return advancedConfigComposite; + case 3: return keystoreConfigComposite; + case 4: return aboutConfigComposite; + } + throw new NoSuchElementException(); + } + }; + } + }; + private Iterable<ConfigurationCompositeBase> composites() { return _compositeIterable; } + /** * The TabFolder */ @@ -254,110 +282,33 @@ public class ConfigurationComposite extends StateComposite { this.configProvider = provider; if (this.configProvider != null) { // Initialize Configuration Container - this.simpleConfigComposite.initConfiguration(this.configProvider); - this.advancedConfigComposite.initConfiguration(this.configProvider); - this.aboutConfigComposite.initConfiguration(this.configProvider); - - this.simpleConfigComposite.loadConfiguration(); - this.advancedConfigComposite.loadConfiguration(); - this.aboutConfigComposite.loadConfiguration(); - - if (this.keystoreConfigComposite != null) + for (ConfigurationCompositeBase c : composites()) { - this.keystoreConfigComposite.initConfiguration(this.configProvider); - this.keystoreConfigComposite.loadConfiguration(); + c.initConfiguration(this.configProvider); + c.loadConfiguration(); } } } - // TODO lots and lots of duplicate code in here boolean storeConfiguration() { - boolean status = false; - boolean redo = false; - int resumeIndex = 0; try { - do { - try { - this.simpleConfigComposite.validateSettings(resumeIndex); - - redo = false; - status = true; - } catch (ResumableException e) { - log.error("Settings validation failed!", e); - ErrorDialog dialog = new ErrorDialog(getShell(), - e.getMessage(), BUTTONS.ABORT_RETRY_IGNORE); - int rc = dialog.open(); - - redo = (rc == SWT.RETRY); - if (rc == SWT.IGNORE) - { - resumeIndex = e.getResumeIndex(); - redo = true; - } - } - } while (redo); - - if (!status) { - return false; - } - - status = false; - redo = false; - resumeIndex = 0; - - do { - try { - this.advancedConfigComposite.validateSettings(resumeIndex); - - redo = false; - status = true; - } catch (ResumableException e) { - log.error("Settings validation failed!", e); - ErrorDialog dialog = new ErrorDialog(getShell(), - e.getMessage(), BUTTONS.ABORT_RETRY_IGNORE); - int rc = dialog.open(); - - redo = (rc == SWT.RETRY); - if (rc == SWT.IGNORE) - { - resumeIndex = e.getResumeIndex(); - redo = true; - } - } - } while (redo); - - if (!status) { - return false; - } - - if (this.keystoreConfigComposite != null) { - status = false; - redo = false; - resumeIndex = 0; - - do { + for (ConfigurationCompositeBase c : composites()) { + int resumeIndex = 0; + while (true) { try { - this.keystoreConfigComposite.validateSettings(resumeIndex); - - redo = false; - status = true; + c.validateSettings(resumeIndex); + break; } catch (ResumableException e) { - log.error("Settings validation failed!", e); + log.info("Settings validation failed!", e); ErrorDialog dialog = new ErrorDialog(getShell(), - e.getMessage(), BUTTONS.ABORT_RETRY_IGNORE); + e.getMessage(), BUTTONS.ABORT_RETRY_IGNORE); int rc = dialog.open(); - redo = (rc == SWT.RETRY); + if (rc == SWT.ABORT) + return false; if (rc == SWT.IGNORE) - { resumeIndex = e.getResumeIndex(); - redo = true; - } } - } while (redo); - - if (!status) { - return false; } } } catch (Exception e) { @@ -371,36 +322,24 @@ public class ConfigurationComposite extends StateComposite { return false; } - if (!status) { - return false; - } - // Write current Configuration - this.simpleConfigComposite.storeConfiguration(this.configProvider); - this.advancedConfigComposite.storeConfiguration(this.configProvider); - this.aboutConfigComposite.storeConfiguration(this.configProvider); - if (this.keystoreConfigComposite != null) - this.keystoreConfigComposite.storeConfiguration(this.configProvider); - - status = false; - redo = false; - do { + for (ConfigurationCompositeBase c : composites()) + c.storeConfiguration(this.configProvider); + + while (true) { // Save current config to file try { this.configProvider.saveToDisk(); - redo = false; - status = true; + return true; } catch (IOException e) { log.error("Failed to save configuration to file!", e); ErrorDialog dialog = new ErrorDialog(getShell(), Messages.getString("error.FailedToSaveSettings"), BUTTONS.RETRY_CANCEL); - redo = (dialog.open() == SWT.RETRY); - - // return false; + int rc = dialog.open(); + if (rc == SWT.CANCEL) + return false; } - } while (redo); - - return status; + } } /** |