1 5 package org.hibernate.eclipse.console.views; 6 7 import java.beans.PropertyChangeEvent ; 8 import java.util.List ; 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 42 public class QueryPageViewer { 43 44 class LabelProviderImpl implements ITableLabelProvider { 45 public Image getColumnImage(Object element, int columnIndex) { 46 return null; 47 } 48 public String getColumnText(Object element, int columnIndex) { 49 Object value = element; if (element instanceof QueryPage) { 51 value = ((QueryPage) element).getList().get(columnIndex); 52 } 53 54 if (value.getClass().isArray()) { 55 Object [] arr = (Object []) 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 element, String property) { 69 return false; 70 } 71 public void removeListener(ILabelProviderListener listener) { 72 } 73 } 74 75 class ContentProviderImpl implements IStructuredContentProvider { 77 public Object [] getElements(Object inputElement) { 78 if (inputElement instanceof QueryPage) { 79 QueryPage qp = ((QueryPage) inputElement); 80 Object [] objects = qp.getList().toArray(); 81 if(qp.getExceptions().isEmpty()) { 82 return objects; 83 } else { 84 Throwable [] throwables = (Throwable [])qp.getExceptions().toArray(new Throwable [0]); 85 HibernateConsolePlugin.getDefault().logErrorMessage("Exception while executing HQL Query", throwables); 86 return throwables; } 88 } else { 89 return null; 90 } 91 } 92 93 public void dispose() { 94 } 95 96 public void inputChanged(Viewer viewer, Object oldInput, Object 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 152 153 createTable(composite); 154 this.tabItem.setControl(composite); 155 initializePopUpMenu(); 156 157 this.qrView.tabs.setSelection(index-1); 158 } 159 160 161 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 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 212 private int addColumnsToTable(final Table table) { 213 table.setHeaderVisible(true); 214 table.setLinesVisible(true); 215 List 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 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 } 240 241 public void dispose() { 242 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 } 257 }); 258 Menu contextMenu = manager.createContextMenu(this.tableViewer.getControl()); 259 this.tableViewer.getControl().setMenu(contextMenu); 260 } 264 265 266 267 protected ISelection getSelection() { 268 return this.tableViewer.getSelection(); 269 } 270 } 271 | Popular Tags |