Commit b2982269 authored by nargessalehi98's avatar nargessalehi98

without comment

parents
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
File added
Manifest-Version: 1.0
Main-Class: ceit.Main
Manifest-Version: 1.0
Main-Class: ceit.Main
package ceit;
import ceit.gui.CFrame;
import javax.swing.*;
/**
* Run the program
*/
public class Main {
public static void main(String[] args) {
//creat a CFrame
CFrame frame = new CFrame("iNote");
frame.setVisible(true);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
package ceit.gui;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
/**
* Provide program window
*/
public class CFrame extends JFrame implements ActionListener {
//creat a main panel to keep list and text area
private CMainPanel mainPanel;
//creat a menu Items
private JMenuItem newItem;
private JMenuItem saveItem;
private JMenuItem exitItem;
/**
* creat a new CFrame with given name
* @param title name
*/
public CFrame(String title) {
super(title);
initMenuBar(); //create menuBar
initMainPanel(); //create main panel
}
/**
* creat menuBar
*/
private void initMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu jmenu = new JMenu("File");
newItem = new JMenuItem("New");
saveItem = new JMenuItem("Save");
exitItem = new JMenuItem("Exit");
newItem.addActionListener(this);
saveItem.addActionListener(this);
exitItem.addActionListener(this);
jmenu.add(newItem);
jmenu.add(saveItem);
jmenu.add(exitItem);
menuBar.add(jmenu);
setJMenuBar(menuBar);
}
/**
* creat main panel
*/
private void initMainPanel() {
//creat a new main panel
mainPanel = new CMainPanel();
//add to CFrame
add(mainPanel);
}
@Override
/**
* set each action for each item in menu bar
*/
public void actionPerformed(ActionEvent e) {
//new item add a new tab
if (e.getSource() == newItem) {
mainPanel.addNewTab();
} else if (e.getSource() == saveItem) { //save selected item
try {
mainPanel.saveNote();
} catch (IOException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == exitItem) { //exit program and save all tabs
//TODO: Phase1: check all tabs saved ... :done
JTabbedPane jTabbedPane= (JTabbedPane) mainPanel.getComponent(1);
for(int num=0 ;num<jTabbedPane.getTabCount();num++){
try {
mainPanel.saveNote();
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.exit(0);
}
else {
System.out.println("Nothing detected...");
}
}
}
package ceit.gui;
import ceit.utils.FileUtils;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
/**
* Provide panel with list and text area
*/
public class CMainPanel extends JPanel {
//creat a tabbedPane to open and write notes
private JTabbedPane tabbedPane;
//creat a j list to keep notes
private JList<File> directoryList;
/**
* creat new Main Panel
*/
public CMainPanel() {
setLayout(new BorderLayout());
initDirectoryList(); // add JList to main Panel
initTabbedPane(); // add TabbedPane to main panel
addNewTab(); // open new empty tab when user open the application
}
private void initTabbedPane() {
tabbedPane = new JTabbedPane();
add(tabbedPane, BorderLayout.CENTER);
}
private void initDirectoryList() {
File[] files = FileUtils.getFilesInDirectory();
directoryList = new JList<>(files);
directoryList.setBackground(new Color(211, 211, 211));
directoryList.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
directoryList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
directoryList.setVisibleRowCount(-1);
directoryList.setMinimumSize(new Dimension(130, 100));
directoryList.setMaximumSize(new Dimension(130, 100));
directoryList.setFixedCellWidth(130);
directoryList.setCellRenderer(new MyCellRenderer());
directoryList.addMouseListener(new MyMouseAdapter());
add(new JScrollPane(directoryList), BorderLayout.WEST);
}
public void addNewTab() {
JTextArea textPanel = createTextPanel();
textPanel.setText("Write Something here...");
tabbedPane.addTab("Tab " + (tabbedPane.getTabCount() + 1), textPanel);
}
public void openExistingNote(String content) {
JTextArea existPanel = createTextPanel();
existPanel.setText(content);
int tabIndex = tabbedPane.getTabCount() + 1;
tabbedPane.addTab("Tab " + tabIndex, existPanel);
tabbedPane.setSelectedIndex(tabIndex - 1);
}
public void saveNote() throws IOException {
JTextArea textPanel = (JTextArea) tabbedPane.getSelectedComponent();
String note = textPanel.getText();
if (!note.isEmpty()) {
FileUtils.fileWriter(note);
}
updateListGUI();
}
private JTextArea createTextPanel() {
JTextArea textPanel = new JTextArea();
textPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
return textPanel;
}
private void updateListGUI() {
File[] newFiles = FileUtils.getFilesInDirectory();
directoryList.setListData(newFiles);
}
private class MyMouseAdapter extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent eve) {
// Double-click detected
if (eve.getClickCount() == 2) {
int index = directoryList.locationToIndex(eve.getPoint());
System.out.println("Item " + index + " is clicked...");
//TODO: Phase1: Click on file is handled... Just load content into JTextArea :done
File curr[] = FileUtils.getFilesInDirectory();
String content = null;
try {
content = FileUtils.fileReader2(curr[index]);
} catch (IOException e) {
e.printStackTrace();
}
openExistingNote(content);
}
}
}
private class MyCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object object, int index, boolean isSelected, boolean cellHasFocus) {
if (object instanceof File) {
File file = (File) object;
setText(file.getName());
setIcon(FileSystemView.getFileSystemView().getSystemIcon(file));
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setEnabled(list.isEnabled());
}
return this;
}
}
}
package ceit.model;
public class Note {
private String title;
private String content;
private String date;
public Note(String title, String content, String date) {
this.title = title;
this.content = content;
this.date = date;
}
@Override
public String toString() {
return "Note{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
", date='" + date + '\'' +
'}';
}
}
package ceit.utils;
import java.io.*;
/**
* Mange files - Read and Write
*/
public class FileUtils {
//I'm a mac user - if it doesn't work chang NOTES_PATH to ./notes
private static final String NOTES_PATH = "./notes/";
//It's a static initializer. It's executed when the class is loaded.
//It's similar to constructor
static {
boolean isSuccessful = new File(NOTES_PATH).mkdirs();
System.out.println("Creating " + NOTES_PATH + " directory is successful: " + isSuccessful);
System.out.println(new File(NOTES_PATH).exists());
}
/**
* get list of files
* @return list of files
*/
public static File[] getFilesInDirectory() {
return new File(NOTES_PATH).listFiles();
}
/**
* Read files with BufferedReader
* @param file given file
* @return content of file
* @throws IOException
*/
public static String fileReader(File file) throws IOException {
//TODO: Phase1: read content from file:done
StringBuilder content = new StringBuilder();
FileReader reader = new FileReader(file);
BufferedReader in = new BufferedReader(reader);
String line;
while ((line = in.readLine()) != null) {
content.append(line);
content.append("\n");
}
reader.close();
in.close();
return content.toString();
}
/**
* Write given string to file with Buffered Reader
* @param content given string
* @throws IOException
*/
public static void fileWriter(String content) throws IOException {
//TODO: write content on file:done
String fileName = getProperFileName(content);
FileWriter writer = new FileWriter(NOTES_PATH + fileName + ".txt/");
BufferedWriter out = new BufferedWriter(writer);
out.write(content);
out.flush();
out.close();
}
/**
* Read files with FileInputStream
* @param file given file
* @return content of file
* @throws IOException
*/
//TODO: Phase1: define method here for reading file with InputStream
public static String fileReader2(File file) throws IOException {
//TODO: Phase1: read content from file:done
StringBuilder line = new StringBuilder();
try (InputStream in = new FileInputStream(file)) {
int content;
while ((content = in.read()) != -1) {
line.append((char) content);
}
} catch (Exception e) {
e.printStackTrace();
}
return line.toString();
}
/**
* Write to file with FileOutputStream
* @param content given string
* @throws IOException
*/
//TODO: Phase1: define method here for writing file with OutputStream
public static void fileWriter2(String content) throws IOException {
//TODO: write content on file:done
String fileName = getProperFileName(content);
OutputStream out = new FileOutputStream(NOTES_PATH + fileName + ".txt/");
out.write(content.getBytes());
out.flush();
}
//TODO: Phase2: proper methods for handling serialization
private static String getProperFileName(String content) {
int loc = content.indexOf("\n");
if (loc != -1) {
return content.substring(0, loc);
}
if (!content.isEmpty()) {
return content;
}
return System.currentTimeMillis() + "_new file.txt";
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment