1 package com.quantum.view.tableview;
3 import java.sql.SQLException;
4 import java.util.Vector;
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;
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;
24 public class TableAdapter {
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$
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;
39 private Bookmark bookmark = null;
40 private Entity entity = null;
43 private String encoding = ""; //$NON-NLS-1$
45 private TableAdapter(Entity entity) {
47 this.bookmark = entity.getBookmark();
49 private TableAdapter(Bookmark bookmark) {
50 this.bookmark = bookmark;
52 public void fullMode() {
54 pageSize = Integer.MAX_VALUE;
56 public void resetMode() {
58 pageSize = DefaultSizes.PAGE_SIZE;
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);
66 public static TableAdapter createFromTable(Entity entity) {
67 TableAdapter retVal = new TableAdapter(entity);
70 private void loadSize() {
73 totalSize = SQLHelper.getSize(
74 bookmark.getConnection(), entity.getQualifiedName(),
75 bookmark.getAdapter());
77 } catch (SQLException e) {
79 } catch (NotConnectedException e) {
83 public int getStartIndex() {
89 public int getEndIndex() {
90 return offset + rows.size() - 1;
92 public int getTotalSize() {
95 public void nextPage() {
97 offset = offset + pageSize;
98 if (totalSize >= 0 && offset > totalSize) {
99 offset = offset - pageSize;
102 public void previousPage() {
103 offset = offset - pageSize;
108 public boolean hasNextPage() {
109 if (entity != null) {
110 if (offset + pageSize <= totalSize) {
117 public boolean hasPreviousPage() {
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();
132 public void loadData() throws NotConnectedException {
134 if (entity != null) {
135 if (offset > totalSize) {
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);
144 public void resetOffset() {
147 public void setData(SQLResults results) throws NotConnectedException {
148 if (results.isError()) return;
149 int rowCount = results.getRowCount();
150 int columnCount = results.getColumnCount();
152 columnNames = new Vector();
153 for (int col = 1; col <= columnCount; col++) {
154 columnNames.addElement(results.getColumnName(col));
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();
161 rows.addElement(rowData);
163 hasMore = results.hasMore();
164 if (entity == null && results.getMaxSize() >= 0) {
165 if (offset > results.getMaxSize()) {
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());
177 for (int i = 0; i < columnNames.size(); i++) {
178 table.getColumn(i).pack();
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);
185 for (int i = 0; i < columnNames.size(); i++) {
186 table.getColumn(i).pack();
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);
197 tableViewer.setColumnProperties(colNams);
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;
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));
216 public int getPageSize() {
220 public void setFilterSort(FilterSort extra) {
223 public String getTable() {
224 return (entity != null) ? entity.getQualifiedName() : null;
227 public void setQuery(String query) {
230 public String getEncoding() {
234 public void setEncoding(String encoding) {
235 this.encoding = encoding;
237 public String getStatusString() {
238 String status = getStartIndex() + Messages.getString("TableAdapter.to") + getEndIndex() + Messages.getString("TableAdapter.of") + //$NON-NLS-1$ //$NON-NLS-2$
240 if (!encoding.equals(DEFAULT)) {
241 status += " (" + encoding + ")"; //$NON-NLS-1$ //$NON-NLS-2$
243 String filterText = extra.toString();
244 if (!filterText.equals("")) { //$NON-NLS-1$
245 status += " (" + filterText + ")"; //$NON-NLS-1$ //$NON-NLS-2$
247 if (pageSize == Integer.MAX_VALUE) {
248 status += Messages.getString("TableAdapter.full"); //$NON-NLS-1$
255 public Bookmark getBookmark() {
256 return this.bookmark;
261 public Entity getEntity() {