synchronize with newest Quantum CVS sources
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / properties / IndexPropertyPage.java
1 package com.quantum.properties;
2
3 import java.sql.SQLException;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import com.quantum.ImageStore;
8 import com.quantum.Messages;
9 import com.quantum.model.Entity;
10 import com.quantum.model.Index;
11 import com.quantum.model.NotConnectedException;
12
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;
24
25 public class IndexPropertyPage extends BaseEntityPropertyPage {
26         
27         class IndexAdaptor {
28                 private final Index index;
29                 private final int columnNumber;
30                 public IndexAdaptor(Index index, int columnNumber) {
31                         this.index = index;
32                         this.columnNumber = columnNumber;
33                 }
34                 public Index getIndex() {
35                         return this.index;
36                 }
37                 public int getColumnNumber() {
38                         return this.columnNumber;
39                 }
40         }
41     
42         protected void createInformationArea(Composite composite) {
43         if (!Entity.SEQUENCE_TYPE.equals(getEntity().getType())) {
44                         try {
45                                 Index[] indeces = getEntity().getIndexes();
46                         
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));
55                     }
56                         
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);
66                         }
67         }
68         }
69
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));
79                                                 }
80                                         }
81                                         return list.toArray();
82                                 } else {
83                                         return null;
84                                 }
85                         }
86                         public void dispose() {
87                         }
88                         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
89                         }
90                 });
91         
92                 viewer.setLabelProvider(new ITableLabelProvider() {
93
94                         public Image getColumnImage(Object element, int columnIndex) {
95                                 if (columnIndex == 1 && element instanceof IndexAdaptor) {
96                                         return ImageStore.getImage(ImageStore.COLUMN);
97                                 } else {
98                                         return null;
99                                 }
100                         }
101                         public String getColumnText(Object element, int columnIndex) {
102                                 String label = null;
103                                 if (element instanceof IndexAdaptor) {
104                                         IndexAdaptor index = (IndexAdaptor) element;
105                                         switch (columnIndex) {
106                                         case 0:
107                                                 if (index.getColumnNumber() == 0) {
108                                                         label = index.getIndex().getName();
109                                                 }
110                                                 break;
111                                         case 1: 
112                                                 label = index.getIndex().getColumnName(index.getColumnNumber());
113                                                 break;
114                                         case 2: 
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");
119                                                 }
120                                                 break;
121                                         default:
122                                         }
123                                 }
124                                 
125                                 return label == null ? "" : label;
126                         }
127                         public void addListener(ILabelProviderListener listener) {
128                         }
129                         public void dispose() {
130                         }
131                         public boolean isLabelProperty(Object element, String property) {
132                                 return false;
133                         }
134                         public void removeListener(ILabelProviderListener listener) {
135                         }
136                 });
137                 
138                 
139                 viewer.setInput(indeces);
140         }
141 }