package me.scotth0828.main.Window.textareamanagement;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
public class MouseDrag {
textInfo box, last = null;
boolean isPlacingAbove = false;
double boxY;
int boxIndex;
int timeToDrag = 5;
boolean running = false;
boolean canChange = false;
public MouseDrag() {
}
public void setTextBox(textInfo box) {
this.box = box;
}
public void setRunning(boolean running) {
this.running = running;
}
public boolean getRunning() {
return running;
}
public void setCursor(ArrayList<textInfo> field) {
if (getRunning())
return;
new Thread(() -> {
setRunning(true);
if (getRunning()) {
long lastTime = System.nanoTime();
long waitTime = (long) timeToDrag * 1000000000L;
long elapsedTime = 0;
while ((System.nanoTime() < lastTime + waitTime) && getRunning()) {
elapsedTime = (lastTime + waitTime) - System.nanoTime();
//System.out.println("waiting... " + (elapsedTime / 1000000000));
}
if (getRunning())
setHandCursor(field);
if (System.nanoTime() >= lastTime + waitTime) {
canChange = true;
}
}
}).start(); // Thread started
}
public void removeCursor(ArrayList<textInfo> field) {
for (textInfo t : field) {
t.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
}
setRunning(false);
}
public void setHandCursor(ArrayList<textInfo> field) {
for (textInfo t : field) {
t.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
}
public ArrayList<textInfo> update(ArrayList<textInfo> field, int mouseY) {
if (!getRunning())
return null;
if (field.size() <= 1)
return null; // if field is less than or equal to 1, do nothing
boxY = box.getLocationOnScreen().getY();
boxIndex = field.indexOf(box); // index of current textarea and of the one
removeCursor(field);
if (!canChange)
return null;
if (mouseY < boxY || mouseY > boxY + box.getHeight()) { // checks to see if mouse is hovering above or below
// currently dragged text box
for (int i = 0; i < field.size(); i++) { // gets the text box that it is hovering below
if (i != boxIndex) {
textInfo t = field.get(i);
if (mouseY > t.getLocationOnScreen().getY() + (t.getHeight() / 2)) {
last = field.get(i);
}
}
}
if (mouseY < field.get(0).getLocationOnScreen().getY() + (field.get(0).getHeight() / 2)) { // checks whether
// if user wants
// text box
// placed above
// everything
isPlacingAbove = true;
}
if (last != null || isPlacingAbove != false) { // if the position of where user wants the
// textbox to be dropped is not
// null, continue
// above
// where user wants to drop
int lastIndex = (isPlacingAbove) ? 0 : field.indexOf(last);
if (isPlacingAbove) { // places textarea at first index
field.remove(box);
field.add(0, box);
isPlacingAbove = false;
} else if (lastIndex < boxIndex) { // if user is placing above the current textbox
field.remove(box);
field.add(lastIndex + 1, box);
} else if (lastIndex > boxIndex) { // if below....
field.remove(box);
field.add(lastIndex, box);
}
last = null;
isPlacingAbove = false;
canChange = false;
return field;
}
}
return null;
}
}
package me.scotth0828.main.Window.textareamanagement;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import me.scotth0828.main.Window.Window;
import me.scotth0828.main.Window.catBar;
import me.scotth0828.main.Window.listArea;
public class TextAreaManager {
private Map<String, ArrayList<textInfo>> fields;
catBar categoryBar;
listArea lArea;
textInfo currentlyDragged = null; // variables initialized for dragging
boolean isCursorSet = false;
private int currentID = 0;
public int currentSelectedID = -1;
MouseDrag mdrag;
public TextAreaManager(catBar categoryBar, listArea lArea) {
this.categoryBar = categoryBar;
this.lArea = lArea;
fields = new HashMap<>();
mdrag = new MouseDrag();
}
/**
* @author Scott
* @description text area is added into jpanel
* @param text default text for textarea is set
* @param isFirstClick checks to see whether the user has already clicked the
* textarea in the past
* @param isFromSave checks to see whether this textarea has already been
* saved in a previous session
*/
// public void addTextArea(String text, boolean isFirstClick, boolean
// isFromSave) {
public void add(textInfo textArea, boolean isFromSave) {
incrementID(textArea, isFromSave);
setDefaults(textArea);
textArea.addMouseListener(new MouseListener() { // mouse listener
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
if (e.getButton() == MouseEvent.BUTTON1) { // when mouse is pressed down, hand cursor
// shows up to show user dragging is
// occuring
mdrag.setCursor(getField(""));
mdrag.setTextBox(textArea);
}
}
@Override
public void mouseReleased(MouseEvent e) {
ArrayList<textInfo> tempField = mdrag.update(getField(""), e.getYOnScreen()); // field that will be set
// if drag is true
if (tempField != null) {
lArea.getTAPanel().removeAll(); // removes current textareas and resets them with the new
// arrangement
for (textInfo i : tempField) {
lArea.getTAPanel().add(i);
}
lArea.getTAPanel().revalidate();
} else {
mdrag.setRunning(false);
}
if (e.getButton() == MouseEvent.BUTTON3) { // enables/disables edit button
lArea.getEditButton().setEnabled(false);
lArea.getRemoveButton().setEnabled(false);
} else if (e.getButton() == MouseEvent.BUTTON1 && textArea.isEnabled()) {
currentSelectedID = textArea.getID();
lArea.getEditButton().setEnabled(true);
lArea.getRemoveButton().setEnabled(true);
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
if (!isFromSave)
getField("").add(textArea); // adds the text area to the fields array and to
// the jpanel
lArea.getTAPanel().add(textArea);
lArea.getTAPanel().revalidate(); // repaints the jpanel in real time
}
public void incrementID(textInfo textArea, boolean isFromSave) {
if (isFromSave) { // if from save, continue. sets the text and firstclick boolean
textArea.setID(currentID);
currentID++;
if (!textArea.getFirstClick()) // else sets default text and sets color to light gray
textArea.setTextColor(Color.LIGHT_GRAY);
else
textArea.setTextColor(textArea.getDefaultTextColor());
} else {
textArea.setID(currentID);
currentID++;
}
}
public void setDefaults(textInfo textArea) {
textArea.setBackground(Window.COLOR_LISTAREA);
textArea.setDisabledTextColor(Color.BLACK); // sets up default values for text area
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setBorder(BorderFactory.createLineBorder(Color.GRAY));
textArea.setFont(new Font("Arial", Font.PLAIN, 18)); // font, "Arial"
}
public ArrayList<textInfo> getField(String category) { /// need to work on fieldssssssssssssssss, not being
/// removed//// etc/ ///// categorization setting
/*
* if (fields.size() == 0) { // NOTHING FOUND fields.put(catb.getCategory(), new
* ArrayList<textInfo>()); }
*/
if (fields == null)
fields = new HashMap<>();
if (fields.get(categoryBar.getCategory()) == null) {
fields.put(categoryBar.getCategory(), new ArrayList<textInfo>());
}
if (category.equals(""))
return fields.get(categoryBar.getCategory());
return fields.get(categoryBar.getCategory());
}
public Map<String, ArrayList<textInfo>> getAllFields() {
return fields;
}
public void setAllFields(Map<String, ArrayList<textInfo>> fields) {
this.fields = fields;
}
public int getCurrentSelectedID() {
return currentSelectedID;
}
public void setCurrentID(int ID) {
currentID = ID;
}
}
/**
* @author Scott
* @description
* Class that extends JTextarea to allow more functions than given by text area
*/
package me.scotth0828.main.Window.textareamanagement;
import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JTextArea;
import me.scotth0828.main.Window.Window;
public class textInfo extends JTextArea implements MouseListener, FocusListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private int ID = -1; // ID and variables initialized
// private LocalDateTime creationTime;
// private LocalDateTime dueDate;
private Color bg_Color = Window.COLOR_LISTAREA;
private Color bg_Color_done = Color.GREEN;
private final String defaultText = "Type something here . . .";
private final Color defaultTextColor = Color.BLACK;
private boolean firstClick = false;
private boolean isSelected = false;
public textInfo() {
// creationTime = LocalDateTime.now();
this.setForeground(Color.LIGHT_GRAY);
this.addMouseListener(this);
this.addFocusListener(this);
}
public textInfo(int ID, int row, int col) { // constructor. ID, row, col
this.setID(ID);
this.setRows(row);
this.setColumns(col);
// creationTime = LocalDateTime.now();
this.setText(getDefaultText()); // default text area values set
this.setForeground(Color.LIGHT_GRAY);
this.addMouseListener(this); // listeners added
this.addFocusListener(this);
}
public void setSelected(boolean val) {
isSelected = val;
}
public boolean getSelected() {
return isSelected;
}
public void setFirstClick(boolean fc) { // getters and setters
this.firstClick = fc;
}
public boolean getFirstClick() {
return firstClick;
}
public void setID(int ID) {
this.ID = ID;
}
public int getID() {
return ID;
}
public void setColor(Color color) {
bg_Color = color;
this.setBackground(bg_Color);
}
public void setTextColor(Color color) {
this.setForeground(color);
}
public Color getColor() {
return bg_Color;
}
public String getDefaultText() {
return defaultText;
}
public Color getDefaultTextColor() {
return defaultTextColor;
}
@Override
public void mouseClicked(MouseEvent e) { // mouse listeners
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) { // mouse release
if (e.getButton() == MouseEvent.BUTTON3 && firstClick) {
if (getText().trim().equals("")) return;
if (isEnabled()) { // sets text area to green on right click aka completion
setEnabled(false);
setBackground(bg_Color_done);
} else {
setEnabled(true);
setBackground(bg_Color);
}
} else if (e.getButton() == MouseEvent.BUTTON1 && isEnabled() && !firstClick) { // removes default text when
// clicked for first time
setText("");
setForeground(getDefaultTextColor());
firstClick = true;
}
}
@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 focusGained(FocusEvent e) { // focus listeners
setSelected(true);
// System.out.println("FOCUSED : " + getSelected() + " | ID: " +getID());
}
@Override
public void focusLost(FocusEvent e) {
setSelected(false);
}
}