1 package com.quantum.properties;
3 import java.sql.DatabaseMetaData;
4 import java.sql.SQLException;
5 import java.util.ArrayList;
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;
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;
26 public class ReferencesPropertyPage extends BaseEntityPropertyPage {
28 class ColumnDetailsAdaptor {
29 private final int columnNumber;
30 private final ForeignKey foreignKey;
32 public ColumnDetailsAdaptor(ForeignKey foreignKey, int columnNumber) {
33 this.foreignKey = foreignKey;
34 this.columnNumber = columnNumber;
36 public int getColumnNumber() {
37 return this.columnNumber;
39 public ForeignKey getForeignKey() {
40 return this.foreignKey;
44 class TableDetailsAdaptor {
45 private final ForeignKey foreignKey;
47 public TableDetailsAdaptor(ForeignKey foreignKey) {
48 this.foreignKey = foreignKey;
50 public ForeignKey getForeignKey() {
51 return this.foreignKey;
55 protected void createInformationArea(Composite composite) {
56 if (!Entity.SEQUENCE_TYPE.equals(getEntity().getType())) {
58 ForeignKey[] references = getEntity().getReferences();
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));
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);
86 private void setTableContents(TableViewer viewer, ForeignKey[] references) {
87 viewer.setContentProvider(new IStructuredContentProvider() {
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));
101 return list.toArray();
103 public void dispose() {
105 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
109 viewer.setLabelProvider(new ITableLabelProvider() {
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);
125 public String getColumnText(Object element, int columnIndex) {
127 if (element instanceof TableDetailsAdaptor) {
128 label = getColumnText((TableDetailsAdaptor) element, columnIndex);
129 } else if (element instanceof ColumnDetailsAdaptor) {
130 label = getColumnText((ColumnDetailsAdaptor) element, columnIndex);
133 return label == null ? "" : label;
135 private String getColumnText(TableDetailsAdaptor adaptor, int columnIndex) {
136 switch (columnIndex) {
138 return adaptor.getForeignKey().getName();
140 return adaptor.getForeignKey().getLocalEntityQualifiedName();
142 return adaptor.getForeignKey().getForeignEntityQualifiedName();
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");
167 private String getColumnText(ColumnDetailsAdaptor adaptor, int columnIndex) {
168 switch (columnIndex) {
170 return adaptor.getForeignKey().getLocalColumnName(adaptor.getColumnNumber());
172 return adaptor.getForeignKey().getForeignColumnName(adaptor.getColumnNumber());
177 public void addListener(ILabelProviderListener listener) {
179 public void dispose() {
181 public boolean isLabelProperty(Object element, String property) {
184 public void removeListener(ILabelProviderListener listener) {
188 viewer.setInput(references);