Advertisement
Whatsoup

mumble quotes src

Feb 25th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.20 KB | None | 0 0
  1. import org.eclipse.swt.widgets.Display;
  2. import org.eclipse.swt.widgets.Shell;
  3. import org.eclipse.swt.widgets.Text;
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.util.ArrayList;
  11. import java.util.Arrays;
  12. import java.util.Random;
  13.  
  14. import org.eclipse.swt.SWT;
  15. import org.eclipse.swt.widgets.Button;
  16. import org.eclipse.swt.events.SelectionAdapter;
  17. import org.eclipse.swt.events.SelectionEvent;
  18. import org.eclipse.swt.events.MouseAdapter;
  19. import org.eclipse.swt.events.MouseEvent;
  20. import org.eclipse.wb.swt.SWTResourceManager;
  21. import org.eclipse.swt.widgets.Combo;
  22.  
  23. public class mumbleQuotes {
  24.  
  25.     protected Shell shlMumbleQuotes;
  26.     private Text quoteTextField;
  27.     static ArrayList<String> quotesList = new ArrayList<String>();
  28.     static ArrayList<String> mumbleBoys = new ArrayList<String>();
  29.     static String[] mumbleBoysArray;
  30.  
  31.     /**
  32.      * Launch the application.
  33.      *
  34.      * @param args
  35.      */
  36.     public static void main(String[] args) {
  37.         retrieveQuotes();
  38.         try {
  39.             mumbleQuotes window = new mumbleQuotes();
  40.             window.open();
  41.  
  42.         } catch (Exception e) {
  43.             e.printStackTrace();
  44.         }
  45.     }
  46.  
  47.     /**
  48.      * Open the window.
  49.      */
  50.     public void open() {
  51.         Display display = Display.getDefault();
  52.         createContents();
  53.         shlMumbleQuotes.open();
  54.         shlMumbleQuotes.layout();
  55.         while (!shlMumbleQuotes.isDisposed()) {
  56.             if (!display.readAndDispatch()) {
  57.                 display.sleep();
  58.             }
  59.         }
  60.     }
  61.  
  62.     /**
  63.      * Create contents of the window.
  64.      */
  65.     protected void createContents() {
  66.         shlMumbleQuotes = new Shell();
  67.         shlMumbleQuotes.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
  68.         shlMumbleQuotes.setSize(670, 213);
  69.         shlMumbleQuotes.setText("Mumble Quotes");
  70.  
  71.         quoteTextField = new Text(shlMumbleQuotes, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
  72.         quoteTextField.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
  73.         quoteTextField.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
  74.         quoteTextField.setFont(SWTResourceManager.getFont("Consolas", 9, SWT.NORMAL));
  75.         quoteTextField.setEditable(false);
  76.         quoteTextField.setBounds(10, 41, 634, 123);
  77.  
  78.         Button randomQuoteButton = new Button(shlMumbleQuotes, SWT.NONE);
  79.         randomQuoteButton.setFont(SWTResourceManager.getFont("Consolas", 9, SWT.NORMAL));
  80.         randomQuoteButton.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
  81.         randomQuoteButton.addMouseListener(new MouseAdapter() {
  82.             @Override
  83.             public void mouseDown(MouseEvent e) {
  84.                 retrieveQuotes();
  85.                 quoteTextField.setText(getRandomQuote());
  86.             }
  87.         });
  88.         randomQuoteButton.addSelectionListener(new SelectionAdapter() {
  89.             @Override
  90.             public void widgetSelected(SelectionEvent e) {
  91.             }
  92.         });
  93.         randomQuoteButton.setBounds(10, 10, 228, 25);
  94.         randomQuoteButton.setText("Random Quote");
  95.        
  96.         Combo personDropdown = new Combo(shlMumbleQuotes, SWT.NONE | SWT.READ_ONLY);
  97.         personDropdown.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
  98.         personDropdown.setFont(SWTResourceManager.getFont("Consolas", 9, SWT.NORMAL));
  99.         personDropdown.setBounds(502, 12, 142, 23);
  100.         personDropdown.setItems(mumbleBoysArray);
  101.        
  102.         Button randomQuoteByPersonButton = new Button(shlMumbleQuotes, SWT.NONE);
  103.         randomQuoteByPersonButton.addMouseListener(new MouseAdapter() {
  104.             @Override
  105.             public void mouseDown(MouseEvent e) {
  106.                 retrieveQuotes();
  107.                 for (int i = quotesList.size(); i > 0; i--) {
  108.                     if (!(quotesList.get(i - 1).toLowerCase().contains(personDropdown.getText()))) {
  109.                         quotesList.remove(i - 1);
  110.                     }
  111.                 }
  112.                 quoteTextField.setText(getRandomQuote());
  113.             }
  114.         });
  115.         randomQuoteByPersonButton.setFont(SWTResourceManager.getFont("Consolas", 9, SWT.NORMAL));
  116.         randomQuoteByPersonButton.setBounds(244, 10, 252, 25);
  117.         randomQuoteByPersonButton.setText("Quote with:");
  118.        
  119.        
  120.  
  121.     }
  122.  
  123.     public static void retrieveQuotes() {
  124.         try {
  125.             InputStream in = mumbleQuotes.class.getResourceAsStream("/files/quotes.txt");
  126.             BufferedReader br = new BufferedReader(new InputStreamReader(in));
  127.  
  128.             String sCurrentLine;
  129.             String currentQuote = "";
  130.  
  131.             while ((sCurrentLine = br.readLine()) != null) {
  132.                 // System.out.println(sCurrentLine);
  133.                 if (sCurrentLine.equals("")) {
  134.                     quotesList.add(currentQuote);
  135.                     currentQuote = "";
  136.                 } else {
  137.                     currentQuote += sCurrentLine + "\n";
  138.                 }
  139.                
  140.                 if (sCurrentLine.contains(":")) {
  141.                     String currentQuotee = sCurrentLine.split(":", 2)[0].toLowerCase();
  142.                    
  143.                     if (currentQuotee.contains(",")) {
  144.                         currentQuotee = currentQuotee.split(",", 2)[0];
  145.                     }
  146.                    
  147.                     if (!mumbleBoys.contains(currentQuotee)) {
  148.                         mumbleBoys.add(currentQuotee);
  149.                     }
  150.                 }
  151.                
  152.             }
  153.            
  154.         mumbleBoysArray = mumbleBoys.toArray(new String[mumbleBoys.size()]);
  155.         Arrays.sort(mumbleBoysArray);
  156.  
  157.         } catch (IOException e) {
  158.             e.printStackTrace();
  159.         }
  160.  
  161.     }
  162.  
  163.     public static String getRandomQuote() {
  164.         if (quotesList.size() == 0) {
  165.             System.out.println("Quotes list is empty.");
  166.             return "";
  167.         }
  168.  
  169.         else {
  170.             String quote = "";
  171.             Random r = new Random();
  172.                 int randomNum = r.nextInt(quotesList.size());
  173.                 quote = quotesList.get(randomNum);
  174.             return quote;
  175.         }
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement