Advertisement
Guest User

Pigibook

a guest
Mar 7th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.39 KB | None | 0 0
  1. package Pigibook;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.Font;
  5. import java.awt.Toolkit;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.KeyAdapter;
  9. import java.awt.event.KeyEvent;
  10. import java.io.BufferedReader;
  11. import java.io.BufferedWriter;
  12. import java.io.File;
  13. import java.io.FileReader;
  14. import java.io.FileWriter;
  15. import java.io.IOException;
  16. import java.io.InputStreamReader;
  17. import java.io.PrintWriter;
  18. import java.net.Socket;
  19.  
  20. import javax.swing.JCheckBoxMenuItem;
  21. import javax.swing.JFileChooser;
  22. import javax.swing.JFrame;
  23. import javax.swing.JMenu;
  24. import javax.swing.JMenuBar;
  25. import javax.swing.JMenuItem;
  26. import javax.swing.JOptionPane;
  27. import javax.swing.JScrollPane;
  28. import javax.swing.JTextArea;
  29.  
  30. public class Pigibook
  31. {
  32.     JMenuBar menuBar;
  33.     JMenu fileMenu, editMenu, formatMenu, viewMenu, helpMenu, networkingMenu;
  34.     JTextArea textArea;
  35.     JFrame frame;
  36.     JScrollPane scrollPane;
  37.     JMenuItem newItem, openItem, saveAsItem, exitItem, undoItem, copyItem, pasteItem, deleteItem, fontItem, aboutItem, sendItem, receiveItem, connectItem;
  38.     JCheckBoxMenuItem wordWrap, statusBar;
  39.     String[] undoOption = new String[20];
  40.     double dim_x, dim_y, start_width, start_height;
  41.     boolean line_wrap = false;
  42.     String currentSavedWords = "";
  43.     Font font;
  44.     static Pigibook pigibook;
  45.     Socket socket;
  46.     BufferedReader bufferedReader;
  47.     PrintWriter printWriter;
  48.    
  49.     //do all sorts needed at startup, to run smoothly
  50.     public Pigibook()
  51.     {
  52.         font = new Font("Arial", Font.PLAIN, 12);
  53.        
  54.         getStartPosition();
  55.         makeMenuBar();
  56.         importantTextArea();
  57.     }
  58.    
  59.     public static void main(String[] args)
  60.     {
  61.         pigibook = new Pigibook();
  62.         pigibook.initialize();
  63.     }
  64.    
  65.     //initialize the frame
  66.     public void initialize()
  67.     {
  68.         frame = new JFrame("Pigibook");
  69.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  70.         frame.setSize((int)start_width, (int)start_height);
  71.        
  72.         frame.setLocation((int)dim_x / 10, (int)dim_y / 15);
  73.  
  74.         frame.add(scrollPane);
  75.         frame.setJMenuBar(menuBar);
  76.         frame.setVisible(true);
  77.     }
  78.    
  79.     //where on screen does the program show
  80.     public void getStartPosition()
  81.     {
  82.         Toolkit toolkit = Toolkit.getDefaultToolkit();
  83.         Dimension dimension = toolkit.getScreenSize();
  84.         dim_x = dimension.getWidth();
  85.         dim_y = dimension.getHeight();
  86.         start_width = ((double)4 / (double)9) * dim_x;
  87.         start_height = ((double)5 / (double)6) * dim_y;
  88.     }
  89.    
  90.     //menu bar with all its components intact
  91.     public void makeMenuBar()
  92.     {
  93.         menuBar = new JMenuBar();
  94.        
  95.         //for menu bar
  96.         fileMenu = new JMenu("File");
  97.         editMenu = new JMenu("Edit");
  98.         formatMenu = new JMenu("Format");
  99.         viewMenu = new JMenu("View");
  100.         helpMenu = new JMenu("Help");
  101.         networkingMenu = new JMenu("Networking");
  102.        
  103.         //for file menu
  104.         newItem = menuItem("New", fileMenu);
  105.         openItem = menuItem("Open", fileMenu);
  106.         saveAsItem = menuItem("Save As", fileMenu);
  107.         exitItem = menuItem("Exit", fileMenu);
  108.        
  109.         //for edit menu
  110.         undoItem = menuItem("Undo", editMenu);
  111.         copyItem = menuItem("Copy", editMenu);
  112.         pasteItem = menuItem("Paste", editMenu);
  113.         deleteItem = menuItem("Delete", editMenu);
  114.        
  115.         //for format menu
  116.         wordWrap = checkBoxMenuItem("Word Wrap", formatMenu);
  117.         fontItem = menuItem("Font", formatMenu);
  118.        
  119.         //for view menu
  120.         statusBar = checkBoxMenuItem("Status Bar", viewMenu);
  121.        
  122.         //for help menu
  123.         aboutItem = menuItem("About Pigibook", helpMenu);  
  124.        
  125.         //for networking menu
  126.         sendItem = menuItem("Send", networkingMenu);
  127.         receiveItem = menuItem("Receive", networkingMenu);
  128.         connectItem = menuItem("Connect...", networkingMenu);
  129.        
  130.         menuBar.add(fileMenu);
  131.         menuBar.add(editMenu);
  132.         menuBar.add(formatMenu);
  133.         menuBar.add(viewMenu);
  134.         menuBar.add(helpMenu);
  135.         menuBar.add(networkingMenu);
  136.     }
  137.    
  138.     //make each menu's item's functions
  139.     public JMenuItem menuItem(String itemName, JMenu menuName)
  140.     {
  141.         JMenuItem theMenuItem = new JMenuItem(itemName);
  142.        
  143.         theMenuItem.addActionListener(new ListenForAction());
  144.        
  145.         menuName.add(theMenuItem);
  146.        
  147.         return theMenuItem;
  148.     }
  149.    
  150.     //for check box menu items
  151.     public JCheckBoxMenuItem checkBoxMenuItem(String itemName, JMenu menuName)
  152.     {
  153.         JCheckBoxMenuItem theMenuItem = new JCheckBoxMenuItem(itemName);
  154.        
  155.         theMenuItem.addActionListener(new ListenForAction());
  156.        
  157.         menuName.add(theMenuItem);
  158.        
  159.         return theMenuItem;
  160.     }
  161.    
  162.     //considering text area, one important aspect of Pigibook
  163.     public void importantTextArea()
  164.     {
  165.         textArea = new JTextArea(1, 1);
  166.         textArea.setFont(font);
  167.        
  168.         changeListenerForTextArea();
  169.        
  170.         scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  171.     }
  172.    
  173.     //change listener for text area
  174.     public void changeListenerForTextArea()
  175.     {
  176.         textArea.addKeyListener(new KeyAdapter()
  177.         {
  178.             public void keyPressed(KeyEvent event)
  179.             {
  180.                 sendMessages();
  181.             }
  182.         });
  183.     }
  184.    
  185.     //listen for action -> respond accordingly
  186.     private class ListenForAction implements ActionListener
  187.     {
  188.         public void actionPerformed(ActionEvent actionEvent)
  189.         {
  190.             if (actionEvent.getSource() == wordWrap)
  191.             {
  192.                 wordWrapAction();
  193.             }
  194.             else if (actionEvent.getSource() == newItem)
  195.             {
  196.                 newItemAction();
  197.             }
  198.             else if (actionEvent.getSource() == openItem)
  199.             {
  200.                 openItemAction();
  201.             }
  202.             else if (actionEvent.getSource() == saveAsItem)
  203.             {
  204.                 saveAsItemAction();
  205.             }
  206.             else if (actionEvent.getSource() == exitItem)
  207.             {
  208.                 exitItemAction();
  209.             }
  210.             else if (actionEvent.getSource() == undoItem)
  211.             {
  212.                 undoItemAction();
  213.             }
  214.             else if (actionEvent.getSource() == copyItem)
  215.             {
  216.                 copyItemAction();
  217.             }
  218.             else if (actionEvent.getSource() == pasteItem)
  219.             {
  220.                 pasteItemAction();
  221.             }
  222.             else if (actionEvent.getSource() == deleteItem)
  223.             {
  224.                 deleteItemAction();
  225.             }
  226.             else if (actionEvent.getSource() == aboutItem)
  227.             {
  228.                 aboutAuthorAction();
  229.             }
  230.             else if (actionEvent.getSource() == fontItem)
  231.             {
  232.                 fontItemAction();
  233.             }
  234.             else if (actionEvent.getSource() == connectItem)
  235.             {
  236.                 connectItemAction();
  237.             }
  238.         }
  239.        
  240.         //align text for nice view
  241.         public void wordWrapAction()
  242.         {
  243.             if (!line_wrap)
  244.             {
  245.                 line_wrap = true;
  246.                
  247.                 textArea.setLineWrap(true);
  248.                 textArea.setWrapStyleWord(true);
  249.             }
  250.             else
  251.             {
  252.                 line_wrap = false;
  253.                
  254.                 textArea.setLineWrap(false);
  255.                 textArea.setWrapStyleWord(false);
  256.             }
  257.         }
  258.        
  259.         //delete with/without save
  260.         public void newItemAction()
  261.         {
  262.             specialExit();
  263.         }
  264.        
  265.         //load up an already written file
  266.         public void openItemAction()
  267.         {
  268.             JFileChooser fileChooser = new JFileChooser();
  269.             fileChooser.showOpenDialog(null);
  270.             openFile(fileChooser.getSelectedFile());
  271.         }
  272.        
  273.         //save a new text to a location
  274.         public void saveAsItemAction()
  275.         {
  276.             JFileChooser fileChooser = new JFileChooser();
  277.             fileChooser.showSaveDialog(null);
  278.             saveFile(fileChooser.getSelectedFile());
  279.         }
  280.        
  281.         //exit like X on window
  282.         public void exitItemAction()
  283.         {
  284.             specialExit();
  285.             System.exit(0);
  286.         }
  287.        
  288.         //save with readers, writer...
  289.         public void saveFile(File file)
  290.         {
  291.             try {
  292.                 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
  293.                
  294.                 bufferedWriter.write(textArea.getText());
  295.                
  296.                 bufferedWriter.close();
  297.             } catch (Exception ex) {
  298.                 ex.printStackTrace();
  299.             }
  300.         }
  301.        
  302.         //open with writers, reader...
  303.         public void openFile(File file)
  304.         {
  305.             String readedText = null;
  306.             String endGameText = "";
  307.            
  308.             try {
  309.                 BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
  310.                
  311.                 while ((readedText = bufferedReader.readLine()) != null)
  312.                 {
  313.                     endGameText += readedText;
  314.                 }              
  315.                
  316.                 bufferedReader.close();
  317.             } catch (Exception ex) {
  318.                 ex.printStackTrace();
  319.             }
  320.            
  321.             textArea.setText(endGameText);
  322.         }
  323.        
  324.         //undo some/anyhow words
  325.         public void undoItemAction()
  326.         {
  327.            
  328.         }
  329.        
  330.         //copy some/anyhow words
  331.         public void copyItemAction()
  332.         {
  333.             currentSavedWords = textArea.getSelectedText();
  334.         }
  335.        
  336.         //paste some/anyhow words
  337.         public void pasteItemAction()
  338.         {
  339.             textArea.insert(currentSavedWords, textArea.getCaretPosition());
  340.         }
  341.        
  342.         //delete some/anyhow words
  343.         public void deleteItemAction()
  344.         {
  345.             textArea.replaceSelection("");
  346.         }
  347.        
  348.         //special exit choice for a lot of things
  349.         public void specialExit()
  350.         {
  351.             int response = JOptionPane.showConfirmDialog(null, "Would you like to save current changes?     ");
  352.            
  353.             if (response == JOptionPane.YES_OPTION)
  354.             {
  355.                 saveAsItemAction();
  356.                
  357.                 textArea.setText("");
  358.             }
  359.             else if (response == JOptionPane.NO_OPTION)
  360.             {
  361.                 textArea.setText("");
  362.             }
  363.             else {}
  364.         }
  365.        
  366.         //about the author!!!
  367.         public void aboutAuthorAction()
  368.         {
  369.             JOptionPane.showMessageDialog(null, "Pigibook alpha\nVersion: 1.0001a (Build 3)\nProgram was create in year 2016, by an\nexcellent man, who undergoes the nickname Pigi.\nIt is licensed under Pigy corparate.", "About author", JOptionPane.INFORMATION_MESSAGE);
  370.         }
  371.        
  372.         //changing fonts
  373.         public void fontItemAction()
  374.         {
  375.             FontChooser fontChooser = new FontChooser(font);
  376.             fontChooser.showFontDialog();
  377.  
  378.             font = fontChooser.getFontAttributes();
  379.             textArea.setFont(font);
  380.             System.out.println(fontChooser);
  381.         }
  382.     }
  383.    
  384.     //connect with all clients
  385.     public void connectItemAction()
  386.     {
  387.         try {
  388.             socket = new Socket("localhost", 3547);
  389.            
  390.             printWriter = new PrintWriter(socket.getOutputStream(), true);
  391.            
  392.             receiveMessages();
  393.         } catch (IOException ex) {
  394.             ex.printStackTrace();
  395.         }
  396.     }
  397.        
  398.     //send every character through network
  399.     public void sendMessages()
  400.     {
  401.         try {
  402.             printWriter.println(String.valueOf(textArea.getText()));
  403.         } catch (Exception ex) {
  404.             ex.printStackTrace();
  405.         }
  406.     }
  407.    
  408.     //receive every character through network
  409.     public void receiveMessages()
  410.     {
  411.         new Thread(new ClientHandler()).start();
  412.     }
  413.    
  414.     //thread to handle all the receiving
  415.     private class ClientHandler implements Runnable
  416.     {
  417.         public void run()
  418.         {
  419.             String message = null;
  420.                
  421.             try {
  422.                 bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  423.                
  424.                 while ((message = bufferedReader.readLine()) != null)
  425.                 {
  426.                     if (!message.equals(""))
  427.                     {
  428.                         textArea.setText(message);
  429.                         textArea.setCaretPosition(textArea.getDocument().getLength());
  430.                     }
  431.                 }
  432.             } catch (IOException ex) {
  433.                 ex.printStackTrace();
  434.             }
  435.         }
  436.     }
  437. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement