KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.hibernate.eclipse.console.views;
2
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.Collections JavaDoc;
7 import java.util.HashSet JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Set JavaDoc;
11
12 import javax.swing.event.ListDataEvent JavaDoc;
13 import javax.swing.event.ListDataListener JavaDoc;
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 /**
41  */

42
43 public class QueryPageTabView extends ViewPart implements ISelectionProvider {
44     protected TabFolder tabs = null;
45
46     private Set JavaDoc listeners = Collections.synchronizedSet(new HashSet JavaDoc());
47     
48     protected List JavaDoc pageViewers = Collections.synchronizedList(new ArrayList JavaDoc());
49     
50     ListDataListener JavaDoc dataListener = new ListDataListener JavaDoc() {
51         public void contentsChanged(ListDataEvent JavaDoc e) {
52             rebuild();
53
54         }
55
56         public void intervalAdded(ListDataEvent JavaDoc e) {
57             contentsChanged(e);
58
59         }
60
61         public void intervalRemoved(ListDataEvent JavaDoc e) {
62             contentsChanged(e);
63
64         }
65     };
66
67     private QueryPageTabViewActionGroup actionGroup;
68     
69     /**
70      * Generic contructor
71      */

72     public QueryPageTabView() {
73         KnownConfigurations.getInstance().getQueryPageModel().addListDataListener(dataListener);
74     }
75
76
77     protected void rebuild() {
78         QueryPage selection = getSelectedQueryPage();
79         
80         Collection JavaDoc additions = getAddedResultSets();
81         for (Iterator JavaDoc i = additions.iterator(); i.hasNext();) {
82             QueryPage results = (QueryPage) i.next();
83             this.pageViewers.add(new QueryPageViewer(this, results));
84         }
85         
86         Collection JavaDoc deletions = getRemovedResultSets();
87         for (Iterator JavaDoc 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         // Handle firing selection changes here to cover for when the model fire changes
96
// but the Tab widget doesn't (e.g. the first page).
97
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 JavaDoc i = this.listeners.iterator(); i.hasNext();) {
150             ISelectionChangedListener listener = (ISelectionChangedListener) i.next();
151             listener.selectionChanged(new SelectionChangedEvent(this, selection));
152         }
153         
154         /*QueryPageViewer results = getSelectedQueryPageViewer();
155         if (results != null) {
156             results.updateStatusLine();
157         } else {
158             getViewSite().getActionBars().getStatusLineManager().setMessage("");
159         }*/

160     }
161     
162     /** fire event that query-tab is changed **/
163     protected void fireSelectionChangedEvent() {
164         ISelection selection = getSelection();
165         fireSelectionChangedEvent(selection);
166     }
167
168     public Object JavaDoc getAdapter(Class JavaDoc 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     /**
188      * @return
189      */

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 JavaDoc 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     /**
218      * @return
219      */

220     private Collection JavaDoc getRemovedResultSets() {
221         Collection JavaDoc collection = KnownConfigurations.getInstance().getQueryPageModel().getPagesAsList();
222         Collection JavaDoc visible = getQueryPages();
223         visible.removeAll(collection);
224         return visible;
225     }
226
227     private Collection JavaDoc getAddedResultSets() {
228         Collection JavaDoc collection = KnownConfigurations.getInstance().getQueryPageModel().getPagesAsList();
229         collection.removeAll(getQueryPages());
230         return collection;
231     }
232     
233     private Collection JavaDoc getQueryPages() {
234         List JavaDoc list = new ArrayList JavaDoc();
235         for (Iterator JavaDoc 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 JavaDoc 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