1 package com.quantum.properties;
3 import java.sql.SQLException;
4 import java.util.ArrayList;
7 import com.quantum.Messages;
8 import com.quantum.QuantumPlugin;
9 import com.quantum.model.Entity;
10 import com.quantum.model.Index;
11 import com.quantum.model.NotConnectedException;
13 import org.eclipse.jface.viewers.ILabelProviderListener;
14 import org.eclipse.jface.viewers.IStructuredContentProvider;
15 import org.eclipse.jface.viewers.ITableLabelProvider;
16 import org.eclipse.jface.viewers.TableViewer;
17 import org.eclipse.jface.viewers.Viewer;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Table;
23 import org.eclipse.swt.widgets.TableColumn;
25 public class IndexPropertyPage extends BaseEntityPropertyPage {
28 private final Index index;
29 private final int columnNumber;
30 public IndexAdaptor(Index index, int columnNumber) {
32 this.columnNumber = columnNumber;
34 public Index getIndex() {
37 public int getColumnNumber() {
38 return this.columnNumber;
42 protected void createInformationArea(Composite composite) {
43 if (!Entity.SEQUENCE_TYPE.equals(getEntity().getType())) {
45 Index[] indeces = getEntity().getIndexes();
47 Table table = new Table(composite, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
48 table.setHeaderVisible(true);
49 GridData gridData = new GridData(GridData.FILL_BOTH);
50 gridData.horizontalSpan = 2;
51 table.setLayoutData(gridData);
52 for (int i = 0, length = 3; i < length; i++) {
53 TableColumn column = new TableColumn(table, SWT.NONE);
54 column.setText(Messages.getString(getClass(), "column" + i));
57 TableViewer viewer = new TableViewer(table);
58 setTableContents(viewer, indeces);
59 setColumnWidths(table);
60 } catch (NotConnectedException e) {
61 createErrorMessage(composite, e);
62 } catch (SQLException e) {
63 createErrorMessage(composite, e);
64 } catch (RuntimeException e) {
65 createErrorMessage(composite, e);
70 private void setTableContents(TableViewer viewer, Index[] indeces) {
71 viewer.setContentProvider(new IStructuredContentProvider() {
72 public Object[] getElements(Object inputElement) {
73 if (inputElement instanceof Index[]) {
74 Index[] indeces = (Index[]) inputElement;
75 List list = new ArrayList();
76 for (int i = 0, length = indeces == null ? 0 : indeces.length; i < length; i++) {
77 for (int j = 0, columns = indeces[i].getNumberOfColumns(); j < columns; j++) {
78 list.add(new IndexAdaptor(indeces[i], j));
81 return list.toArray();
86 public void dispose() {
88 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
92 viewer.setLabelProvider(new ITableLabelProvider() {
94 public Image getColumnImage(Object element, int columnIndex) {
95 if (columnIndex == 1 && element instanceof IndexAdaptor) {
96 return QuantumPlugin.getImage("column.gif");
101 public String getColumnText(Object element, int columnIndex) {
103 if (element instanceof IndexAdaptor) {
104 IndexAdaptor index = (IndexAdaptor) element;
105 switch (columnIndex) {
107 if (index.getColumnNumber() == 0) {
108 label = index.getIndex().getName();
112 label = index.getIndex().getColumnName(index.getColumnNumber());
115 if (index.getIndex().isAscending(index.getColumnNumber())) {
116 label = Messages.getString(IndexPropertyPage.class, "ascending");
117 } else if (index.getIndex().isDescending(index.getColumnNumber())) {
118 label = Messages.getString(IndexPropertyPage.class, "descending");
125 return label == null ? "" : label;
127 public void addListener(ILabelProviderListener listener) {
129 public void dispose() {
131 public boolean isLabelProperty(Object element, String property) {
134 public void removeListener(ILabelProviderListener listener) {
139 viewer.setInput(indeces);