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.ImageStore;
12 import com.quantum.Messages;
13 import com.quantum.QuantumPlugin;
14 import com.quantum.actions.CustomCopyAction;
15 import com.quantum.actions.DeleteColumnAction;
16 import com.quantum.actions.DeleteObjectAction;
17 import com.quantum.actions.DeleteSubsetAction;
18 import com.quantum.actions.ExportXMLAction;
19 import com.quantum.actions.NewSubsetAction;
20 import com.quantum.actions.ViewTableAction;
21 import com.quantum.view.bookmark.ColumnNode;
22 import com.quantum.view.bookmark.TreeNode;
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.action.IMenuListener;
26 import org.eclipse.jface.action.IMenuManager;
27 import org.eclipse.jface.action.IToolBarManager;
28 import org.eclipse.jface.action.MenuManager;
29 import org.eclipse.jface.viewers.DoubleClickEvent;
30 import org.eclipse.jface.viewers.IDoubleClickListener;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.jface.viewers.StructuredSelection;
33 import org.eclipse.jface.viewers.TreeViewer;
34 import org.eclipse.swt.SWT;
35 import org.eclipse.swt.dnd.TextTransfer;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.Menu;
41 import org.eclipse.ui.IActionBars;
42 import org.eclipse.ui.IWorkbenchActionConstants;
43 import org.eclipse.ui.part.ViewPart;
44 import org.w3c.dom.Document;
45 import org.w3c.dom.Element;
46 import org.xml.sax.InputSource;
47 import org.xml.sax.SAXException;
54 public class SubsetView extends ViewPart {
56 private SubsetContentProvider provider = SubsetContentProvider.getInstance();
57 private NewSubsetAction newSubsetAction;
58 private DeleteSubsetAction deleteSubsetAction;
59 private DeleteObjectAction deleteObjectAction;
60 private DeleteColumnAction deleteColumnAction;
61 private CustomCopyAction customCopyAction;
63 private ViewTableAction viewTableAction;
64 private ExportXMLAction exportXMLAction;
65 private static SubsetView instance = null;
66 private TreeViewer treeViewer;
68 public synchronized static SubsetView getInstance() {
72 * Returns the current selected object in the tree. If it's a multiple selection, return the first.
75 public Object getCurrent() {
76 if (treeViewer == null) return null;
77 return ((StructuredSelection) treeViewer.getSelection())
81 * Returns the current selected objects in the tree, in the form of a StructuredSelection.
84 public StructuredSelection getSelection() {
85 if (treeViewer == null) return null;
86 return ((StructuredSelection) treeViewer.getSelection());
90 * Navigates the tree to get the current subset (root) of the selected element.
91 * If it's a multiple selection, it takes the first one.
94 public SubsetNode getCurrentSubset() {
95 TreeNode current = (TreeNode) getCurrent();
97 return getRoot(current);
101 * Navigates a given TreeNode till finds a SubsetNode
105 private static SubsetNode getRoot(TreeNode node){
106 while (!( node instanceof SubsetNode))
108 node = node.getParent();
110 return (SubsetNode) node;
116 * Deletes the current node (first selected) in the tree
118 public void deleteCurrent() {
119 provider.removeSubset(getCurrentSubset());
120 treeViewer.refresh();
123 public void refreshSubsetData(){
124 //if (treeViewer == null) return;
125 //SubsetContentProvider provider = (SubsetContentProvider) treeViewer.getContentProvider();
126 //Object[] objects = provider.getElements(Root.ROOT);
127 //for (int i = 0; i < objects.length; i++) {
128 //SubsetNode current = (SubsetNode) objects[i];
133 public void expandCurrent(SubsetNode node) {
134 treeViewer.setExpandedState(node, true);
135 treeViewer.refresh(node, false);
137 public void refresh() {
138 treeViewer.refresh();
140 public void disconnect() {
143 public void createPartControl(org.eclipse.swt.widgets.Composite parent) {
146 Composite main = new Composite(parent, SWT.NONE);
147 GridLayout layout = new GridLayout(1, false);
148 layout.horizontalSpacing = 0;
149 layout.verticalSpacing = 0;
150 main.setLayout(layout);
153 treeViewer = new TreeViewer(main);
154 treeViewer.setContentProvider(provider);
155 treeViewer.setLabelProvider(new SubsetLabelProvider());
156 treeViewer.setInput(SubsetRoot.ROOT);
157 MenuManager manager = new MenuManager();
158 manager.setRemoveAllWhenShown(true);
159 Menu fTextContextMenu =
160 manager.createContextMenu(treeViewer.getControl());
161 treeViewer.getControl().setMenu(fTextContextMenu);
162 treeViewer.addDoubleClickListener(new IDoubleClickListener() {
163 public void doubleClick(DoubleClickEvent event) {
164 Object sel = getCurrent();
165 if (sel instanceof ObjectNode) {
166 viewTableAction.run();
171 manager.addMenuListener(new IMenuListener() {
172 public void menuAboutToShow(IMenuManager mgr) {
173 Object sel = getCurrent();
174 if (sel instanceof SubsetNode) {
175 mgr.add(deleteSubsetAction);
176 deleteSubsetAction.setText(Messages.getString("SubsetView.Delete")); //$NON-NLS-1$
177 deleteSubsetAction.setImageDescriptor(
178 ImageStore.getImageDescriptor(ImageStore.DELETE));
179 mgr.add(pasteAction);
180 pasteAction.setText(Messages.getString("SubsetView.Paste")); //$NON-NLS-1$
181 pasteAction.setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.PASTE));
182 mgr.add(exportXMLAction);
183 exportXMLAction.setText(Messages.getString("bookmarkview.exportXML")); //$NON-NLS-1$
184 exportXMLAction.setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.XML));
186 } else if (sel instanceof ObjectNode) {
187 mgr.add(deleteObjectAction);
188 deleteObjectAction.setText(Messages.getString("SubsetView.Delete")); //$NON-NLS-1$
189 deleteObjectAction.setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.DELETE));
190 mgr.add(viewTableAction);
191 viewTableAction.setText(Messages.getString("bookmarkview.viewTable")); //$NON-NLS-1$
192 viewTableAction.setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.OPEN_TABLE));
193 mgr.add(exportXMLAction);
194 exportXMLAction.setText(Messages.getString("bookmarkview.exportXML")); //$NON-NLS-1$
195 exportXMLAction.setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.XML));
196 customCopyAction.selectionChanged(
197 (IStructuredSelection) treeViewer.getSelection());
198 mgr.add(customCopyAction);
199 } else if (sel instanceof ColumnNode){
200 mgr.add(deleteColumnAction);
201 deleteColumnAction.setText(Messages.getString("SubsetView.Delete")); //$NON-NLS-1$
202 deleteColumnAction.setImageDescriptor(ImageStore.getImageDescriptor(ImageStore.DELETE));
203 customCopyAction.selectionChanged(
204 (IStructuredSelection) treeViewer.getSelection());
205 mgr.add(customCopyAction);
208 mgr.add(newSubsetAction);
213 GridData gridData = new GridData();
214 gridData.horizontalAlignment = GridData.FILL;
215 gridData.verticalAlignment = GridData.FILL;
216 gridData.grabExcessHorizontalSpace = true;
217 gridData.grabExcessVerticalSpace = true;
218 treeViewer.getControl().setLayoutData(gridData);
219 status = new Label(main, SWT.NONE);
220 gridData = new GridData();
221 gridData.horizontalAlignment = GridData.FILL;
222 gridData.grabExcessHorizontalSpace = true;
223 status.setLayoutData(gridData);
225 IActionBars bars = getViewSite().getActionBars();
226 bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction);
227 bars.setGlobalActionHandler(IWorkbenchActionConstants.DELETE, deleteSubsetAction);
229 IToolBarManager toolBar = getViewSite().getActionBars().getToolBarManager();
230 toolBar.add(newSubsetAction);
232 status.setText(Messages.getString("bookmarkview.done")); //$NON-NLS-1$
234 public void initActions() {
235 newSubsetAction = new NewSubsetAction();
236 newSubsetAction.setText("New Subset"); //$NON-NLS-1$
237 newSubsetAction.setToolTipText(Messages.getString("SubsetView.CreatesANewEmptySubset")); //$NON-NLS-1$
238 newSubsetAction.setImageDescriptor(
239 ImageStore.getImageDescriptor(ImageStore.SUBSET));
240 newSubsetAction.init(this);
241 deleteColumnAction = new DeleteColumnAction();
242 deleteColumnAction.setText("Delete Column"); //$NON-NLS-1$
243 deleteColumnAction.setToolTipText(Messages.getString("SubsetView.DeletesTheSelectedColumns")); //$NON-NLS-1$
244 deleteColumnAction.setImageDescriptor(
245 ImageStore.getImageDescriptor(ImageStore.DELETE));
246 deleteColumnAction.init(this);
247 deleteObjectAction = new DeleteObjectAction();
248 deleteObjectAction.setText("Delete Object"); //$NON-NLS-1$
249 deleteObjectAction.setToolTipText(Messages.getString("SubsetView.DeletesTheSelectedObject")); //$NON-NLS-1$
250 deleteObjectAction.setImageDescriptor(
251 ImageStore.getImageDescriptor(ImageStore.DELETE));
252 deleteObjectAction.init(this);
253 deleteSubsetAction = new DeleteSubsetAction();
254 deleteSubsetAction.setText("Delete Subset"); //$NON-NLS-1$
255 deleteSubsetAction.setToolTipText(Messages.getString("SubsetView.DeletesTheSelectedSubset")); //$NON-NLS-1$
256 deleteSubsetAction.setImageDescriptor(
257 ImageStore.getImageDescriptor(ImageStore.DELETE));
258 deleteSubsetAction.init(this);
259 // viewTableAction = new ViewTableAction();
260 // viewTableAction.setText(Messages.getString("bookmarkview.viewTable")); //$NON-NLS-1$
261 // viewTableAction.setImageDescriptor(
262 // QuantumPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
263 // viewTableAction.init(this);
264 exportXMLAction = new ExportXMLAction();
265 exportXMLAction.setText(Messages.getString("bookmarkview.exportXML")); //$NON-NLS-1$
266 exportXMLAction.setImageDescriptor(
267 ImageStore.getImageDescriptor(ImageStore.XML));
268 exportXMLAction.init(this);
270 this.customCopyAction = new CustomCopyAction(1); // 1 is unused, just in case more custom copies are defined
271 this.customCopyAction.setText(Messages.getString("bookmarkview.customCopyAction")); //$NON-NLS-1$
272 this.customCopyAction.setImageDescriptor(
273 ImageStore.getImageDescriptor(ImageStore.COPY));
278 public void setFocus() {
281 private Action pasteAction = new Action() {
283 Object sel = getCurrent();
284 if (sel == null) return;
285 if (!(sel instanceof SubsetNode)) return;
287 SubsetNode subset = (SubsetNode) sel;
289 TextTransfer transfer = TextTransfer.getInstance();
290 String xmlMetaData = (String) QuantumPlugin.getDefault().getSysClip().getContents(transfer);
291 StringReader text = new StringReader(xmlMetaData);
292 InputSource source = new InputSource(text);
293 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
294 DocumentBuilder parser;
297 parser = factory.newDocumentBuilder();
298 doc = parser.parse(source);
299 } catch (ParserConfigurationException e) {
302 } catch (SAXException e) {
305 } catch (IOException e) {
309 Element root = doc.getDocumentElement();
310 subset.importXML(root, false);
311 treeViewer.refresh();
312 provider.setHasChanged(true);
317 public void addNewSubset(SubsetNode subset) {
318 provider.addSubset(subset);
319 treeViewer.refresh();
321 public void setStatus(String text) {
322 status.setText(text);
324 public void dispose(){
329 * Returs a Vector with all the elements of the treeViewer
332 public Vector getElements(){
333 Vector result = new Vector();
334 if (treeViewer == null) return result;
335 SubsetContentProvider provider = (SubsetContentProvider) treeViewer.getContentProvider();
336 Object[] objects = provider.getElements(SubsetRoot.ROOT);
337 for (int i = 0; i < objects.length; i++) {
338 SubsetNode current = (SubsetNode) objects[i];
344 public void deleteColumn (ColumnNode column){
345 provider.deleteColumn(column);
346 treeViewer.refresh();
349 public void deleteObject (ObjectNode object){
350 provider.deleteObject(object);
351 treeViewer.refresh();