/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package icons; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.io.PrintStream; import java.net.MalformedURLException; import java.net.URL; import javax.swing.AbstractAction; import javax.swing.ActionMap; import javax.swing.InputMap; import javax.swing.JComponent; import javax.swing.KeyStroke; import javax.swing.text.BadLocationException; import se.kth.karamel.webservice.KaramelServiceApplication; public class LaunchLogDialog extends javax.swing.JDialog { private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(LaunchLogDialog.class); private final int port; private final Image image; private PrintStream printStream; /** * A return status code - returned if Cancel button has been pressed */ public static final int RET_CANCEL = 0; /** * A return status code - returned if OK button has been pressed */ public static final int RET_OK = 1; /** * Creates new form LaunchLogDialog * * @param parent * @param modal * @param port for dropwizard * @param image icon */ public LaunchLogDialog(java.awt.Frame parent, int port, Image image) { super(parent, "Karamel", false); this.port = port; this.image = image; initComponents(); // Close the dialog when Esc is pressed String cancelName = "cancel"; InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName); ActionMap actionMap = getRootPane().getActionMap(); actionMap.put(cancelName, new AbstractAction() { public void actionPerformed(ActionEvent e) { doClose(RET_CANCEL); } }); printStream = new PrintStream(new CustomOutputStream(jTextAreaKaramelStdOut)); System.setOut(printStream); System.setErr(printStream); } /** * @return the return status of this dialog - one of RET_OK or RET_CANCEL */ public int getReturnStatus() { return returnStatus; } /** * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The * content of this method is always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { launchButton = new javax.swing.JButton(); closelButton = new javax.swing.JButton(); jScrollPaneKaramelStdOut = new javax.swing.JScrollPane(); jTextAreaKaramelStdOut = new javax.swing.JTextArea(); clearButton = new javax.swing.JButton(); jButton1 = new javax.swing.JButton(); setIconImage(this.image); setIconImages(null); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { closeDialog(evt); } }); launchButton.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N launchButton.setText("Launch Browser"); launchButton.setMaximumSize(new java.awt.Dimension(140, 33)); launchButton.setMinimumSize(new java.awt.Dimension(140, 33)); launchButton.setPreferredSize(new java.awt.Dimension(163, 33)); launchButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { launchButtonActionPerformed(evt); } }); closelButton.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N closelButton.setText("Close"); closelButton.setMaximumSize(new java.awt.Dimension(140, 33)); closelButton.setMinimumSize(new java.awt.Dimension(140, 33)); closelButton.setPreferredSize(new java.awt.Dimension(163, 33)); closelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { closelButtonActionPerformed(evt); } }); jScrollPaneKaramelStdOut.setDoubleBuffered(true); jTextAreaKaramelStdOut.setEditable(false); jTextAreaKaramelStdOut.setBackground(new java.awt.Color(4, 4, 4)); jTextAreaKaramelStdOut.setColumns(20); jTextAreaKaramelStdOut.setFont(new java.awt.Font("Courier", 0, 14)); // NOI18N jTextAreaKaramelStdOut.setForeground(new java.awt.Color(250, 250, 250)); jTextAreaKaramelStdOut.setLineWrap(true); jTextAreaKaramelStdOut.setRows(15); jTextAreaKaramelStdOut.setTabSize(4); jTextAreaKaramelStdOut.setText(" __ __ __ \n/ | / | / | \n$$ | __ ______ ______ ______ _____ ____ ______ $$ | $$ | ______ ______ \n$$ | / | / \\ / \\ / \\ / \\/ \\ / \\ $$ | $$ | / \\ / \\ \n$$ |_/$$/ $$$$$$ |/$$$$$$ |$$$$$$ |$$$$$$ $$$$ |/$$$$$$ |$$ | $$ |/$$$$$$ |/$$$$$$ |\n$$ $$< / $$ |$$ | $$/ / $$ |$$ | $$ | $$ |$$ $$ |$$ | $$ |$$ | $$ |$$ | $$ |\n$$$$$$ \\ /$$$$$$$ |$$ | /$$$$$$$ |$$ | $$ | $$ |$$$$$$$$/ $$ | $$ |$$ \\__$$ |$$ \\__$$ |\n$$ | $$ |$$ $$ |$$ | $$ $$ |$$ | $$ | $$ |$$ |$$ | $$ |$$ $$/ $$ $$ |\n$$/ $$/ $$$$$$$/ $$/ $$$$$$$/ $$/ $$/ $$/ $$$$$$$/ $$/ $$/ $$$$$$/ $$$$$$$ |\n / \\__$$ |\n $$ $$/ \n $$$$$$/ "); jTextAreaKaramelStdOut.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(186, 242, 247))); jTextAreaKaramelStdOut.setCaretColor(new java.awt.Color(254, 254, 254)); jTextAreaKaramelStdOut.setDisabledTextColor(new java.awt.Color(241, 240, 238)); jTextAreaKaramelStdOut.setSelectedTextColor(new java.awt.Color(252, 251, 38)); jScrollPaneKaramelStdOut.setViewportView(jTextAreaKaramelStdOut); clearButton.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N clearButton.setText("Clear Screen"); clearButton.setPreferredSize(new java.awt.Dimension(163, 33)); clearButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { clearButtonActionPerformed(evt); } }); jButton1.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N jButton1.setText("Shutdown"); jButton1.setMaximumSize(new java.awt.Dimension(163, 31)); jButton1.setMinimumSize(new java.awt.Dimension(163, 31)); jButton1.setPreferredSize(new java.awt.Dimension(163, 31)); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(launchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(clearButton, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(closelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPaneKaramelStdOut, javax.swing.GroupLayout.DEFAULT_SIZE, 853, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPaneKaramelStdOut, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(closelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(launchButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(clearButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) ); getRootPane().setDefaultButton(launchButton); pack(); }// </editor-fold>//GEN-END:initComponents private void launchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_launchButtonActionPerformed try { KaramelServiceApplication.openWebpage(new URL("http://localhost:" + this.port + "/index.html")); } catch (MalformedURLException ex) { logger.error("", ex); } }//GEN-LAST:event_launchButtonActionPerformed private void closelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closelButtonActionPerformed doClose(RET_OK); }//GEN-LAST:event_closelButtonActionPerformed /** * Closes the dialog */ private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog doClose(RET_OK); }//GEN-LAST:event_closeDialog private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed try { jTextAreaKaramelStdOut.getDocument().remove(0, jTextAreaKaramelStdOut.getDocument().getLength()); } catch (BadLocationException ex) { logger.error("", ex); } }//GEN-LAST:event_clearButtonActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed logger.info("User requested to exit Karamel from the TrayIcon."); System.exit(0); }//GEN-LAST:event_jButton1ActionPerformed private void doClose(int retStatus) { returnStatus = retStatus; setVisible(false); dispose(); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton clearButton; private javax.swing.JButton closelButton; private javax.swing.JButton jButton1; private javax.swing.JScrollPane jScrollPaneKaramelStdOut; private javax.swing.JTextArea jTextAreaKaramelStdOut; private javax.swing.JButton launchButton; // End of variables declaration//GEN-END:variables private int returnStatus = RET_CANCEL; }