KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > views > QueryPageViewer


1 /*
2  * Created on 2004-10-30 by max
3  *
4  */

5 package org.hibernate.eclipse.console.views;
6
7 import java.beans.PropertyChangeEvent JavaDoc;
8 import java.util.List JavaDoc;
9
10 import org.eclipse.jface.action.IMenuListener;
11 import org.eclipse.jface.action.IMenuManager;
12 import org.eclipse.jface.action.MenuManager;
13 import org.eclipse.jface.viewers.DoubleClickEvent;
14 import org.eclipse.jface.viewers.IDoubleClickListener;
15 import org.eclipse.jface.viewers.ILabelProviderListener;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.ISelectionChangedListener;
18 import org.eclipse.jface.viewers.IStructuredContentProvider;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.ITableLabelProvider;
21 import org.eclipse.jface.viewers.SelectionChangedEvent;
22 import org.eclipse.jface.viewers.TableViewer;
23 import org.eclipse.jface.viewers.Viewer;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Menu;
30 import org.eclipse.swt.widgets.TabItem;
31 import org.eclipse.swt.widgets.Table;
32 import org.eclipse.swt.widgets.TableColumn;
33 import org.eclipse.ui.ISelectionService;
34 import org.hibernate.console.QueryPage;
35 import org.hibernate.eclipse.console.HibernateConsolePlugin;
36
37
38 /**
39  * @author max
40  *
41  */

42 public class QueryPageViewer {
43
44     class LabelProviderImpl implements ITableLabelProvider {
45         public Image getColumnImage(Object JavaDoc element, int columnIndex) {
46             return null;
47         }
48         public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
49             Object JavaDoc value = element; //TODO: should just be a columnprovider per querypage....
50
if (element instanceof QueryPage) {
51                 value = ((QueryPage) element).getList().get(columnIndex);
52             }
53             
54             if (value.getClass().isArray()) {
55                 Object JavaDoc[] arr = (Object JavaDoc[]) value;
56                 if (columnIndex > arr.length - 1) {
57                     return "<Unknown value>";
58                 }
59                 return "" + arr[columnIndex];
60             } else {
61             return value == null ? "" : value.toString();
62             }
63         }
64         public void addListener(ILabelProviderListener listener) {
65         }
66         public void dispose() {
67         }
68         public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
69             return false;
70         }
71         public void removeListener(ILabelProviderListener listener) {
72         }
73     }
74     
75     // should map to our table model instead
76
class ContentProviderImpl implements IStructuredContentProvider {
77         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
78             if (inputElement instanceof QueryPage) {
79                 QueryPage qp = ((QueryPage) inputElement);
80                 Object JavaDoc[] objects = qp.getList().toArray();
81                 if(qp.getExceptions().isEmpty()) {
82                     return objects;
83                 } else {
84                     Throwable JavaDoc[] throwables = (Throwable JavaDoc[])qp.getExceptions().toArray(new Throwable JavaDoc[0]);
85                     HibernateConsolePlugin.getDefault().logErrorMessage("Exception while executing HQL Query", throwables);
86                     return throwables; // TODO: provide actual error page
87
}
88             } else {
89                 return null;
90             }
91         }
92
93         public void dispose() {
94         }
95
96         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
97         }
98         
99     }
100     
101
102     private final QueryPage queryPage;
103     private TableViewer tableViewer;
104     private TabItem tabItem;
105     private final QueryPageTabView qrView;
106     
107     public QueryPageViewer(QueryPageTabView qrView, QueryPage queryPage) {
108         this.qrView = qrView;
109         this.queryPage = queryPage;
110         
111         createControl();
112         
113         
114     }
115     
116     protected TabItem getTabItem() {
117         return this.tabItem;
118     }
119     
120     public Table getTable() {
121         return this.tableViewer.getTable();
122     }
123     
124     protected void createControl() {
125         this.tabItem = new TabItem(this.qrView.tabs, SWT.NONE);
126         
127         int index = this.qrView.tabs.getItems().length;
128         Composite composite = new Composite(this.qrView.tabs, SWT.NONE);
129         GridLayout layout = new GridLayout();
130         layout.marginWidth = 5;
131         layout.marginHeight = 5;
132         composite.setLayout(layout);
133         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
134
135         this.tabItem.setText(this.queryPage.getQueryString().replace('\n', ' ').replace('\r', ' '));
136         /* different icon dependent on java/hql etc.
137         if (this.queryPage.getEntity() == null) {
138             this.tabItem.setImage(ImageStore.getImage(ImageStore.SCRIPT));
139             this.tabItem.setText(this.queryPage.getBookmark().getName());
140             this.tabItem.setToolTipText(this.queryPage.getQuery());
141         } else if (this.queryPage.isMetaData()) {
142             this.tabItem.setImage(ImageStore.getImage(ImageStore.TABLE_DETAILS));
143             this.tabItem.setText(this.queryPage.getBookmark().getName() + ":" +
144                     this.queryPage.getEntity().getQualifiedName());
145             tabItem.setToolTipText(this.queryPage.getEntity().getQualifiedName());
146         } else {
147             this.tabItem.setImage(ImageStore.getImage(ImageStore.TABLE));
148             this.tabItem.setText(this.queryPage.getBookmark().getName() + ":" +
149                     this.queryPage.getEntity().getQualifiedName());
150             this.tabItem.setToolTipText(this.queryPage.getEntity().getQualifiedName());
151         }*/

152         
153         createTable(composite);
154         this.tabItem.setControl(composite);
155         initializePopUpMenu();
156
157         this.qrView.tabs.setSelection(index-1);
158     }
159
160
161     /**
162      * @param tabItem
163      * @param composite
164      */

165     private void createTable(Composite composite) {
166         final Table table = new Table(composite, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
167         table.setLayout(new GridLayout());
168         table.setLayoutData(new GridData(GridData.FILL_BOTH));
169
170         addColumnsToTable(table);
171         this.tableViewer = new TableViewer(table);
172         this.tableViewer.setLabelProvider(new LabelProviderImpl());
173         this.tableViewer.setContentProvider(new ContentProviderImpl());
174         this.tableViewer.setInput(this.queryPage);
175
176         this.tableViewer.addDoubleClickListener(new IDoubleClickListener () {
177             public void doubleClick(DoubleClickEvent event) {
178                 tableDoubleClicked();
179             }
180         });
181         
182         this.tableViewer.addSelectionChangedListener(new ISelectionChangedListener () {
183             public void selectionChanged(SelectionChangedEvent event) {
184                 tableDoubleClicked();
185             }
186         });
187         packColumns(table);
188     }
189
190     private void tableDoubleClicked ()
191     {
192         ISelection selection = tableViewer.getSelection();
193         if (selection != null && !selection.isEmpty() && selection instanceof IStructuredSelection)
194         {
195             qrView.fireSelectionChangedEvent(selection);
196         }
197     }
198         
199     /**
200      * @param table
201      */

202     private void packColumns(final Table table) {
203         for (int i = 0; i < table.getColumnCount(); i++) {
204             table.getColumn(i).pack();
205         }
206     }
207
208     /**
209      * @param table
210      * @return
211      */

212     private int addColumnsToTable(final Table table) {
213         table.setHeaderVisible(true);
214         table.setLinesVisible(true);
215         List JavaDoc columns = this.queryPage.getPathNames();
216         int columnCount = columns.size();
217         for (int i = 0; i < columnCount; i++) {
218             TableColumn column = new TableColumn(table, SWT.NONE);
219             column.setText(columns.get(i).toString());
220         }
221         return columnCount;
222     }
223
224     public void propertyChange(PropertyChangeEvent JavaDoc event) {
225         if ("rows".equals(event.getPropertyName())) {
226             this.tableViewer.refresh();
227         } else if ("columns".equals(event.getPropertyName())) {
228             Table table = this.tableViewer.getTable();
229             TableColumn[] columns = table.getColumns();
230             for (int i = 0, length = columns == null ? 0 : columns.length; i < length; i++) {
231                 columns[i].dispose();
232             }
233             addColumnsToTable(table);
234             this.tableViewer.setInput(this.queryPage);
235             packColumns(table);
236             table.layout();
237         }
238     // updateStatusLine();
239
}
240     
241     public void dispose() {
242         //this.queryPage.removePropertyChangeListener(this);
243
this.tabItem.dispose();
244     }
245
246     protected QueryPage getQueryPage() {
247         return this.queryPage;
248     }
249     
250     private void initializePopUpMenu() {
251         MenuManager manager = new MenuManager();
252         manager.setRemoveAllWhenShown(true);
253         manager.addMenuListener(new IMenuListener() {
254             public void menuAboutToShow(IMenuManager menuManager) {
255                 //QueryPageViewer.this.tableView.actionGroup.fillContextMenu(menuManager);
256
}
257         });
258         Menu contextMenu = manager.createContextMenu(this.tableViewer.getControl());
259         this.tableViewer.getControl().setMenu(contextMenu);
260         // register the menu to the site so that we can allow
261
// actions to be plugged in
262
//this.tableView.getSite().registerContextMenu(manager, this.tableView);
263
}
264
265     
266     
267     protected ISelection getSelection() {
268         return this.tableViewer.getSelection();
269     }
270 }
271
Popular Tags