Package org.ifmo.clgp.controller

Source Code of org.ifmo.clgp.controller.Controller

package org.ifmo.clgp.controller;

import java.awt.Point;
import java.io.*;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import org.ifmo.clgp.model.*;
import org.ifmo.clgp.model.settings.Settings;
import org.ifmo.clgp.view.View;
import org.ifmo.clgp.view.dialog.CreateImageDialog;
//import org.ifmo.clgp.view.dialog.SelectProfileDialog;

/**
*
* @author dmitry_zamula
*/
public class Controller {

    private View view;
    private Model model;
    private String pathRead = "/live/image/blankconf/syslinux.cfg";
    private String pathWrite = "/live/image/syslinux/syslinux.cfg";
//    private String pathRead = "clonezilla/blankconf/syslinux.cfg";
//    private String pathWrite = "clonezilla/syslinux/syslinuxTMP.cfg";
    private final String appendClonezilla = "initrd=/live/initrd.img boot=live"
            + " config noswap nolocales edd=on nomodeset noprompt ocs_"
            + "prerun=\"mount " + Settings.getProperty("partition.to.save.images") + " /home/partimag\" ocs_live_run=\"ocs-sr -q2 -c -j2 -z1 -i 2000 -p true savedisk " +
            "IMAGENAME " + Settings.getProperty("partition.to.make.image") + "\""
            + " ocs_live_extra_param=\"\" ocs_live_keymap=\"NONE\" ocs_live_batch=\"no\""
            + " ocs_lang=\"ru_RU.UTF-8\" vga=788 ip=frommedia  nosplash";
    private DefaultMutableTreeNode lastNode;

    public Controller(View view, Model model) {
        this.view = view;
        this.model = model;

        view.addListeners(new ImageMetainfController(this), new PopupActivationListener(this),
                new PopupMenuActionListener(this), new ViewButtonListener(this));
    }

    public DefaultMutableTreeNode getLastNode() {
        return lastNode;
    }

    public void setLastNode(DefaultMutableTreeNode lastNode) {
        this.lastNode = lastNode;
    }

    public void setUserObjectFromJTree(Object objectFromTree) {
        model.setUserObjectFromJTree(objectFromTree);
    }
   
    public void setUserObjectPath(String path) {
        model.setUserObjectPath(path);
    }

    public void changeViewMetainf() {
        Object objectFromTree = model.getUserObjectFromJTree();
        if (objectFromTree instanceof Image) {
            view.enableImageMetainf();
            view.updateImageMetainf((Image) objectFromTree);
        } else {
            view.disableImageMetainf();
        }
    }

    public void activatePopup(Point point) {
        view.activatePopup(point);
    }

    public void createFolder() {
        String userInput = JOptionPane.showInputDialog(view, "Введите имя папки", "Создание папки",
                JOptionPane.PLAIN_MESSAGE);
        if (userInput != null) {
            ImageNode newFolder = model.createFolder(userInput);
            view.updateTree(getLastNode(), newFolder);
        }
    }

    public void createImage() {
        CreateImageDialog cid = new CreateImageDialog(view, true);
        cid.setVisible(true);
        String imageName = cid.getImageName();

        if (imageName != null) {
            int selected = JOptionPane.showConfirmDialog(view, "Для продолжения требуется перезагрузка",
                    "Требуется перезагрузка", JOptionPane.YES_NO_OPTION);
            if (selected == 0) {
                Image newImage = new Image();
                newImage.setName(imageName);
                newImage.setDescription(cid.getImageDesc());
                newImage.setCreationDate(new Date());
                //TODO setOperationSystem()
                ((ImageNode) model.getUserObjectFromJTree()).setImages(newImage);
                StructureIOManager.getInstance().marshal(StructureManager.getInstance().getRoot());

                //запихиваем в syslinux.cfg всю инфу, чтобы грузиться в клонзиллу ребутаем комп
                try {
                    BufferedReader reader = new BufferedReader(new FileReader(pathRead));
                    BufferedWriter writer = new BufferedWriter(new FileWriter(pathWrite));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        line = line.replace("$NAMEPROFILE$", newImage.getName());
                        line = line.replace("$DESC$", newImage.getDescription());
                        String fullPathToImageOnDisk = model.getUserObjectPath() + "/" + imageName;
                        String params = appendClonezilla.replace("IMAGENAME", fullPathToImageOnDisk);
                        line = line.replace("$APPEND$", params);
                        writer.write(line);
                        writer.newLine();
                    }
                    writer.flush();

                    Runtime.getRuntime().exec("shutdown -r now"); //TODO ребут пока так
                } catch (IOException e1) {
                    e1.printStackTrace();
                }               
            }
        }
    }

    public void deleteObjectFromStructure() {
        if (getLastNode() != null) {
            Object userObject = model.getUserObjectFromJTree();
            TreeNode parent = getLastNode().getParent();
            if (parent != null) { //You can not delete root folder
                ImageNode userObjectParent = (ImageNode) ((DefaultMutableTreeNode) parent).getUserObject();
                if (userObject instanceof ImageNode) {
                    userObjectParent.getNodes().remove(userObject);
                } else if (userObject instanceof Image) {
                    userObjectParent.getImages().remove(userObject);
                }
                StructureIOManager.getInstance().marshal(StructureManager.getInstance().getRoot());
                view.removeTreeNode(getLastNode());
            }
        }
    }

    public void modifyImageMetaInf() {
        Image image = view.getSelectedImage();
        Object userObject = model.getUserObjectFromJTree();
        if (userObject instanceof Image) {
            Image oldImage = (Image) userObject;
            oldImage.setCapacity(image.getCapacity());
            oldImage.setCreationDate(image.getCreationDate());
            oldImage.setDescription(image.getDescription());
            oldImage.setOperationSystem(image.getOperationSystem());
            //oldImage.setProfiles(null);
        }
        StructureIOManager.getInstance().marshal(StructureManager.getInstance().getRoot());
    }

    public void installImage() {
//        if (model.getUserObjectFromJTree() instanceof Image) {
//            SelectProfileDialog cpd = new SelectProfileDialog(view, true);
//            cpd.setVisible(true);
//            Profile profile = cpd.getProfile();
//
//            if (profile != null) {
//            System.out.println("Magic will be here...");
//            int selected = JOptionPane.showConfirmDialog(view, "Для продолжения требуется перезагрузка",
//                    "Требуется перезагрузка", JOptionPane.YES_NO_OPTION);
//            if (selected == 0) {
//                Image newImage = new Image();
//                newImage.setName(imageName);
//                newImage.setDescription(cid.getImageDesc());
//                newImage.setCreationDate(new Date());
//                //TODO setOperationSystem()
//                ((ImageNode) model.getUserObjectFromJTree()).setImages(newImage);
//                StructureIOManager.getInstance().marshal(StructureManager.getInstance().getRoot());
//
//                try {
//                    BufferedReader reader = new BufferedReader(new FileReader(pathRead));
//                    BufferedWriter writer = new BufferedWriter(new FileWriter(pathWrite));
//                    String line;
//                    while ((line = reader.readLine()) != null) {
//                        line = line.replace("$NAMEPROFILE$", newImage.getName());
//                        line = line.replace("$DESC$", newImage.getDescription());
//                        String params = appendClonezilla.replace("IMAGENAME", imageName);
//                        line = line.replace("$APPEND$", params);
//                        writer.write(line);
//                        writer.newLine();
//                    }
//                    writer.flush();
//
//                    Runtime.getRuntime().exec("shutdown -r now"); //TODO ребут пока так
//                } catch (IOException e1) {
//                    e1.printStackTrace();
//                }
//                //TODO запихиваем в syslinux.cfg всю инфу, чтобы грузиться в клонзиллу ребутаем комп
//
//            }
//            }
//        }
    }
}
TOP

Related Classes of org.ifmo.clgp.controller.Controller

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.