1 package com.quantum.view.tableview;
3 import java.sql.SQLException;
4 import java.util.Vector;
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;
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;
25 public class TableAdapter {
27 private Table table = null;
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$
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;
42 private Bookmark bookmark = null;
43 private Entity entity = null;
46 private String encoding = ""; //$NON-NLS-1$
48 private TableAdapter(Entity entity) {
50 this.bookmark = entity.getBookmark();
52 private TableAdapter(Bookmark bookmark) {
53 this.bookmark = bookmark;
55 public void fullMode() {
57 pageSize = Integer.MAX_VALUE;
59 public void resetMode() {
61 pageSize = DefaultSizes.PAGE_SIZE;
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);
69 public static TableAdapter createFromTable(Entity entity) {
70 TableAdapter retVal = new TableAdapter(entity);
73 private void loadSize() {
76 totalSize = SQLHelper.getSize(
77 bookmark.getConnection(), entity.getCondQualifiedName(),
78 AdapterFactory.getInstance().getAdapter(bookmark.getType()));
80 } catch (SQLException e) {
82 } catch (NotConnectedException e) {
86 public int getStartIndex() {
92 public int getEndIndex() {
93 return offset + rows.size() - 1;
95 public int getTotalSize() {
98 public void nextPage() {
100 offset = offset + pageSize;
101 if (totalSize >= 0 && offset > totalSize) {
102 offset = offset - pageSize;
105 public void previousPage() {
106 offset = offset - pageSize;
111 public boolean hasNextPage() {
112 if (entity != null) {
113 if (offset + pageSize <= totalSize) {
120 public boolean hasPreviousPage() {
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();
135 public void loadData() throws NotConnectedException {
137 if (entity != null) {
138 if (offset > totalSize) {
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);
147 public void resetOffset() {
150 public void setData(SQLResults results) throws NotConnectedException {
151 if (results.isError()) return;
152 int rowCount = results.getRowCount();
153 int columnCount = results.getColumnCount();
155 columnNames = new Vector();
156 for (int col = 1; col <= columnCount; col++) {
157 columnNames.addElement(results.getColumnName(col));
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();
164 rows.addElement(rowData);
166 hasMore = results.hasMore();
167 if (entity == null && results.getMaxSize() >= 0) {
168 if (offset > results.getMaxSize()) {
174 public void loadTable(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());
181 for (int i = 0; i < columnNames.size(); i++) {
182 table.getColumn(i).pack();
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);
189 for (int i = 0; i < columnNames.size(); i++) {
190 table.getColumn(i).pack();
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);
201 tableViewer.setColumnProperties(colNams);
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;
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));
220 public int getPageSize() {
224 public void setFilterSort(FilterSort extra) {
227 public String getTable() {
228 return (entity != null) ? entity.getCondQualifiedName() : null;
231 public void setQuery(String query) {
234 public String getEncoding() {
238 public void setEncoding(String encoding) {
239 this.encoding = encoding;
241 public String getStatusString() {
242 String status = getStartIndex() + Messages.getString("TableAdapter.to") + getEndIndex() + Messages.getString("TableAdapter.of") + //$NON-NLS-1$ //$NON-NLS-2$
244 if (!encoding.equals(DEFAULT)) {
245 status += " (" + encoding + ")"; //$NON-NLS-1$ //$NON-NLS-2$
247 String filterText = extra.toString();
248 if (!filterText.equals("")) { //$NON-NLS-1$
249 status += " (" + filterText + ")"; //$NON-NLS-1$ //$NON-NLS-2$
251 if (pageSize == Integer.MAX_VALUE) {
252 status += Messages.getString("TableAdapter.full"); //$NON-NLS-1$
259 public Bookmark getBookmark() {
260 return this.bookmark;
265 public Entity getEntity() {