1 package com.quantum.view.subset;
3 import java.io.IOException;
4 import java.io.StringReader;
5 import java.util.Vector;
7 import javax.xml.parsers.DocumentBuilder;
8 import javax.xml.parsers.DocumentBuilderFactory;
9 import javax.xml.parsers.ParserConfigurationException;
11 import com.quantum.Messages;
12 import com.quantum.QuantumPlugin;
13 import com.quantum.actions.CustomCopyAction;
14 import com.quantum.actions.DeleteColumnAction;
15 import com.quantum.actions.DeleteObjectAction;
16 import com.quantum.actions.DeleteSubsetAction;
17 import com.quantum.actions.ExportXMLAction;
18 import com.quantum.actions.NewSubsetAction;
19 import com.quantum.actions.ViewTableAction;
20 import com.quantum.view.bookmark.ColumnNode;
21 import com.quantum.view.bookmark.TreeNode;
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.action.IMenuListener;
25 import org.eclipse.jface.action.IMenuManager;
26 import org.eclipse.jface.action.IToolBarManager;
27 import org.eclipse.jface.action.MenuManager;
28 import org.eclipse.jface.viewers.DoubleClickEvent;
29 import org.eclipse.jface.viewers.IDoubleClickListener;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.viewers.StructuredSelection;
32 import org.eclipse.jface.viewers.TreeViewer;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.dnd.TextTransfer;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Menu;
40 import org.eclipse.ui.IActionBars;
41 import org.eclipse.ui.IWorkbenchActionConstants;
42 import org.eclipse.ui.part.ViewPart;
43 import org.w3c.dom.Document;
44 import org.w3c.dom.Element;
45 import org.xml.sax.InputSource;
46 import org.xml.sax.SAXException;
53 public class SubsetView extends ViewPart {
55 private SubsetContentProvider provider = SubsetContentProvider.getInstance();
56 private NewSubsetAction newSubsetAction;
57 private DeleteSubsetAction deleteSubsetAction;
58 private DeleteObjectAction deleteObjectAction;
59 private DeleteColumnAction deleteColumnAction;
60 private CustomCopyAction customCopyAction;
62 private ViewTableAction viewTableAction;
63 private ExportXMLAction exportXMLAction;
64 private static SubsetView instance = null;
65 private TreeViewer treeViewer;
67 public synchronized static SubsetView getInstance() {
71 * Returns the current selected object in the tree. If it's a multiple selection, return the first.
74 public Object getCurrent() {
75 if (treeViewer == null) return null;
76 return ((StructuredSelection) treeViewer.getSelection())
80 * Returns the current selected objects in the tree, in the form of a StructuredSelection.
83 public StructuredSelection getSelection() {
84 if (treeViewer == null) return null;
85 return ((StructuredSelection) treeViewer.getSelection());
89 * Navigates the tree to get the current subset (root) of the selected element.
90 * If it's a multiple selection, it takes the first one.
93 public SubsetNode getCurrentSubset() {
94 TreeNode current = (TreeNode) getCurrent();
96 return getRoot(current);
100 * Navigates a given TreeNode till finds a SubsetNode
104 private static SubsetNode getRoot(TreeNode node){
105 while (!( node instanceof SubsetNode))
107 node = (TreeNode) node.getParent();
109 return (SubsetNode) node;
115 * Deletes the current node (first selected) in the tree
117 public void deleteCurrent() {
118 provider.removeSubset(getCurrentSubset());
119 treeViewer.refresh();
122 public void refreshSubsetData(){
123 //if (treeViewer == null) return;
124 //SubsetContentProvider provider = (SubsetContentProvider) treeViewer.getContentProvider();
125 //Object[] objects = provider.getElements(Root.ROOT);
126 //for (int i = 0; i < objects.length; i++) {
127 //SubsetNode current = (SubsetNode) objects[i];
132 public void expandCurrent(SubsetNode node) {
133 treeViewer.setExpandedState(node, true);
134 treeViewer.refresh(node, false);
136 public void refresh() {
137 treeViewer.refresh();
139 public void disconnect() {
142 public void createPartControl(org.eclipse.swt.widgets.Composite parent) {
145 Composite main = new Composite(parent, SWT.NONE);
146 GridLayout layout = new GridLayout(1, false);
147 layout.horizontalSpacing = 0;
148 layout.verticalSpacing = 0;
149 main.setLayout(layout);
152 treeViewer = new TreeViewer(main);
153 treeViewer.setContentProvider(provider);
154 treeViewer.setLabelProvider(new SubsetLabelProvider());
155 treeViewer.setInput(SubsetRoot.ROOT);
156 MenuManager manager = new MenuManager();
157 manager.setRemoveAllWhenShown(true);
158 Menu fTextContextMenu =
159 manager.createContextMenu(treeViewer.getControl());
160 treeViewer.getControl().setMenu(fTextContextMenu);
161 treeViewer.addDoubleClickListener(new IDoubleClickListener() {
162 public void doubleClick(DoubleClickEvent event) {
163 Object sel = getCurrent();
164 if (sel instanceof ObjectNode) {
165 viewTableAction.run();
170 manager.addMenuListener(new IMenuListener() {
171 public void menuAboutToShow(IMenuManager mgr) {
172 Object sel = getCurrent();
173 if (sel instanceof SubsetNode) {
174 mgr.add(deleteSubsetAction);
175 deleteSubsetAction.setText(Messages.getString("SubsetView.Delete")); //$NON-NLS-1$
176 deleteSubsetAction.setImageDescriptor(
177 QuantumPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
178 mgr.add(pasteAction);
179 pasteAction.setText(Messages.getString("SubsetView.Paste")); //$NON-NLS-1$
180 pasteAction.setImageDescriptor(QuantumPlugin.getImageDescriptor("paste.gif")); //$NON-NLS-1$
181 mgr.add(exportXMLAction);
182 exportXMLAction.setText(Messages.getString("bookmarkview.exportXML")); //$NON-NLS-1$
183 exportXMLAction.setImageDescriptor(QuantumPlugin.getImageDescriptor("xml.gif")); //$NON-NLS-1$
185 } else if (sel instanceof ObjectNode) {
186 mgr.add(deleteObjectAction);
187 deleteObjectAction.setText(Messages.getString("SubsetView.Delete")); //$NON-NLS-1$
188 deleteObjectAction.setImageDescriptor(QuantumPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
189 mgr.add(viewTableAction);
190 viewTableAction.setText(Messages.getString("bookmarkview.viewTable")); //$NON-NLS-1$
191 viewTableAction.setImageDescriptor(QuantumPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
192 mgr.add(exportXMLAction);
193 exportXMLAction.setText(Messages.getString("bookmarkview.exportXML")); //$NON-NLS-1$
194 exportXMLAction.setImageDescriptor(QuantumPlugin.getImageDescriptor("xml.gif")); //$NON-NLS-1$
195 customCopyAction.selectionChanged(
196 (IStructuredSelection) treeViewer.getSelection());
197 mgr.add(customCopyAction);
198 } else if (sel instanceof ColumnNode){
199 mgr.add(deleteColumnAction);
200 deleteColumnAction.setText(Messages.getString("SubsetView.Delete")); //$NON-NLS-1$
201 deleteColumnAction.setImageDescriptor(QuantumPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
202 customCopyAction.selectionChanged(
203 (IStructuredSelection) treeViewer.getSelection());
204 mgr.add(customCopyAction);
207 mgr.add(newSubsetAction);
212 GridData gridData = new GridData();
213 gridData.horizontalAlignment = GridData.FILL;
214 gridData.verticalAlignment = GridData.FILL;
215 gridData.grabExcessHorizontalSpace = true;
216 gridData.grabExcessVerticalSpace = true;
217 treeViewer.getControl().setLayoutData(gridData);
218 status = new Label(main, SWT.NONE);
219 gridData = new GridData();
220 gridData.horizontalAlignment = GridData.FILL;
221 gridData.grabExcessHorizontalSpace = true;
222 status.setLayoutData(gridData);
224 IActionBars bars = getViewSite().getActionBars();
225 bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction);
226 bars.setGlobalActionHandler(IWorkbenchActionConstants.DELETE, deleteSubsetAction);
228 IToolBarManager toolBar = getViewSite().getActionBars().getToolBarManager();
229 toolBar.add(newSubsetAction);
231 status.setText(Messages.getString("bookmarkview.done")); //$NON-NLS-1$
233 public void initActions() {
234 newSubsetAction = new NewSubsetAction();
235 newSubsetAction.setText("New Subset"); //$NON-NLS-1$
236 newSubsetAction.setToolTipText(Messages.getString("SubsetView.CreatesANewEmptySubset")); //$NON-NLS-1$
237 newSubsetAction.setImageDescriptor(
238 QuantumPlugin.getImageDescriptor("subset.gif")); //$NON-NLS-1$
239 newSubsetAction.init(this);
240 deleteColumnAction = new DeleteColumnAction();
241 deleteColumnAction.setText("Delete Column"); //$NON-NLS-1$
242 deleteColumnAction.setToolTipText(Messages.getString("SubsetView.DeletesTheSelectedColumns")); //$NON-NLS-1$
243 deleteColumnAction.setImageDescriptor(
244 QuantumPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
245 deleteColumnAction.init(this);
246 deleteObjectAction = new DeleteObjectAction();
247 deleteObjectAction.setText("Delete Object"); //$NON-NLS-1$
248 deleteObjectAction.setToolTipText(Messages.getString("SubsetView.DeletesTheSelectedObject")); //$NON-NLS-1$
249 deleteObjectAction.setImageDescriptor(
250 QuantumPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
251 deleteObjectAction.init(this);
252 deleteSubsetAction = new DeleteSubsetAction();
253 deleteSubsetAction.setText("Delete Subset"); //$NON-NLS-1$
254 deleteSubsetAction.setToolTipText(Messages.getString("SubsetView.DeletesTheSelectedSubset")); //$NON-NLS-1$
255 deleteSubsetAction.setImageDescriptor(
256 QuantumPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
257 deleteSubsetAction.init(this);
258 // viewTableAction = new ViewTableAction();
259 // viewTableAction.setText(Messages.getString("bookmarkview.viewTable")); //$NON-NLS-1$
260 // viewTableAction.setImageDescriptor(
261 // QuantumPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
262 // viewTableAction.init(this);
263 exportXMLAction = new ExportXMLAction();
264 exportXMLAction.setText(Messages.getString("bookmarkview.exportXML")); //$NON-NLS-1$
265 exportXMLAction.setImageDescriptor(
266 QuantumPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
267 exportXMLAction.init(this);
269 this.customCopyAction = new CustomCopyAction(this,1); // 1 is unused, just in case more custom copies are defined
270 this.customCopyAction.setText(Messages.getString("bookmarkview.customCopyAction")); //$NON-NLS-1$
271 this.customCopyAction.setImageDescriptor(
272 QuantumPlugin.getImageDescriptor("copy.gif")); //$NON-NLS-1$
277 public void setFocus() {
280 private Action pasteAction = new Action() {
282 Object sel = getCurrent();
283 if (sel == null) return;
284 if (!(sel instanceof SubsetNode)) return;
286 SubsetNode subset = (SubsetNode) sel;
288 TextTransfer transfer = TextTransfer.getInstance();
289 String xmlMetaData = (String) QuantumPlugin.getDefault().getSysClip().getContents(transfer);
290 StringReader text = new StringReader(xmlMetaData);
291 InputSource source = new InputSource(text);
292 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
293 DocumentBuilder parser;
296 parser = factory.newDocumentBuilder();
297 doc = parser.parse(source);
298 } catch (ParserConfigurationException e) {
301 } catch (SAXException e) {
304 } catch (IOException e) {
308 Element root = doc.getDocumentElement();
309 subset.importXML(root, false);
310 treeViewer.refresh();
311 provider.setHasChanged(true);
316 public void addNewSubset(SubsetNode subset) {
317 provider.addSubset(subset);
318 treeViewer.refresh();
320 public void setStatus(String text) {
321 status.setText(text);
323 public void dispose(){
328 * Returs a Vector with all the elements of the treeViewer
331 public Vector getElements(){
332 Vector result = new Vector();
333 if (treeViewer == null) return result;
334 SubsetContentProvider provider = (SubsetContentProvider) treeViewer.getContentProvider();
335 Object[] objects = provider.getElements(SubsetRoot.ROOT);
336 for (int i = 0; i < objects.length; i++) {
337 SubsetNode current = (SubsetNode) objects[i];
343 public void deleteColumn (ColumnNode column){
344 provider.deleteColumn(column);
345 treeViewer.refresh();
348 public void deleteObject (ObjectNode object){
349 provider.deleteObject(object);
350 treeViewer.refresh();