misc
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / properties / DatabaseInformationPropertyPage.java
1 package com.quantum.properties;
2
3 import java.sql.SQLException;
4
5 import com.quantum.Messages;
6 import com.quantum.QuantumPlugin;
7 import com.quantum.model.Bookmark;
8 import com.quantum.model.DataType;
9 import com.quantum.model.NotConnectedException;
10 import com.quantum.view.bookmark.TreeNode;
11
12 import org.eclipse.jface.viewers.ILabelProviderListener;
13 import org.eclipse.jface.viewers.IStructuredContentProvider;
14 import org.eclipse.jface.viewers.ITableLabelProvider;
15 import org.eclipse.jface.viewers.TableViewer;
16 import org.eclipse.jface.viewers.Viewer;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Table;
25 import org.eclipse.swt.widgets.TableColumn;
26 import org.eclipse.ui.dialogs.PropertyPage;
27
28 public class DatabaseInformationPropertyPage extends PropertyPage {
29
30         public DatabaseInformationPropertyPage() {
31                 super();
32         }
33
34     protected Control createContents(Composite parent) {
35
36         Composite composite = new Composite(parent, SWT.NONE);
37         GridLayout layout = new GridLayout();
38         layout.numColumns = 2;
39         composite.setLayout(layout);
40         GridData data = new GridData(GridData.FILL_BOTH);
41         composite.setLayoutData(data);
42         Bookmark bookmark =
43             ((TreeNode) getElement()).getBookmark();
44
45                 createDatabaseNameArea(composite, bookmark);
46         createTypesArea(composite, bookmark);
47         
48         return composite;
49         }
50
51         /**
52          * @param composite
53          * @param bookmark
54          */
55         private void createTypesArea(Composite composite, Bookmark bookmark) {
56                 if (bookmark.isConnected()) {
57                 try {
58                         DataType[] dataTypes = bookmark.getDatabase().getTypes();
59                         Label label = new Label(composite, SWT.NONE);
60                         label.setText(Messages.getString(getClass(), "dataTypes"));
61                         GridData data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_BEGINNING);
62                         data.horizontalSpan = 2;
63                         label.setLayoutData(data);
64                 
65                         Table table = new Table(composite, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
66                 table.setHeaderVisible(true);
67                 GridData gridData = new GridData(GridData.FILL_BOTH);
68                 gridData.horizontalSpan = 2;
69                 gridData.heightHint = 200;
70                 table.setLayoutData(gridData);
71                 for (int i = 0, length = 2; i < length; i++) {
72                     TableColumn column = new TableColumn(table, SWT.NONE);
73                     column.setText(Messages.getString(getClass(), "column" + i));
74                 }
75                         TableViewer viewer = new TableViewer(table);
76                         viewer.setContentProvider(new IStructuredContentProvider() {
77                                         public Object[] getElements(Object inputElement) {
78                                                 if (inputElement instanceof DataType[]) {
79                                                         return (DataType[]) inputElement;
80                                                 } else {
81                                                         return null;
82                                                 }
83                                         }
84                                         public void dispose() {
85                                         }
86                                         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
87                                         }
88                         });
89                         viewer.setLabelProvider(new ITableLabelProvider() {
90                                         public Image getColumnImage(Object element, int columnIndex) {
91                                                 return null;
92                                         }
93                                         public String getColumnText(Object element, int columnIndex) {
94                                                 String result = null;
95                                                 if (element != null && element instanceof DataType) {
96                                                         DataType dataType = (DataType) element;
97                                                         switch (columnIndex) {
98                                                         case 0:
99                                                                 result = dataType.getDatabaseTypeName();
100                                                                 break;
101                                                         case 1:
102                                                                 result = dataType.getJavaNameType();
103                                                                 break;
104                                                         default:
105                                                                 result = null;
106                                                         }
107                                                 }
108                                                 return result == null ? "" : result;
109                                         }
110                                         public void addListener(ILabelProviderListener listener) {
111                                         }
112                                         public void dispose() {
113                                         }
114                                         public boolean isLabelProperty(Object element, String property) {
115                                                 return false;
116                                         }
117                                         public void removeListener(ILabelProviderListener listener) {
118                                         }
119                         });
120                         
121                         viewer.setInput(dataTypes);
122                         
123                 for (int i = 0, length = table.getColumnCount(); i < length; i++) {
124                     table.getColumn(i).pack();
125                 }
126                 
127                 } catch (SQLException e) {
128                         createErrorMessage(composite, e);
129                 } catch (NotConnectedException e) {
130                         createErrorMessage(composite, e);
131                 } catch (RuntimeException e) {
132                         createErrorMessage(composite, e);
133                         }
134         }
135         }
136
137         /**
138          * @param composite
139          */
140         private void createErrorMessage(Composite composite, Exception e) {
141                 Label icon = new Label(composite, SWT.NONE);
142                 icon.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
143                 icon.setImage(QuantumPlugin.getImage("warning.gif"));
144                 
145                 Label error = new Label(composite, SWT.NONE);
146                 error.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING));
147                 error.setText(Messages.getString(getClass(), "error") 
148                                 + (e.getMessage() == null ? "" : "\n" + e.getMessage()));
149         }
150
151         /**
152          * @param composite
153          * @param bookmark
154          */
155         private void createDatabaseNameArea(Composite composite, Bookmark bookmark) {
156                 Label productLabel = new Label(composite, SWT.NONE);
157                 productLabel.setText(Messages.getString(getClass(), "product"));
158
159         Label productDescriptionLabel = new Label(composite, SWT.NONE);
160
161         String description = null;
162         if (bookmark.isConnected()) {
163                 try {
164                     description = bookmark.getDatabase().getInformation();
165                 } catch (NotConnectedException e) {
166                         createErrorMessage(composite, e);
167                 } catch (SQLException e) {
168                         createErrorMessage(composite, e);
169                 } catch (RuntimeException e) {
170                         createErrorMessage(composite, e);
171                 }
172         }
173         if (description == null) {
174             description = Messages.getString(getClass(), "unknown");
175         }
176         productDescriptionLabel.setText(description);
177         }
178
179         protected void performDefaults() {
180         }
181         
182         public boolean performOk() {
183                 return true;
184         }
185
186 }