Advertisement
angrymasteryoda

Custom Console

Jan 30th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.24 KB | None | 0 0
  1. package com.michael.tech.api.console;
  2.  
  3.  
  4. import com.michael.tech.api.Time;
  5. import java.awt.Toolkit;
  6. import java.awt.datatransfer.Clipboard;
  7. import java.awt.datatransfer.StringSelection;
  8.  
  9. /*
  10.  * To change this template, choose Tools | Templates
  11.  * and open the template in the editor.
  12.  */
  13.  
  14. /**
  15.  *
  16.  * @author Michael Risher
  17.  */
  18. public class RunConsole extends javax.swing.JFrame {
  19.  
  20.     // Variables declaration - do not modify//GEN-BEGIN:variables
  21.     private static javax.swing.JTextArea console;
  22.     public static javax.swing.JTextField input;
  23.     private javax.swing.JScrollPane jScrollPane1;
  24.     // End of variables declaration//GEN-END:variables
  25.     public static boolean waitForUser = false;
  26.     public static boolean waitingForResponse = false;
  27.    
  28.     private static boolean EchoTime = true;
  29.     private boolean echoClassPath = false;
  30.     private String classPathName = "";
  31.    
  32.     /**
  33.      * Creates new Console
  34.      */
  35.     public RunConsole() {
  36.         initComponents();
  37.     }
  38.  
  39.     /**
  40.      * This method is called from within the constructor to initialize the form.
  41.      * WARNING: Do NOT modify this code. The content of this method is always
  42.      * regenerated by the Form Editor.
  43.      */
  44.     @SuppressWarnings("unchecked")
  45.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  46.     private void initComponents() {
  47.  
  48.         jScrollPane1 = new javax.swing.JScrollPane();
  49.         console = new javax.swing.JTextArea();
  50.         input = new javax.swing.JTextField();
  51.  
  52.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  53.  
  54.         console.setBackground(new java.awt.Color(102, 102, 102));
  55.         console.setColumns(20);
  56.         console.setEditable(false);
  57.         console.setForeground(new java.awt.Color(255, 51, 51));
  58.         console.setLineWrap(true);
  59.         console.setRows(5);
  60.         jScrollPane1.setViewportView(console);
  61.  
  62.         input.setBackground(new java.awt.Color(102, 102, 102));
  63.         input.setForeground(new java.awt.Color(255, 51, 51));
  64.         input.addActionListener(new java.awt.event.ActionListener() {
  65.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  66.                 inputActionPerformed(evt);
  67.             }
  68.         });
  69.  
  70.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  71.         getContentPane().setLayout(layout);
  72.         layout.setHorizontalGroup(
  73.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  74.             .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
  75.             .addGroup(layout.createSequentialGroup()
  76.                 .addContainerGap()
  77.                 .addComponent(input)
  78.                 .addContainerGap())
  79.         );
  80.         layout.setVerticalGroup(
  81.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  82.             .addGroup(layout.createSequentialGroup()
  83.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
  84.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  85.                 .addComponent(input, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  86.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  87.         );
  88.  
  89.         pack();
  90.     }// </editor-fold>//GEN-END:initComponents
  91.  
  92.     private void inputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputActionPerformed
  93.         toConsole(input.getText());
  94.         if(waitingForResponse){
  95.             waitForUser = false;
  96.         }
  97.         input.setText("");
  98.     }//GEN-LAST:event_inputActionPerformed
  99.    
  100.     /*public static void main(String args[]) {
  101. //         *
  102. //         * Set the Nimbus look and feel
  103. //         *
  104.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  105. //    
  106. //         * If Nimbus (introduced in Java SE 6) is not available, stay with the
  107. //         * default look and feel. For details see
  108. //         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  109.              
  110.         try {
  111.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  112.                 if ("Nimbus".equals(info.getName())) {
  113.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  114.                     break;
  115.                 }
  116.             }
  117.         } catch (ClassNotFoundException ex) {
  118.             java.util.logging.Logger.getLogger(Run.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  119.         } catch (InstantiationException ex) {
  120.             java.util.logging.Logger.getLogger(Run.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  121.         } catch (IllegalAccessException ex) {
  122.             java.util.logging.Logger.getLogger(Run.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  123.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  124.             java.util.logging.Logger.getLogger(Run.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  125.         }
  126.         //</editor-fold>
  127.  
  128. //         *
  129. //         * Create and display the form
  130. //         *
  131.         java.awt.EventQueue.invokeLater(new Runnable() {
  132.  
  133.             public void run() {
  134.                 new Run().setVisible(true);
  135.                 //new TestScreen().setVisible(true);
  136.                
  137.             }
  138.         });
  139.     }*/
  140.    
  141.     public static void startConsole(){
  142.         try {
  143.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  144.                 if ("Nimbus".equals(info.getName())) {
  145.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  146.                     break;
  147.                 }
  148.             }
  149.         } catch (ClassNotFoundException ex) {
  150.             java.util.logging.Logger.getLogger(RunConsole.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  151.         } catch (InstantiationException ex) {
  152.             java.util.logging.Logger.getLogger(RunConsole.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  153.         } catch (IllegalAccessException ex) {
  154.             java.util.logging.Logger.getLogger(RunConsole.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  155.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  156.             java.util.logging.Logger.getLogger(RunConsole.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  157.         }
  158.  
  159.         /*
  160.          * Create and display the form
  161.          */
  162.         java.awt.EventQueue.invokeLater(new Runnable() {
  163.  
  164.             public void run() {
  165.                 new RunConsole().setVisible(true);
  166.             }
  167.         });
  168.     }
  169.    
  170.     public static String getConsole(){
  171.         return console.getText();
  172.     }
  173.    
  174.     /**
  175.      * Set the entire console's text
  176.      * @param s
  177.      */
  178.     public static void setConsole(String s){
  179.         console.setText(s);
  180.     }
  181.    
  182.     /**
  183.      * add a line to the console using the time stamp
  184.      * @param s
  185.      */
  186.     private static void addConsole(String s){
  187.         //String o = console.getText();
  188.         console.setText( ( console.getText().isEmpty()) ? s : (console.getText() + "\n" + s) );
  189.     }
  190.    
  191.     /**
  192.      * add a line to the console with the time stamp
  193.      * @param s
  194.      */
  195.     public static void toConsole(String s, boolean timeStamp, boolean classPath, String className){
  196.         if(s.startsWith("/")){
  197.             doCommand(s);
  198.             return;
  199.         }
  200.         Time t = new Time();
  201.         t.getSYSPrint();
  202.         String time = "[" + t.toMilitary() + "] ";
  203.         if(EchoTime || timeStamp){
  204.             addConsole(time + s);
  205.         }
  206.         else if(classPath){
  207.             addConsole(className);
  208.         }
  209.         else{
  210.             addConsole(s);
  211.         }
  212.     }
  213.    
  214.     public static void toConsole(String s, boolean classPath, String className){
  215.         toConsole(s, false, classPath, className);
  216.     }
  217.    
  218.     public static void toConsole(String s, boolean timeStamp){
  219.         toConsole(s, timeStamp, false, null);
  220.     }
  221.    
  222.     public static void toConsole(String s){
  223.         toConsole(s, false, false, null);
  224.     }
  225.    
  226.     private static void doCommand(String s){
  227.         if(s.equalsIgnoreCase("/copy")){
  228.             setClipboardContents(getConsole());
  229.             addConsole("Copied all of the above text");
  230.         }
  231.         if(s.equalsIgnoreCase("/echo")){
  232.            EchoTime = !EchoTime;
  233.         }
  234.     }
  235.    
  236.     private static void setClipboardContents(String s){
  237.         StringSelection stringSelection = new StringSelection( s );
  238.         Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
  239.         clip.setContents(stringSelection, null);
  240.     }
  241.    
  242.     public static String waitForInput(){
  243.         waitForUser = true;
  244.         waitingForResponse = true;
  245.         String s = null;
  246.         while(waitForUser){
  247.             s = input.getText();
  248.         }
  249.         waitingForResponse = false;
  250.         return s;
  251.     }
  252.  
  253.    
  254.     public String getClassPathName() {
  255.         return classPathName;
  256.     }
  257.  
  258.    
  259.     public void setClassPathName(String classPathName) {
  260.         this.classPathName = classPathName;
  261.     }
  262.  
  263.    
  264.     public void setEchoClassPath(boolean echoClassPath) {
  265.         this.echoClassPath = echoClassPath;
  266.     }
  267.    
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement