Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Shell;
- import org.eclipse.swt.widgets.Text;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.widgets.Button;
- import org.eclipse.swt.events.SelectionAdapter;
- import org.eclipse.swt.events.SelectionEvent;
- import org.eclipse.swt.events.MouseAdapter;
- import org.eclipse.swt.events.MouseEvent;
- import org.eclipse.wb.swt.SWTResourceManager;
- import org.eclipse.swt.widgets.Combo;
- public class mumbleQuotes {
- protected Shell shlMumbleQuotes;
- private Text quoteTextField;
- static ArrayList<String> quotesList = new ArrayList<String>();
- static ArrayList<String> mumbleBoys = new ArrayList<String>();
- static String[] mumbleBoysArray;
- /**
- * Launch the application.
- *
- * @param args
- */
- public static void main(String[] args) {
- retrieveQuotes();
- try {
- mumbleQuotes window = new mumbleQuotes();
- window.open();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Open the window.
- */
- public void open() {
- Display display = Display.getDefault();
- createContents();
- shlMumbleQuotes.open();
- shlMumbleQuotes.layout();
- while (!shlMumbleQuotes.isDisposed()) {
- if (!display.readAndDispatch()) {
- display.sleep();
- }
- }
- }
- /**
- * Create contents of the window.
- */
- protected void createContents() {
- shlMumbleQuotes = new Shell();
- shlMumbleQuotes.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
- shlMumbleQuotes.setSize(670, 213);
- shlMumbleQuotes.setText("Mumble Quotes");
- quoteTextField = new Text(shlMumbleQuotes, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
- quoteTextField.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
- quoteTextField.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
- quoteTextField.setFont(SWTResourceManager.getFont("Consolas", 9, SWT.NORMAL));
- quoteTextField.setEditable(false);
- quoteTextField.setBounds(10, 41, 634, 123);
- Button randomQuoteButton = new Button(shlMumbleQuotes, SWT.NONE);
- randomQuoteButton.setFont(SWTResourceManager.getFont("Consolas", 9, SWT.NORMAL));
- randomQuoteButton.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
- randomQuoteButton.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseDown(MouseEvent e) {
- retrieveQuotes();
- quoteTextField.setText(getRandomQuote());
- }
- });
- randomQuoteButton.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- }
- });
- randomQuoteButton.setBounds(10, 10, 228, 25);
- randomQuoteButton.setText("Random Quote");
- Combo personDropdown = new Combo(shlMumbleQuotes, SWT.NONE | SWT.READ_ONLY);
- personDropdown.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
- personDropdown.setFont(SWTResourceManager.getFont("Consolas", 9, SWT.NORMAL));
- personDropdown.setBounds(502, 12, 142, 23);
- personDropdown.setItems(mumbleBoysArray);
- Button randomQuoteByPersonButton = new Button(shlMumbleQuotes, SWT.NONE);
- randomQuoteByPersonButton.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseDown(MouseEvent e) {
- retrieveQuotes();
- for (int i = quotesList.size(); i > 0; i--) {
- if (!(quotesList.get(i - 1).toLowerCase().contains(personDropdown.getText()))) {
- quotesList.remove(i - 1);
- }
- }
- quoteTextField.setText(getRandomQuote());
- }
- });
- randomQuoteByPersonButton.setFont(SWTResourceManager.getFont("Consolas", 9, SWT.NORMAL));
- randomQuoteByPersonButton.setBounds(244, 10, 252, 25);
- randomQuoteByPersonButton.setText("Quote with:");
- }
- public static void retrieveQuotes() {
- try {
- InputStream in = mumbleQuotes.class.getResourceAsStream("/files/quotes.txt");
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- String sCurrentLine;
- String currentQuote = "";
- while ((sCurrentLine = br.readLine()) != null) {
- // System.out.println(sCurrentLine);
- if (sCurrentLine.equals("")) {
- quotesList.add(currentQuote);
- currentQuote = "";
- } else {
- currentQuote += sCurrentLine + "\n";
- }
- if (sCurrentLine.contains(":")) {
- String currentQuotee = sCurrentLine.split(":", 2)[0].toLowerCase();
- if (currentQuotee.contains(",")) {
- currentQuotee = currentQuotee.split(",", 2)[0];
- }
- if (!mumbleBoys.contains(currentQuotee)) {
- mumbleBoys.add(currentQuotee);
- }
- }
- }
- mumbleBoysArray = mumbleBoys.toArray(new String[mumbleBoys.size()]);
- Arrays.sort(mumbleBoysArray);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static String getRandomQuote() {
- if (quotesList.size() == 0) {
- System.out.println("Quotes list is empty.");
- return "";
- }
- else {
- String quote = "";
- Random r = new Random();
- int randomNum = r.nextInt(quotesList.size());
- quote = quotesList.get(randomNum);
- return quote;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement