MediaUI.java Source code

Java tutorial

Introduction

Here is the source code for MediaUI.java

Source

import org.eclipse.swt.SWT;
import java.io.*;
import java.util.*;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;
import com.mongodb.gridfs.*;

import com.cloudgarden.resource.SWTResourceManager;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MediaUI extends org.eclipse.swt.widgets.Composite {
    private SearchController searchController;
    private Text SearchInput;
    private Table gallery;
    private Composite ListPreview;
    private Button editBtn;
    private Composite composite1;
    private ProfileTable profileData;
    private Table profileTable;
    private Button GO;
    private CCombo ProfileSelect;
    private Composite SearchBar;
    private CLabel MediaDisplayScreen;
    private MediaDisplayController mediaDisplayController;
    private ToolItem Save;
    private ToolItem Delete;
    private ToolItem Add;
    private ToolBar toolBar1;
    private boolean editBtnSelected;
    private MediaSystemManagement media;
    //   private SelectionAdapter tableListener;
    private GalleryTable galleryTable;
    private boolean freeze;
    private SelectionAdapter tableListener;
    private TableEditor editor;
    private Text profileTableTextEditor;
    private boolean searchByProfile;

    {
        //Register as a resource user - SWTResourceManager will
        //handle the obtaining and disposing of resources
        SWTResourceManager.registerResourceUser(this);
    }

    public MediaUI(Composite parent, int style) {
        super(parent, style);
        media = new MediaSystemManagement();
        media.init();
        mediaDisplayController = new MediaDisplayController();
        freeze = false;
        searchByProfile = false;

        tableListener = new SelectionAdapter() {
            public void widgetSelected(SelectionEvent evt) {
                profileTableWidgetSelected(evt);
            }
        };

        initGUI();

        editor = new TableEditor(profileTable);
        editor.horizontalAlignment = SWT.LEFT;
        editor.grabHorizontal = true;
        editor.minimumWidth = 50;
    }

    /**
    * Initializes the GUI.
    */
    private void initGUI() {
        try {
            this.setSize(633, 376);
            this.setBackground(SWTResourceManager.getColor(192, 192, 192));
            FormLayout thisLayout = new FormLayout();
            this.setLayout(thisLayout);
            {
                //            ListPreview = new Composite(this, SWT.NONE);
                //            GridLayout ListPreviewLayout = new GridLayout();
                //            ListPreviewLayout.numColumns=1;
                //            ListPreviewLayout.makeColumnsEqualWidth = true;
                //            FormData ListPreviewLData = new FormData();
                //            ListPreviewLData.left =  new FormAttachment(0, 1000, 0);
                //            ListPreviewLData.top =  new FormAttachment(0, 1000, 43);
                //            ListPreviewLData.width = 64;
                //            ListPreviewLData.height = 321;
                //            ListPreviewLData.bottom =  new FormAttachment(1000, 1000, -12);
                //            ListPreviewLData.right =  new FormAttachment(101, 1000, 0);
                //            ListPreview.setLayoutData(ListPreviewLData);
                //            ListPreview.setLayout(ListPreviewLayout);
                {
                    FormData galleryLData = new FormData();
                    galleryLData.width = 63;
                    galleryLData.height = 304;
                    galleryLData.top = new FormAttachment(0, 1000, 43);
                    galleryLData.bottom = new FormAttachment(1000, 1000, -12);
                    galleryLData.left = new FormAttachment(0, 1000, -2);
                    gallery = new Table(this, SWT.NONE);
                    gallery.setLayoutData(galleryLData);
                    gallery.addSelectionListener(new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent evt) {
                            galleryWidgetSelected(evt);
                        }
                    });
                }
                {
                    galleryTable = new GalleryTable(gallery, media.mongo);
                    media.loadGallery(galleryTable);
                }
            }
            {
                composite1 = new Composite(this, SWT.NONE);
                GridLayout composite1Layout = new GridLayout();
                composite1Layout.makeColumnsEqualWidth = true;
                FormData composite1LData = new FormData();
                composite1LData.left = new FormAttachment(721, 1000, 0);
                composite1LData.top = new FormAttachment(0, 1000, 43);
                composite1LData.width = 177;
                composite1LData.height = 321;
                composite1LData.right = new FormAttachment(1000, 1000, 0);
                composite1LData.bottom = new FormAttachment(1000, 1000, -12);
                composite1.setLayoutData(composite1LData);
                composite1.setLayout(composite1Layout);
                {
                    GridData profileTableLData = new GridData();
                    profileTableLData.grabExcessVerticalSpace = true;
                    profileTableLData.grabExcessHorizontalSpace = true;
                    profileTableLData.verticalAlignment = GridData.FILL;
                    profileTableLData.horizontalAlignment = GridData.FILL;
                    profileTable = new Table(composite1, SWT.NONE);
                    profileTable.setLayoutData(profileTableLData);

                    profileData = new ProfileTable(profileTable);
                    profileData.setMetadata(media.metadata);
                    profileData.update();

                }
                {
                    GridData btnLayoutLData = new GridData();
                    btnLayoutLData.grabExcessHorizontalSpace = true;
                    btnLayoutLData.horizontalAlignment = GridData.FILL;
                    btnLayoutLData.heightHint = 28;
                    Composite btnLayout = new Composite(composite1, SWT.None);
                    btnLayout.setLayoutData(btnLayoutLData);
                    {
                        editBtnSelected = false;
                        editBtn = new Button(btnLayout, SWT.PUSH | SWT.CENTER);
                        editBtn.setText("Edit");
                        editBtn.setBounds(12, 5, 50, 20);
                        editBtn.addSelectionListener(new SelectionAdapter() {
                            public void widgetSelected(SelectionEvent evt) {
                                editBtnWidgetSelected(evt);
                            }
                        });
                    }
                }

            }

            {
                SearchBar = new Composite(this, SWT.NONE);
                GridLayout SearchBarLayout = new GridLayout();
                SearchBarLayout.numColumns = 3;
                //            SearchBarLayout.marginWidth=5;
                //            SearchBarLayout.makeColumnsEqualWidth = true;
                FormData SearchBarLData = new FormData();
                SearchBarLData.left = new FormAttachment(626, 1000, 0);
                SearchBarLData.top = new FormAttachment(0, 1000, 0);
                SearchBarLData.width = 236;
                SearchBarLData.height = 31;
                SearchBarLData.right = new FormAttachment(1000, 1000, -1);
                SearchBar.setLayoutData(SearchBarLData);
                SearchBar.setLayout(SearchBarLayout);
                {
                    ProfileSelect = new CCombo(SearchBar, SWT.NONE);
                    GridData ProfileSelectLData = new GridData();
                    ProfileSelectLData.widthHint = 77;
                    ProfileSelectLData.heightHint = 20;
                    ProfileSelectLData.horizontalAlignment = GridData.END;
                    ProfileSelectLData.verticalAlignment = GridData.CENTER;
                    ProfileSelectLData.grabExcessHorizontalSpace = true;
                    ProfileSelect.setLayoutData(ProfileSelectLData);
                    ProfileSelect.setText("Profile");

                }
                {
                    SearchInput = new Text(SearchBar, SWT.NONE);
                    GridData SearchInputLData = new GridData();
                    SearchInputLData.horizontalAlignment = GridData.FILL;
                    SearchInputLData.grabExcessHorizontalSpace = true;
                    SearchInputLData.grabExcessVerticalSpace = true;
                    SearchInputLData.heightHint = 20;
                    SearchInput.setLayoutData(SearchInputLData);
                }
                {
                    searchController = new SearchController(ProfileSelect, SearchInput);
                }
                {
                    GO = new Button(SearchBar, SWT.PUSH | SWT.CENTER);
                    GO.setText("GO");
                    GridData GOLData = new GridData();
                    GOLData.horizontalAlignment = GridData.END;
                    GOLData.widthHint = 40;
                    GOLData.verticalAlignment = GridData.CENTER;
                    GOLData.grabExcessHorizontalSpace = true;
                    GO.setLayoutData(GOLData);
                    GO.addSelectionListener(new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent evt) {
                            GOWidgetSelected(evt);
                        }
                    });
                }
            }
            {
                MediaDisplayScreen = new CLabel(this, SWT.NONE);
                FormData MediaDisplayLData = new FormData();
                MediaDisplayLData.top = new FormAttachment(0, 1000, 43);
                MediaDisplayLData.width = 354;
                MediaDisplayLData.height = 321;
                MediaDisplayLData.right = new FormAttachment(702, 1000, 0);
                MediaDisplayLData.bottom = new FormAttachment(1000, 1000, -12);
                MediaDisplayLData.left = new FormAttachment(0, 1000, 90);
                MediaDisplayScreen.setLayoutData(MediaDisplayLData);
                MediaDisplayScreen.setAlignment(SWT.CENTER);

                //            mediaDisplayController.show(MediaDisplayScreen, "E:\\Test.png");
                //            Image image=new Image(Display.getDefault(),"E:\\Test.png");
                //            image=MediaDisplayController.resize(image, 100, 100);
                //            MediaDisplayScreen.setImage(image);
            }

            {
                FormData toolBar1LData = new FormData();
                toolBar1LData.left = new FormAttachment(0, 1000, 0);
                toolBar1LData.top = new FormAttachment(0, 1000, 0);
                toolBar1LData.width = 396;
                toolBar1LData.height = 31;
                toolBar1LData.right = new FormAttachment(626, 1000, 0);
                toolBar1 = new ToolBar(this, SWT.NONE);
                toolBar1.setLayoutData(toolBar1LData);
                toolBar1.setTouchEnabled(true);
                {
                    Add = new ToolItem(toolBar1, SWT.PUSH);
                    Add.setText("Add");
                    //               Add.setImage(SWTResourceManager.getImage("icon/add.png"));
                    Add.setSelection(true);
                    Add.addSelectionListener(new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent evt) {
                            AddWidgetSelected(evt);
                        }
                    });
                }
                {
                    Delete = new ToolItem(toolBar1, SWT.NONE);
                    Delete.setText("Delete");
                    Delete.addSelectionListener(new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent evt) {
                            DeleteWidgetSelected(evt);
                        }
                    });
                }
                {
                    Save = new ToolItem(toolBar1, SWT.NONE);
                    Save.setText("SaveTo");
                    Save.addSelectionListener(new SelectionAdapter() {
                        public void widgetSelected(SelectionEvent evt) {
                            SaveWidgetSelected(evt);
                        }
                    });
                }
            }
            this.layout();
            {
                int i = galleryTable.getSelectedImageId();
                if (i > -1) {
                    galleryTable.selectImage(i);
                    mediaDisplayController.show(MediaDisplayScreen, galleryTable.getGridFSDBFile(),
                            galleryTable.getMetadata());
                    profileData.setMetadata(galleryTable.getMetadata());
                    profileData.update();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
    * Auto-generated main method to display this 
    * org.eclipse.swt.widgets.Composite inside a new Shell.
    */
    public static void main(String[] args) {
        Display display = Display.getDefault();
        Shell shell = new Shell(display);
        MediaUI inst = new MediaUI(shell, SWT.NULL);
        Point size = inst.getSize();
        shell.setLayout(new FillLayout());
        shell.layout();
        if (size.x == 0 && size.y == 0) {
            inst.pack();
            shell.pack();
        } else {
            //         Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
            Rectangle shellBounds = shell.computeTrim(0, 0, 1000, 600);
            shell.setSize(shellBounds.width, shellBounds.height);
        }
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }

    private void AddWidgetSelected(SelectionEvent evt) {
        System.out.println("Add.widgetSelected, event=" + evt);
        //TODO add your code for Add.widgetSelected
        addFile(evt);
    }

    private void addFile(SelectionEvent evt) {
        FileDialog addFileDialog = new FileDialog(getShell(), SWT.OPEN);
        addFileDialog.open();
        String filepath = addFileDialog.getFilterPath();
        String filename = addFileDialog.getFileName();
        media.filePath = filepath + "\\" + filename;
        //?? output the media picture to the screen, haveMediaLoaded=true
        mediaDisplayController.show(MediaDisplayScreen, getLegalFilePath(media.filePath));
        profileData.delete();
        editBtnWidgetSelected(evt);
    }

    private String getLegalFilePath(String str) {
        String fileExtension = getFileExtension(str);
        String defaultVideoPath = MediaUI.class.getResource("icon/video.jpg").getPath();
        String defaultMusicPath = MediaUI.class.getResource("icon/music.png").getPath();
        String unknownType = MediaUI.class.getResource("icon/unknownType.png").getPath();
        String type = unknownType;
        switch (galleryTable.getType(fileExtension)) {
        case 0:
            type = str;
            break;
        case 1:
            type = defaultVideoPath;
            break;
        case 2:
            type = defaultMusicPath;
            break;
        case -1:
            type = unknownType;
            break;
        }
        return type;
    }

    private String getFileExtension(String filename) {
        int i = filename.lastIndexOf(".") + 1;
        String type = filename.substring(i);
        return type;
    }

    private void editBtnWidgetSelected(SelectionEvent evt) {
        System.out.println("editBtn.widgetSelected, event=" + evt);
        //TODO add your code for editBtn.widgetSelected
        if (mediaDisplayController.haveMediaDisplayed == false)
            return;
        if (editBtnSelected == false) {
            freeze = true;
            editBtnSelected = true;
            editBtn.setText("Confirm");
            profileTable.setSelection(0);
            profileTable.addSelectionListener(tableListener);
        } else {
            freeze = false;
            editBtnSelected = false;
            editBtn.setText("Edit");
            profileTable.removeSelectionListener(tableListener);
            profileTableTextEditor.dispose();
            if (searchByProfile == false) {
                if (profileData.isNewlyAdded()) {
                    MediaMetadata meta = profileData.getModifiedMetadata();
                    meta.width = mediaDisplayController.imageWidth;
                    meta.height = mediaDisplayController.imageHeight;
                    meta.type = getFileExtension(media.filePath);
                    meta = media.insert(mediaDisplayController.filePath, meta);
                    profileData.setMetadata(meta);
                    profileData.update();
                    galleryTable.addMeta(meta);
                } else {
                    media.update(profileData.getModifiedMetadata());
                }
            }
        }
    }

    private void profileTableWidgetSelected(SelectionEvent evt) {
        System.out.println("profileTable.widgetSelected, event=" + evt);
        //TODO add your code for profileTable.widgetSelected
        Control oldEditor = editor.getEditor();
        if (oldEditor != null)
            oldEditor.dispose();

        // Identify the selected row
        TableItem item = (TableItem) evt.item;
        if (item == null)
            return;

        // The control that will be the editor must be a child of the Table
        profileTableTextEditor = new Text(profileTable, SWT.NONE);
        final int EDITABLECOLUMN = 1;
        profileTableTextEditor.setText(item.getText(EDITABLECOLUMN));
        profileTableTextEditor.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent me) {
                Text text = (Text) editor.getEditor();
                editor.getItem().setText(EDITABLECOLUMN, text.getText());
            }
        });
        profileTableTextEditor.selectAll();
        profileTableTextEditor.setFocus();
        editor.setEditor(profileTableTextEditor, item, EDITABLECOLUMN);
    }

    private void SaveWidgetSelected(SelectionEvent evt) {
        System.out.println("Save.widgetSelected, event=" + evt);
        //TODO add your code for Save.widgetSelected
        if (mediaDisplayController.haveMediaDisplayed == false)
            return;
        FileDialog saveFile = new FileDialog(getShell(), SWT.SAVE);
        saveFile.open();
        String filepath = saveFile.getFilterPath();
        String fileName = saveFile.getFileName();
        //      System.out.println(filepath+"\\"+fileName);
        filepath = filepath + "\\" + fileName + "." + profileData.getMetadataCopy().type;
        GridFSDBFile gridFSDBFile = galleryTable.getGridFSDBFile();
        try {
            File outputfile = new File(filepath);
            gridFSDBFile.writeTo(outputfile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void DeleteWidgetSelected(SelectionEvent evt) {
        System.out.println("Delete.widgetSelected, event=" + evt);
        //TODO add your code for Delete.widgetSelected
        media.delete(galleryTable.getMetadata());
        mediaDisplayController.delete(MediaDisplayScreen);
        profileData.delete();
        galleryTable.deleteCurrentImage();
    }

    private void galleryWidgetSelected(SelectionEvent evt) {
        System.out.println("gallery.widgetSelected, event=" + evt);
        //TODO add your code for gallery.widgetSelected
        //   if(freeze==true) return;
        int i = gallery.getSelectionIndex();
        if (i > -1) {
            galleryTable.selectImage(i);
            //mediaDisplayController.show(MediaDisplayScreen, galleryTable.getGridFSDBFile(),galleryTable.getMetadata());
            mediaDisplayController.show(MediaDisplayScreen, galleryTable.getSelectedImage(),
                    galleryTable.getMetadata());
            profileData.setMetadata(galleryTable.getMetadata());
            profileData.update();
        }

    }

    private void GOWidgetSelected(SelectionEvent evt) {
        System.out.println("GO.widgetSelected, event=" + evt);
        //TODO add your code for GO.widgetSelected
        String[] searchClause;
        searchClause = searchController.getSearchClause();
        ArrayList<MediaMetadata> list;
        if (searchClause[0] == null) {
            media.loadGallery(galleryTable);
            return;
        }
        if (searchClause[0].equals("searchLikeThis")) {
            list = media.searchByExample(profileData.getMetadataCopy());
        } else if (searchClause[0].equals("fullTextSearch")) {
            if (searchClause[1].equals(""))
                return;
            list = media.searchFullText(searchClause[1]);
        } else if (searchClause[0].equals("searchByProfile")) {
            if (searchByProfile == false) {
                searchByProfile = true;
                mediaDisplayController.printText(MediaDisplayScreen, "Enter search info on the right side");
                editBtnWidgetSelected(evt);
                list = galleryTable.getList();
            } else {
                searchByProfile = false;
                list = media.searchByExample(profileData.getModifiedMetadata());
            }
        } else {
            if (searchClause[1].equals(""))
                return;
            list = media.search(searchClause[0], searchClause[1]);
        }

        galleryTable.setContentList(list);
        int i = galleryTable.getSelectedImageId();
        if (i > -1) {
            galleryTable.selectImage(i);
            mediaDisplayController.show(MediaDisplayScreen, galleryTable.getGridFSDBFile(),
                    galleryTable.getMetadata());
            profileData.setMetadata(galleryTable.getMetadata());
            profileData.update();
        }

    }
}