new update site
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / tableview / TableView.java
1 package com.quantum.view.tableview;
2
3 import java.beans.PropertyChangeEvent;
4 import java.beans.PropertyChangeListener;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Collection;
8 import java.util.Collections;
9 import java.util.HashSet;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.Set;
13
14 import com.quantum.QuantumPlugin;
15 import com.quantum.sql.SQLResultSetCollection;
16 import com.quantum.sql.SQLResultSetResults;
17
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.ISelectionChangedListener;
20 import org.eclipse.jface.viewers.ISelectionProvider;
21 import org.eclipse.jface.viewers.SelectionChangedEvent;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.swt.SWT;
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
32 /**
33  * The Table View. Displays tables and Queries.
34  */
35 public class TableView extends ViewPart implements ISelectionProvider, PropertyChangeListener {
36
37         protected TabFolder tabs = null;
38         
39         private Set listeners = Collections.synchronizedSet(new HashSet());
40         
41         protected List resultSetViewers = Collections.synchronizedList(new ArrayList());
42         protected TableViewActionGroup actionGroup;
43  
44         /**
45          * Generic contructor
46          */
47         public TableView() {
48                 SQLResultSetCollection.getInstance().addPropertyChangeListener(this);
49         }
50
51
52         public void setFocus() {
53         }
54         
55         public void dispose() {
56                 SQLResultSetCollection.getInstance().removePropertyChangeListener(this);
57                 super.dispose();
58         }
59         
60     /**
61      * Gets the instance of the TableView.  This view can appear on multiple 
62      * perspectives, but tabs within the view are shared no matter which perspective
63      * is open.
64      * 
65      * @return the TableView instance.
66      */
67         public static TableView getInstance() {
68                 return (TableView) QuantumPlugin.getDefault().getView("com.quantum.view.tableview.TableView");
69         }
70         
71         public void createPartControl(Composite parent) {
72                 this.tabs = new TabFolder(parent, SWT.NONE);
73                 this.tabs.addSelectionListener(new SelectionAdapter() {
74                         public void widgetSelected(SelectionEvent event) {
75                                 fireSelectionChangedEvent();
76                         }
77                 });
78
79                 SQLResultSetResults[] resultSets = SQLResultSetCollection.getInstance().getResultSets();
80                 for (int i = 0, length = resultSets == null ? 0 : resultSets.length; i < length; i++) {
81                         this.resultSetViewers.add(new ResultSetViewer(this, resultSets[i]));
82                 }
83                 
84                 initActions();
85         }
86         
87         public void initActions() {
88                 
89         this.actionGroup = new TableViewActionGroup(this);
90
91         IActionBars actionBars = getViewSite().getActionBars();
92         this.actionGroup.fillActionBars(actionBars);
93         }
94
95         public void addSelectionChangedListener(ISelectionChangedListener listener) {
96                 this.listeners.add(listener);
97         }
98         
99         protected void fireSelectionChangedEvent() {
100                 ISelection selection = getSelection();
101                 for (Iterator i = this.listeners.iterator(); i.hasNext();) {
102                         ISelectionChangedListener listener = (ISelectionChangedListener) i.next();
103                         listener.selectionChanged(new SelectionChangedEvent(this, selection));
104                 }
105                 
106                 ResultSetViewer results = getSelectedResultSetViewer();
107                 if (results != null) {
108                         results.updateStatusLine();
109                 } else {
110                         getViewSite().getActionBars().getStatusLineManager().setMessage("");
111                 }
112         }
113
114         public ISelection getSelection() {
115                 SQLResultSetResults selection = getSelectedResultSet();
116                 return selection == null 
117                         ? new StructuredSelection() 
118                         : new StructuredSelection(selection);
119         }
120
121         /**
122          * @return
123          */
124         protected SQLResultSetResults getSelectedResultSet() {
125                 ResultSetViewer viewer = getSelectedResultSetViewer();
126                 return viewer == null ? null : viewer.getResultSet();
127         }
128
129         protected ResultSetViewer getSelectedResultSetViewer() {
130                 ResultSetViewer selection = null;
131                 int index = this.tabs.getSelectionIndex();
132                 if (index >= 0) {
133                         TabItem item = this.tabs.getItem(index);
134                         for (Iterator i = this.resultSetViewers.iterator(); 
135                                         selection == null && i.hasNext();) {
136                                 ResultSetViewer viewer = (ResultSetViewer) i.next();
137                                 if (item == viewer.getTabItem()) {
138                                         selection = viewer;
139                                 }
140                         }
141                 }
142                 return selection;
143         }
144
145
146         public void removeSelectionChangedListener(ISelectionChangedListener listener) {
147                 this.listeners.remove(listener);
148         }
149
150         public void setSelection(ISelection selection) {
151         }
152
153         public void propertyChange(PropertyChangeEvent event) {
154                 if ("resultSets".equals(event.getPropertyName())) {
155                         SQLResultSetResults selection = getSelectedResultSet();
156                         
157                         Collection additions = getAddedResultSets();
158                         for (Iterator i = additions.iterator(); i.hasNext();) {
159                                 SQLResultSetResults results = (SQLResultSetResults) i.next();
160                                 this.resultSetViewers.add(new ResultSetViewer(this, results));
161                         }
162                         
163                         Collection deletions = getRemovedResultSets(); 
164                         for (Iterator i = deletions.iterator(); i.hasNext();) {
165                                 SQLResultSetResults results = (SQLResultSetResults) i.next();
166                                 
167                                 ResultSetViewer viewer = findViewerFor(results);
168                                 this.resultSetViewers.remove(viewer);
169                                 viewer.dispose();
170                         }
171                         
172                         SQLResultSetResults newSelection = getSelectedResultSet();
173                         if (selection != null && newSelection == null) {
174                                 fireSelectionChangedEvent();
175                         } else if (selection == null && newSelection != null) {
176                                 fireSelectionChangedEvent();
177                         } else if (selection != null && !selection.equals(newSelection)) {
178                                 fireSelectionChangedEvent();
179                         }
180                 }
181         }
182         
183         /**
184          * @return
185          */
186         private Collection getRemovedResultSets() {
187                 SQLResultSetResults[] results = SQLResultSetCollection.getInstance().getResultSets();
188                 Collection collection = (results == null) 
189                                 ? new ArrayList() 
190                                 : new ArrayList(Arrays.asList(results));
191                 Collection visible = getResultSets();
192                 visible.removeAll(collection);
193                 return visible;
194         }
195
196         private Collection getAddedResultSets() {
197                 SQLResultSetResults[] results = SQLResultSetCollection.getInstance().getResultSets();
198                 Collection collection = (results == null) 
199                                 ? new ArrayList() 
200                                 : new ArrayList(Arrays.asList(results));
201                 collection.removeAll(getResultSets());
202                 return collection;
203         }
204         
205         private Collection getResultSets() {
206                 List list = new ArrayList();
207                 for (Iterator i = this.resultSetViewers.iterator(); i.hasNext();) {
208                         ResultSetViewer viewer = (ResultSetViewer) i.next();
209                         list.add(viewer.getResultSet());
210                 }
211                 return list;
212         }
213         
214         private ResultSetViewer findViewerFor(SQLResultSetResults results) {
215                 ResultSetViewer viewer = null;
216                 for (Iterator i = this.resultSetViewers.iterator(); viewer == null && i.hasNext();) {
217                         ResultSetViewer temp = (ResultSetViewer) i.next();
218                         if (results != null && results.equals(temp.getResultSet())) {
219                                 viewer = temp;
220                         }
221                 }
222                 return viewer;
223         }
224 }