package com.nexirius.ulc.example1;
import com.nexirius.framework.application.Application;
import com.nexirius.framework.application.DialogManager;
import com.nexirius.framework.dataeditor.SimpleEditorCreator;
import com.nexirius.framework.datamodel.DataModel;
import com.nexirius.framework.datamodel.DefaultDataModelCommand;
import com.nexirius.framework.dataviewer.DataViewer;
import com.nexirius.framework.dataviewer.ProgressViewerCreator;
import com.nexirius.framework.dataviewer.ViewerCreatorMap;
import com.nexirius.framework.dataviewer.ViewerFactory;
import com.nexirius.framework.swing.SwingViewer;
import com.nexirius.ulc.example1.controller.SwingController;
import com.nexirius.ulc.example1.datamodel.ItemArrayModel;
import com.nexirius.ulc.example1.datamodel.ItemListModel;
import com.nexirius.ulc.example1.dataviewer.ItemListViewer;
import com.nexirius.ulc.example1.layout.ItemArrayLayout;
import com.nexirius.ulc.example1.persistence.PersistenceManager;
import javax.swing.*;
import java.awt.*;
public class ExampleSwingApplication extends Application {
ItemListModel mainModel;
public ExampleSwingApplication(String[] argv) {
super(argv);
}
public String getApplicationName() {
return "ExampleUlcApplication";
}
public String getApplicationTitle() {
return getFactory().getClientResource().getLabel(getApplicationName());
}
public void preInit() {
mainModel = new ItemListModel();
mainModel.addDataModelListener(new SwingController(mainModel));
mainModel.appendMethod(new ExitCommand());
PersistenceManager.init();
try {
mainModel.reloadCommand();
} catch (Exception e) {
e.printStackTrace(); //TODO
}
}
public void init() {
ViewerCreatorMap map = getFactory().getViewerCreatorMap();
map.registerSwingViewer(ItemListModel.class, ItemListViewer.class);
map.register(ItemArrayModel.class, new ItemArrayLayout(), false);
//map.registerSwingViewer(DoubleModel.class, DoubleViewer.class);
try {
DataViewer viewer = getFactory().createDefaultEditor(mainModel);
getMainPanel().setLayout(new GridLayout(1, 1));
getMainPanel().add(viewer.getJComponent());
} catch (Exception e) {
e.printStackTrace(); //TODO
}
getMainFrame().pack();
DialogManager.center(getMainFrame(), true);
ImageIcon icon = (ImageIcon) getFactory().getClientResource().getIcon(getApplicationName());
getMainFrame().setIconImage(icon.getImage());
}
public static class DoubleViewer implements SwingViewer {
protected DataModel d;
public JComponent createJComponent(ViewerFactory factory) {
JPanel panel = new JPanel(new GridLayout(1, 0));
panel.add(factory.createViewer(new SimpleEditorCreator(), d).getJComponent());
panel.add(factory.createViewer(new ProgressViewerCreator(), d).getJComponent());
return panel;
}
public boolean isEditor() {
return true;
}
public void setDataModel(DataModel model) {
d = model;
}
public DataModel getDataModel() {
return d;
}
}
public void postInit() {
}
public DataModel getApplicationModel() {
return mainModel;
}
public void exit() {
try {
PersistenceManager.save(mainModel.getArray());
} catch (Exception e) {
e.printStackTrace(); //TODO
}
super.exit();
}
public class ExitCommand extends DefaultDataModelCommand {
public ExitCommand() {
super("Exit");
}
public void doAction() {
exit();
}
}
public static void main(String[] argv) {
new ExampleSwingApplication(argv).run();
}
}