com.hangum.tadpole.rdb.core.dialog.commons.MapViewerDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.hangum.tadpole.rdb.core.dialog.commons.MapViewerDialog.java

Source

/*******************************************************************************
 * Copyright (c) 2016 hangum.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     hangum - initial API and implementation
 ******************************************************************************/
package com.hangum.tadpole.rdb.core.dialog.commons;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import com.hangum.tadpole.commons.util.GlobalImageUtils;
import com.hangum.tadpole.engine.query.dao.system.userdb.DBOtherDAO;
import com.hangum.tadpole.mongodb.core.Messages;

/**
 * Map dialog
 * 
 * @author hangum
 *
 */
public class MapViewerDialog extends Dialog {
    private String strGroupName;
    private DBOtherDAO dbOtherDao;
    private TableViewer tableViewer;

    /**
     * Create the dialog.
     * @param parentShell
     */
    public MapViewerDialog(Shell parentShell, String strGroupName, DBOtherDAO dao) {
        super(parentShell);
        setShellStyle(SWT.RESIZE | SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);

        this.strGroupName = strGroupName;
        this.dbOtherDao = dao;
    }

    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        if (!"".equals(strGroupName))
            newShell.setText(String.format("%s [%s] information", strGroupName, dbOtherDao.getName()));
        else
            newShell.setText(String.format("%s information", dbOtherDao.getName()));
        newShell.setImage(GlobalImageUtils.getTadpoleIcon());
    }

    /**
     * Create contents of the dialog.
     * @param parent
     */
    @Override
    protected Control createDialogArea(Composite parent) {
        Composite container = (Composite) super.createDialogArea(parent);
        GridLayout gridLayout = (GridLayout) container.getLayout();
        gridLayout.verticalSpacing = 3;
        gridLayout.horizontalSpacing = 3;
        gridLayout.marginHeight = 3;
        gridLayout.marginWidth = 3;

        Composite composite = new Composite(container, SWT.NONE);
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        composite.setLayout(new GridLayout(1, false));

        tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
        Table table = tableViewer.getTable();
        table.setLinesVisible(true);
        table.setHeaderVisible(true);
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

        TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
        TableColumn tblclmnName = tableViewerColumn.getColumn();
        tblclmnName.setWidth(100);
        tblclmnName.setText("Name");

        TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
        TableColumn tblclmnValue = tableViewerColumn_1.getColumn();
        tblclmnValue.setWidth(312);
        tblclmnValue.setText("Value");

        tableViewer.setContentProvider(new ArrayContentProvider());
        tableViewer.setLabelProvider(new MapLabelProvider());
        initData();

        return container;
    }

    /**
     * init data
     */
    private void initData() {
        Map mapData = (Map) dbOtherDao.getUserObject();
        List listMap = new ArrayList<>();

        Set<String> setKey = mapData.keySet();
        for (String string : setKey) {
            listMap.add(new KeyValueObj(string, "" + mapData.get(string)));
        }

        tableViewer.setInput(listMap);
    }

    /**
     * Create contents of the button bar.
     * @param parent
     */
    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        createButton(parent, IDialogConstants.OK_ID, Messages.get().Close, true);
    }

    /**
     * Return the initial size of the dialog.
     */
    @Override
    protected Point getInitialSize() {
        return new Point(450, 500);
    }
}

/**
 * map key value object
 * @author hangum
 *
 */
class KeyValueObj {
    String name = "";
    String value = "";

    public KeyValueObj(String name, String value) {
        this.name = name;
        this.value = value;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the value
     */
    public String getValue() {
        return value;
    }

    /**
     * @param value the value to set
     */
    public void setValue(String value) {
        this.value = value;
    }

}

/**
 * MapLabel Provider
 * @author hangum
 *
 */
class MapLabelProvider extends LabelProvider implements ITableLabelProvider {

    @Override
    public Image getColumnImage(Object element, int columnIndex) {
        return null;
    }

    @Override
    public String getColumnText(Object element, int columnIndex) {
        KeyValueObj obj = (KeyValueObj) element;
        if (columnIndex == 0)
            return obj.getName();
        return obj.getValue();
    }

}