package app;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Locale;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import org.ytreza.application.ApplicationProperty;
import org.ytreza.data.database.jdbc.ConnectionNotDefinedException;
import org.ytreza.tool.internationalization.Tr;
import app.database.TableFactory;
import app.database.TableFactoryJdbc;
import app.database.jdbc.MainConnection;
import app.database.model.note.ModelNote;
import app.database.model.note.TableNote;
import app.view.note.WindowNote;
import org.ytreza.data.table.CantSetUpTableException;
import org.ytreza.data.table.UnexpectedFactoryTypeException;
import org.ytreza.data.table.exception.ItemException;
import org.ytreza.data.table.exception.TableAlreadyRegisteredException;
import org.ytreza.data.table.exception.UnableToSaveException;
import org.ytreza.data.table.exception.UnregisteredTableError;
public class Application {
private static Application application;
private Application () {
}
ApplicationProperty properties;
private TableFactory tableFactory;
public void run(String[] args) {
initialize();
if (args.length > 0) {
if (args[0].equals("-saveall")) {
saveAllNotes();
}
}else {
runMainWindow();
}
}
private void saveAllNotes() throws UnregisteredTableError {
TableNote table = getTableFactory().getTableNote();
try {
System.out.println("d�but");
for (ModelNote note : table.getAllItems()) {
note.getTagList();
note.save();
}
System.out.println("fin");
} catch (ItemException e) {
// AUTO_TODO Auto-generated catch block
e.printStackTrace();
} catch (UnableToSaveException e) {
// AUTO_TODO Auto-generated catch block
e.printStackTrace();
}
}
private void runMainWindow() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
WindowNote window = new WindowNote(getTableFactory());
window.setVisible(true);
}
});
}
private void initialize() {
readProperties();
initializeLanguage();
initializeDatabase();
saveProperties();
}
private void readProperties() {
properties = ApplicationProperty.getProperties("./memo.prop");
}
private void saveProperties() {
try {
properties.save();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Impossible de charger les propri�t�s.\n" + e.getMessage());
e.printStackTrace();
}
}
private void initializeLanguage() {
Tr.configLangage(new Locale(properties.getLanguage(), properties.getCountry()));
}
private void initializeDatabase() {
try {
initializeMainConnection();
createFileInDatabase();
} catch (SQLException e) {
// AUTO_TODO Auto-generated catch block
e.printStackTrace();
} catch (ConnectionNotDefinedException e) {
// AUTO_TODO Auto-generated catch block
e.printStackTrace();
} catch (TableAlreadyRegisteredException e) {
// AUTO_TODO Auto-generated catch block
e.printStackTrace();
} catch (UnexpectedFactoryTypeException e) {
// AUTO_TODO Auto-generated catch block
e.printStackTrace();
} catch (CantSetUpTableException e) {
// AUTO_TODO Auto-generated catch block
e.printStackTrace();
}
}
private void createFileInDatabase() throws SQLException, ConnectionNotDefinedException, TableAlreadyRegisteredException, UnexpectedFactoryTypeException, CantSetUpTableException {
tableFactory = new TableFactoryJdbc(MainConnection.getMainConnection());
}
public TableFactory getTableFactory() {
return tableFactory;
}
private void initializeMainConnection() throws SQLException {
MainConnection.initMainConnection(properties.getConnectionName(), properties.getConnectionInfo(), properties.getUserName(), properties.getPassword());
}
public void close() {
try {
Tr.save();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Impossible de sauvegarder les propri�t�s.\n" + e.getMessage());
e.printStackTrace();
}
}
public static Application getApplication() {
if (application == null){
application = new Application();
}
return application;
}
}