Merge origin/master

This commit is contained in:
Victor 2014-02-12 17:44:42 +02:00
commit a33683c490
7 changed files with 51 additions and 59 deletions

View File

@ -1,4 +1,4 @@
package tse.lr5;
package com.annimon.io;
import java.io.BufferedReader;
import java.io.File;
@ -85,11 +85,10 @@ public class CsvReader<T> {
}
String line;
while ( (line = reader.readLine()) != null ) {
T obj = null;
try {
if (!line.isEmpty()) {
String[] params = line.split(separator);
obj = (T) handler.createObject(params);
handler.createObject(params);
}
} catch (RuntimeException ex) {
Util.handleException(ex);

View File

@ -9,7 +9,6 @@ import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
/**
*

View File

@ -1,13 +1,13 @@
package tse.lr3;
import com.annimon.io.CsvReader;
import tse.lr2.*;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
@ -151,31 +151,25 @@ public class LR_3_Tasks {
}
private List<Ellipse> readFromCSV(String filename) throws IOException {
List<Ellipse> list = new ArrayList<>();
BufferedReader reader = new BufferedReader(
new InputStreamReader( new FileInputStream(filename), "UTF-8" )
);
reader.readLine(); // Класс,X1,Y1,X2,Y2,X3,Y3,X4,Y4
String line;
while ( (line = reader.readLine()) != null ) {
Ellipse ellipse = null;
try {
ellipse = readFromCsvLine(line);
} catch (RuntimeException ex) {
System.out.println(ex.toString());
CsvReader<Ellipse> csvReader = new CsvReader<>(new File(filename));
csvReader.setReaderHandler(new CsvReader.ReaderHandler<Ellipse>() {
@Override
public void onStartRead(File file) { }
@Override
public Ellipse createObject(String[] params) {
return readFromCsvLine(params);
}
if (ellipse != null) list.add(ellipse);
}
reader.close();
return list;
@Override
public void onFinishRead(File file) { }
});
return csvReader.readCsvToList(true);
}
private Ellipse readFromCsvLine(String line) {
private Ellipse readFromCsvLine(String[] params) {
final int POINTS_COUNT = 4;
if (line.isEmpty()) throw new RuntimeException("Пустая строка");
String[] params = line.split(",");
if (params.length < 9) throw new RuntimeException("Неверное количество параметров");
final int[] coords = new int[POINTS_COUNT * 2];
for (int i = 0; i < coords.length; i++) {
coords[i] = Integer.parseInt(params[i + 1]);

View File

@ -60,14 +60,17 @@ public class NotePad {
return getName() + ". " + getDescription();
}
public static NotePad readFromCsvLine(String line) throws ParseException {
if (line.isEmpty()) throw new RuntimeException("Пустая строка");
String[] params = line.split("\t");
public static NotePad readFromCsvLine(String[] params) {
if (params.length != 4) throw new RuntimeException("Неверное количество параметров");
String name = params[0];
String description = params[1];
Date data = new SimpleDateFormat(DATE_PATTERN).parse(params[2]);
Date data;
try {
data = new SimpleDateFormat(DATE_PATTERN).parse(params[2]);
} catch (ParseException ex) {
data = new Date(System.currentTimeMillis());
}
boolean important = Boolean.parseBoolean(params[3]);
return new NotePad(name, description, data, important);

View File

@ -1,13 +1,11 @@
package tse.lr4;
import java.io.BufferedReader;
import com.annimon.io.CsvReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -30,14 +28,14 @@ public class NotePadManager {
return instance;
}
private ArrayList<NotePad> notepads;
private List<NotePad> notepads;
public NotePadManager() {
notepads = (ArrayList<NotePad>) readFromCSV(FILENAME);
notepads = readFromCSV(FILENAME);
if (notepads == null) notepads = new ArrayList<>();
}
public ArrayList<NotePad> getNotepads() {
public List<NotePad> getNotepads() {
return notepads;
}
@ -67,7 +65,7 @@ public class NotePadManager {
createNewEntry(name, desc, date, important);
}
private void saveToCSV(String filename, ArrayList<NotePad> list) {
private void saveToCSV(String filename, List<NotePad> list) {
try {
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(filename), "UTF-8")
@ -84,25 +82,23 @@ public class NotePadManager {
}
}
private Object readFromCSV(String filename) {
try {
List<NotePad> list = new ArrayList<>();
BufferedReader reader = new BufferedReader(
new InputStreamReader( new FileInputStream(filename), "UTF-8" )
);
reader.readLine(); // Имя,Описание,Дата,Важное
String line;
while ( (line = reader.readLine()) != null ) {
NotePad pad = null;
try {
pad = NotePad.readFromCsvLine(line);
} catch (RuntimeException | ParseException ex) {
System.out.println(ex.toString());
}
if (pad != null) list.add(pad);
private List<NotePad> readFromCSV(String filename) {
CsvReader<NotePad> csvReader = new CsvReader<>(new File(filename));
csvReader.setReaderHandler(new CsvReader.ReaderHandler<NotePad>() {
@Override
public void onStartRead(File file) { }
@Override
public NotePad createObject(String[] params) {
return NotePad.readFromCsvLine(params);
}
reader.close();
return list;
@Override
public void onFinishRead(File file) { }
});
try {
return csvReader.readCsvToList();
} catch (IOException ex) {
Util.handleException(ex);
}

View File

@ -3,9 +3,9 @@ package tse.lr4;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
@ -199,7 +199,7 @@ public class PadPanel extends JPanel {
return NotePadManager.getInstance().getNotepads().get(padIndex);
}
private ArrayList<NotePad> getNotepads() {
private List<NotePad> getNotepads() {
return NotePadManager.getInstance().getNotepads();
}

View File

@ -1,5 +1,6 @@
package tse.lr5;
import com.annimon.io.CsvReader;
import java.awt.Color;
import java.awt.Point;
import java.io.File;