]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.team.ui/src/org/simantics/team/ui/SynchroniseView.java
Remove usage of deprecated SimanticsUI-methods
[simantics/platform.git] / bundles / org.simantics.team.ui / src / org / simantics / team / ui / SynchroniseView.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.team.ui;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Iterator;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.eclipse.jface.action.GroupMarker;
21 import org.eclipse.jface.action.IMenuListener;
22 import org.eclipse.jface.action.IMenuManager;
23 import org.eclipse.jface.action.MenuManager;
24 import org.eclipse.jface.layout.GridDataFactory;
25 import org.eclipse.jface.layout.TreeColumnLayout;
26 import org.eclipse.jface.resource.JFaceResources;
27 import org.eclipse.jface.resource.LocalResourceManager;
28 import org.eclipse.jface.viewers.ColumnLabelProvider;
29 import org.eclipse.jface.viewers.ColumnWeightData;
30 import org.eclipse.jface.viewers.ITreeContentProvider;
31 import org.eclipse.jface.viewers.LabelProvider;
32 import org.eclipse.jface.viewers.TreeViewer;
33 import org.eclipse.jface.viewers.TreeViewerColumn;
34 import org.eclipse.jface.viewers.Viewer;
35 import org.eclipse.jface.viewers.ViewerCell;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.graphics.Image;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Menu;
41 import org.eclipse.swt.widgets.Shell;
42 import org.eclipse.ui.IMemento;
43 import org.eclipse.ui.IViewSite;
44 import org.eclipse.ui.IWorkbenchActionConstants;
45 import org.eclipse.ui.IWorkbenchPartSite;
46 import org.eclipse.ui.IWorkbenchSite;
47 import org.eclipse.ui.PartInitException;
48 import org.eclipse.ui.part.IPageSite;
49 import org.eclipse.ui.part.ViewPart;
50 import org.simantics.Simantics;
51 import org.simantics.browsing.ui.GraphExplorer;
52 import org.simantics.browsing.ui.graph.impl.SessionContextInputSource;
53 import org.simantics.browsing.ui.swt.DefaultSelectionDataResolver;
54 import org.simantics.browsing.ui.swt.GraphExplorerFactory;
55 import org.simantics.browsing.ui.swt.ViewArgumentUtils;
56 import org.simantics.databoard.Bindings;
57 import org.simantics.db.ChangeSetIdentifier;
58 import org.simantics.db.ReadGraph;
59 import org.simantics.db.Resource;
60 import org.simantics.db.Session;
61 import org.simantics.db.exception.DatabaseException;
62 import org.simantics.db.management.ISessionContext;
63 import org.simantics.db.management.ISessionContextChangedListener;
64 import org.simantics.db.management.ISessionContextProvider;
65 import org.simantics.db.management.SessionContextChangedEvent;
66 import org.simantics.db.request.Read;
67 import org.simantics.layer0.Layer0;
68 import org.simantics.project.ProjectKeys;
69 import org.simantics.team.internal.Images;
70 import org.simantics.ui.SimanticsUI;
71 import org.simantics.utils.datastructures.disposable.DisposeState;
72 import org.simantics.utils.datastructures.hints.HintListenerAdapter;
73 import org.simantics.utils.datastructures.hints.HintTracker;
74 import org.simantics.utils.datastructures.hints.IHintContext.Key;
75 import org.simantics.utils.datastructures.hints.IHintListener;
76 import org.simantics.utils.datastructures.hints.IHintObservable;
77 import org.simantics.utils.datastructures.hints.IHintTracker;
78 import org.simantics.utils.ui.LayoutUtils;
79
80 /**
81  * @author Kalle Kondelin
82  */
83 public class SynchroniseView extends ViewPart {
84
85     public SynchroniseView() {
86     }
87
88     public class SessionContextProjectTracker extends HintTracker {
89         public SessionContextProjectTracker() {
90             IHintListener activeProjectListener = new HintListenerAdapter() {
91                 @Override
92                 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
93                     applySessionContext(getSessionContext());
94                 }
95             };
96             addKeyHintListener(ProjectKeys.KEY_PROJECT, activeProjectListener);
97         }
98     }
99     
100     protected LocalResourceManager           resourceManager;
101
102     protected Composite                      parent;
103
104     protected TreeViewer                     treeViewer;
105
106     private Map<String, String>              args;
107
108     private ISessionContextProvider          contextProvider;
109
110     private ISessionContext                  sessionContext;
111
112     protected IMemento                       memento;
113
114     private IHintTracker                     sessionContextTracker = new SessionContextProjectTracker();
115
116     private DisposeState                     disposeState          = DisposeState.Alive;
117
118     protected ISessionContextChangedListener contextChangeListener = new ISessionContextChangedListener() {
119         @Override
120         public void sessionContextChanged(SessionContextChangedEvent event) {
121             sessionContext = event.getNewValue();
122             sessionContextTracker.track(sessionContext);
123         }
124     };
125
126     protected void setSessionContextTracker(IHintTracker tracker) {
127         this.sessionContextTracker = tracker;
128     }
129
130     protected Map<String, String> getViewArguments() {
131         return args;
132     }
133
134     protected DisposeState getDisposeState() {
135         return disposeState;
136     }
137
138     public ISessionContext getSessionContext() {
139         return sessionContext;
140     }
141
142     public ISessionContextProvider getSessionContextProvider() {
143         return contextProvider;
144     }
145
146     @Override
147     public void createPartControl(Composite parent) {
148         this.parent = parent;
149         this.treeViewer = new TreeViewer(parent, SWT.SINGLE | SWT.FULL_SELECTION);
150         this.resourceManager = new LocalResourceManager(JFaceResources.getResources(parent.getDisplay()), treeViewer.getTree());
151         Images.getInstance(JFaceResources.getResources());
152         TreeColumnLayout ad = new TreeColumnLayout();
153         parent.setLayout(ad);
154         treeViewer.getTree().setHeaderVisible(true);
155         //treeViewer.getTree().setLinesVisible(true);
156         //treeViewer.setUseHashlookup(true);
157         //treeViewer.setAutoExpandLevel(3);
158
159         TreeViewerColumn nameColumn = new TreeViewerColumn(treeViewer, SWT.LEFT);
160         TreeViewerColumn typeColumn = new TreeViewerColumn(treeViewer, SWT.LEFT);
161 //        TreeViewerColumn idColumn = new TreeViewerColumn(treeViewer, SWT.LEFT);
162 //        TreeViewerColumn lookupIdColumn = new TreeViewerColumn(treeViewer, SWT.LEFT);
163 //        TreeViewerColumn zColumn = new TreeViewerColumn(treeViewer, SWT.LEFT);
164
165         nameColumn.setLabelProvider(new MyColumnLabelProvider());
166         typeColumn.setLabelProvider(new MyColumnLabelProvider());
167 //        idColumn.setLabelProvider(new MyColumnLabelProvider());
168 //        lookupIdColumn.setLabelProvider(new MyColumnLabelProvider());
169 //        zColumn.setLabelProvider(new MyColumnLabelProvider());
170
171         nameColumn.getColumn().setText("Name");
172         nameColumn.getColumn().setWidth(20);
173         ad.setColumnData(nameColumn.getColumn(), new ColumnWeightData(80, 100));
174         typeColumn.getColumn().setText("Type");
175         typeColumn.getColumn().setWidth(20);
176         ad.setColumnData(typeColumn.getColumn(), new ColumnWeightData(20, 120));
177 //        idColumn.getColumn().setText("ID");
178 //        idColumn.getColumn().setWidth(20);
179 //        ad.setColumnData(idColumn.getColumn(), new ColumnWeightData(10, 50));
180 //        lookupIdColumn.getColumn().setText("Lookup ID");
181 //        lookupIdColumn.getColumn().setWidth(20);
182 //        ad.setColumnData(lookupIdColumn.getColumn(), new ColumnWeightData(50, 100));
183 //        zColumn.getColumn().setText("Z");
184 //        zColumn.getColumn().setWidth(70);
185 //        ad.setColumnData(zColumn.getColumn(), new ColumnWeightData(10, 70));
186
187         createControls(parent);
188         attachToSession();
189         
190     }
191
192     /**
193      * Invoked when this viewpart is disposed. Unhooks the view from its
194      * ISessionContextProvider. Overriding is allowed but super.dispose() must
195      * be called.
196      * 
197      * @see org.eclipse.ui.part.WorkbenchPart#dispose()
198      */
199     @Override
200     public void dispose() {
201         disposeState = DisposeState.Disposing;
202         try {
203             if (contextProvider != null) {
204                 contextProvider.removeContextChangedListener(contextChangeListener);
205                 contextProvider = null;
206             }
207             sessionContextTracker.untrack();
208             resourceManager.dispose();
209             resourceManager = null;
210             args = null;
211             sessionContext = null;
212             parent = null;
213             super.dispose();
214         } finally {
215            disposeState = DisposeState.Disposed;
216         }
217     }
218
219     @Override
220     public void setFocus() {
221         
222     }
223
224     @Override
225     public void init(IViewSite site) throws PartInitException {
226         super.init(site);
227         this.args = ViewArgumentUtils.parseViewArguments(this);
228     }
229
230     @Override
231     public void init(IViewSite site, IMemento memento) throws PartInitException {
232         super.init(site, memento);
233         this.args = ViewArgumentUtils.parseViewArguments(this);
234         this.memento = memento;
235     }
236
237     @Override
238     public void saveState(IMemento memento) {
239         if (this.memento != null) {
240             memento.putMemento(this.memento);
241         }
242 //        if (explorer != null)
243 //            explorer.saveState(memento);
244     }
245     
246     protected final void attachToSession() {
247         // Track active ISessionContext changes
248         contextProvider = SimanticsUI.getSessionContextProvider(getViewSite().getWorkbenchWindow());
249         contextProvider.addContextChangedListener(contextChangeListener);
250
251         // Perform first-time initialization of the explorer and its input.
252         applySessionContext(contextProvider.getSessionContext());
253     }
254
255 //    private abstract class NameAndDescriptionToolTip extends ToolTip {
256 //        public NameAndDescriptionToolTip(Control control, int style) {
257 //            super(control, style, false);
258 //        }
259 //
260 //        protected abstract Object getModelElement(Event event);
261 //
262 //        /**
263 //         * Adds logic to only show a tooltip if a meaningful item is under the
264 //         * cursor.
265 //         */
266 //        protected boolean shouldCreateToolTip(Event event) {
267 //            if (!super.shouldCreateToolTip(event))
268 //                return false;
269 //            Object tableElement = getModelElement(event); 
270 //            return tableElement != null && tableElement instanceof DisplayElement;
271 //        }
272 //
273 //        protected Composite createToolTipContentArea(Event event,
274 //                Composite parent) {
275 //            Object modelElement = getModelElement(event);
276 //
277 //            Image iconImage = null;
278 //            String nameString = "no name";
279 //
280 //            if (modelElement instanceof DisplayElement) {
281 //                iconImage = null;
282 //                nameString = "name";
283 //            }
284 //
285 //            // Create the content area
286 //            Composite composite = new Composite(parent, SWT.NONE);
287 //            composite.setBackground(parent.getDisplay().getSystemColor(
288 //                    SWT.COLOR_INFO_BACKGROUND));
289 //            composite.setLayout(new GridLayout(2, false));
290 //
291 //            // The title area with the icon (if there is one) and label.
292 //            Label title = createEntry(composite, iconImage, nameString);
293 ////            title.setFont(tooltipHeading);
294 //            GridDataFactory.createFrom((GridData)title.getLayoutData())
295 //                .hint(SWT.DEFAULT, SWT.DEFAULT)
296 ////                .minSize(MIN_TOOLTIP_WIDTH, 1)
297 //                .applyTo(title);
298 //
299 //            // The description (if there is one)
300 ////            String descriptionString = "description";
301 ////            if (descriptionString != null)
302 ////                createEntry(composite, null, descriptionString);
303 //
304 //            // Other Content to add
305 //            addContent(composite, modelElement);
306 //
307 //            return composite;
308 //        }
309 //
310 //        /**
311 //         * Adds a line of information to <code>parent</code>. If
312 //         * <code>icon</code> is not <code>null</code>, an icon is placed on the
313 //         * left, and then a label with <code>text</code>.
314 //         * 
315 //         * @param parent
316 //         *            the composite to add the entry to
317 //         * @param icon
318 //         *            the icon to place next to the text. <code>null</code> for
319 //         *            none.
320 //         * @param text
321 //         *            the text to display
322 //         * @return the created label
323 //         */
324 //        protected Label createEntry(Composite parent, Image icon, String text) {
325 //            if (icon != null) {
326 //                Label iconLabel = new Label(parent, SWT.NONE);
327 //                iconLabel.setImage(icon);
328 //                iconLabel.setBackground(parent.getDisplay().getSystemColor(
329 //                        SWT.COLOR_INFO_BACKGROUND));
330 //                iconLabel.setData(new GridData());
331 //            }
332 //
333 //            Label textLabel = new Label(parent, SWT.WRAP);
334 //            
335 //            if(icon == null) {
336 //                GridDataFactory.generate(textLabel, 2, 1);
337 //            } else {
338 //                GridDataFactory.generate(textLabel, 1, 1);
339 //            }
340 //            
341 //            textLabel.setText(text);
342 //            textLabel.setBackground(parent.getDisplay().getSystemColor(
343 //                    SWT.COLOR_INFO_BACKGROUND));
344 //            return textLabel;
345 //        }
346 //
347 //        /**
348 //         * Adds a line of information to <code>parent</code>. If
349 //         * <code>icon</code> is not <code>null</code>, an icon is placed on the
350 //         * left, and then a label with <code>text</code>, which supports using
351 //         * anchor tags to creates links
352 //         * 
353 //         * @param parent
354 //         *            the composite to add the entry to
355 //         * @param icon
356 //         *            the icon to place next to the text. <code>null</code> for
357 //         *            none.
358 //         * @param text
359 //         *            the text to display
360 //         * @return the created link
361 //         */
362 //        protected Text createEntryWithText(Composite parent, Image icon, String text) {
363 //            if (icon != null) {
364 //                Label iconLabel = new Label(parent, SWT.NONE);
365 //                iconLabel.setImage(icon);
366 //                iconLabel.setBackground(parent.getDisplay().getSystemColor(
367 //                        SWT.COLOR_INFO_BACKGROUND));
368 //                iconLabel.setData(new GridData());
369 //            }
370 //            Text texts = new Text(parent, SWT.READ_ONLY | SWT.MULTI | SWT.V_SCROLL);
371 //            if(icon == null) {
372 //                GridDataFactory.generate(texts, 2, 1);
373 //            }
374 //            texts.setText(text);
375 //            texts.setBackground(parent.getDisplay().getSystemColor(
376 //                    SWT.COLOR_INFO_BACKGROUND));
377 //            return texts;
378 //        }
379 //
380 //        protected void addContent(Composite destination, Object modelElement) {
381 //        }
382 //    }
383 /*
384     private class ItemDetailToolTip extends NameAndDescriptionToolTip {
385         private final boolean DEBUG = false;
386         private TreeViewer viewer;
387         private Tree tree;
388         private ItemDetailToolTip(TreeViewer viewer, Tree tree, ViewerFilter filter) {
389             super(tree,NO_RECREATE);
390             this.tree = tree;
391             this.viewer = viewer;
392             this.setHideOnMouseDown(false);
393         }
394         public Point getLocation(Point tipSize, Event event) {
395             // try to position the tooltip at the bottom of the cell
396             ViewerCell cell = viewer.getCell(new Point(event.x, event.y));
397             if( cell != null )
398                 return tree.toDisplay(event.x,cell.getBounds().y+cell.getBounds().height);
399             return super.getLocation(tipSize, event);
400         }
401         protected Object getToolTipArea(Event event) {
402             // Ensure that the tooltip is hidden when the cell is left
403             return viewer.getCell(new Point(event.x, event.y));
404         }
405         protected void addContent(Composite destination, Object modelElement) {
406             final DisplayElement item = (DisplayElement)modelElement;
407             if (DEBUG)
408                 System.out.println("add content");
409             String text = null;
410             if (null != item) {
411                 text = modelElement.toString();
412                 createEntryWithText(destination, null, text.toString());
413             }
414         }
415         protected Object getModelElement(Event event) {
416             org.eclipse.swt.widgets.TreeItem treeItem = tree.getItem(new Point(event.x, event.y));
417             if (treeItem == null)
418                 return null;
419             return treeItem.getData();
420         }
421     }*/
422     class MyColumnLabelProvider extends ColumnLabelProvider {
423         @Override
424         public void update(ViewerCell cell) {
425             //NodeProxy proxy = (NodeProxy) cell.getElement();
426             cell.setText("Kraa");
427             cell.setImage(Images.getInstance().OTHER_IMAGE);
428         }
429     }
430     protected void createControls(final Composite parent) {
431         parent.setLayout(LayoutUtils.createNoBorderGridLayout(1, false));
432         GridDataFactory.fillDefaults().grab(true, true).applyTo(treeViewer.getTree());
433         treeViewer.setContentProvider(new UndoContentProvider(Simantics.getSession()));
434         treeViewer.setLabelProvider(new LabelProvider() {
435             @Override
436             public Image getImage(Object element) {
437                 if (element instanceof ChangeSetElement)
438                     return Images.getInstance().CHANGE_SET_IMAGE;
439                 else if (element instanceof DisplayElement)
440                     return Images.getInstance().DISPLAY_IMAGE;
441                 else if (element instanceof ResourceElement)
442                     return Images.getInstance().RESOURCE_IMAGE;
443                 else if (element instanceof StringElement)
444                     return Images.getInstance().STRING_IMAGE;
445                 else if (element instanceof UndoCombinedElement)
446                     return Images.getInstance().COMBINED_IMAGE;
447                 else if (element instanceof RedoContextElement)
448                     return Images.getInstance().REDO_IMAGE;
449                 else if (element instanceof UndoContextElement)
450                     return Images.getInstance().UNDO_IMAGE;
451                 else
452                     return Images.getInstance().OTHER_IMAGE;
453             }
454             @Override
455             public String getText(Object element) {
456                 return element.toString();
457             }
458         });
459         //viewer.setSorter(new ViewerSorter());
460         treeViewer.setInput(this);
461         Control control = treeViewer.getControl();
462 //        ISelectionProvider selectionProvider = new ISelectionProvider() {
463 //            @Override
464 //            public void addSelectionChangedListener(ISelectionChangedListener listener) {
465 //            }
466 //            @Override
467 //            public ISelection getSelection() {
468 //                return null;
469 //            }
470 //            @Override
471 //            public void removeSelectionChangedListener(ISelectionChangedListener listener) {
472 //            }
473 //            @Override
474 //            public void setSelection(ISelection selection) {
475 //            }
476 //        };
477         createContextMenu(control, getSite());
478
479 //        ImageDescriptor desc = BundleUtils.getImageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_undo.png");
480 //        getViewSite().getActionBars().getToolBarManager().add(new Action("GetUndoHistory", desc) {
481 //            @Override
482 //            public void run() {
483 //                treeViewer.setContentProvider(new UndoContentProvider(Simantics.getSession()));
484 //            }
485 //        });
486 //        getViewSite().getActionBars().getToolBarManager().add(new Action("DumpSelectedRevision", Activator.BIN_CLOSED_ICON) {
487 //            @Override
488 //            public void run() {
489 //                IStructuredSelection is = (IStructuredSelection)viewer.getSelection();
490 //                Object o = is.getFirstElement();
491 //                if (o instanceof ChangeSetElement) {
492 //                    ChangeSetElement ce = (ChangeSetElement)o;
493 //                    long csid = ce.getChangeSetIdentifier().getId();
494 //                    ManagementSupport ms = Simantics.getSession().getService(ManagementSupport.class);
495 //                    try {
496 //                        ms.dumpRevision(csid);
497 //                        ShowMessage.showInformation("DumpSelectedRevision", "Operation was successful.");
498 //                    } catch (DatabaseException e) {
499 //                        ExceptionUtils.logAndShowError(e);
500 //                    }
501 //                }
502 //            }
503 //        });
504     }
505     public IMenuManager createContextMenu(Control control, IWorkbenchSite site) {
506         final MenuManager mm = new MenuManager("#TeamViewPopup", "#TeamViewPopup");
507         mm.setRemoveAllWhenShown(true);
508         mm.addMenuListener(new IMenuListener() {
509             @Override
510             public void menuAboutToShow(IMenuManager manager) {
511                 mm.add(new GroupMarker(IWorkbenchActionConstants.WB_START));
512             }
513         });
514         Menu menu = mm.createContextMenu(control);
515         control.setMenu(menu);
516         if (site != null) {
517             if (site instanceof IWorkbenchPartSite) {
518                 ((IWorkbenchPartSite) site).registerContextMenu(mm.getId(), mm, site.getSelectionProvider());
519             } else if (site instanceof IPageSite) {
520                 ((IPageSite) site).registerContextMenu(mm.getId(), mm, site.getSelectionProvider());
521             }
522         }
523         return mm;
524     }
525     /**
526      * @param parent
527      * @return
528      */
529     protected GraphExplorer createExplorerControl(Composite parent) {
530         return GraphExplorerFactory.getInstance().selectionDataResolver(new DefaultSelectionDataResolver()).create(parent);
531     }
532
533     // Needed for preventing unnecessary re-initialization of the explorer with the same input.
534 //    private Object currentInput;
535
536     /**
537      * Invoke this to reinitialize the explorer and reset its input. The input
538      * will be resolved from the specified ISessionContext based on the
539      * {@link SessionContextInputSource} that is currently in use. If the input
540      * is identical to the previous input, nothing will be done.
541      * 
542      * @param context
543      */
544     protected final boolean applySessionContext(ISessionContext context) {
545         // If control is not alive anymore, do nothing.
546         if (disposeState != DisposeState.Alive)
547             return false;
548
549         this.sessionContext = context;
550
551         // Start tracking the session context.
552         // This will cause IHintListeners of the sessionContextTracker to fire.
553         // For this we need the above input equality (identity) checking.
554         sessionContextTracker.track(sessionContext);
555         return true;
556     }
557 }
558
559 class IncomingContentProvider implements ITreeContentProvider {
560     class IncomingElement extends ChangeSetElement {
561         IncomingElement(Session session, ChangeSetIdentifier cs) {
562             super(session, cs);
563         }
564     }
565     IncomingElement[] changeSetIds;
566     IncomingContentProvider(Session session, Shell parent) {
567         //this.session = session;
568 //        PublishSynchroniseSupport ps = session.getService(PublishSynchroniseSupport.class);
569 //        try {
570             Collection<ChangeSetIdentifier> cids = new ArrayList<ChangeSetIdentifier>();
571             /*if (null == cids)
572                 changeSetIds = new IncomingElement[0];
573             else */{
574                 changeSetIds = new IncomingElement[cids.size()];
575                 Iterator<ChangeSetIdentifier> it = cids.iterator();
576                 int i = 0;
577                 while (it.hasNext()) {
578                     changeSetIds[i++] = new IncomingElement(session, it.next());
579                 }
580                 assert(i == cids.size());
581             }
582 //        } catch (DatabaseException e) {
583 //            changeSetIds = new IncomingElement[0];
584 //            ExceptionUtils.logAndShowError(e);
585 //        }
586     }
587     
588     @Override
589     public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
590     }
591
592     @Override
593     public void dispose() {
594     }
595
596     @Override
597     public Object[] getElements(Object inputElement) {
598         return changeSetIds;
599     }
600
601     @Override
602     public boolean hasChildren(Object element) {
603         return changeSetIds.length > 0;
604     }
605
606     @Override
607     public Object getParent(Object element) {
608         return null;
609     }
610
611     @Override
612     public Object[] getChildren(Object parentElement) {
613         return null;
614     }
615 }
616
617 class OutgoingContentProvider implements ITreeContentProvider {
618     class OutgoingElement extends ChangeSetElement {
619         OutgoingElement(Session session, ChangeSetIdentifier cs) {
620             super(session, cs);
621         }
622     }
623     OutgoingElement[] changeSetIds;
624     OutgoingContentProvider(Session session) {
625 //        PublishSynchroniseSupport ps = session.getService(PublishSynchroniseSupport.class);
626 //        try {
627             Collection<ChangeSetIdentifier> cids = new ArrayList<ChangeSetIdentifier>();
628             /*if (null == cids)
629                 changeSetIds = new OutgoingElement[0];
630             else */{
631                 changeSetIds = new OutgoingElement[cids.size()];
632                 Iterator<ChangeSetIdentifier> it = cids.iterator();
633                 int i = 0;
634                 while (it.hasNext()) {
635                     changeSetIds[i++] = new OutgoingElement(session, it.next());
636                 }
637                 assert(i == cids.size());
638             }
639 //        } catch (DatabaseException e) {
640 //            ExceptionUtils.logAndShowError(e);
641 //            changeSetIds = new OutgoingElement[0];
642 //        }
643     }
644     
645     @Override
646     public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
647     }
648
649     @Override
650     public void dispose() {
651     }
652
653     @Override
654     public Object[] getElements(Object inputElement) {
655         return changeSetIds;
656     }
657
658     @Override
659     public boolean hasChildren(Object element) {
660         if (element instanceof  TreeElement)
661             return ((TreeElement)element).hasChildren();
662         return false;
663     }
664
665     @Override
666     public Object getParent(Object element) {
667         return null;
668     }
669
670     @Override
671     public Object[] getChildren(Object parentElement) {
672         if (parentElement instanceof  TreeElement)
673             return ((TreeElement)parentElement).getChildren();
674         else
675             return null;
676     }
677 }
678
679
680
681 class ResourceElement extends TreeElement {
682     private Session session;
683     private String resourceUri;
684     private String resourceName;
685     private Resource resource;
686     ResourceElement(Session session, String resource) {
687         this.session = session;
688         this.resourceUri = resource;
689     }
690     ResourceElement(Session session, Resource resource) {
691         this.session = session;
692         this.resource = resource;
693     }
694     @Override
695     protected Image getIdImage() {
696         return Images.getInstance().RESOURCE_IMAGE;
697     }
698     @Override
699     protected String getIdText() {
700         String s = resource.toString();
701         return s.substring(0, Math.min(10, s.length()));
702     }
703     private String getNameByUri() {
704         try {
705             String t = session.syncRequest(new Read<String>() {
706                 @Override
707                 public String perform(ReadGraph g) throws DatabaseException {
708                     resource = g.getResource(resourceUri);
709                     Layer0 l0 = Layer0.getInstance(g);
710                     resourceName = g.getPossibleRelatedValue(resource, l0.HasName, Bindings.STRING);
711                     if (null == resourceName) {
712                         Set<Resource> types = g.getTypes(resource);
713                         if (types.size() > 0) {
714                             Iterator<Resource> i = types.iterator();
715                             Resource r = i.next();
716                             resourceName = g.getPossibleRelatedValue(r, l0.HasName, Bindings.STRING);
717                         }
718                     }
719                     if (null == resourceName)
720                         resourceName = resourceUri;
721                     return resourceName;
722                 }
723             });
724             return t;
725         } catch (DatabaseException e) {
726         }
727         return null;
728     }
729     private String getName() {
730         try {
731             String t = session.syncRequest(new Read<String>() {
732                 @Override
733                 public String perform(ReadGraph g) throws DatabaseException {
734                     Layer0 l0 = Layer0.getInstance(g);
735                     resourceName = g.getPossibleRelatedValue(resource, l0.HasName, Bindings.STRING);
736                     if (null == resourceName) {
737                         Set<Resource> types = g.getTypes(resource);
738                         if (types.size() > 0) {
739                             Iterator<Resource> i = types.iterator();
740                             Resource r = i.next();
741                             resourceName = g.getPossibleRelatedValue(r, l0.HasName, Bindings.STRING);
742                         }
743                     }
744                     if (null == resourceName)
745                         resourceName = resourceUri;
746                     return resourceName;
747                 }
748             });
749             return t;
750         } catch (DatabaseException e) {
751         }
752         return null;
753     }
754     private Resource getParent() {
755         try {
756             Resource parent = session.syncRequest(new Read<Resource>() {
757                 @Override
758                 public Resource perform(ReadGraph g) throws DatabaseException {
759                     Layer0 l0 = Layer0.getInstance(g);
760                     return g.getPossibleObject(resource, l0.PartOf);
761                 }
762                 
763             });
764             return parent;
765         } catch (DatabaseException e) {
766         }
767         return null;
768     }
769     public String toString() {
770         if (null == resourceName && null != resourceUri)
771             return getNameByUri();
772         else if (null != resource && null == resourceUri)
773             return getName();
774         return resourceName;
775     }
776     @Override
777     boolean hasChildren() {
778         Resource p = getParent();
779         if (null != p)
780             return true;
781         else
782             return false;
783     }
784     @Override
785     Object[] getChildren() {
786         Resource p = getParent();
787         if (null == p)
788             return new Object[0];
789         Object[] objects = new Object[1];
790         objects[0] = new ResourceElement(session, p);
791         return objects;
792     }
793 }