package gui;
import gui.actions.ActionManager;
import gui.commands.Command;
import gui.commands.DeleteCommand;
import gui.commands.DrawCommand;
import gui.commands.MoveByOne;
import gui.commands.MoveCommand;
import gui.commands.ResizeByOne;
import gui.commands.ResizeCommand;
import gui.listeners.KeyProcessor;
import gui.states.StateType;
import gui.states.States;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.KeyboardFocusManager;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import preseci_krugova.Crtanje;
import preseci_krugova.Krug;
import preseci_krugova.Tacka;
import preseci_krugova.TraziPreseke;
@SuppressWarnings("serial")
public class MainWindow extends JFrame {
private static MainWindow instance;
private String title = "Circle Intersection";
private DrawCanvas drawPanel;
private ToolBar tools;
private States states;
private StatusBar statusBar;
private TraziPreseke traziPreseke;
private Menu menu;
private Stack<Command> doneCommands;
private Stack<Command> undoneCommands;
private Krug selected;
private int oldSelectedR;
private Tacka oldSelectedCenter;
private boolean started = false;
private boolean menusUp = true;
private File currentFile;
private boolean fileChanged = false;
private MainWindow() {
traziPreseke = new TraziPreseke();
doneCommands = new Stack<Command>();
undoneCommands = new Stack<Command>();
initGUI();
addWindowListener(new WindowListener() {
@Override
public void windowClosing(WindowEvent e) {
quitNice();
}
public void windowOpened(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
});
}
private void initListen() {
drawPanel.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
states.getCurrent().mouseMoved(e);
if (menusUp) {
drawImage();
}
}
public void mouseDragged(MouseEvent e) {}
});
drawPanel.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
states.getCurrent().mouseClicked(e);
}
public void mouseReleased(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
});
addWindowFocusListener(new WindowFocusListener() {
@Override
public void windowGainedFocus(WindowEvent e) {
drawImage();
}
public void windowLostFocus(WindowEvent e) {}
});
KeyProcessor mkl = new KeyProcessor();
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(mkl);
}
private void initGUI() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
drawPanel = new DrawCanvas(traziPreseke);
tools = new ToolBar();
states = new States();
statusBar = new StatusBar();
menu = new Menu();
setJMenuBar(menu);
drawPanel.setPreferredSize(new Dimension(Crtanje.WIDTH, Crtanje.HEIGHT));
drawPanel.setIgnoreRepaint(true);
setLayout(new BorderLayout());
add(drawPanel, BorderLayout.CENTER);
add(tools, BorderLayout.NORTH);
add(statusBar, BorderLayout.SOUTH);
pack();
setTitle(title);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
}
public void drawImage() {
if (started) {
// Graphics g = drawPanel.getGraphics();
//
// g.drawImage(traziPreseke.crtaj(), 0, 0, null);
//
// g.dispose();
drawPanel.repaint();
}
}
public void setMenusUp(boolean menusUp) {
this.menusUp = menusUp;
}
public void newFile() {
ActionManager.getInstance().saveEnabled(true);
if (!started) {
started = true;
drawPanel.setOk(true);
initListen();
}
ActionManager.getInstance().selectEnabled(true);
ActionManager.getInstance().drawEnabled(true);
ActionManager.getInstance().algorithmEnabled(false);
ActionManager.getInstance().modifiersEnabled(false);
traziPreseke.brisiKrugove();
setTitle(title + " - *(Untitled)");
drawImage();
}
public void openFile() {
if (!started) {
started = true;
drawPanel.setOk(true);
initListen();
}
JFileChooser jfc = new JFileChooser();
jfc.setCurrentDirectory(new File("./examples"));
jfc.setFileFilter(new KrugoviFilter());
if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
ActionManager.getInstance().selectEnabled(true);
ActionManager.getInstance().drawEnabled(true);
ActionManager.getInstance().algorithmEnabled(false);
ActionManager.getInstance().modifiersEnabled(false);
traziPreseke.brisiKrugove();
currentFile = jfc.getSelectedFile();
read(currentFile);
ActionManager.getInstance().startEnabled(true);
setTitle(title + " - " + currentFile.getName());
}
drawImage();
}
public void quitNice() {
if (fileChanged) {
int result = JOptionPane.showConfirmDialog(this,
"You have unsaved work. Do you wish to save?",
"Hang on!",
JOptionPane.INFORMATION_MESSAGE);
if (result == JOptionPane.CANCEL_OPTION) {
//bas nas briga
return;
} else if (result == JOptionPane.YES_OPTION){
//ajd bas, ce snimimo
save();
}
//i izadjemo ako je kliknuo da nece da snimi
System.exit(0);
} else {
System.exit(0);
}
}
public void unselect() {
if (selected != null) {
selected.setSelektovan(false);
}
selected = null;
ActionManager.getInstance().modifiersEnabled(false);
}
public void select(int x, int y) {
Krug selektovan = traziPreseke.selektuj(x, y);
unselect();
if (selektovan != null) {
selected = selektovan;
selektovan.setSelektovan(true);
ActionManager.getInstance().modifiersEnabled(true);
}
drawImage();
}
public Crtanje getCrtanje() {
return traziPreseke.getCrtanje();
}
public Krug getSelected() {
return selected;
}
public static MainWindow getInstance() {
if (instance == null) {
instance = new MainWindow();
}
return instance;
}
public States getStates() {
return states;
}
public StatusBar getStatusBar() {
return statusBar;
}
public static void main(String[] args) {
MainWindow.getInstance();
}
public void addCircle(Krug nov) {
traziPreseke.dodajKrug(nov);
ActionManager.getInstance().startEnabled(true);
fileChanged();
drawImage();
}
public void delete() {
traziPreseke.brisiKrug(selected);
states.goToState(StateType.Select);
unselect();
fileChanged();
drawImage();
}
public void delete(Krug toDelete) {
traziPreseke.brisiKrug(toDelete);
states.goToState(StateType.Select);
unselect();
fileChanged();
drawImage();
}
public void startCount() {
statusBar.setState("Counting");
unselect();
states.goToState(StateType.Null);
traziPreseke.traziPreseke();
ActionManager.getInstance().nextStepEnabled(true);
ActionManager.getInstance().toEndEnabled(true);
ActionManager.getInstance().drawEnabled(false);
ActionManager.getInstance().selectEnabled(false);
ActionManager.getInstance().startEnabled(false);
drawImage();
}
public void nextStep() {
if (!traziPreseke.naredniKorak()) {
states.goToState(StateType.Select);
ActionManager.getInstance().drawEnabled(true);
ActionManager.getInstance().selectEnabled(true);
ActionManager.getInstance().startEnabled(true);
ActionManager.getInstance().nextStepEnabled(false);
ActionManager.getInstance().toEndEnabled(false);
traziPreseke.resetujStatusKrugova();
}
statusBar.setCount(traziPreseke.getBrPreseka());
drawImage();
}
public void toEnd() {
while (traziPreseke.naredniKorak()) ;
states.goToState(StateType.Select);
statusBar.setCount(traziPreseke.getBrPreseka());
ActionManager.getInstance().drawEnabled(true);
ActionManager.getInstance().selectEnabled(true);
ActionManager.getInstance().startEnabled(true);
ActionManager.getInstance().nextStepEnabled(false);
ActionManager.getInstance().toEndEnabled(false);
traziPreseke.resetujStatusKrugova();
drawImage();
}
public void save() {
if (currentFile == null) {
saveAs();
} else {
setTitle(title + " - " + currentFile.getName());
write(currentFile);
fileChanged=false;
}
}
public void fileChanged() {
fileChanged=true;
if (currentFile != null) {
setTitle(title + " - *" + currentFile.getName());
}
}
public void saveAs() {
JFileChooser jfc = new JFileChooser();
jfc.setFileFilter(new KrugoviFilter());
jfc.setCurrentDirectory(new File("./examples"));
if (jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
if (!jfc.getSelectedFile().getName().endsWith(".krugovi")) {
currentFile = new File(jfc.getSelectedFile().getAbsolutePath() + ".krugovi");
} else {
currentFile = jfc.getSelectedFile();
}
setTitle(title + " - " + currentFile.getName());
write(currentFile);
fileChanged=false;
}
}
private void write(File f) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(currentFile));
writer.write(traziPreseke.getSviKrugovi().size() + "\n");
for (Krug krug : traziPreseke.getSviKrugovi()) {
writer.write(krug.getCentar().getX() + " " + krug.getCentar().getY() + " " +
krug.getPoluprecnik() + "\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void read(File f) {
try {
Scanner sc = new Scanner(f);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int x, y;
int r;
x = sc.nextInt();
y = sc.nextInt();
r = sc.nextInt();
traziPreseke.dodajKrug(new Krug(new Tacka(x,y), r));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void executeMoveSelectionByOne(int direction) {
Command comm = new MoveByOne(selected, direction);
executeCommand(comm);
}
public void executeResizeSelectionByOne(boolean up) {
Command comm = new ResizeByOne(selected, up);
executeCommand(comm);
}
public void executeDelete() {
Command comm = new DeleteCommand();
executeCommand(comm);
}
public void executeDraw(Krug toDraw) {
Command comm = new DrawCommand(toDraw);
executeCommand(comm);
}
public void startResize() {
oldSelectedR = getSelected().getPoluprecnik();
}
public void executeResize() {
Command comm = new ResizeCommand(oldSelectedR, selected.getPoluprecnik(), selected);
executeCommand(comm);
}
public void startMove() {
oldSelectedCenter = getSelected().getCentar();
}
public void executeMove() {
Command comm = new MoveCommand(oldSelectedCenter, selected.getCentar(), selected);
executeCommand(comm);
}
private void executeCommand(Command comm) {
comm.execute();
doneCommands.push(comm);
undoneCommands.clear();
ActionManager.getInstance().redoEnabled(false);
ActionManager.getInstance().undoEnabled(true);
}
public void undo() {
if (doneCommands.size() > 0) {
Command comm = doneCommands.pop();
comm.undo();
undoneCommands.push(comm);
}
ActionManager.getInstance().redoEnabled(true);
if (doneCommands.size() == 0) {
ActionManager.getInstance().undoEnabled(false);
}
drawImage();
}
public void redo() {
if (undoneCommands.size() > 0) {
Command comm = undoneCommands.pop();
comm.execute();
doneCommands.push(comm);
}
if (undoneCommands.size() == 0) {
ActionManager.getInstance().redoEnabled(false);
}
drawImage();
}
}