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