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