17d7b296140dc8b006d3d8e587b9d1241b2adc10
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / tableview / TableAdapter.java
1 package com.quantum.view.tableview;
2
3 import java.sql.SQLException;
4 import java.util.Vector;
5
6 import com.quantum.Messages;
7 import com.quantum.adapters.AdapterFactory;
8 import com.quantum.adapters.DatabaseAdapter;
9 import com.quantum.model.Bookmark;
10 import com.quantum.model.Entity;
11 import com.quantum.model.NotConnectedException;
12 import com.quantum.sql.FilterSort;
13 import com.quantum.sql.SQLHelper;
14 import com.quantum.sql.SQLResults;
15
16 import org.eclipse.jface.viewers.CellEditor;
17 import org.eclipse.jface.viewers.TableViewer;
18 import org.eclipse.jface.viewers.TextCellEditor;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.widgets.Table;
21 import org.eclipse.swt.widgets.TableColumn;
22 import org.eclipse.swt.widgets.TableItem;
23 import org.eclipse.swt.widgets.Text;
24
25 public class TableAdapter {
26         
27         private Table table = null;
28         
29         public static final String DEFAULT = ""; //$NON-NLS-1$
30         public static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
31         public static final String UTF_16 = "UTF-16"; //$NON-NLS-1$
32         
33         private int pageSize = DefaultSizes.PAGE_SIZE;
34         private int maxColumnSize = DefaultSizes.MAX_COLUMN_SIZE;
35         private FilterSort extra = new FilterSort();
36         private int offset = 1;
37         private int totalSize = -1;
38         private Vector rows = new Vector();
39         private Vector columnNames = new Vector();
40         private boolean hasMore = false;
41         
42         private Bookmark bookmark = null;       
43     private Entity entity = null;
44         private String query;
45         
46         private String encoding = ""; //$NON-NLS-1$
47         
48     private TableAdapter(Entity entity) {
49         this.entity = entity;
50         this.bookmark = entity.getBookmark();
51     }
52         private TableAdapter(Bookmark bookmark) {
53                 this.bookmark = bookmark;
54         }
55         public void fullMode() {
56                 offset = 1;
57                 pageSize = Integer.MAX_VALUE;
58         }
59         public void resetMode() {
60                 offset = 1;
61                 pageSize = DefaultSizes.PAGE_SIZE;
62         }
63         public static TableAdapter createFromQuery(Bookmark bookmark, SQLResults results) throws NotConnectedException {
64                 TableAdapter retVal = new TableAdapter(bookmark);
65                 retVal.setQuery(results.getQuery());
66                 retVal.setData(results);
67                 return retVal;
68         }
69         public static TableAdapter createFromTable(Entity entity) {
70                 TableAdapter retVal = new TableAdapter(entity); 
71                 return retVal;
72         }
73         private void loadSize() {
74         try {
75                 if (entity != null) {
76                 totalSize = SQLHelper.getSize(
77                     bookmark.getConnection(), entity.getCondQualifiedName(), 
78                         AdapterFactory.getInstance().getAdapter(bookmark.getType()));
79                 }
80         } catch (SQLException e) {
81             e.printStackTrace();
82         } catch (NotConnectedException e) {
83             e.printStackTrace();
84         }
85         }
86         public int getStartIndex() {
87                 if (totalSize == 0) {
88                         return 0;
89                 }
90                 return offset;
91         }
92         public int getEndIndex() {
93                 return offset + rows.size() - 1;
94         }
95         public int getTotalSize() {
96                 return totalSize;
97         }
98         public void nextPage() {
99                 loadSize();
100                 offset = offset + pageSize;
101                 if (totalSize >= 0 && offset > totalSize) {
102                         offset = offset - pageSize;
103                 }
104         }
105         public void previousPage() {
106                 offset = offset - pageSize;
107                 if (offset < 1) {
108                         offset = 1;
109                 }
110         }
111         public boolean hasNextPage() {
112                 if (entity != null) {
113                         if (offset + pageSize <= totalSize) {
114                                 return true;
115                         }
116                         return false;
117                 }
118                 return hasMore;
119         }
120         public boolean hasPreviousPage() {
121                 if (offset > 1) {
122                         return true;
123                 }
124                 return false;
125         }
126
127         public String getQuery() {
128                 if (entity != null) {
129                         DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(bookmark.getType());
130                         if (adapter == null) throw new RuntimeException();
131                         else return adapter.getTableQuery((entity).getCondQualifiedName()) + extra.toString();
132                 }
133                 return query;
134         }
135         public void loadData() throws NotConnectedException {
136                 loadSize();
137                 if (entity != null) {
138                         if (offset > totalSize) {
139                                 offset = 1;
140                         }
141                 }
142                 String query = getQuery();
143                 System.out.println(offset + Messages.getString("TableAdapter.to") + (offset + pageSize - 1)); //$NON-NLS-1$
144                 SQLResults results = SQLHelper.getResults(bookmark.getConnection(), query, offset, offset + pageSize - 1, maxColumnSize, encoding);
145                 setData(results);
146         }
147         public void resetOffset() {
148                 offset = 1;
149         }
150         public void setData(SQLResults results) throws NotConnectedException {
151         if (results.isError()) return;
152                 int rowCount = results.getRowCount();
153                 int columnCount = results.getColumnCount();
154                 rows = new Vector();
155                 columnNames = new Vector();
156                 for (int col = 1; col <= columnCount; col++) {
157                         columnNames.addElement(results.getColumnName(col));
158                 }
159                 for (int row = 1; row <= rowCount; row++) {
160                         String rowData[] = new String[columnCount];
161                         for (int col = 1; col <= columnCount; col++) {
162                                 rowData[col - 1] = results.getElement(col, row).toString();
163                         }
164                         rows.addElement(rowData);
165                 }
166                 hasMore = results.hasMore();
167                 if (entity == null && results.getMaxSize() >= 0) {
168                         if (offset > results.getMaxSize()) {
169                                 offset = 1;
170                                 loadData();
171                         }
172                 }
173         }
174         public void loadTable(Table table) {
175                 this.table = table;
176                 table.setHeaderVisible(true);
177                 for (int i = 0; i < columnNames.size(); i++) {
178                         TableColumn column = new TableColumn(table, SWT.NONE);
179                         column.setText(columnNames.elementAt(i).toString());
180                 }
181                 for (int i = 0; i < columnNames.size(); i++) {
182                         table.getColumn(i).pack();
183                 }
184                 for (int row = 0; row < rows.size(); row++) {
185                         TableItem item = new TableItem(table, SWT.NONE);
186                         String itemData[] = (String[]) rows.elementAt(row);
187                         item.setText(itemData);
188                 }
189                 for (int i = 0; i < columnNames.size(); i++) {
190                         table.getColumn(i).pack();
191                 }
192         }
193         
194         public TableViewer addTableViewer(Table table) {
195                 TableViewer tableViewer = new TableViewer(table);
196                 tableViewer.setUseHashlookup(true);
197                 String[] colNams = new String[columnNames.size()];
198                 for (int i = 0; i < columnNames.size(); i++) {
199                         colNams[i] = (String) columnNames.get(i);
200                 }
201                 tableViewer.setColumnProperties(colNams);
202
203                 // Create the cell editors
204                 CellEditor[] editors = new CellEditor[columnNames.size()];
205                 for (int i = 0; i < columnNames.size(); i++) {
206                         TextCellEditor textEditor = new TextCellEditor(table);
207                         ((Text) textEditor.getControl()).setTextLimit(60);
208                         editors[i] = textEditor;
209                 }
210                 // Assign the cell editors to the viewer 
211                 tableViewer.setCellEditors(editors);
212                 // Set the cell modifier for the viewer
213                 //tableViewer.setCellModifier(new MetaDataModifier(this));
214                 // Set the default sorter for the viewer 
215                 //tableViewer.setSorter(new ExampleTaskSorter(ExampleTaskSorter.DESCRIPTION));
216                 
217                 return tableViewer;
218         }
219         
220         public int getPageSize() {
221                 return pageSize;
222         }
223         
224         public void setFilterSort(FilterSort extra) {
225                 this.extra = extra;
226         }
227         public String getTable() {
228                 return (entity != null) ? entity.getCondQualifiedName() : null;
229         }
230
231         public void setQuery(String query) {
232                 this.query = query;
233         }
234         public String getEncoding() {
235                 return encoding;
236         }
237
238         public void setEncoding(String encoding) {
239                 this.encoding = encoding;
240         }
241         public String getStatusString() {
242                 String status = getStartIndex() + Messages.getString("TableAdapter.to") + getEndIndex() + Messages.getString("TableAdapter.of") + //$NON-NLS-1$ //$NON-NLS-2$
243                                 getTotalSize();
244                 if (!encoding.equals(DEFAULT)) {
245                         status += " (" + encoding + ")"; //$NON-NLS-1$ //$NON-NLS-2$
246                 }
247                 String filterText = extra.toString();
248                 if (!filterText.equals("")) { //$NON-NLS-1$
249                         status += " (" + filterText + ")"; //$NON-NLS-1$ //$NON-NLS-2$
250                 }
251                 if (pageSize == Integer.MAX_VALUE) {
252                         status += Messages.getString("TableAdapter.full"); //$NON-NLS-1$
253                 }
254                 return status;
255         }
256         /**
257          * @return
258          */
259         public Bookmark getBookmark() {
260                 return this.bookmark;
261         }
262     /**
263      * @return
264      */
265     public Entity getEntity() {
266         return entity;
267     }
268
269 }