diff options
| author | 2017-03-08 08:17:04 +0100 | |
|---|---|---|
| committer | 2017-03-08 08:17:04 +0100 | |
| commit | 6e464ea251cab63c776ece0b2a356f1498ffa294 (patch) | |
| tree | 5ed30c03f5ac4cd2d6877874f5ede576049954f7 /src/main/java/cuchaz/enigma/gui/dialog | |
| parent | Drop unix case style and implement hashCode when equals is overrided (diff) | |
| download | enigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.tar.gz enigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.tar.xz enigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.zip | |
Follow Fabric guidelines
Diffstat (limited to 'src/main/java/cuchaz/enigma/gui/dialog')
3 files changed, 180 insertions, 188 deletions
diff --git a/src/main/java/cuchaz/enigma/gui/dialog/AboutDialog.java b/src/main/java/cuchaz/enigma/gui/dialog/AboutDialog.java index f690b15..7b3234d 100644 --- a/src/main/java/cuchaz/enigma/gui/dialog/AboutDialog.java +++ b/src/main/java/cuchaz/enigma/gui/dialog/AboutDialog.java | |||
| @@ -8,63 +8,60 @@ | |||
| 8 | * Contributors: | 8 | * Contributors: |
| 9 | * Jeff Martin - initial API and implementation | 9 | * Jeff Martin - initial API and implementation |
| 10 | ******************************************************************************/ | 10 | ******************************************************************************/ |
| 11 | package cuchaz.enigma.gui.dialog; | ||
| 12 | |||
| 13 | import java.awt.Color; | ||
| 14 | import java.awt.Container; | ||
| 15 | import java.awt.Cursor; | ||
| 16 | import java.awt.FlowLayout; | ||
| 17 | import java.io.IOException; | ||
| 18 | 11 | ||
| 19 | import javax.swing.*; | 12 | package cuchaz.enigma.gui.dialog; |
| 20 | 13 | ||
| 21 | import cuchaz.enigma.Constants; | 14 | import cuchaz.enigma.Constants; |
| 22 | import cuchaz.enigma.utils.Utils; | 15 | import cuchaz.enigma.utils.Utils; |
| 23 | 16 | ||
| 17 | import javax.swing.*; | ||
| 18 | import java.awt.*; | ||
| 19 | import java.io.IOException; | ||
| 20 | |||
| 24 | public class AboutDialog { | 21 | public class AboutDialog { |
| 25 | 22 | ||
| 26 | public static void show(JFrame parent) { | 23 | public static void show(JFrame parent) { |
| 27 | // init frame | 24 | // init frame |
| 28 | final JFrame frame = new JFrame(Constants.NAME + " - About"); | 25 | final JFrame frame = new JFrame(Constants.NAME + " - About"); |
| 29 | final Container pane = frame.getContentPane(); | 26 | final Container pane = frame.getContentPane(); |
| 30 | pane.setLayout(new FlowLayout()); | 27 | pane.setLayout(new FlowLayout()); |
| 31 | 28 | ||
| 32 | // load the content | 29 | // load the content |
| 33 | try { | 30 | try { |
| 34 | String html = Utils.readResourceToString("/about.html"); | 31 | String html = Utils.readResourceToString("/about.html"); |
| 35 | html = String.format(html, Constants.NAME, Constants.VERSION); | 32 | html = String.format(html, Constants.NAME, Constants.VERSION); |
| 36 | JLabel label = new JLabel(html); | 33 | JLabel label = new JLabel(html); |
| 37 | label.setHorizontalAlignment(JLabel.CENTER); | 34 | label.setHorizontalAlignment(JLabel.CENTER); |
| 38 | pane.add(label); | 35 | pane.add(label); |
| 39 | } catch (IOException ex) { | 36 | } catch (IOException ex) { |
| 40 | throw new Error(ex); | 37 | throw new Error(ex); |
| 41 | } | 38 | } |
| 42 | 39 | ||
| 43 | // show the link | 40 | // show the link |
| 44 | String html = "<html><a href=\"%s\">%s</a></html>"; | 41 | String html = "<html><a href=\"%s\">%s</a></html>"; |
| 45 | html = String.format(html, Constants.URL, Constants.URL); | 42 | html = String.format(html, Constants.URL, Constants.URL); |
| 46 | JButton link = new JButton(html); | 43 | JButton link = new JButton(html); |
| 47 | link.addActionListener(event -> Utils.openUrl(Constants.URL)); | 44 | link.addActionListener(event -> Utils.openUrl(Constants.URL)); |
| 48 | link.setBorderPainted(false); | 45 | link.setBorderPainted(false); |
| 49 | link.setOpaque(false); | 46 | link.setOpaque(false); |
| 50 | link.setBackground(Color.WHITE); | 47 | link.setBackground(Color.WHITE); |
| 51 | link.setCursor(new Cursor(Cursor.HAND_CURSOR)); | 48 | link.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
| 52 | link.setFocusable(false); | 49 | link.setFocusable(false); |
| 53 | JPanel linkPanel = new JPanel(); | 50 | JPanel linkPanel = new JPanel(); |
| 54 | linkPanel.add(link); | 51 | linkPanel.add(link); |
| 55 | pane.add(linkPanel); | 52 | pane.add(linkPanel); |
| 56 | 53 | ||
| 57 | // show ok button | 54 | // show ok button |
| 58 | JButton okButton = new JButton("Ok"); | 55 | JButton okButton = new JButton("Ok"); |
| 59 | pane.add(okButton); | 56 | pane.add(okButton); |
| 60 | okButton.addActionListener(arg0 -> frame.dispose()); | 57 | okButton.addActionListener(arg0 -> frame.dispose()); |
| 61 | 58 | ||
| 62 | // show the frame | 59 | // show the frame |
| 63 | pane.doLayout(); | 60 | pane.doLayout(); |
| 64 | frame.setSize(400, 220); | 61 | frame.setSize(400, 220); |
| 65 | frame.setResizable(false); | 62 | frame.setResizable(false); |
| 66 | frame.setLocationRelativeTo(parent); | 63 | frame.setLocationRelativeTo(parent); |
| 67 | frame.setVisible(true); | 64 | frame.setVisible(true); |
| 68 | frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); | 65 | frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
| 69 | } | 66 | } |
| 70 | } | 67 | } |
diff --git a/src/main/java/cuchaz/enigma/gui/dialog/CrashDialog.java b/src/main/java/cuchaz/enigma/gui/dialog/CrashDialog.java index 5c1d9ff..04dd5d7 100644 --- a/src/main/java/cuchaz/enigma/gui/dialog/CrashDialog.java +++ b/src/main/java/cuchaz/enigma/gui/dialog/CrashDialog.java | |||
| @@ -8,80 +8,78 @@ | |||
| 8 | * Contributors: | 8 | * Contributors: |
| 9 | * Jeff Martin - initial API and implementation | 9 | * Jeff Martin - initial API and implementation |
| 10 | ******************************************************************************/ | 10 | ******************************************************************************/ |
| 11 | package cuchaz.enigma.gui.dialog; | ||
| 12 | 11 | ||
| 13 | import java.awt.BorderLayout; | 12 | package cuchaz.enigma.gui.dialog; |
| 14 | import java.awt.Container; | ||
| 15 | import java.awt.FlowLayout; | ||
| 16 | import java.io.PrintWriter; | ||
| 17 | import java.io.StringWriter; | ||
| 18 | |||
| 19 | import javax.swing.*; | ||
| 20 | 13 | ||
| 21 | import cuchaz.enigma.Constants; | 14 | import cuchaz.enigma.Constants; |
| 22 | import cuchaz.enigma.utils.Utils; | 15 | import cuchaz.enigma.utils.Utils; |
| 23 | 16 | ||
| 17 | import javax.swing.*; | ||
| 18 | import java.awt.*; | ||
| 19 | import java.io.PrintWriter; | ||
| 20 | import java.io.StringWriter; | ||
| 21 | |||
| 24 | public class CrashDialog { | 22 | public class CrashDialog { |
| 25 | 23 | ||
| 26 | private static CrashDialog instance = null; | 24 | private static CrashDialog instance = null; |
| 27 | 25 | ||
| 28 | private JFrame frame; | 26 | private JFrame frame; |
| 29 | private JTextArea text; | 27 | private JTextArea text; |
| 30 | 28 | ||
| 31 | private CrashDialog(JFrame parent) { | 29 | private CrashDialog(JFrame parent) { |
| 32 | // init frame | 30 | // init frame |
| 33 | frame = new JFrame(Constants.NAME + " - Crash Report"); | 31 | frame = new JFrame(Constants.NAME + " - Crash Report"); |
| 34 | final Container pane = frame.getContentPane(); | 32 | final Container pane = frame.getContentPane(); |
| 35 | pane.setLayout(new BorderLayout()); | 33 | pane.setLayout(new BorderLayout()); |
| 36 | 34 | ||
| 37 | JLabel label = new JLabel(Constants.NAME + " has crashed! =("); | 35 | JLabel label = new JLabel(Constants.NAME + " has crashed! =("); |
| 38 | label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); | 36 | label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); |
| 39 | pane.add(label, BorderLayout.NORTH); | 37 | pane.add(label, BorderLayout.NORTH); |
| 40 | 38 | ||
| 41 | // report panel | 39 | // report panel |
| 42 | text = new JTextArea(); | 40 | text = new JTextArea(); |
| 43 | text.setTabSize(2); | 41 | text.setTabSize(2); |
| 44 | pane.add(new JScrollPane(text), BorderLayout.CENTER); | 42 | pane.add(new JScrollPane(text), BorderLayout.CENTER); |
| 45 | 43 | ||
| 46 | // buttons panel | 44 | // buttons panel |
| 47 | JPanel buttonsPanel = new JPanel(); | 45 | JPanel buttonsPanel = new JPanel(); |
| 48 | FlowLayout buttonsLayout = new FlowLayout(); | 46 | FlowLayout buttonsLayout = new FlowLayout(); |
| 49 | buttonsLayout.setAlignment(FlowLayout.RIGHT); | 47 | buttonsLayout.setAlignment(FlowLayout.RIGHT); |
| 50 | buttonsPanel.setLayout(buttonsLayout); | 48 | buttonsPanel.setLayout(buttonsLayout); |
| 51 | buttonsPanel.add(Utils.unboldLabel(new JLabel("If you choose exit, you will lose any unsaved work."))); | 49 | buttonsPanel.add(Utils.unboldLabel(new JLabel("If you choose exit, you will lose any unsaved work."))); |
| 52 | JButton ignoreButton = new JButton("Ignore"); | 50 | JButton ignoreButton = new JButton("Ignore"); |
| 53 | ignoreButton.addActionListener(event -> { | 51 | ignoreButton.addActionListener(event -> { |
| 54 | // close (hide) the dialog | 52 | // close (hide) the dialog |
| 55 | frame.setVisible(false); | 53 | frame.setVisible(false); |
| 56 | }); | 54 | }); |
| 57 | buttonsPanel.add(ignoreButton); | 55 | buttonsPanel.add(ignoreButton); |
| 58 | JButton exitButton = new JButton("Exit"); | 56 | JButton exitButton = new JButton("Exit"); |
| 59 | exitButton.addActionListener(event -> { | 57 | exitButton.addActionListener(event -> { |
| 60 | // exit enigma | 58 | // exit enigma |
| 61 | System.exit(1); | 59 | System.exit(1); |
| 62 | }); | 60 | }); |
| 63 | buttonsPanel.add(exitButton); | 61 | buttonsPanel.add(exitButton); |
| 64 | pane.add(buttonsPanel, BorderLayout.SOUTH); | 62 | pane.add(buttonsPanel, BorderLayout.SOUTH); |
| 65 | 63 | ||
| 66 | // show the frame | 64 | // show the frame |
| 67 | frame.setSize(600, 400); | 65 | frame.setSize(600, 400); |
| 68 | frame.setLocationRelativeTo(parent); | 66 | frame.setLocationRelativeTo(parent); |
| 69 | frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); | 67 | frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); |
| 70 | } | 68 | } |
| 71 | 69 | ||
| 72 | public static void init(JFrame parent) { | 70 | public static void init(JFrame parent) { |
| 73 | instance = new CrashDialog(parent); | 71 | instance = new CrashDialog(parent); |
| 74 | } | 72 | } |
| 75 | 73 | ||
| 76 | public static void show(Throwable ex) { | 74 | public static void show(Throwable ex) { |
| 77 | // get the error report | 75 | // get the error report |
| 78 | StringWriter buf = new StringWriter(); | 76 | StringWriter buf = new StringWriter(); |
| 79 | ex.printStackTrace(new PrintWriter(buf)); | 77 | ex.printStackTrace(new PrintWriter(buf)); |
| 80 | String report = buf.toString(); | 78 | String report = buf.toString(); |
| 81 | 79 | ||
| 82 | // show it! | 80 | // show it! |
| 83 | instance.text.setText(report); | 81 | instance.text.setText(report); |
| 84 | instance.frame.doLayout(); | 82 | instance.frame.doLayout(); |
| 85 | instance.frame.setVisible(true); | 83 | instance.frame.setVisible(true); |
| 86 | } | 84 | } |
| 87 | } | 85 | } |
diff --git a/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java b/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java index 8df22a7..5f04833 100644 --- a/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java +++ b/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java | |||
| @@ -8,92 +8,89 @@ | |||
| 8 | * Contributors: | 8 | * Contributors: |
| 9 | * Jeff Martin - initial API and implementation | 9 | * Jeff Martin - initial API and implementation |
| 10 | ******************************************************************************/ | 10 | ******************************************************************************/ |
| 11 | package cuchaz.enigma.gui.dialog; | ||
| 12 | |||
| 13 | import java.awt.BorderLayout; | ||
| 14 | import java.awt.Container; | ||
| 15 | import java.awt.Dimension; | ||
| 16 | import java.awt.FlowLayout; | ||
| 17 | 11 | ||
| 18 | import javax.swing.*; | 12 | package cuchaz.enigma.gui.dialog; |
| 19 | 13 | ||
| 20 | import cuchaz.enigma.Constants; | 14 | import cuchaz.enigma.Constants; |
| 21 | import cuchaz.enigma.Deobfuscator.ProgressListener; | 15 | import cuchaz.enigma.Deobfuscator.ProgressListener; |
| 22 | import cuchaz.enigma.utils.Utils; | 16 | import cuchaz.enigma.utils.Utils; |
| 23 | 17 | ||
| 18 | import javax.swing.*; | ||
| 19 | import java.awt.*; | ||
| 20 | |||
| 24 | public class ProgressDialog implements ProgressListener, AutoCloseable { | 21 | public class ProgressDialog implements ProgressListener, AutoCloseable { |
| 25 | 22 | ||
| 26 | private JFrame frame; | 23 | private JFrame frame; |
| 27 | private JLabel labelTitle; | 24 | private JLabel labelTitle; |
| 28 | private JLabel labelText; | 25 | private JLabel labelText; |
| 29 | private JProgressBar progress; | 26 | private JProgressBar progress; |
| 30 | 27 | ||
| 31 | public ProgressDialog(JFrame parent) { | 28 | public ProgressDialog(JFrame parent) { |
| 32 | 29 | ||
| 33 | // init frame | 30 | // init frame |
| 34 | this.frame = new JFrame(Constants.NAME + " - Operation in progress"); | 31 | this.frame = new JFrame(Constants.NAME + " - Operation in progress"); |
| 35 | final Container pane = this.frame.getContentPane(); | 32 | final Container pane = this.frame.getContentPane(); |
| 36 | FlowLayout layout = new FlowLayout(); | 33 | FlowLayout layout = new FlowLayout(); |
| 37 | layout.setAlignment(FlowLayout.LEFT); | 34 | layout.setAlignment(FlowLayout.LEFT); |
| 38 | pane.setLayout(layout); | 35 | pane.setLayout(layout); |
| 39 | 36 | ||
| 40 | this.labelTitle = new JLabel(); | 37 | this.labelTitle = new JLabel(); |
| 41 | pane.add(this.labelTitle); | 38 | pane.add(this.labelTitle); |
| 42 | 39 | ||
| 43 | // set up the progress bar | 40 | // set up the progress bar |
| 44 | JPanel panel = new JPanel(); | 41 | JPanel panel = new JPanel(); |
| 45 | pane.add(panel); | 42 | pane.add(panel); |
| 46 | panel.setLayout(new BorderLayout()); | 43 | panel.setLayout(new BorderLayout()); |
| 47 | this.labelText = Utils.unboldLabel(new JLabel()); | 44 | this.labelText = Utils.unboldLabel(new JLabel()); |
| 48 | this.progress = new JProgressBar(); | 45 | this.progress = new JProgressBar(); |
| 49 | this.labelText.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0)); | 46 | this.labelText.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0)); |
| 50 | panel.add(this.labelText, BorderLayout.NORTH); | 47 | panel.add(this.labelText, BorderLayout.NORTH); |
| 51 | panel.add(this.progress, BorderLayout.CENTER); | 48 | panel.add(this.progress, BorderLayout.CENTER); |
| 52 | panel.setPreferredSize(new Dimension(360, 50)); | 49 | panel.setPreferredSize(new Dimension(360, 50)); |
| 53 | 50 | ||
| 54 | // show the frame | 51 | // show the frame |
| 55 | pane.doLayout(); | 52 | pane.doLayout(); |
| 56 | this.frame.setSize(400, 120); | 53 | this.frame.setSize(400, 120); |
| 57 | this.frame.setResizable(false); | 54 | this.frame.setResizable(false); |
| 58 | this.frame.setLocationRelativeTo(parent); | 55 | this.frame.setLocationRelativeTo(parent); |
| 59 | this.frame.setVisible(true); | 56 | this.frame.setVisible(true); |
| 60 | this.frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); | 57 | this.frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); |
| 61 | } | 58 | } |
| 62 | 59 | ||
| 63 | public void close() { | 60 | public static void runInThread(final JFrame parent, final ProgressRunnable runnable) { |
| 64 | this.frame.dispose(); | 61 | new Thread(() -> |
| 65 | } | 62 | { |
| 66 | 63 | try (ProgressDialog progress = new ProgressDialog(parent)) { | |
| 67 | @Override | 64 | runnable.run(progress); |
| 68 | public void init(int totalWork, String title) { | 65 | } catch (Exception ex) { |
| 69 | this.labelTitle.setText(title); | 66 | throw new Error(ex); |
| 70 | this.progress.setMinimum(0); | 67 | } |
| 71 | this.progress.setMaximum(totalWork); | 68 | }).start(); |
| 72 | this.progress.setValue(0); | 69 | } |
| 73 | } | 70 | |
| 74 | 71 | public void close() { | |
| 75 | @Override | 72 | this.frame.dispose(); |
| 76 | public void onProgress(int numDone, String message) { | 73 | } |
| 77 | this.labelText.setText(message); | 74 | |
| 78 | this.progress.setValue(numDone); | 75 | @Override |
| 79 | 76 | public void init(int totalWork, String title) { | |
| 80 | // update the frame | 77 | this.labelTitle.setText(title); |
| 81 | this.frame.validate(); | 78 | this.progress.setMinimum(0); |
| 82 | this.frame.repaint(); | 79 | this.progress.setMaximum(totalWork); |
| 83 | } | 80 | this.progress.setValue(0); |
| 84 | 81 | } | |
| 85 | public interface ProgressRunnable { | 82 | |
| 86 | void run(ProgressListener listener) throws Exception; | 83 | @Override |
| 87 | } | 84 | public void onProgress(int numDone, String message) { |
| 88 | 85 | this.labelText.setText(message); | |
| 89 | public static void runInThread(final JFrame parent, final ProgressRunnable runnable) { | 86 | this.progress.setValue(numDone); |
| 90 | new Thread(() -> | 87 | |
| 91 | { | 88 | // update the frame |
| 92 | try (ProgressDialog progress = new ProgressDialog(parent)) { | 89 | this.frame.validate(); |
| 93 | runnable.run(progress); | 90 | this.frame.repaint(); |
| 94 | } catch (Exception ex) { | 91 | } |
| 95 | throw new Error(ex); | 92 | |
| 96 | } | 93 | public interface ProgressRunnable { |
| 97 | }).start(); | 94 | void run(ProgressListener listener) throws Exception; |
| 98 | } | 95 | } |
| 99 | } | 96 | } |