From 392114e3e63e14b7c15729ce32ed4cde596f5dcb Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Sat, 13 Mar 2021 14:00:18 +0100 Subject: [PATCH] Cleaned up the user interface a lot. --- pom.xml | 8 ++- .../blockbookbinder/BlockBookBinder.java | 3 + .../control/ConvertToBookActionListener.java | 29 --------- .../blockbookbinder/control/ImportAction.java | 19 ------ .../export/ExportBookToMinecraftAction.java | 39 +++++++++++ .../CleanSourceAction.java} | 23 ++++--- .../source/CompileFromSourceAction.java | 32 +++++++++ .../control/source/ImportSourceAction.java | 21 ++++++ .../blockbookbinder/util/VersionReader.java | 30 +++++++++ .../blockbookbinder/view/MainFrame.java | 65 +++++++++---------- .../blockbookbinder/view/SourceTextPanel.java | 17 +---- .../view/export/ExportToBookDialog.java | 4 +- src/main/resources/application.properties | 2 + 13 files changed, 182 insertions(+), 110 deletions(-) delete mode 100644 src/main/java/nl/andrewlalis/blockbookbinder/control/ConvertToBookActionListener.java delete mode 100644 src/main/java/nl/andrewlalis/blockbookbinder/control/ImportAction.java create mode 100644 src/main/java/nl/andrewlalis/blockbookbinder/control/export/ExportBookToMinecraftAction.java rename src/main/java/nl/andrewlalis/blockbookbinder/control/{CleanSourceActionListener.java => source/CleanSourceAction.java} (68%) create mode 100644 src/main/java/nl/andrewlalis/blockbookbinder/control/source/CompileFromSourceAction.java create mode 100644 src/main/java/nl/andrewlalis/blockbookbinder/control/source/ImportSourceAction.java create mode 100644 src/main/java/nl/andrewlalis/blockbookbinder/util/VersionReader.java diff --git a/pom.xml b/pom.xml index 4af87d0..8e4cce3 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ nl.andrewlalis BlockBookBinder - 1.1.0 + 1.2.0 12 @@ -62,5 +62,11 @@ flatlaf 1.0-rc3 + + + org.apache.maven + maven-model + 3.6.3 + \ No newline at end of file diff --git a/src/main/java/nl/andrewlalis/blockbookbinder/BlockBookBinder.java b/src/main/java/nl/andrewlalis/blockbookbinder/BlockBookBinder.java index a31fda5..ccaf4c3 100644 --- a/src/main/java/nl/andrewlalis/blockbookbinder/BlockBookBinder.java +++ b/src/main/java/nl/andrewlalis/blockbookbinder/BlockBookBinder.java @@ -1,6 +1,7 @@ package nl.andrewlalis.blockbookbinder; import com.formdev.flatlaf.FlatDarkLaf; +import nl.andrewlalis.blockbookbinder.util.VersionReader; import nl.andrewlalis.blockbookbinder.view.MainFrame; import javax.swing.*; @@ -9,6 +10,8 @@ import javax.swing.*; * The main class for the application. */ public class BlockBookBinder { + public static final String VERSION = VersionReader.getVersion(); + public static void main(String[] args) { SwingUtilities.invokeLater(() -> { FlatDarkLaf.install(); diff --git a/src/main/java/nl/andrewlalis/blockbookbinder/control/ConvertToBookActionListener.java b/src/main/java/nl/andrewlalis/blockbookbinder/control/ConvertToBookActionListener.java deleted file mode 100644 index b2b1c73..0000000 --- a/src/main/java/nl/andrewlalis/blockbookbinder/control/ConvertToBookActionListener.java +++ /dev/null @@ -1,29 +0,0 @@ -package nl.andrewlalis.blockbookbinder.control; - -import nl.andrewlalis.blockbookbinder.model.build.BookBuilder; -import nl.andrewlalis.blockbookbinder.view.book.BookPreviewPanel; -import nl.andrewlalis.blockbookbinder.view.SourceTextPanel; - -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -/** - * Action listener that, when activated, converts the text from the source panel - * into a formatted book. - */ -public class ConvertToBookActionListener implements ActionListener { - private final SourceTextPanel sourceTextPanel; - private final BookPreviewPanel bookPreviewPanel; - - public ConvertToBookActionListener(SourceTextPanel sourceTextPanel, BookPreviewPanel bookPreviewPanel) { - this.sourceTextPanel = sourceTextPanel; - this.bookPreviewPanel = bookPreviewPanel; - } - - @Override - public void actionPerformed(ActionEvent e) { - this.bookPreviewPanel.setBook( - new BookBuilder().build(this.sourceTextPanel.getSourceText()) - ); - } -} diff --git a/src/main/java/nl/andrewlalis/blockbookbinder/control/ImportAction.java b/src/main/java/nl/andrewlalis/blockbookbinder/control/ImportAction.java deleted file mode 100644 index f0c2b6e..0000000 --- a/src/main/java/nl/andrewlalis/blockbookbinder/control/ImportAction.java +++ /dev/null @@ -1,19 +0,0 @@ -package nl.andrewlalis.blockbookbinder.control; - -import javax.swing.*; -import java.awt.event.ActionEvent; - -public class ImportAction extends AbstractAction { - public ImportAction(String name) { - super(name); - } - - public ImportAction(String name, Icon icon) { - super(name, icon); - } - - @Override - public void actionPerformed(ActionEvent e) { - System.out.println("Import!"); - } -} diff --git a/src/main/java/nl/andrewlalis/blockbookbinder/control/export/ExportBookToMinecraftAction.java b/src/main/java/nl/andrewlalis/blockbookbinder/control/export/ExportBookToMinecraftAction.java new file mode 100644 index 0000000..f324244 --- /dev/null +++ b/src/main/java/nl/andrewlalis/blockbookbinder/control/export/ExportBookToMinecraftAction.java @@ -0,0 +1,39 @@ +package nl.andrewlalis.blockbookbinder.control.export; + +import lombok.Getter; +import lombok.Setter; +import nl.andrewlalis.blockbookbinder.model.Book; +import nl.andrewlalis.blockbookbinder.view.book.BookPreviewPanel; +import nl.andrewlalis.blockbookbinder.view.export.ExportToBookDialog; + +import javax.swing.*; +import java.awt.event.ActionEvent; + +public class ExportBookToMinecraftAction extends AbstractAction { + @Getter + private static final ExportBookToMinecraftAction instance = new ExportBookToMinecraftAction(); + + public ExportBookToMinecraftAction() { + super("Export to Minecraft"); + this.putValue(SHORT_DESCRIPTION, "Export the current book to Minecraft."); + } + + @Setter + private BookPreviewPanel bookPreviewPanel; + + @Override + public void actionPerformed(ActionEvent e) { + final Book book = bookPreviewPanel.getBook(); + if (book == null || book.getPageCount() == 0) { + JOptionPane.showMessageDialog( + this.bookPreviewPanel.getRootPane(), + "Cannot export an empty book.", + "Empty Book", + JOptionPane.WARNING_MESSAGE + ); + return; + } + ExportToBookDialog dialog = new ExportToBookDialog(SwingUtilities.getWindowAncestor(this.bookPreviewPanel), bookPreviewPanel.getBook()); + dialog.setupAndShow(); + } +} diff --git a/src/main/java/nl/andrewlalis/blockbookbinder/control/CleanSourceActionListener.java b/src/main/java/nl/andrewlalis/blockbookbinder/control/source/CleanSourceAction.java similarity index 68% rename from src/main/java/nl/andrewlalis/blockbookbinder/control/CleanSourceActionListener.java rename to src/main/java/nl/andrewlalis/blockbookbinder/control/source/CleanSourceAction.java index 794adf6..cac31ba 100644 --- a/src/main/java/nl/andrewlalis/blockbookbinder/control/CleanSourceActionListener.java +++ b/src/main/java/nl/andrewlalis/blockbookbinder/control/source/CleanSourceAction.java @@ -1,19 +1,22 @@ -package nl.andrewlalis.blockbookbinder.control; +package nl.andrewlalis.blockbookbinder.control.source; +import lombok.Getter; +import lombok.Setter; import nl.andrewlalis.blockbookbinder.view.SourceTextPanel; +import javax.swing.*; import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -/** - * Listener for an action where the source text is 'cleaned' by removing any - * trailing whitespace, and removing unnecessary newlines. - */ -public class CleanSourceActionListener implements ActionListener { - private final SourceTextPanel sourceTextPanel; +public class CleanSourceAction extends AbstractAction { + @Getter + private final static CleanSourceAction instance = new CleanSourceAction(); - public CleanSourceActionListener(SourceTextPanel sourceTextPanel) { - this.sourceTextPanel = sourceTextPanel; + @Setter + private SourceTextPanel sourceTextPanel; + + public CleanSourceAction() { + super("Clean Source"); + this.putValue(SHORT_DESCRIPTION, "Clean up the source text."); } @Override diff --git a/src/main/java/nl/andrewlalis/blockbookbinder/control/source/CompileFromSourceAction.java b/src/main/java/nl/andrewlalis/blockbookbinder/control/source/CompileFromSourceAction.java new file mode 100644 index 0000000..3587b78 --- /dev/null +++ b/src/main/java/nl/andrewlalis/blockbookbinder/control/source/CompileFromSourceAction.java @@ -0,0 +1,32 @@ +package nl.andrewlalis.blockbookbinder.control.source; + +import lombok.Getter; +import lombok.Setter; +import nl.andrewlalis.blockbookbinder.model.build.BookBuilder; +import nl.andrewlalis.blockbookbinder.view.SourceTextPanel; +import nl.andrewlalis.blockbookbinder.view.book.BookPreviewPanel; + +import javax.swing.*; +import java.awt.event.ActionEvent; + +public class CompileFromSourceAction extends AbstractAction { + @Getter + private static final CompileFromSourceAction instance = new CompileFromSourceAction(); + + @Setter + private SourceTextPanel sourceTextPanel; + @Setter + private BookPreviewPanel bookPreviewPanel; + + public CompileFromSourceAction() { + super("Compile From Source"); + this.putValue(SHORT_DESCRIPTION, "Compile the current source text into a book."); + } + + @Override + public void actionPerformed(ActionEvent e) { + this.bookPreviewPanel.setBook( + new BookBuilder().build(this.sourceTextPanel.getSourceText()) + ); + } +} diff --git a/src/main/java/nl/andrewlalis/blockbookbinder/control/source/ImportSourceAction.java b/src/main/java/nl/andrewlalis/blockbookbinder/control/source/ImportSourceAction.java new file mode 100644 index 0000000..4ec167c --- /dev/null +++ b/src/main/java/nl/andrewlalis/blockbookbinder/control/source/ImportSourceAction.java @@ -0,0 +1,21 @@ +package nl.andrewlalis.blockbookbinder.control.source; + +import lombok.Getter; + +import javax.swing.*; +import java.awt.event.ActionEvent; + +public class ImportSourceAction extends AbstractAction { + @Getter + private static final ImportSourceAction instance = new ImportSourceAction(); + + public ImportSourceAction() { + super("Import Source"); + this.putValue(SHORT_DESCRIPTION, "Import source text from a file."); + } + + @Override + public void actionPerformed(ActionEvent e) { + + } +} diff --git a/src/main/java/nl/andrewlalis/blockbookbinder/util/VersionReader.java b/src/main/java/nl/andrewlalis/blockbookbinder/util/VersionReader.java new file mode 100644 index 0000000..bc34580 --- /dev/null +++ b/src/main/java/nl/andrewlalis/blockbookbinder/util/VersionReader.java @@ -0,0 +1,30 @@ +package nl.andrewlalis.blockbookbinder.util; + +import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class VersionReader { + public static String getVersion() { + MavenXpp3Reader reader = new MavenXpp3Reader(); + try { + Model model; + if ((new File("pom.xml")).exists()) { + model = reader.read(new FileReader("pom.xml")); + } else { + model = reader.read(new InputStreamReader( + VersionReader.class.getResourceAsStream("/META-INF/maven/nl.andrewlalis/BlockBookBinder/pom.xml") + )); + } + return model.getVersion(); + } catch (IOException | XmlPullParserException e) { + e.printStackTrace(); + return "Unknown"; + } + } +} diff --git a/src/main/java/nl/andrewlalis/blockbookbinder/view/MainFrame.java b/src/main/java/nl/andrewlalis/blockbookbinder/view/MainFrame.java index 53df354..28f763d 100644 --- a/src/main/java/nl/andrewlalis/blockbookbinder/view/MainFrame.java +++ b/src/main/java/nl/andrewlalis/blockbookbinder/view/MainFrame.java @@ -1,10 +1,13 @@ package nl.andrewlalis.blockbookbinder.view; -import nl.andrewlalis.blockbookbinder.model.Book; +import nl.andrewlalis.blockbookbinder.BlockBookBinder; +import nl.andrewlalis.blockbookbinder.control.export.ExportBookToMinecraftAction; +import nl.andrewlalis.blockbookbinder.control.source.CleanSourceAction; +import nl.andrewlalis.blockbookbinder.control.source.CompileFromSourceAction; +import nl.andrewlalis.blockbookbinder.control.source.ImportSourceAction; import nl.andrewlalis.blockbookbinder.util.ApplicationProperties; import nl.andrewlalis.blockbookbinder.view.about.AboutDialog; import nl.andrewlalis.blockbookbinder.view.book.BookPreviewPanel; -import nl.andrewlalis.blockbookbinder.view.export.ExportToBookDialog; import javax.swing.*; import java.awt.*; @@ -19,7 +22,7 @@ public class MainFrame extends JFrame { ApplicationProperties.getIntProp("frame.default_width"), ApplicationProperties.getIntProp("frame.default_height") )); - this.setTitle(ApplicationProperties.getProp("frame.title")); + this.setTitle(ApplicationProperties.getProp("frame.title") + " Version " + BlockBookBinder.VERSION); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); final URL iconUrl = this.getClass().getClassLoader().getResource("images/book_and_quill.png"); if (iconUrl != null) { @@ -34,15 +37,41 @@ public class MainFrame extends JFrame { this.setVisible(true); } + private Container buildContentPane() { + JPanel mainPanel = new JPanel(new BorderLayout()); + JPanel doublePanel = new JPanel(new GridLayout(1, 2)); + + BookPreviewPanel bookPreviewPanel = new BookPreviewPanel(); + doublePanel.add(bookPreviewPanel); + CompileFromSourceAction.getInstance().setBookPreviewPanel(bookPreviewPanel); + ExportBookToMinecraftAction.getInstance().setBookPreviewPanel(bookPreviewPanel); + + SourceTextPanel sourceTextPanel = new SourceTextPanel(); + doublePanel.add(sourceTextPanel); + CompileFromSourceAction.getInstance().setSourceTextPanel(sourceTextPanel); + CleanSourceAction.getInstance().setSourceTextPanel(sourceTextPanel); + + mainPanel.add(doublePanel, BorderLayout.CENTER); + + return mainPanel; + } + private JMenuBar buildMenuBar() { JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); + fileMenu.add(ImportSourceAction.getInstance()); JMenuItem exitItem = new JMenuItem("Exit"); exitItem.addActionListener(e -> this.dispose()); fileMenu.add(exitItem); menuBar.add(fileMenu); + JMenu bookMenu = new JMenu("Book"); + bookMenu.add(CompileFromSourceAction.getInstance()); + bookMenu.add(CleanSourceAction.getInstance()); + bookMenu.add(ExportBookToMinecraftAction.getInstance()); + menuBar.add(bookMenu); + JMenu helpMenu = new JMenu("Help"); JMenuItem aboutItem = new JMenuItem("About"); aboutItem.addActionListener(e -> { @@ -55,36 +84,6 @@ public class MainFrame extends JFrame { return menuBar; } - private Container buildContentPane() { - JPanel mainPanel = new JPanel(new BorderLayout()); - - JPanel doublePanel = new JPanel(new GridLayout(1, 2)); - BookPreviewPanel bookPreviewPanel = new BookPreviewPanel(); - doublePanel.add(bookPreviewPanel); - SourceTextPanel sourceTextPanel = new SourceTextPanel(bookPreviewPanel); - doublePanel.add(sourceTextPanel); - mainPanel.add(doublePanel, BorderLayout.CENTER); - - JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); - JButton exportButton = new JButton("Export to Book"); - JButton cancelExportButton = new JButton("Cancel Export"); - cancelExportButton.setEnabled(false); - exportButton.addActionListener(e -> { - final Book book = bookPreviewPanel.getBook(); - if (book == null || book.getPageCount() == 0) { - JOptionPane.showMessageDialog(this, "Cannot export an empty book.", "Empty Book", JOptionPane.WARNING_MESSAGE); - return; - } - ExportToBookDialog dialog = new ExportToBookDialog(this, bookPreviewPanel.getBook()); - dialog.setupAndShow(); - }); - bottomPanel.add(exportButton); - bottomPanel.add(cancelExportButton); - mainPanel.add(bottomPanel, BorderLayout.SOUTH); - - return mainPanel; - } - @Override public void dispose() { super.dispose(); diff --git a/src/main/java/nl/andrewlalis/blockbookbinder/view/SourceTextPanel.java b/src/main/java/nl/andrewlalis/blockbookbinder/view/SourceTextPanel.java index 8ca6319..b0e26ab 100644 --- a/src/main/java/nl/andrewlalis/blockbookbinder/view/SourceTextPanel.java +++ b/src/main/java/nl/andrewlalis/blockbookbinder/view/SourceTextPanel.java @@ -1,9 +1,5 @@ package nl.andrewlalis.blockbookbinder.view; -import nl.andrewlalis.blockbookbinder.control.CleanSourceActionListener; -import nl.andrewlalis.blockbookbinder.control.ConvertToBookActionListener; -import nl.andrewlalis.blockbookbinder.view.book.BookPreviewPanel; - import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.*; @@ -15,7 +11,7 @@ import java.awt.*; public class SourceTextPanel extends JPanel { private final JTextArea textArea; - public SourceTextPanel(BookPreviewPanel bookPreviewPanel) { + public SourceTextPanel() { super(new BorderLayout()); this.add(new JLabel("Source Text"), BorderLayout.NORTH); @@ -26,17 +22,6 @@ public class SourceTextPanel extends JPanel { this.textArea.setLineWrap(true); JScrollPane scrollWrappedMainTextArea = new JScrollPane(this.textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); this.add(scrollWrappedMainTextArea, BorderLayout.CENTER); - - JPanel rightPanelButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); - this.add(rightPanelButtonPanel, BorderLayout.SOUTH); - - JButton convertButton = new JButton("Convert to Book"); - convertButton.addActionListener(new ConvertToBookActionListener(this, bookPreviewPanel)); - rightPanelButtonPanel.add(convertButton); - - JButton cleanButton = new JButton("Clean"); - cleanButton.addActionListener(new CleanSourceActionListener(this)); - rightPanelButtonPanel.add(cleanButton); } public String getSourceText() { diff --git a/src/main/java/nl/andrewlalis/blockbookbinder/view/export/ExportToBookDialog.java b/src/main/java/nl/andrewlalis/blockbookbinder/view/export/ExportToBookDialog.java index a613b31..4d1d42f 100644 --- a/src/main/java/nl/andrewlalis/blockbookbinder/view/export/ExportToBookDialog.java +++ b/src/main/java/nl/andrewlalis/blockbookbinder/view/export/ExportToBookDialog.java @@ -31,8 +31,8 @@ public class ExportToBookDialog extends JDialog { private Thread exporterThread; private BookExporter exporterRunnable; - public ExportToBookDialog(Frame owner, Book book) { - super(owner, "Export to Book", true); + public ExportToBookDialog(Window owner, Book book) { + super(owner, "Export to Book"); this.book = book; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7264766..87e1343 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,5 @@ +version=1.2.0 + # Settings for the application's GUI. frame.title=Block Book Binder frame.default_width=800