package com.quantum.properties; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.quantum.Messages; import com.quantum.QuantumPlugin; import com.quantum.model.Entity; import com.quantum.model.Index; import com.quantum.model.NotConnectedException; import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.ITableLabelProvider; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; public class IndexPropertyPage extends BaseEntityPropertyPage { class IndexAdaptor { private final Index index; private final int columnNumber; public IndexAdaptor(Index index, int columnNumber) { this.index = index; this.columnNumber = columnNumber; } public Index getIndex() { return this.index; } public int getColumnNumber() { return this.columnNumber; } } protected void createInformationArea(Composite composite) { if (!Entity.SEQUENCE_TYPE.equals(getEntity().getType())) { try { Index[] indeces = getEntity().getIndexes(); Table table = new Table(composite, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER); table.setHeaderVisible(true); GridData gridData = new GridData(GridData.FILL_BOTH); gridData.horizontalSpan = 2; table.setLayoutData(gridData); for (int i = 0, length = 3; i < length; i++) { TableColumn column = new TableColumn(table, SWT.NONE); column.setText(Messages.getString(getClass(), "column" + i)); } TableViewer viewer = new TableViewer(table); setTableContents(viewer, indeces); setColumnWidths(table); } catch (NotConnectedException e) { createErrorMessage(composite, e); } catch (SQLException e) { createErrorMessage(composite, e); } catch (RuntimeException e) { createErrorMessage(composite, e); } } } private void setTableContents(TableViewer viewer, Index[] indeces) { viewer.setContentProvider(new IStructuredContentProvider() { public Object[] getElements(Object inputElement) { if (inputElement instanceof Index[]) { Index[] indeces = (Index[]) inputElement; List list = new ArrayList(); for (int i = 0, length = indeces == null ? 0 : indeces.length; i < length; i++) { for (int j = 0, columns = indeces[i].getNumberOfColumns(); j < columns; j++) { list.add(new IndexAdaptor(indeces[i], j)); } } return list.toArray(); } else { return null; } } public void dispose() { } public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { } }); viewer.setLabelProvider(new ITableLabelProvider() { public Image getColumnImage(Object element, int columnIndex) { if (columnIndex == 1 && element instanceof IndexAdaptor) { return QuantumPlugin.getImage("column.gif"); } else { return null; } } public String getColumnText(Object element, int columnIndex) { String label = null; if (element instanceof IndexAdaptor) { IndexAdaptor index = (IndexAdaptor) element; switch (columnIndex) { case 0: if (index.getColumnNumber() == 0) { label = index.getIndex().getName(); } break; case 1: label = index.getIndex().getColumnName(index.getColumnNumber()); break; case 2: if (index.getIndex().isAscending(index.getColumnNumber())) { label = Messages.getString(IndexPropertyPage.class, "ascending"); } else if (index.getIndex().isDescending(index.getColumnNumber())) { label = Messages.getString(IndexPropertyPage.class, "descending"); } break; default: } } return label == null ? "" : label; } public void addListener(ILabelProviderListener listener) { } public void dispose() { } public boolean isLabelProperty(Object element, String property) { return false; } public void removeListener(ILabelProviderListener listener) { } }); viewer.setInput(indeces); } }