Search in sources :

Example 1 with ProgressWindow

use of games.strategy.ui.ProgressWindow in project triplea by triplea-game.

the class PBEMMessagePoster method postTurn.

public static void postTurn(final String title, final HistoryLog historyLog, final boolean includeSaveGame, final PBEMMessagePoster posterPbem, final IAbstractForumPosterDelegate postingDelegate, final MainGameFrame mainGameFrame, final JComponent postButton) {
    String message = "";
    final IForumPoster turnSummaryMsgr = posterPbem.getForumPoster();
    final StringBuilder sb = new StringBuilder();
    if (turnSummaryMsgr != null) {
        sb.append(message).append("Post ").append(title).append(" ");
        if (includeSaveGame) {
            sb.append("and save game ");
        }
        sb.append("to ").append(turnSummaryMsgr.getDisplayName()).append("?\n");
    }
    final IEmailSender emailSender = posterPbem.getEmailSender();
    if (emailSender != null) {
        sb.append("Send email to ").append(emailSender.getToAddress()).append("?\n");
    }
    message = sb.toString();
    final int choice = JOptionPane.showConfirmDialog(mainGameFrame, message, "Post " + title + "?", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null);
    if (choice == 0) {
        if (postButton != null) {
            postButton.setEnabled(false);
        }
        final ProgressWindow progressWindow = new ProgressWindow(mainGameFrame, "Posting " + title + "...");
        progressWindow.setVisible(true);
        // start a new thread for posting the summary.
        new Thread(() -> {
            boolean postOk = true;
            File saveGameFile = null;
            if (postingDelegate != null) {
                postingDelegate.setHasPostedTurnSummary(true);
            }
            try {
                saveGameFile = File.createTempFile("triplea", GameDataFileUtils.getExtension());
                if (saveGameFile != null) {
                    mainGameFrame.getGame().saveGame(saveGameFile);
                    posterPbem.setSaveGame(saveGameFile);
                }
            } catch (final Exception e) {
                postOk = false;
                ClientLogger.logQuietly("Failed to create save game", e);
            }
            posterPbem.setTurnSummary(historyLog.toString());
            try {
                // forward the poster to the delegate which invokes post() on the poster
                if (postingDelegate != null) {
                    if (!postingDelegate.postTurnSummary(posterPbem, title, includeSaveGame)) {
                        postOk = false;
                    }
                } else {
                    if (!posterPbem.post(null, title, includeSaveGame)) {
                        postOk = false;
                    }
                }
            } catch (final Exception e) {
                postOk = false;
                ClientLogger.logQuietly("Failed to post save game to forum", e);
            }
            if (postingDelegate != null) {
                postingDelegate.setHasPostedTurnSummary(postOk);
            }
            final StringBuilder sb1 = new StringBuilder();
            if (posterPbem.getForumPoster() != null) {
                final String saveGameRef = posterPbem.getSaveGameRef();
                final String turnSummaryRef = posterPbem.getTurnSummaryRef();
                if (saveGameRef != null) {
                    sb1.append("\nSave Game : ").append(saveGameRef);
                }
                if (turnSummaryRef != null) {
                    sb1.append("\nSummary Text: ").append(turnSummaryRef);
                }
            }
            if (posterPbem.getEmailSender() != null) {
                sb1.append("\nEmails: ").append(posterPbem.getEmailSendStatus());
            }
            historyLog.getWriter().println(sb1.toString());
            if (historyLog.isVisible()) {
                historyLog.setVisible(true);
            }
            try {
                if (saveGameFile != null && !saveGameFile.delete()) {
                    System.out.println((new StringBuilder()).append("INFO TripleA PBEM/PBF poster couldn't delete temporary savegame: ").append(saveGameFile.getCanonicalPath()).toString());
                }
            } catch (final IOException e) {
                ClientLogger.logQuietly("save game file = " + saveGameFile, e);
            }
            progressWindow.setVisible(false);
            progressWindow.removeAll();
            progressWindow.dispose();
            final boolean finalPostOk = postOk;
            final String finalMessage = sb1.toString();
            SwingUtilities.invokeLater(() -> {
                if (postButton != null) {
                    postButton.setEnabled(!finalPostOk);
                }
                if (finalPostOk) {
                    JOptionPane.showMessageDialog(mainGameFrame, finalMessage, title + " Posted", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(mainGameFrame, finalMessage, title + " Posted", JOptionPane.ERROR_MESSAGE);
                }
            });
        }).start();
    }
}
Also used : ProgressWindow(games.strategy.ui.ProgressWindow) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException)

Example 2 with ProgressWindow

use of games.strategy.ui.ProgressWindow in project triplea by triplea-game.

the class EmailSenderEditor method testEmail.

/**
 * Tests the email sender. This must be called from the swing event thread
 */
private void testEmail() {
    final ProgressWindow progressWindow = GameRunner.newProgressWindow("Sending test email...");
    progressWindow.setVisible(true);
    // start a background thread
    new Thread(() -> {
        // initialize variables to error state, override if successful
        String message = "An unknown occurred, report this as a bug on the TripleA dev forum";
        int messageType = JOptionPane.ERROR_MESSAGE;
        try {
            final File dummy = new File(ClientFileSystemHelper.getUserRootFolder(), "dummySave.txt");
            dummy.deleteOnExit();
            try (OutputStream fout = new FileOutputStream(dummy)) {
                fout.write("This file would normally be a save game".getBytes(StandardCharsets.UTF_8));
            }
            final String html = "<html><body><h1>Success</h1><p>This was a test email sent by TripleA<p></body></html>";
            ((IEmailSender) getBean()).sendEmail("TripleA Test", html, dummy, "dummy.txt");
            // email was sent, or an exception would have been thrown
            message = "Email sent, it should arrive shortly, otherwise check your spam folder";
            messageType = JOptionPane.INFORMATION_MESSAGE;
        } catch (final IOException ioe) {
            message = "Unable to send email, check SMTP server credentials: " + Ascii.truncate(ioe.getMessage(), 200, "...");
            ClientLogger.logError(message, ioe);
        } finally {
            // now that we have a result, marshall it back unto the swing thread
            final String finalMessage = message;
            final int finalMessageType = messageType;
            SwingUtilities.invokeLater(() -> GameRunner.showMessageDialog(finalMessage, GameRunner.Title.of("Email Test"), finalMessageType));
            progressWindow.setVisible(false);
        }
    }).start();
}
Also used : ProgressWindow(games.strategy.ui.ProgressWindow) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 3 with ProgressWindow

use of games.strategy.ui.ProgressWindow in project triplea by triplea-game.

the class ForumPosterEditor method testForum.

/**
 * Tests the Forum poster.
 */
void testForum() {
    final IForumPoster poster = (IForumPoster) getBean();
    final ProgressWindow progressWindow = GameRunner.newProgressWindow(poster.getTestMessage());
    progressWindow.setVisible(true);
    // start a background thread
    new Thread(() -> {
        if (poster.getIncludeSaveGame()) {
            try {
                final File f = File.createTempFile("123", "test");
                f.deleteOnExit();
                /*
           * For .txt use this:
           * final FileOutputStream fout = new FileOutputStream(f);
           * fout.write("Test upload".getBytes());
           * fout.close();
           * poster.addSaveGame(f, "test.txt");
           */
                // For .jpg use this:
                final BufferedImage image = new BufferedImage(130, 40, BufferedImage.TYPE_INT_RGB);
                final Graphics g = image.getGraphics();
                g.drawString("Testing file upload", 10, 20);
                try {
                    ImageIO.write(image, "jpg", f);
                } catch (final IOException e) {
                // ignore
                }
                poster.addSaveGame(f, "Test.jpg");
            } catch (final IOException e) {
            // ignore
            }
        }
        poster.postTurnSummary("Test summary from TripleA, engine version: " + ClientContext.engineVersion() + ", time: " + TimeManager.getLocalizedTime(), "Testing Forum poster");
        progressWindow.setVisible(false);
        // now that we have a result, marshall it back unto the swing thread
        SwingUtilities.invokeLater(() -> GameRunner.showMessageDialog(bean.getTurnSummaryRef(), GameRunner.Title.of("Test Turn Summary Post"), JOptionPane.INFORMATION_MESSAGE));
    }).start();
}
Also used : Graphics(java.awt.Graphics) ProgressWindow(games.strategy.ui.ProgressWindow) IOException(java.io.IOException) IForumPoster(games.strategy.engine.pbem.IForumPoster) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Aggregations

ProgressWindow (games.strategy.ui.ProgressWindow)3 File (java.io.File)3 IOException (java.io.IOException)3 IForumPoster (games.strategy.engine.pbem.IForumPoster)1 Graphics (java.awt.Graphics)1 BufferedImage (java.awt.image.BufferedImage)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1