package com.nexirius.jnex.example;
import com.nexirius.framework.application.Application;
import com.nexirius.framework.application.DialogManager;
import com.nexirius.framework.datamodel.DataModel;
import com.nexirius.framework.datamodel.DataModelFile;
import com.nexirius.framework.datamodel.DefaultDataModelCommand;
import com.nexirius.framework.dataviewer.DataViewer;
import com.nexirius.framework.dataviewer.ViewerCreatorMap;
import com.nexirius.framework.swing.SwingViewerCreator;
import com.nexirius.jnex.example.datamodel.MainModel;
import com.nexirius.jnex.example.datamodel.MediaArrayModel;
import com.nexirius.jnex.example.dataviewer.MainViewer;
import com.nexirius.jnex.example.layout.MediaArrayLayout;
import javax.swing.*;
import java.awt.*;
public class MediaApplication extends Application {
MainModel mainModel;
DataModelFile file;
public MediaApplication(String argv[]) {
super(argv);
}
public DataModel getApplicationModel() {
return mainModel;
}
public String getApplicationName() {
return "MediaApplication";
}
public void preInit() {
mainModel = new MainModel();
file = new DataModelFile(mainModel.getMediaArray());
file.setDirectory("c:\\temp");
file.setExtension(".txt");
file.setType("MediaFile", 1);
// append the exit command to the main model
mainModel.appendMethod(new NewCommand());
mainModel.appendMethod(new OpenCommand());
mainModel.appendMethod(new SaveCommand());
mainModel.appendMethod(new SaveAsCommand());
mainModel.appendMethod(new ExitCommand());
}
public void init() {
ViewerCreatorMap map = factory.getViewerCreatorMap();
map.register(MainModel.class, new SwingViewerCreator(MainViewer.class));
map.register(MediaArrayModel.class, new MediaArrayLayout(), false);
try {
// create an editor for the main model
DataViewer editor = getFactory().createViewer(mainModel, true);
// access the Swing JComponents
JComponent editorComponent = editor.getJComponent();
// insert generated JComponents into the application window
getMainPanel().setLayout(new BorderLayout());
getMainPanel().add(editorComponent, BorderLayout.CENTER);
getMainFrame().pack();
DialogManager.center(getMainFrame(), true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void exit() {
if (file.getNeedSaving()) {
if (DialogManager.ask("SaveFirst", "Yes", "No", true)) {
try {
file.save();
} catch (Exception ex) {
ex.printStackTrace();
DialogManager.error(ex.getMessage());
}
}
}
System.exit(0);
}
class NewCommand extends DefaultDataModelCommand {
public NewCommand() {
super("New");
}
public void doAction() {
file.newCommand();
}
}
class OpenCommand extends DefaultDataModelCommand {
public OpenCommand() {
super("Open");
}
public void doAction() {
try {
file.open();
} catch (Exception ex) {
ex.printStackTrace();
DialogManager.error(ex.getMessage());
}
}
}
class SaveCommand extends DefaultDataModelCommand {
public SaveCommand() {
super("Save");
}
public void doAction() {
try {
file.save();
} catch (Exception ex) {
ex.printStackTrace();
DialogManager.error(ex.getMessage());
}
}
}
class SaveAsCommand extends DefaultDataModelCommand {
public SaveAsCommand() {
super("SaveAs");
}
public void doAction() {
try {
file.saveAs();
} catch (Exception ex) {
ex.printStackTrace();
DialogManager.error(ex.getMessage());
}
}
}
class ExitCommand extends DefaultDataModelCommand {
public ExitCommand() {
super("Exit");
}
public void doAction() {
exit();
}
}
public static void main(String argv[]) {
String plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try {
UIManager.setLookAndFeel(plaf);
} catch (Exception e) {
System.out.println("Can't set '" + plaf + "' look and feel");
System.exit(0);
}
new MediaApplication(argv).run();
}
}