com.hangum.tadpole.util.tables.SQLResultLabelProvider.java Source code

Java tutorial

Introduction

Here is the source code for com.hangum.tadpole.util.tables.SQLResultLabelProvider.java

Source

/*******************************************************************************
 * Copyright (c) 2012 Cho Hyun Jong.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Cho Hyun Jong - initial API and implementation
 ******************************************************************************/
package com.hangum.tadpole.util.tables;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
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.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

/**
 * SQLResult? LabelProvider
 * 
 * @author hangum
 *
 */
public class SQLResultLabelProvider extends LabelProvider implements ITableLabelProvider {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(SQLResultLabelProvider.class);

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

    @SuppressWarnings("unchecked")
    public String getColumnText(Object element, int columnIndex) {
        HashMap<Integer, String> rsResult = (HashMap<Integer, String>) element;

        return rsResult.get(columnIndex);
    }

    /**
     * table? Column? ?.
     */
    public static void createTableColumn(final TableViewer tableViewer, final HashMap<Integer, String> mapColumns,
            final Map<Integer, Integer> mapColumnType, final SQLResultSorter tableSorter) {
        //  column? .
        Table table = tableViewer.getTable();
        int columnCount = table.getColumnCount();
        for (int i = 0; i < columnCount; i++) {
            table.getColumn(0).dispose();
        }

        try {
            for (int i = 0; i < mapColumns.size(); i++) {
                final int index = i;

                final TableViewerColumn tv = new TableViewerColumn(tableViewer, SWT.NONE);
                final TableColumn tc = tv.getColumn();
                tc.setText(mapColumns.get(i));

                tc.setResizable(true);
                tc.setMoveable(true);

                tc.addSelectionListener(new SelectionAdapter() {
                    @Override
                    public void widgetSelected(SelectionEvent e) {
                        tableSorter.setColumn(index);
                        int dir = tableViewer.getTable().getSortDirection();
                        if (tableViewer.getTable().getSortColumn() == tc) {
                            dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
                        } else {
                            dir = SWT.DOWN;
                        }
                        tableViewer.getTable().setSortDirection(dir);
                        tableViewer.getTable().setSortColumn(tc);
                        tableViewer.refresh();
                    }
                });

            } // end for

        } catch (Exception e) {
            logger.error("SQLResult TableViewer", e);
        }
    }

}