1 package com.quantum.view.tableview;
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;
14 import com.quantum.QuantumPlugin;
15 import com.quantum.sql.SQLResultSetCollection;
16 import com.quantum.sql.SQLResultSetResults;
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;
33 * The Table View. Displays tables and Queries.
35 public class TableView extends ViewPart implements ISelectionProvider, PropertyChangeListener {
37 protected TabFolder tabs = null;
39 private Set listeners = Collections.synchronizedSet(new HashSet());
41 protected List resultSetViewers = Collections.synchronizedList(new ArrayList());
42 protected TableViewActionGroup actionGroup;
48 SQLResultSetCollection.getInstance().addPropertyChangeListener(this);
52 public void setFocus() {
55 public void dispose() {
56 SQLResultSetCollection.getInstance().removePropertyChangeListener(this);
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
65 * @return the TableView instance.
67 public static TableView getInstance() {
68 return (TableView) QuantumPlugin.getDefault().getView("com.quantum.view.tableview.TableView");
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();
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]));
87 public void initActions() {
89 this.actionGroup = new TableViewActionGroup(this);
91 IActionBars actionBars = getViewSite().getActionBars();
92 this.actionGroup.fillActionBars(actionBars);
95 public void addSelectionChangedListener(ISelectionChangedListener listener) {
96 this.listeners.add(listener);
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));
106 ResultSetViewer results = getSelectedResultSetViewer();
107 if (results != null) {
108 results.updateStatusLine();
110 getViewSite().getActionBars().getStatusLineManager().setMessage("");
114 public ISelection getSelection() {
115 SQLResultSetResults selection = getSelectedResultSet();
116 return selection == null
117 ? new StructuredSelection()
118 : new StructuredSelection(selection);
124 protected SQLResultSetResults getSelectedResultSet() {
125 ResultSetViewer viewer = getSelectedResultSetViewer();
126 return viewer == null ? null : viewer.getResultSet();
129 protected ResultSetViewer getSelectedResultSetViewer() {
130 ResultSetViewer selection = null;
131 int index = this.tabs.getSelectionIndex();
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()) {
146 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
147 this.listeners.remove(listener);
150 public void setSelection(ISelection selection) {
153 public void propertyChange(PropertyChangeEvent event) {
154 if ("resultSets".equals(event.getPropertyName())) {
155 SQLResultSetResults selection = getSelectedResultSet();
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));
163 Collection deletions = getRemovedResultSets();
164 for (Iterator i = deletions.iterator(); i.hasNext();) {
165 SQLResultSetResults results = (SQLResultSetResults) i.next();
167 ResultSetViewer viewer = findViewerFor(results);
168 this.resultSetViewers.remove(viewer);
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();
186 private Collection getRemovedResultSets() {
187 SQLResultSetResults[] results = SQLResultSetCollection.getInstance().getResultSets();
188 Collection collection = (results == null)
190 : new ArrayList(Arrays.asList(results));
191 Collection visible = getResultSets();
192 visible.removeAll(collection);
196 private Collection getAddedResultSets() {
197 SQLResultSetResults[] results = SQLResultSetCollection.getInstance().getResultSets();
198 Collection collection = (results == null)
200 : new ArrayList(Arrays.asList(results));
201 collection.removeAll(getResultSets());
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());
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())) {