com.squadd.UI.MainInformationAboutGroup.java Source code

Java tutorial

Introduction

Here is the source code for com.squadd.UI.MainInformationAboutGroup.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.squadd.UI;

import com.squadd.javaBeans.File;
import com.squadd.javaBeans.Group;
import com.squadd.javaBeans.Place;
import com.squadd.managers.DBManager;
import com.squadd.technical.Display;
import com.squadd.technical.ImageGetter;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Image;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;
import java.util.Date;

/**
 *
 * @author SharkNado
 */
public class MainInformationAboutGroup extends HorizontalLayout {
    private GroupMainInfoLayout groupInfo;
    private EditVersionLayout editVersion;
    private Group gr;

    public MainInformationAboutGroup(Group group) {
        groupInfo = new GroupMainInfoLayout(group);
        editVersion = new EditVersionLayout(group);
        editVersion.setVisible(false);
        gr = group;

        buildLayout();
        configureActions(groupInfo.getEdit(), editVersion.getSave(), editVersion.getCancel(),
                groupInfo.getUploadPhoto(), editVersion.getUploadPhoto());
    }

    private void buildLayout() {
        addComponents(groupInfo, editVersion);
    }

    private void configureActions(Button edit, Button save, Button cancel, Button uploadFirst,
            Button uploadSecond) {
        ClickListener sv = new ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                editVersion.setVisible(false);
                groupInfo.setVisible(true);
                applyChanges();
                sendToBDChanges();
            }
        };

        ClickListener ed = new ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                groupInfo.setVisible(false);
                editVersion.setVisible(true);
            }
        };

        ClickListener canc = new ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                editVersion.setVisible(false);
                groupInfo.setVisible(true);
            }
        };

        ClickListener upload1 = new ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                UploadGroupImageWindow window = new UploadGroupImageWindow(gr);
                getUI().addWindow(window);
                Button yes = window.yes;
                yes.addClickListener(new ClickListener() {
                    @Override
                    public void buttonClick(Button.ClickEvent event) {
                        deleteOldFile();
                        addNewFile();
                        window.close();
                    }
                });
            }
        };

        ClickListener upload2 = new ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                UploadGroupImageWindow window = new UploadGroupImageWindow(gr);
                getUI().addWindow(window);
                Button yes = window.yes;
                yes.addClickListener(new ClickListener() {
                    @Override
                    public void buttonClick(Button.ClickEvent event) {
                        deleteOldFile();
                        addNewFile();
                        window.close();
                    }
                });
            }

        };

        uploadFirst.addClickListener(upload1);
        uploadSecond.addClickListener(upload2);
        save.addClickListener(sv);
        cancel.addClickListener(canc);
        edit.addClickListener(ed);
    }

    private void deleteOldFile() {
        DBManager db = new DBManager();
        File file = db.getFilId(gr);
        if (file != null) {
            java.io.File toDel = new java.io.File(file.getPath());
            toDel.delete();
            db.disconnect(gr, file);
        }
    }

    private void addNewFile() {
        File file = new File();
        DBManager db = new DBManager();
        String path = new ImageGetter().getPath(gr);
        file.setPath(path);
        db.addFile(file);
        file = db.getFile(path);
        db.connect(gr, file);
    }

    private void applyChanges() {
        Label groupName = groupInfo.getGroupName();
        Label placeName = groupInfo.getPlaceName();
        Label date = groupInfo.getDate();
        Label description = groupInfo.getDescr();

        String valGr = new String("Group Name: " + editVersion.getGroupName().getValue());
        String valPl = new String("Place: " + editVersion.getPlaceName().getValue());
        String valDt = (editVersion.getDate().getValue() == null) ? "Place: "
                : new String("Date: " + editVersion.getDate().getValue().toString());
        String valDs = new String("Description: " + editVersion.getDescr().getValue());

        groupName.setValue(valGr);
        placeName.setValue(valPl);
        date.setValue(valDt);
        description.setValue(valDs);
    }

    private void sendToBDChanges() {
        gr.setDescription(editVersion.getDescr().getValue());
        gr.setName(editVersion.getGroupName().getValue());
        gr.setTime(editVersion.getDate().getValue());
        DBManager db = new DBManager();
        db.add(gr);
        Place place = gr.getPlaceId();
        place.setCity(editVersion.getPlaceName().getValue());
        db.add(place);
    }
}