import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.event.*;
import java.time.LocalDateTime;
import java.time.ZoneId;
//타이머 thread
class Time extends Thread {
LocalDateTime clock;
ZoneId map = ZoneId.of("Asia/Seoul");
JLabel shower;
public Time(JLabel lbl) {
clock = LocalDateTime.now(map);
this.shower = lbl;
}
@Override
public void run() {
while(true) {
clock = LocalDateTime.now(map);
shower.setText(
clock.getYear() + "/ " +(Integer.toString(clock.getMonthValue()).length() == 1 ? "0" : "") + clock.getMonthValue() + "/ " + (Integer.toString(clock.getDayOfYear()).length() == 1 ? "0" : "") + clock.getDayOfYear()
+ " |:| " + (Integer.toString(clock.getHour()).length() == 1 ? "0" : "") + clock.getHour() + ": " + (Integer.toString(clock.getMinute()).length() == 1 ? "0" : "") + clock.getMinute() + ": " + (Integer.toString(clock.getSecond()).length() == 1 ? "0" : "") + clock.getSecond()
+" ");
try {
Thread.sleep(125);
}
catch (InterruptedException e) {
return;
}
}
}
}
class SavingMachine extends Thread {
Vector<Tab> tab = new Vector<Tab>();
boolean running = true;
public SavingMachine(Vector<Tab> tab) {
this.tab = tab;
}
public void Saving() {
for(int i = 0; i < tab.size(); i++) {
try (FileOutputStream fos = new FileOutputStream(tab.get(i).writingRoute, false)) {
} catch (IOException e) {
e.printStackTrace();
}
try {
tab.get(i).FW = new FileWriter(tab.get(i).writing);
tab.get(i).PW = new PrintWriter(tab.get(i).FW);
tab.get(i).PW.print(Encryption(tab.get(i).name) + "\n");
if(tab.get(i).BGM == null) {
tab.get(i).PW.print(Encryption("null") + "\n");
}
else {
tab.get(i).PW.print(Encryption(tab.get(i).BGM) + "\n");
}
tab.get(i).PW.print(Encryption(tab.get(i).BGI.getImage().toString()) + "\n");
tab.get(i).PW.close();
} catch(IOException e) {
}
}
}
@Override
public void run() {
while (running == true) {
Saving();
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
}
}
}
public String Encryption(String Sentence) {
StringBuffer subString = new StringBuffer();
Vector<String> subVec = new Vector<String>();
for(int ord = 0; ord < Sentence.length(); ord++) {
subVec.add(Integer.toString(ord).length() + Integer.toString(ord) + Integer.toString(Integer.toString((int)Sentence.toCharArray()[ord]).length()) + Integer.toString((int)Sentence.toCharArray()[ord]));
}
while(true) {
String s = subVec.get((int)(Math.random()*subVec.size()));
subString.append(s);
subVec.remove(s);
if(subVec.size() <= 0) {
break;
}
}
return subString.toString();
}
}
class Tab extends JPanel {
int number;
String name, BGM,folderRoute,writingRoute;
ImageIcon BGI;
File folder,writing;
FileWriter FW;
PrintWriter PW;
public Tab(int nb) {
this.number = nb;
name = "New Tab";
folderRoute = "C:\\Users\\user\\Downloads\\HS Project\\Tab " + nb;
folder = new File(folderRoute);
writingRoute = "C:\\Users\\user\\Downloads\\HS Project\\Tab " + nb + "\\document.txt";
writing = new File(writingRoute);
if(!folder.exists() ) {
try {
folder.mkdir();
writing.createNewFile();
} catch(Exception e) {
e.getStackTrace();
}
}
BGI = new ImageIcon("C:\\Users\\user\\Downloads\\HS Project\\Default.png");
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(BGI.getImage(), 0, 0, getWidth(), getHeight(), this);
}
}
class SettingCs extends JFrame {
JPanel pnl;
JButton Del = new JButton("Delete");
public SettingCs(Vector<Tab> tab, int now) {
this.setTitle("Tab Setting");
this.setSize(500,700);
this.setVisible(true);
this.setResizable(false);
this.setLayout(null);
this.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) { }
@Override
public void windowIconified(WindowEvent e) { }
@Override
public void windowDeiconified(WindowEvent e) { }
@Override
public void windowDeactivated(WindowEvent e) { }
@Override
public void windowClosing(WindowEvent e) {
Main x =new Main();
change(x);
}
@Override
public void windowClosed(WindowEvent e) { }
@Override
public void windowActivated(WindowEvent e) { }
});
pnl = tab.get(now);
pnl.setSize(250,350);
LineBorder LB = new LineBorder(Color.black,1);
pnl.setBorder(LB);
System.out.println("k");
System.out.println(pnl == null);
System.out.println(this == null);
//this.add(pnl);
Del.setForeground(Color.black);
Del.setBackground(Color.red);
this.add(Del);
Del.setSize(100,40);
Del.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//System.out.println(tab.size());
File df = new File("C:\\Users\\user\\Downloads\\HS Project\\Tab " + (tab.size()-1));
System.out.println(df);
if(df.exists()) {
System.out.println("k");
File[] df_list = df.listFiles();
System.out.println(df_list.length);
for(int i = 0; i < df_list.length; i++) {
df_list[i].delete();
df_list[i].delete();
}
df.delete();
System.exit(0);
System.out.println("is exist");
}
else {
System.out.println("don't exist");
}
tab.remove(now);
System.out.println(tab.size());
for(int i = now+1; i < tab.size(); i++) {
//System.out.println(now);
tab.get(i).number -= 1;
tab.get(i).folderRoute = "C:\\Users\\user\\Downloads\\HS Project\\Tab " + tab.get(i).number;
tab.get(i).folder = new File(tab.get(i).folderRoute);
tab.get(i).writingRoute = "C:\\Users\\user\\Downloads\\HS Project\\Tab " + tab.get(i).number + "\\document.txt";
tab.get(i).writing = new File(tab.get(i).writingRoute);
System.out.println(i);
System.out.println(tab.get(i).number);
System.out.println(tab.get(i).folderRoute);
System.out.println("1234");
}
Main x =new Main();
change(x);
//System.out.println(now);
//System.out.println(tab.size());
}
});
}
public void change(Main x) {
System.out.println(this.getLocation());
x.setLocation(this.getLocation());
System.out.println(x.getLocation());
this.dispose();
}
}
//Main 화면
public class Main extends JFrame {
Container cnt = getContentPane();
TabPane TP = new TabPane();
Bar BR = new Bar();
SavingMachine ts = new SavingMachine(TP.tab);
public Main() {
setTitle("Main");
setSize(500,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
cnt.add(BR, BorderLayout.NORTH);
cnt.add(TP, BorderLayout.CENTER);
ts.running = true;
ts.start();
}
public void change(SettingCs x) {
x.setLocation(this.getLocation());
}
class Bar extends JMenuBar {
JMenu [] Menu = new JMenu[4];
HashMap<JMenu, Vector<JMenuItem>> Item = new HashMap<JMenu, Vector<JMenuItem>>();
JLabel timeshower = new JLabel();
Time th = new Time(timeshower);
public Bar() {
this.add(timeshower);
for(int i = 0; i < Menu.length; i++) {
Menu[i] = new JMenu();
Item.put(Menu[i], new Vector<JMenuItem>());
this.add(Menu[i]);
}
Menu[0].setText("Setting");
JMISetting();
th.start();
}
public void JMISetting() {
Item.get(Menu[0]).add(new JMenuItem("Sound and Video Setting"));
Menu[0].add(Item.get(Menu[0]).get(0));
}
}
class TabPane extends JTabbedPane {
JButton AddTab = new JButton();
Vector<Tab> tab = new Vector<Tab>();
public TabPane() {
load();
AddTab.setOpaque(false);
AddTab.setBorder(null);
AddTab.setContentAreaFilled(false);
AddTab.setFocusPainted(false);
this.add("+",AddTab);
if(tab.size() == 0) {
System.out.println(tab.size());
change();
}
this.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
change();
}
});
this.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) { }
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getButton() == MouseEvent.BUTTON2) {
System.out.println(TP.getSelectedIndex());
SettingCs x = new SettingCs(TP.tab,TP.getSelectedIndex());
Main.this.change(x);
ts.running = false;
dispose();
}
}
@Override
public void mouseExited(MouseEvent e) { }
@Override
public void mouseEntered(MouseEvent e) { }
@Override
public void mouseClicked(MouseEvent e) { }
});
}
public void change() {
if(this.getSelectedComponent().equals(AddTab)) {
tab.add(new Tab(tab.size()));
this.remove(AddTab);
this.addTab("New Tab", tab.get(tab.size()-1));
this.add("+",AddTab);
}
}
public void load() {
for(int i = 0 ;; i++) {
if(new File("C:\\Users\\user\\Downloads\\HS Project\\Tab " + i).exists()) {
tab.add(new Tab(i));
//tab.get(i).name =
try {
BufferedReader reader = new BufferedReader(new FileReader(tab.get(i).writingRoute));
String str;
str = reader.readLine();
tab.get(i).name = Decryption(str);
str = reader.readLine();
tab.get(i).BGM = (Decryption(str).equals("null") ? Decryption(str) : null);
str = reader.readLine();
tab.get(i).BGI = new ImageIcon(Decryption(str));
this.add(tab.get(i).name,tab.get(i));
} catch(IOException e) {
}
}
else {
break;
}
}
}
public String Decryption(String Sentence) {
StringBuffer subString = new StringBuffer(Sentence);
HashMap<String, String> subHash = new HashMap<String,String>();
int pos = 0;
while(true) {
int g1 = Character.getNumericValue(subString.toString().toCharArray()[pos]);
String key = subString.substring(pos+1, pos+g1+1);
pos += g1 + 1;
int g2 = Character.getNumericValue(subString.toString().toCharArray()[pos]);
String value = String.valueOf((char)Integer.parseInt(subString.substring(pos+1, pos + g2 + 1)));
pos += g2 + 1;
subHash.put(key, value);
if(pos >= subString.toString().length()) {
break;
}
}
subString = new StringBuffer();
for(int i = 0; i < subHash.size(); i++) {
subString.append(subHash.get(Integer.toString(i)));
}
return subString.toString();
}
}
public static void main(String[] args) {
new Main();
}
}