Portfolio
  • Home
  • About
/**
 * @author Scott
 * @description
 * This class will set up the title bar for the application
 */
package me.scotth0828.main.Window;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TitleBar extends JPanel implements MouseListener, MouseMotionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	JFrame frame; // JFrame variable

	public TitleBar(JFrame frame) { // constructor
		this.frame = frame;

		this.setPreferredSize(new Dimension(Window.WINDOW_WIDTH, Window.WINDOW_HEIGHT / 10)); // size set
		this.setBackground(Window.COLOR_TITLE);

		JLabel title = new JLabel("TO DO LIST");
		
		LocalDateTime dt = LocalDateTime.now();
		DateTimeFormatter format = DateTimeFormatter.ofPattern("MMMM dd, yyyy");
		
		title.setText(dt.format(format));

		title.setPreferredSize(new Dimension(Window.WINDOW_WIDTH, Window.WINDOW_HEIGHT / 10)); // title size
		title.setFont(new Font("Times New Roman", Font.BOLD, 42)); // font

		title.setHorizontalAlignment(JLabel.CENTER);

		title.setForeground(Color.BLACK);

		this.add(title); // title added

		this.addMouseListener(this); // listeners are setup
		this.addMouseMotionListener(this);
	}

	private int xx = 0, yy = 0; // variables for dragging feature

	@Override
	public void mouseClicked(MouseEvent e) { // mouse listeners for moving around the application
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		xx = e.getX(); // mouse x and y
		yy = e.getY();

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		int x = e.getXOnScreen();
		int y = e.getYOnScreen();

		frame.setLocation(x - xx, y - yy); // application moves according to mouse drag of application
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub

	}

}
/**
 * @author Scott
 * @description
 * Sets up main window while adding in all components.
 */
package me.scotth0828.main.Window;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;

import me.scotth0828.main.Data.data;

public class Window extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final int WINDOW_WIDTH = 400, WINDOW_HEIGHT = 800; // Window sizes are set

	public static final Color COLOR_TITLE = new Color(250, 250, 250); // static colors are set
	public static final Color COLOR_SIDEBAR = new Color(250, 250, 250);
	public static final Color COLOR_LISTAREA = new Color(255, 255, 224);

	/*
	 * @description: JFrame is set up with all components.
	 */
	public Window() {
		this.setSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT)); // size set

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // close operation

		this.setResizable(false); // set resizable to false

		this.getContentPane().setLayout(new BorderLayout()); // layout is set

		new data(); // save class is called

		this.add(new TitleBar(this), BorderLayout.NORTH); // title bar added

		this.add(new listArea(), BorderLayout.CENTER); // list area class added

		this.setUndecorated(true); // default minimize, close buttons removed

		this.pack(); // packed

		this.setLocationRelativeTo(null); // centered

		this.setFocusable(true); // frame focused

		this.setVisible(true); // frame set to visible

	}
}
package me.scotth0828.main.Window;

import java.awt.Color;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class catBar extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JLabel text;

	private String category = "default";

	public catBar() {

		this.setBackground(Color.ORANGE);

		text = new JLabel("DEFAULT TEXT");

		this.add(text);

	}

	public void setText(String text) {
		this.text.setText(text);
	}

	public String getText() {
		return text.getText();
	}

	public void setCategory(String cat) {
		this.category = cat;
		setText(cat);
	}

	public String getCategory() {
		return category;
	}
}
/**
 * @author Scott
 * @description
 * The class where everything having to do with text areas and buttons will be 
 */
package me.scotth0828.main.Window;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import me.scotth0828.main.Data.data;
import me.scotth0828.main.EditFrame.editFrame;
import me.scotth0828.main.Window.textareamanagement.TextAreaManager;
import me.scotth0828.main.Window.textareamanagement.textInfo;

public class listArea extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JPanel textAreasPanel; // variable initializations

	private JButton editButton, removeButton, nextPageButton;

	private catBar catb;

	private TextAreaManager textManager;

	public listArea() { // constructor

		this.setLayout(new BorderLayout()); // layout set

		this.setPreferredSize(new Dimension(Window.WINDOW_WIDTH, 720)); // size

		catb = new catBar();
		catb.setBackground(new Color(240, 240, 240));
		catb.setText(catb.getCategory().toUpperCase());

		this.add(catb, BorderLayout.NORTH);

		textAreasPanel = new JPanel(); // main panel

		BoxLayout bl = new BoxLayout(textAreasPanel, BoxLayout.Y_AXIS);
		textAreasPanel.setLayout(bl);

		JScrollPane scroll = new JScrollPane(textAreasPanel); // scroll panel

		scroll.getVerticalScrollBar().setUnitIncrement(16); // scroll bar increment increased

		scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

		this.add(scroll, BorderLayout.CENTER);

		textManager = new TextAreaManager(catb, this);

		JButton addButton = new JButton("Add"); // buttons created for side panel
		editButton = new JButton("Edit");
		removeButton = new JButton("Remove");
		nextPageButton = new JButton("Next Page");
		JButton newPageButton = new JButton("New Page");

		JPanel buttonPanel = new JPanel();

		buttonPanel.setBackground(Window.COLOR_SIDEBAR);
		buttonPanel.setLayout(new GridLayout(10, 1));

		buttonPanel.add(addButton); // buttons added
		buttonPanel.add(editButton);
		buttonPanel.add(removeButton);
		buttonPanel.add(nextPageButton);
		buttonPanel.add(newPageButton);

		JButton closeButton = new JButton("Close"); // close button created

		// Set preferred size for buttons
		addButton.setPreferredSize(new Dimension(80, 50)); // button sizes
		editButton.setPreferredSize(new Dimension(80, 50));
		closeButton.setPreferredSize(new Dimension(80, 100));

		editButton.setEnabled(false); // edit button disabled
		nextPageButton.setEnabled(false);
		removeButton.setEnabled(false);

		JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

		bottomPanel.setBackground(Window.COLOR_SIDEBAR); // background color set
		bottomPanel.add(closeButton);

		JPanel combinedPanel = new JPanel(new BorderLayout());
		combinedPanel.setBackground(Window.COLOR_SIDEBAR);

		combinedPanel.add(buttonPanel, BorderLayout.NORTH);
		combinedPanel.add(bottomPanel, BorderLayout.SOUTH);

		this.add(combinedPanel, BorderLayout.EAST);

		addButton.addActionListener(this); // action listeners added
		editButton.addActionListener(this);
		closeButton.addActionListener(this);
		removeButton.addActionListener(this);
		nextPageButton.addActionListener(this);
		newPageButton.addActionListener(this);

		setCategory("Useful Notes");

		autoSave(); // auto save function called
		
		
		if (textManager.getAllFields().keySet().size() > 1) { // enables next page button on startup if more than 1 category
			nextPageButton.setEnabled(true);
		}

	}

	public void setCategory(String text) {

		//

		catb.setCategory(text);
		textAreasPanel.removeAll();
		textAreasPanel.revalidate();
		textAreasPanel.repaint();

		textManager.setCurrentID(0);
		loadTextAreasFromSave();
		
		
	}

	/**
	 * @author Scott
	 * @description A thread is created where is a while loop is called every 5
	 *              seconds to save all the information in the application
	 */
	public void autoSave() {
		new Thread(() -> {
			while (true) {
				try {

					data.save(textManager.getAllFields()); // saves info

					Thread.sleep(5000); // sleeps for 5 seconds
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start(); // Thread started
	}

	/**
	 * @author Scott
	 * @description text is loaded into the textareas
	 */
	public void loadTextAreasFromSave() {
		textManager.setAllFields(data.load());

		if (textManager.getAllFields() == null || textManager.getAllFields().get(catb.getCategory()) == null
				|| textManager.getAllFields().get(catb.getCategory()).size() == 0)
			return;

		for (Map.Entry<String, ArrayList<textInfo>> entry : textManager.getAllFields().entrySet()) {

			try {
				if (entry.getKey().equals(catb.getCategory())) {
					for (textInfo i : entry.getValue()) {
						// textManager.add(i.getText(), i.getFirstClick(), true)
						textManager.add(i, true);
					}
					return;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Add")) { // called when add button is clicked
			textInfo tinfo = new textInfo(0, 4, 20);
			tinfo.setFirstClick(false);
			textManager.add(tinfo, false);

		} else if (e.getActionCommand().equals("Edit")) { // edit class is called
			for (textInfo ti : textManager.getField("")) {
				if (ti.getID() == textManager.getCurrentSelectedID()) {
					new editFrame(ti);
				}
			}

		} else if (e.getActionCommand().equals("Remove")) { // text area is removed and data is saved

			ArrayList<textInfo> array = textManager.getField("");

			for (textInfo ti : array) {

				if (ti.getID() == textManager.getCurrentSelectedID()) {
					textAreasPanel.remove(ti);
					textManager.getField("").remove(ti);
					revalidate();
					data.save(textManager.getAllFields());
					return;
				}
			}

		} else if (e.getActionCommand().equals("Close")) { // close button clicked, application closed
			data.save(textManager.getAllFields());
			System.exit(0);
		} else if (e.getActionCommand().equals("Next Page")) {

			if (textManager.getAllFields() == null || textManager.getAllFields().size() <= 1) {
				return;
			}

			ArrayList<String> keys = new ArrayList<>();

			for (String s : textManager.getAllFields().keySet()) {
				keys.add(s);
			}

			int index = keys.indexOf(catb.getCategory());

			String k;

			if (keys.size() - 1 <= index)
				k = keys.get(0);
			else
				k = keys.get(index + 1);

			setCategory(k);

		} else if (e.getActionCommand().equals("New Page")) {

			String userInput = JOptionPane.showInputDialog(null, "Enter a name for the category:");

			while (userInput == "")
				userInput = JOptionPane.showInputDialog(null, "Enter a name for the category:");

			if (userInput == null)
				return;

			if (textManager.getAllFields().get(userInput) == null)
				textManager.getAllFields().put(userInput, new ArrayList<textInfo>());

			setCategory(userInput);
			
			nextPageButton.setEnabled(true);
		}

	}

	public JPanel getTAPanel() {
		return textAreasPanel;
	}

	public JButton getEditButton() {
		return editButton;
	}
	
	public JButton getRemoveButton() {
		return removeButton;
	}

}
textareamanagement
>
© 2021 Copyright: scottsportfolio