From 6e464ea251cab63c776ece0b2a356f1498ffa294 Mon Sep 17 00:00:00 2001 From: Thog Date: Wed, 8 Mar 2017 08:17:04 +0100 Subject: Follow Fabric guidelines --- .../java/cuchaz/enigma/gui/dialog/AboutDialog.java | 93 ++++++------ .../java/cuchaz/enigma/gui/dialog/CrashDialog.java | 118 ++++++++-------- .../cuchaz/enigma/gui/dialog/ProgressDialog.java | 157 ++++++++++----------- 3 files changed, 180 insertions(+), 188 deletions(-) (limited to 'src/main/java/cuchaz/enigma/gui/dialog') 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 @@ * Contributors: * Jeff Martin - initial API and implementation ******************************************************************************/ -package cuchaz.enigma.gui.dialog; - -import java.awt.Color; -import java.awt.Container; -import java.awt.Cursor; -import java.awt.FlowLayout; -import java.io.IOException; -import javax.swing.*; +package cuchaz.enigma.gui.dialog; import cuchaz.enigma.Constants; import cuchaz.enigma.utils.Utils; +import javax.swing.*; +import java.awt.*; +import java.io.IOException; + public class AboutDialog { - public static void show(JFrame parent) { - // init frame - final JFrame frame = new JFrame(Constants.NAME + " - About"); - final Container pane = frame.getContentPane(); - pane.setLayout(new FlowLayout()); + public static void show(JFrame parent) { + // init frame + final JFrame frame = new JFrame(Constants.NAME + " - About"); + final Container pane = frame.getContentPane(); + pane.setLayout(new FlowLayout()); - // load the content - try { - String html = Utils.readResourceToString("/about.html"); - html = String.format(html, Constants.NAME, Constants.VERSION); - JLabel label = new JLabel(html); - label.setHorizontalAlignment(JLabel.CENTER); - pane.add(label); - } catch (IOException ex) { - throw new Error(ex); - } + // load the content + try { + String html = Utils.readResourceToString("/about.html"); + html = String.format(html, Constants.NAME, Constants.VERSION); + JLabel label = new JLabel(html); + label.setHorizontalAlignment(JLabel.CENTER); + pane.add(label); + } catch (IOException ex) { + throw new Error(ex); + } - // show the link - String html = "%s"; - html = String.format(html, Constants.URL, Constants.URL); - JButton link = new JButton(html); - link.addActionListener(event -> Utils.openUrl(Constants.URL)); - link.setBorderPainted(false); - link.setOpaque(false); - link.setBackground(Color.WHITE); - link.setCursor(new Cursor(Cursor.HAND_CURSOR)); - link.setFocusable(false); - JPanel linkPanel = new JPanel(); - linkPanel.add(link); - pane.add(linkPanel); + // show the link + String html = "%s"; + html = String.format(html, Constants.URL, Constants.URL); + JButton link = new JButton(html); + link.addActionListener(event -> Utils.openUrl(Constants.URL)); + link.setBorderPainted(false); + link.setOpaque(false); + link.setBackground(Color.WHITE); + link.setCursor(new Cursor(Cursor.HAND_CURSOR)); + link.setFocusable(false); + JPanel linkPanel = new JPanel(); + linkPanel.add(link); + pane.add(linkPanel); - // show ok button - JButton okButton = new JButton("Ok"); - pane.add(okButton); - okButton.addActionListener(arg0 -> frame.dispose()); + // show ok button + JButton okButton = new JButton("Ok"); + pane.add(okButton); + okButton.addActionListener(arg0 -> frame.dispose()); - // show the frame - pane.doLayout(); - frame.setSize(400, 220); - frame.setResizable(false); - frame.setLocationRelativeTo(parent); - frame.setVisible(true); - frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); - } + // show the frame + pane.doLayout(); + frame.setSize(400, 220); + frame.setResizable(false); + frame.setLocationRelativeTo(parent); + frame.setVisible(true); + frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); + } } 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 @@ * Contributors: * Jeff Martin - initial API and implementation ******************************************************************************/ -package cuchaz.enigma.gui.dialog; -import java.awt.BorderLayout; -import java.awt.Container; -import java.awt.FlowLayout; -import java.io.PrintWriter; -import java.io.StringWriter; - -import javax.swing.*; +package cuchaz.enigma.gui.dialog; import cuchaz.enigma.Constants; import cuchaz.enigma.utils.Utils; +import javax.swing.*; +import java.awt.*; +import java.io.PrintWriter; +import java.io.StringWriter; + public class CrashDialog { - private static CrashDialog instance = null; + private static CrashDialog instance = null; - private JFrame frame; - private JTextArea text; + private JFrame frame; + private JTextArea text; - private CrashDialog(JFrame parent) { - // init frame - frame = new JFrame(Constants.NAME + " - Crash Report"); - final Container pane = frame.getContentPane(); - pane.setLayout(new BorderLayout()); + private CrashDialog(JFrame parent) { + // init frame + frame = new JFrame(Constants.NAME + " - Crash Report"); + final Container pane = frame.getContentPane(); + pane.setLayout(new BorderLayout()); - JLabel label = new JLabel(Constants.NAME + " has crashed! =("); - label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); - pane.add(label, BorderLayout.NORTH); + JLabel label = new JLabel(Constants.NAME + " has crashed! =("); + label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); + pane.add(label, BorderLayout.NORTH); - // report panel - text = new JTextArea(); - text.setTabSize(2); - pane.add(new JScrollPane(text), BorderLayout.CENTER); + // report panel + text = new JTextArea(); + text.setTabSize(2); + pane.add(new JScrollPane(text), BorderLayout.CENTER); - // buttons panel - JPanel buttonsPanel = new JPanel(); - FlowLayout buttonsLayout = new FlowLayout(); - buttonsLayout.setAlignment(FlowLayout.RIGHT); - buttonsPanel.setLayout(buttonsLayout); - buttonsPanel.add(Utils.unboldLabel(new JLabel("If you choose exit, you will lose any unsaved work."))); - JButton ignoreButton = new JButton("Ignore"); - ignoreButton.addActionListener(event -> { - // close (hide) the dialog - frame.setVisible(false); - }); - buttonsPanel.add(ignoreButton); - JButton exitButton = new JButton("Exit"); - exitButton.addActionListener(event -> { - // exit enigma - System.exit(1); - }); - buttonsPanel.add(exitButton); - pane.add(buttonsPanel, BorderLayout.SOUTH); + // buttons panel + JPanel buttonsPanel = new JPanel(); + FlowLayout buttonsLayout = new FlowLayout(); + buttonsLayout.setAlignment(FlowLayout.RIGHT); + buttonsPanel.setLayout(buttonsLayout); + buttonsPanel.add(Utils.unboldLabel(new JLabel("If you choose exit, you will lose any unsaved work."))); + JButton ignoreButton = new JButton("Ignore"); + ignoreButton.addActionListener(event -> { + // close (hide) the dialog + frame.setVisible(false); + }); + buttonsPanel.add(ignoreButton); + JButton exitButton = new JButton("Exit"); + exitButton.addActionListener(event -> { + // exit enigma + System.exit(1); + }); + buttonsPanel.add(exitButton); + pane.add(buttonsPanel, BorderLayout.SOUTH); - // show the frame - frame.setSize(600, 400); - frame.setLocationRelativeTo(parent); - frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); - } + // show the frame + frame.setSize(600, 400); + frame.setLocationRelativeTo(parent); + frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); + } - public static void init(JFrame parent) { - instance = new CrashDialog(parent); - } + public static void init(JFrame parent) { + instance = new CrashDialog(parent); + } - public static void show(Throwable ex) { - // get the error report - StringWriter buf = new StringWriter(); - ex.printStackTrace(new PrintWriter(buf)); - String report = buf.toString(); + public static void show(Throwable ex) { + // get the error report + StringWriter buf = new StringWriter(); + ex.printStackTrace(new PrintWriter(buf)); + String report = buf.toString(); - // show it! - instance.text.setText(report); - instance.frame.doLayout(); - instance.frame.setVisible(true); - } + // show it! + instance.text.setText(report); + instance.frame.doLayout(); + instance.frame.setVisible(true); + } } 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 @@ * Contributors: * Jeff Martin - initial API and implementation ******************************************************************************/ -package cuchaz.enigma.gui.dialog; - -import java.awt.BorderLayout; -import java.awt.Container; -import java.awt.Dimension; -import java.awt.FlowLayout; -import javax.swing.*; +package cuchaz.enigma.gui.dialog; import cuchaz.enigma.Constants; import cuchaz.enigma.Deobfuscator.ProgressListener; import cuchaz.enigma.utils.Utils; +import javax.swing.*; +import java.awt.*; + public class ProgressDialog implements ProgressListener, AutoCloseable { - private JFrame frame; - private JLabel labelTitle; - private JLabel labelText; - private JProgressBar progress; - - public ProgressDialog(JFrame parent) { - - // init frame - this.frame = new JFrame(Constants.NAME + " - Operation in progress"); - final Container pane = this.frame.getContentPane(); - FlowLayout layout = new FlowLayout(); - layout.setAlignment(FlowLayout.LEFT); - pane.setLayout(layout); - - this.labelTitle = new JLabel(); - pane.add(this.labelTitle); - - // set up the progress bar - JPanel panel = new JPanel(); - pane.add(panel); - panel.setLayout(new BorderLayout()); - this.labelText = Utils.unboldLabel(new JLabel()); - this.progress = new JProgressBar(); - this.labelText.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0)); - panel.add(this.labelText, BorderLayout.NORTH); - panel.add(this.progress, BorderLayout.CENTER); - panel.setPreferredSize(new Dimension(360, 50)); - - // show the frame - pane.doLayout(); - this.frame.setSize(400, 120); - this.frame.setResizable(false); - this.frame.setLocationRelativeTo(parent); - this.frame.setVisible(true); - this.frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); - } - - public void close() { - this.frame.dispose(); - } - - @Override - public void init(int totalWork, String title) { - this.labelTitle.setText(title); - this.progress.setMinimum(0); - this.progress.setMaximum(totalWork); - this.progress.setValue(0); - } - - @Override - public void onProgress(int numDone, String message) { - this.labelText.setText(message); - this.progress.setValue(numDone); - - // update the frame - this.frame.validate(); - this.frame.repaint(); - } - - public interface ProgressRunnable { - void run(ProgressListener listener) throws Exception; - } - - public static void runInThread(final JFrame parent, final ProgressRunnable runnable) { - new Thread(() -> - { - try (ProgressDialog progress = new ProgressDialog(parent)) { - runnable.run(progress); - } catch (Exception ex) { - throw new Error(ex); - } - }).start(); - } + private JFrame frame; + private JLabel labelTitle; + private JLabel labelText; + private JProgressBar progress; + + public ProgressDialog(JFrame parent) { + + // init frame + this.frame = new JFrame(Constants.NAME + " - Operation in progress"); + final Container pane = this.frame.getContentPane(); + FlowLayout layout = new FlowLayout(); + layout.setAlignment(FlowLayout.LEFT); + pane.setLayout(layout); + + this.labelTitle = new JLabel(); + pane.add(this.labelTitle); + + // set up the progress bar + JPanel panel = new JPanel(); + pane.add(panel); + panel.setLayout(new BorderLayout()); + this.labelText = Utils.unboldLabel(new JLabel()); + this.progress = new JProgressBar(); + this.labelText.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0)); + panel.add(this.labelText, BorderLayout.NORTH); + panel.add(this.progress, BorderLayout.CENTER); + panel.setPreferredSize(new Dimension(360, 50)); + + // show the frame + pane.doLayout(); + this.frame.setSize(400, 120); + this.frame.setResizable(false); + this.frame.setLocationRelativeTo(parent); + this.frame.setVisible(true); + this.frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); + } + + public static void runInThread(final JFrame parent, final ProgressRunnable runnable) { + new Thread(() -> + { + try (ProgressDialog progress = new ProgressDialog(parent)) { + runnable.run(progress); + } catch (Exception ex) { + throw new Error(ex); + } + }).start(); + } + + public void close() { + this.frame.dispose(); + } + + @Override + public void init(int totalWork, String title) { + this.labelTitle.setText(title); + this.progress.setMinimum(0); + this.progress.setMaximum(totalWork); + this.progress.setValue(0); + } + + @Override + public void onProgress(int numDone, String message) { + this.labelText.setText(message); + this.progress.setValue(numDone); + + // update the frame + this.frame.validate(); + this.frame.repaint(); + } + + public interface ProgressRunnable { + void run(ProgressListener listener) throws Exception; + } } -- cgit v1.2.3