1) Added missing strings for italic, underline and strike through.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / view / tableview / TableAdapter.java
1 package net.sourceforge.phpdt.sql.view.tableview;
2
3 import java.util.Vector;
4
5 import org.eclipse.swt.SWT;
6 import org.eclipse.swt.widgets.Table;
7 import org.eclipse.swt.widgets.TableColumn;
8 import org.eclipse.swt.widgets.TableItem;
9
10 import net.sourceforge.phpdt.sql.Messages;
11 import net.sourceforge.phpdt.sql.adapters.AdapterFactory;
12 import net.sourceforge.phpdt.sql.adapters.DatabaseAdapter;
13 import net.sourceforge.phpdt.sql.adapters.DatabaseInfo;
14 import net.sourceforge.phpdt.sql.sql.FilterSort;
15 import net.sourceforge.phpdt.sql.sql.SQLHelper;
16 import net.sourceforge.phpdt.sql.sql.SQLResults;
17 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkNode;
18
19 public class TableAdapter {
20         public static final String DEFAULT = ""; //$NON-NLS-1$
21         public static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
22         public static final String UTF_16 = "UTF-16"; //$NON-NLS-1$
23         
24         private int pageSize = DefaultSizes.PAGE_SIZE;
25         private int maxColumnSize = DefaultSizes.MAX_COLUMN_SIZE;
26         private FilterSort extra = new FilterSort();
27         private int offset = 1;
28         private int totalSize = -1;
29         private Vector rows = new Vector();
30         private Vector columnNames = new Vector();
31         private boolean hasMore = false;
32         
33         private BookmarkNode bookmark = null;   
34         private String table;
35         private String query;
36         
37         private String encoding = ""; //$NON-NLS-1$
38         
39         private TableAdapter(BookmarkNode bookmark) {
40                 this.bookmark = bookmark;
41         }
42         public void fullMode() {
43                 offset = 1;
44                 pageSize = Integer.MAX_VALUE;
45         }
46         public void resetMode() {
47                 offset = 1;
48                 pageSize = DefaultSizes.PAGE_SIZE;
49         }
50         public static TableAdapter createFromQuery(BookmarkNode bookmark, SQLResults results) {
51                 TableAdapter retVal = new TableAdapter(bookmark);
52                 retVal.setQuery(results.getQuery());
53                 retVal.setData(results);
54                 return retVal;
55         }
56         public static TableAdapter createFromTable(BookmarkNode bookmark, String table) {
57                 TableAdapter retVal = new TableAdapter(bookmark);
58                 retVal.setTable(table);
59                 return retVal;
60         }
61         private void loadSize() {
62                 if (table != null) {
63                         totalSize = SQLHelper.getSize(bookmark, getTableCountQuery());
64                 }
65         }
66         public int getStartIndex() {
67                 if (totalSize == 0) {
68                         return 0;
69                 }
70                 return offset;
71         }
72         public int getEndIndex() {
73                 return offset + rows.size() - 1;
74         }
75         public int getTotalSize() {
76                 return totalSize;
77         }
78         public void nextPage() {
79                 loadSize();
80                 offset = offset + pageSize;
81                 if (totalSize >= 0 && offset > totalSize) {
82                         offset = offset - pageSize;
83                 }
84         }
85         public void previousPage() {
86                 offset = offset - pageSize;
87                 if (offset < 1) {
88                         offset = 1;
89                 }
90         }
91         public boolean hasNextPage() {
92                 if (table != null) {
93                         if (offset + pageSize <= totalSize) {
94                                 return true;
95                         }
96                         return false;
97                 }
98                 return hasMore;
99         }
100         public boolean hasPreviousPage() {
101                 if (offset > 1) {
102                         return true;
103                 }
104                 return false;
105         }
106         public String getTableCountQuery() {
107                 if (table != null && bookmark != null) {
108                         return SQLHelper.getFullTableName(bookmark, table);
109                 }
110                 return query;
111         }
112         public String getQuery() {
113                 if (table != null) {
114                         DatabaseAdapter adapter = AdapterFactory.getInstance().getAdapter(bookmark.getType());
115                         if (adapter == null) throw new RuntimeException();
116                         else return adapter.getShowTableQuery(DatabaseInfo.create(bookmark), table) + extra.toString();
117                 }
118                 return query;
119         }
120         public void loadData() {
121                 loadSize();
122                 if (table != null) {
123                         if (offset > totalSize) {
124                                 offset = 1;
125                         }
126                 }
127                 String query = getQuery();
128                 System.out.println(offset + Messages.getString("TableAdapter.to") + (offset + pageSize - 1)); //$NON-NLS-1$
129                 SQLResults results = SQLHelper.getResults(bookmark, query, offset, offset + pageSize - 1, maxColumnSize, encoding);
130                 setData(results);
131         }
132         public void resetOffset() {
133                 offset = 1;
134         }
135         public void setData(SQLResults results) {
136                 int rowCount = results.getRowCount();
137                 int columnCount = results.getColumnCount();
138                 rows = new Vector();
139                 columnNames = new Vector();
140                 for (int col = 1; col <= columnCount; col++) {
141                         columnNames.addElement(results.getColumnName(col));
142                 }
143                 for (int row = 1; row <= rowCount; row++) {
144                         String rowData[] = new String[columnCount];
145                         for (int col = 1; col <= columnCount; col++) {
146                                 rowData[col - 1] = results.getElement(col, row).toString();
147                         }
148                         rows.addElement(rowData);
149                 }
150                 hasMore = results.hasMore();
151                 if (table == null && results.getMaxSize() >= 0) {
152                         if (offset > results.getMaxSize()) {
153                                 offset = 1;
154                                 loadData();
155                         }
156                 }
157         }
158         public void loadTable(Table table) {
159                 table.setHeaderVisible(true);
160                 for (int i = 0; i < columnNames.size(); i++) {
161                         TableColumn column = new TableColumn(table, SWT.NONE);
162                         column.setText(columnNames.elementAt(i).toString());
163                 }
164                 for (int i = 0; i < columnNames.size(); i++) {
165                         table.getColumn(i).pack();
166                 }
167                 for (int row = 0; row < rows.size(); row++) {
168                         TableItem item = new TableItem(table, SWT.NONE);
169                         String itemData[] = (String[]) rows.elementAt(row);
170                         item.setText(itemData);
171                 }
172                 for (int i = 0; i < columnNames.size(); i++) {
173                         table.getColumn(i).pack();
174                 }
175         }
176         
177         public int getPageSize() {
178                 return pageSize;
179         }
180         
181         public void setFilterSort(FilterSort extra) {
182                 this.extra = extra;
183         }
184         public String getTable() {
185                 return table;
186         }
187
188         public void setTable(String table) {
189                 this.table = table;
190         }
191         public void setQuery(String query) {
192                 this.query = query;
193         }
194         public String getEncoding() {
195                 return encoding;
196         }
197
198         public void setEncoding(String encoding) {
199                 this.encoding = encoding;
200         }
201         public String getStatusString() {
202                 String status = getStartIndex() + Messages.getString("TableAdapter.to") + getEndIndex() + Messages.getString("TableAdapter.of") + //$NON-NLS-1$ //$NON-NLS-2$
203                                 getTotalSize();
204                 if (!encoding.equals(DEFAULT)) {
205                         status += " (" + encoding + ")"; //$NON-NLS-1$ //$NON-NLS-2$
206                 }
207                 String filterText = extra.toString();
208                 if (!filterText.equals("")) { //$NON-NLS-1$
209                         status += " (" + filterText + ")"; //$NON-NLS-1$ //$NON-NLS-2$
210                 }
211                 if (pageSize == Integer.MAX_VALUE) {
212                         status += Messages.getString("TableAdapter.full"); //$NON-NLS-1$
213                 }
214                 return status;
215         }
216         /**
217          * @return
218          */
219         public BookmarkNode getBookmark() {
220                 return bookmark;
221         }
222
223 }