Files
Appthinker/AppThinker/src/AppThinker.java

197 lines
7.0 KiB
Java

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.io.*;
/**
* La classe principale du logiciel AppThinker.
* @author V.BOULANGER
*/
public class AppThinker {
//JavaDoc tags : @param @return @throws @author @version @see @since @serial @deprecated
private static Project _project;
private static AppThinkerWindow _window;
/**
* La méthode principale exécutée
* @param args Les arguments de la méthode principale.
*/
public static void main(String[] args) {
_window = new AppThinkerWindow();
}
/**
* Récupère la fenêtre de l'application
* @return La fenêtre de l'application.
*/
public static AppThinkerWindow getWindow(){
return _window;
}
/**
* Crée un nouveau Projet.
*/
public static void newProject(){
_project = new Project();
displayProject();
_window.getStatusbar().setFileMessage(_project.getName());
_window.setProject(_project);
_project.getUmlDiagram().displayDiagram();
_window.getStatusbar().setStatusMessage("The project was created.");
}
/**
* Opère les modifications nécessaires sur la fenêtre pour accueillir le projet.
*/
public static void displayProject(){
_window.setTitle((_project.getPath() == null) ? "AppThinker - " + _project.getName() + "*" : "AppThinker - " + _project.getName() + " (" + _project.getPath() + ")");
_window.getMenubar().setProjectEnable(true);
_project.getUmlDiagram().getToolbar().setEnabled(true);
}
/**
* Ouvre un projet existant dans la fenêtre.
*/
public static void openProject(){
FileNameExtensionFilter fileFilter = new FileNameExtensionFilter("AppThinker project", "appt");
JFileChooser dialog = new JFileChooser();
dialog.setDialogType(JFileChooser.OPEN_DIALOG);
dialog.setDialogTitle("Open an AppThinker project");
dialog.setFileFilter(fileFilter);
dialog.setAcceptAllFileFilterUsed(false);
if(dialog.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
ObjectInputStream ois = null;
Project project = null;
try {
final FileInputStream fichier = new FileInputStream(dialog.getSelectedFile().getPath());
ois = new ObjectInputStream(fichier);
project = (Project) ois.readObject();
} catch (final java.io.IOException e) {
e.printStackTrace();
} catch (final ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (ois != null) {
ois.close();
_project = project;
_window.getStatusbar().setFileMessage(_project.getName());
_window.setProject(_project);
_project.getUmlDiagram().displayDiagram();
displayProject();
}
} catch (final IOException ex) {
ex.printStackTrace();
}
}
}
}
/**
* Sauvegarde le projet en cours.
*/
public static void saveProject(){
//Si le projet ne contient pas de path, on demande à l'enregistrer dans un emplacement
FileNameExtensionFilter fileFilter = new FileNameExtensionFilter("AppThinker project", "appt");
String path = _project.getPath();
if(path == null){
JFileChooser dialog = new JFileChooser();
dialog.setDialogTitle("Save an AppThinker project");
dialog.setDialogType(JFileChooser.SAVE_DIALOG);
dialog.setFileFilter(fileFilter);
dialog.setAcceptAllFileFilterUsed(false);
if(dialog.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
String filePath = dialog.getSelectedFile().getPath();
path = (filePath.contains(".appt")) ? filePath : filePath + ".appt";
}
}
//On serialize le projet dans un fichier
_project.setPath(path);
ObjectOutputStream oos = null;
try {
final FileOutputStream fichier = new FileOutputStream(path);
oos = new ObjectOutputStream(fichier);
oos.writeObject(_project);
oos.flush();
} catch (final java.io.IOException e) {
e.printStackTrace();
} finally {
try {
if (oos != null) {
oos.flush();
oos.close();
displayProject();
_window.getStatusbar().setStatusMessage("The project was saved successfully.");
}
} catch (final IOException ex) {
ex.printStackTrace();
}
}
}
/**
* Sauvegarde le projet en cours à un autre emplacement.
*/
public static void saveAsProject(){
//Enregistrer le projet sous un autre emplacement
FileNameExtensionFilter fileFilter = new FileNameExtensionFilter("AppThinker project", "appt");
String path = _project.getPath();
JFileChooser dialog = new JFileChooser();
dialog.setDialogTitle("Save an AppThinker project");
dialog.setDialogType(JFileChooser.SAVE_DIALOG);
dialog.setFileFilter(fileFilter);
dialog.setAcceptAllFileFilterUsed(false);
if(dialog.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
String filePath = dialog.getSelectedFile().getPath();
path = (filePath.contains(".appt")) ? filePath : filePath + ".appt";
}
//On serialize le projet dans un fichier
_project.setPath(path);
ObjectOutputStream oos = null;
try {
final FileOutputStream fichier = new FileOutputStream(path);
oos = new ObjectOutputStream(fichier);
oos.writeObject(_project);
oos.flush();
} catch (final java.io.IOException e) {
e.printStackTrace();
} finally {
try {
if (oos != null) {
oos.flush();
oos.close();
displayProject();
_window.getStatusbar().setStatusMessage("The project was saved on a new location.");
}
} catch (final IOException ex) {
ex.printStackTrace();
}
}
}
/**
* Ferme le projet en cours.
*/
public static void closeProject(){
_window.setTitle("AppThinker");
_window.getMenubar().setProjectEnable(false);
_project.getUmlDiagram().getToolbar().setEnabled(false);
_window.getStatusbar().setStatusMessage("The project has been closed.");
_window.getStatusbar().setFileMessage("No project opened.");
_window.clearProject();
_project = null;
}
/**
* Récupère le projet en cours.
* @return Le projet en cours.
*/
public static Project getProject(){
return _project;
}
}