added file extension php5
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / properties / ReferencesPropertyPage.java
1 package com.quantum.properties;
2
3 import java.sql.DatabaseMetaData;
4 import java.sql.SQLException;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import com.quantum.ImageStore;
9 import com.quantum.Messages;
10 import com.quantum.model.Entity;
11 import com.quantum.model.ForeignKey;
12 import com.quantum.model.NotConnectedException;
13
14 import org.eclipse.jface.viewers.ILabelProviderListener;
15 import org.eclipse.jface.viewers.IStructuredContentProvider;
16 import org.eclipse.jface.viewers.ITableLabelProvider;
17 import org.eclipse.jface.viewers.TableViewer;
18 import org.eclipse.jface.viewers.Viewer;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Table;
24 import org.eclipse.swt.widgets.TableColumn;
25
26 public class ReferencesPropertyPage extends BaseEntityPropertyPage {
27         
28         class ColumnDetailsAdaptor {
29                 private final int columnNumber;
30                 private final ForeignKey foreignKey;
31
32                 public ColumnDetailsAdaptor(ForeignKey foreignKey, int columnNumber) {
33                         this.foreignKey = foreignKey;
34                         this.columnNumber = columnNumber;
35                 }
36                 public int getColumnNumber() {
37                         return this.columnNumber;
38                 }
39                 public ForeignKey getForeignKey() {
40                         return this.foreignKey;
41                 }
42         }
43     
44         class TableDetailsAdaptor {
45                 private final ForeignKey foreignKey;
46
47                 public TableDetailsAdaptor(ForeignKey foreignKey) {
48                         this.foreignKey = foreignKey;
49                 }
50                 public ForeignKey getForeignKey() {
51                         return this.foreignKey;
52                 }
53         }
54     
55         protected void createInformationArea(Composite composite) {
56         if (!Entity.SEQUENCE_TYPE.equals(getEntity().getType())) {
57                         try {
58                                 ForeignKey[] references = getEntity().getReferences();
59                 
60                         Table table = new Table(composite, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
61                     table.setHeaderVisible(true);
62                     GridData gridData = new GridData(GridData.FILL_BOTH);
63                     gridData.horizontalSpan = 2;
64                     table.setLayoutData(gridData);
65                     for (int i = 0, length = 4; i < length; i++) {
66                         TableColumn column = new TableColumn(table, SWT.NONE);
67                         column.setText(Messages.getString(getClass(), "column" + i));
68                     }
69                         
70                         TableViewer viewer = new TableViewer(table);
71                     setTableContents(viewer, references);
72                     setColumnWidths(table);
73                         } catch (NotConnectedException e) {
74                                 createErrorMessage(composite, e);
75                         } catch (SQLException e) {
76                                 createErrorMessage(composite, e);
77                         } catch (RuntimeException e) {
78                                 createErrorMessage(composite, e);
79                         }
80         }
81         }
82
83         /**
84          * @param viewer
85          */
86         private void setTableContents(TableViewer viewer, ForeignKey[] references) {
87                 viewer.setContentProvider(new IStructuredContentProvider() {
88
89                         public Object[] getElements(Object inputElement) {
90                                 List list = new ArrayList();
91                                 if (inputElement instanceof ForeignKey[]) {
92                                         ForeignKey[] keys = (ForeignKey[]) inputElement;
93                                         for (int i = 0, length = keys == null ? 0
94                                                         : keys.length; i < length; i++) {
95                                                 list.add(new TableDetailsAdaptor(keys[i]));
96                                                 for (int j = 0, columns = keys[i].getNumberOfColumns(); j < columns; j++) {
97                                                         list.add(new ColumnDetailsAdaptor(keys[i], j));
98                                                 }
99                                         }
100                                 }
101                                 return list.toArray();
102                         }
103                         public void dispose() {
104                         }
105                         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
106                         }
107                 });
108                 
109                 viewer.setLabelProvider(new ITableLabelProvider() {
110
111                         public Image getColumnImage(Object element, int columnIndex) {
112                                 if ((columnIndex == 1 || columnIndex == 2) 
113                                                 && element instanceof ColumnDetailsAdaptor) {
114                                         return ImageStore.getImage(ImageStore.COLUMN);
115                                 } else if ((columnIndex == 1 || columnIndex == 2) 
116                                                 && element instanceof TableDetailsAdaptor) {
117                                         return ImageStore.getImage(ImageStore.TABLE);
118                                 } else if ((columnIndex == 0) 
119                                                 && element instanceof TableDetailsAdaptor) {
120                                         return ImageStore.getImage(ImageStore.FOREIGNKEY);
121                                 } else {
122                                         return null;
123                                 }
124                         }
125                         public String getColumnText(Object element, int columnIndex) {
126                                 String label = null;
127                                 if (element instanceof TableDetailsAdaptor) {
128                                         label = getColumnText((TableDetailsAdaptor) element,  columnIndex);
129                                 } else if (element instanceof ColumnDetailsAdaptor) {
130                                                 label = getColumnText((ColumnDetailsAdaptor) element,  columnIndex);
131                                 }
132                                 
133                                 return label == null ? "" : label;
134                         }
135                         private String getColumnText(TableDetailsAdaptor adaptor,  int columnIndex) {
136                                 switch (columnIndex) {
137                                 case 0:
138                                         return adaptor.getForeignKey().getName();
139                                 case 1:
140                                         return adaptor.getForeignKey().getLocalEntityQualifiedName();
141                                 case 2:
142                                         return adaptor.getForeignKey().getForeignEntityQualifiedName();
143                                 case 3:
144                                         ForeignKey key = adaptor.getForeignKey();
145                                         if (key.getDeleteRule() == DatabaseMetaData.importedKeyCascade) {
146                                                 return Messages.getString(
147                                                                 ReferencesPropertyPage.class, "importedKeyCascade");
148                                         } else if (key.getDeleteRule() == DatabaseMetaData.importedKeyNoAction) {
149                                                         return Messages.getString(
150                                                                         ReferencesPropertyPage.class, "importedKeyNoAction");
151                                         } else if (key.getDeleteRule() == DatabaseMetaData.importedKeyRestrict) {
152                                                 return Messages.getString(
153                                                                 ReferencesPropertyPage.class, "importedKeyRestrict");
154                                         } else if (key.getDeleteRule() == DatabaseMetaData.importedKeySetDefault) {
155                                                 return Messages.getString(
156                                                                 ReferencesPropertyPage.class, "importedKeySetDefault");
157                                         } else if (key.getDeleteRule() == DatabaseMetaData.importedKeySetNull) {
158                                                 return Messages.getString(
159                                                                 ReferencesPropertyPage.class, "importedKeySetNull");
160                                         } else {
161                                                 return null;
162                                         }
163                                 default:
164                                         return null;
165                                 }
166                         }
167                         private String getColumnText(ColumnDetailsAdaptor adaptor,  int columnIndex) {
168                                 switch (columnIndex) {
169                                 case 1:
170                                         return adaptor.getForeignKey().getLocalColumnName(adaptor.getColumnNumber());
171                                 case 2:
172                                         return adaptor.getForeignKey().getForeignColumnName(adaptor.getColumnNumber());
173                                 default:
174                                         return null;
175                                 }
176                         }
177                         public void addListener(ILabelProviderListener listener) {
178                         }
179                         public void dispose() {
180                         }
181                         public boolean isLabelProperty(Object element, String property) {
182                                 return false;
183                         }
184                         public void removeListener(ILabelProviderListener listener) {
185                         }
186                 });
187                 
188                 viewer.setInput(references);
189         }
190 }