1 package org.hibernate.eclipse.console.views; 2 3 4 import java.util.ArrayList ; 5 import java.util.Collection ; 6 import java.util.Collections ; 7 import java.util.HashSet ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.Set ; 11 12 import javax.swing.event.ListDataEvent ; 13 import javax.swing.event.ListDataListener ; 14 15 import org.eclipse.jface.viewers.ISelection; 16 import org.eclipse.jface.viewers.ISelectionChangedListener; 17 import org.eclipse.jface.viewers.ISelectionProvider; 18 import org.eclipse.jface.viewers.IStructuredSelection; 19 import org.eclipse.jface.viewers.SelectionChangedEvent; 20 import org.eclipse.jface.viewers.StructuredSelection; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.events.DisposeEvent; 23 import org.eclipse.swt.events.DisposeListener; 24 import org.eclipse.swt.events.SelectionAdapter; 25 import org.eclipse.swt.events.SelectionEvent; 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.swt.widgets.TabFolder; 28 import org.eclipse.swt.widgets.TabItem; 29 import org.eclipse.ui.IActionBars; 30 import org.eclipse.ui.part.ViewPart; 31 import org.eclipse.ui.views.properties.IPropertySheetPage; 32 import org.eclipse.ui.views.properties.PropertySheetPage; 33 import org.hibernate.console.KnownConfigurations; 34 import org.hibernate.console.QueryPage; 35 import org.hibernate.eclipse.console.views.properties.HibernatePropertyHelper; 36 import org.hibernate.eclipse.console.views.properties.HibernatePropertySourceProvider; 37 38 39 40 42 43 public class QueryPageTabView extends ViewPart implements ISelectionProvider { 44 protected TabFolder tabs = null; 45 46 private Set listeners = Collections.synchronizedSet(new HashSet ()); 47 48 protected List pageViewers = Collections.synchronizedList(new ArrayList ()); 49 50 ListDataListener dataListener = new ListDataListener () { 51 public void contentsChanged(ListDataEvent e) { 52 rebuild(); 53 54 } 55 56 public void intervalAdded(ListDataEvent e) { 57 contentsChanged(e); 58 59 } 60 61 public void intervalRemoved(ListDataEvent e) { 62 contentsChanged(e); 63 64 } 65 }; 66 67 private QueryPageTabViewActionGroup actionGroup; 68 69 72 public QueryPageTabView() { 73 KnownConfigurations.getInstance().getQueryPageModel().addListDataListener(dataListener); 74 } 75 76 77 protected void rebuild() { 78 QueryPage selection = getSelectedQueryPage(); 79 80 Collection additions = getAddedResultSets(); 81 for (Iterator i = additions.iterator(); i.hasNext();) { 82 QueryPage results = (QueryPage) i.next(); 83 this.pageViewers.add(new QueryPageViewer(this, results)); 84 } 85 86 Collection deletions = getRemovedResultSets(); 87 for (Iterator i = deletions.iterator(); i.hasNext();) { 88 QueryPage results = (QueryPage) i.next(); 89 90 QueryPageViewer viewer = findViewerFor(results); 91 this.pageViewers.remove(viewer); 92 viewer.dispose(); 93 } 94 95 QueryPage newSelection = getSelectedQueryPage(); 98 if (selection != null && newSelection == null) { 99 fireSelectionChangedEvent(); 100 } else if (selection == null && newSelection != null) { 101 fireSelectionChangedEvent(); 102 } else if (selection != null && !selection.equals(newSelection)) { 103 fireSelectionChangedEvent(); 104 } 105 } 106 107 108 public void setFocus() { 109 } 110 111 public void dispose() { 112 KnownConfigurations.getInstance().getQueryPageModel().removeListDataListener(dataListener); 113 super.dispose(); 114 } 115 116 public void createPartControl(Composite parent) { 117 this.tabs = new TabFolder(parent, SWT.NONE); 118 tabs.addDisposeListener(new DisposeListener() { 119 public void widgetDisposed(DisposeEvent e) { 120 System.err.println("should not happen!"); 121 122 } 123 }); 124 this.tabs.addSelectionListener(new SelectionAdapter() { 125 public void widgetSelected(SelectionEvent event) { 126 fireSelectionChangedEvent(); 127 } 128 }); 129 130 rebuild(); 131 132 initActions(); 133 provideSelection(); 134 } 135 136 private void provideSelection() { 137 getSite().setSelectionProvider(this); 138 } 139 140 public void initActions() { 141 142 this.actionGroup = new QueryPageTabViewActionGroup(this,this); 143 144 IActionBars actionBars = getViewSite().getActionBars(); 145 this.actionGroup.fillActionBars(actionBars); 146 } 147 148 public void fireSelectionChangedEvent(ISelection selection) { 149 for (Iterator i = this.listeners.iterator(); i.hasNext();) { 150 ISelectionChangedListener listener = (ISelectionChangedListener) i.next(); 151 listener.selectionChanged(new SelectionChangedEvent(this, selection)); 152 } 153 154 160 } 161 162 163 protected void fireSelectionChangedEvent() { 164 ISelection selection = getSelection(); 165 fireSelectionChangedEvent(selection); 166 } 167 168 public Object getAdapter(Class adapter) { 169 170 if (adapter.equals(IPropertySheetPage.class)) 171 { 172 PropertySheetPage page = new PropertySheetPage(); 173 page.setPropertySourceProvider(new HibernatePropertySourceProvider(this)); 174 return page; 175 } 176 return super.getAdapter(adapter); 177 } 178 179 public ISelection getSelection() { 180 QueryPage selection = getSelectedQueryPage(); 181 182 return selection == null 183 ? new StructuredSelection() 184 : new StructuredSelection(selection); 185 } 186 187 190 public QueryPage getSelectedQueryPage() { 191 QueryPageViewer viewer = getSelectedQueryPageViewer(); 192 return viewer == null ? null : viewer.getQueryPage(); 193 } 194 195 protected QueryPageViewer getSelectedQueryPageViewer() { 196 QueryPageViewer selection = null; 197 if(this.tabs.isDisposed()) { 198 return selection; 199 } else { 200 int index = this.tabs.getSelectionIndex(); 201 if (index >= 0) { 202 TabItem item = this.tabs.getItem(index); 203 for (Iterator i = this.pageViewers.iterator(); 204 selection == null && i.hasNext();) { 205 QueryPageViewer viewer = (QueryPageViewer) i.next(); 206 if (item == viewer.getTabItem()) { 207 selection = viewer; 208 } 209 } 210 } 211 } 212 return selection; 213 } 214 215 216 217 220 private Collection getRemovedResultSets() { 221 Collection collection = KnownConfigurations.getInstance().getQueryPageModel().getPagesAsList(); 222 Collection visible = getQueryPages(); 223 visible.removeAll(collection); 224 return visible; 225 } 226 227 private Collection getAddedResultSets() { 228 Collection collection = KnownConfigurations.getInstance().getQueryPageModel().getPagesAsList(); 229 collection.removeAll(getQueryPages()); 230 return collection; 231 } 232 233 private Collection getQueryPages() { 234 List list = new ArrayList (); 235 for (Iterator i = this.pageViewers.iterator(); i.hasNext();) { 236 QueryPageViewer viewer = (QueryPageViewer) i.next(); 237 list.add(viewer.getQueryPage()); 238 } 239 return list; 240 } 241 242 private QueryPageViewer findViewerFor(QueryPage results) { 243 QueryPageViewer viewer = null; 244 for (Iterator i = this.pageViewers.iterator(); viewer == null && i.hasNext();) { 245 QueryPageViewer temp = (QueryPageViewer) i.next(); 246 if (results != null && results.equals(temp.getQueryPage())) { 247 viewer = temp; 248 } 249 } 250 return viewer; 251 } 252 253 public void addSelectionChangedListener(ISelectionChangedListener listener) { 254 this.listeners.add(listener); 255 } 256 257 public void removeSelectionChangedListener(ISelectionChangedListener listener) { 258 this.listeners.remove(listener); 259 } 260 261 262 public void setSelection(ISelection selection) { 263 System.out.println("Told to select " + selection); 264 } 265 266 267 268 269 } | Popular Tags |