2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.client
.ui
.dist
.views
;
18 import java
.util
.HashMap
;
21 import javax
.jcr
.Repository
;
22 import javax
.jcr
.RepositoryException
;
24 import org
.argeo
.eclipse
.ui
.TreeParent
;
25 import org
.argeo
.eclipse
.ui
.utils
.CommandUtils
;
26 import org
.argeo
.jcr
.ArgeoNames
;
27 import org
.argeo
.slc
.SlcException
;
28 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
29 import org
.argeo
.slc
.client
.ui
.dist
.commands
.CopyWorkspace
;
30 import org
.argeo
.slc
.client
.ui
.dist
.commands
.CreateWorkspace
;
31 import org
.argeo
.slc
.client
.ui
.dist
.commands
.DeleteWorkspace
;
32 import org
.argeo
.slc
.client
.ui
.dist
.commands
.DisplayRepoInformation
;
33 import org
.argeo
.slc
.client
.ui
.dist
.commands
.Fetch
;
34 import org
.argeo
.slc
.client
.ui
.dist
.commands
.MergeWorkspaces
;
35 import org
.argeo
.slc
.client
.ui
.dist
.commands
.NormalizeDistribution
;
36 import org
.argeo
.slc
.client
.ui
.dist
.commands
.NormalizeWorkspace
;
37 import org
.argeo
.slc
.client
.ui
.dist
.commands
.PublishWorkspace
;
38 import org
.argeo
.slc
.client
.ui
.dist
.commands
.RefreshDistributionsView
;
39 import org
.argeo
.slc
.client
.ui
.dist
.commands
.RegisterRepository
;
40 import org
.argeo
.slc
.client
.ui
.dist
.commands
.UnregisterRemoteRepo
;
41 import org
.argeo
.slc
.client
.ui
.dist
.controllers
.DistTreeComparator
;
42 import org
.argeo
.slc
.client
.ui
.dist
.controllers
.DistTreeContentProvider
;
43 import org
.argeo
.slc
.client
.ui
.dist
.controllers
.DistTreeDoubleClickListener
;
44 import org
.argeo
.slc
.client
.ui
.dist
.controllers
.DistTreeLabelProvider
;
45 import org
.argeo
.slc
.client
.ui
.dist
.model
.DistParentElem
;
46 import org
.argeo
.slc
.client
.ui
.dist
.model
.GroupElem
;
47 import org
.argeo
.slc
.client
.ui
.dist
.model
.RepoElem
;
48 import org
.argeo
.slc
.client
.ui
.dist
.model
.WorkspaceElem
;
49 import org
.argeo
.slc
.client
.ui
.dist
.utils
.CommandHelpers
;
50 import org
.argeo
.slc
.jcr
.SlcNames
;
51 import org
.eclipse
.jface
.action
.IContributionItem
;
52 import org
.eclipse
.jface
.action
.IMenuListener
;
53 import org
.eclipse
.jface
.action
.IMenuManager
;
54 import org
.eclipse
.jface
.action
.MenuManager
;
55 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
56 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
57 import org
.eclipse
.jface
.viewers
.TreeViewer
;
58 import org
.eclipse
.jface
.viewers
.TreeViewerColumn
;
59 import org
.eclipse
.jface
.viewers
.Viewer
;
60 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
61 import org
.eclipse
.swt
.SWT
;
62 import org
.eclipse
.swt
.dnd
.DND
;
63 import org
.eclipse
.swt
.dnd
.DragSourceAdapter
;
64 import org
.eclipse
.swt
.dnd
.DragSourceEvent
;
65 import org
.eclipse
.swt
.dnd
.TextTransfer
;
66 import org
.eclipse
.swt
.dnd
.Transfer
;
67 import org
.eclipse
.swt
.dnd
.TransferData
;
68 import org
.eclipse
.swt
.widgets
.Composite
;
69 import org
.eclipse
.swt
.widgets
.Menu
;
70 import org
.eclipse
.swt
.widgets
.Tree
;
71 import org
.eclipse
.ui
.IWorkbenchWindow
;
72 import org
.eclipse
.ui
.part
.ViewPart
;
75 * Browse, manipulate and manage distributions across multiple repositories
76 * (like fetch, merge, publish, etc.).
78 public class DistributionsView
extends ViewPart
implements SlcNames
, ArgeoNames
{
79 // private final static Log log =
80 // LogFactory.getLog(DistributionsView.class);
82 public final static String ID
= DistPlugin
.ID
+ ".distributionsView";
84 /* DEPENDENCY INJECTION */
85 private Repository nodeRepository
;
86 private DistTreeContentProvider treeContentProvider
;
88 private TreeViewer viewer
;
91 public void createPartControl(Composite parent
) {
92 // Define the TableViewer
93 viewer
= new TreeViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
94 | SWT
.FULL_SELECTION
| SWT
.BORDER
);
96 TreeViewerColumn col
= new TreeViewerColumn(viewer
, SWT
.NONE
);
97 col
.getColumn().setWidth(400);
98 col
.setLabelProvider(new DistTreeLabelProvider());
100 final Tree tree
= viewer
.getTree();
101 tree
.setHeaderVisible(false);
102 tree
.setLinesVisible(false);
104 // viewer.setContentProvider(new DistTreeContentProvider());
105 viewer
.setContentProvider(treeContentProvider
);
106 viewer
.addDoubleClickListener(new DistTreeDoubleClickListener());
107 viewer
.setComparator(new DistTreeComparator());
109 // Enable retrieving current tree selected items from outside the view
110 getSite().setSelectionProvider(viewer
);
113 Transfer
[] tt
= new Transfer
[] { TextTransfer
.getInstance() };
114 int operations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
115 viewer
.addDragSupport(operations
, tt
, new ViewDragListener());
116 viewer
.addDropSupport(operations
, tt
, new ViewDropListener(viewer
));
118 MenuManager menuManager
= new MenuManager();
119 Menu menu
= menuManager
.createContextMenu(viewer
.getTree());
120 menuManager
.addMenuListener(new IMenuListener() {
121 public void menuAboutToShow(IMenuManager manager
) {
122 contextMenuAboutToShow(manager
);
125 viewer
.getTree().setMenu(menu
);
126 getSite().registerContextMenu(menuManager
, viewer
);
132 /** Programatically configure the context menu */
133 protected void contextMenuAboutToShow(IMenuManager menuManager
) {
134 IWorkbenchWindow window
= DistPlugin
.getDefault().getWorkbench()
135 .getActiveWorkbenchWindow();
138 // Most of the implemented commands support only one selected
140 boolean singleElement
= ((IStructuredSelection
) viewer
141 .getSelection()).size() == 1;
142 // Get Current selected item :
143 Object firstElement
= ((IStructuredSelection
) viewer
.getSelection())
146 if (firstElement
instanceof TreeParent
147 || firstElement
instanceof DistParentElem
) {
149 String targetRepoPath
= null, workspaceName
= null, workspacePrefix
= null;
150 // String targetRepoUri = null;
151 // Build conditions depending on element type
152 boolean isDistribElem
= false, isRepoElem
= false, isDistribGroupElem
= false;
153 boolean isHomeRepo
= false, isReadOnly
= true;
157 if (firstElement
instanceof WorkspaceElem
) {
158 WorkspaceElem de
= (WorkspaceElem
) firstElement
;
159 re
= de
.getRepoElem();
160 isDistribElem
= true;
161 isReadOnly
= de
.isReadOnly();
162 workspaceName
= de
.getWorkspaceName();
163 } else if (firstElement
instanceof RepoElem
) {
164 re
= (RepoElem
) firstElement
;
166 isHomeRepo
= re
.inHome();
167 isReadOnly
= re
.isReadOnly();
168 } else if (firstElement
instanceof GroupElem
) {
169 GroupElem dge
= (GroupElem
) firstElement
;
170 isReadOnly
= dge
.isReadOnly();
171 isDistribGroupElem
= true;
172 re
= dge
.getRepoElem();
173 workspacePrefix
= dge
.getLabel();
177 // targetRepoUri = re.getUri();
178 targetRepoPath
= re
.getRepoNode().getPath();
182 CommandUtils
.refreshCommand(menuManager
, window
,
183 DisplayRepoInformation
.ID
,
184 DisplayRepoInformation
.DEFAULT_LABEL
,
185 DisplayRepoInformation
.DEFAULT_ICON
, isRepoElem
189 Map
<String
, String
> params
= new HashMap
<String
, String
>();
190 params
.put(CreateWorkspace
.PARAM_TARGET_REPO_PATH
,
192 params
.put(CreateWorkspace
.PARAM_WORKSPACE_PREFIX
,
194 CommandUtils
.refreshParametrizedCommand(menuManager
, window
,
195 CreateWorkspace
.ID
, CreateWorkspace
.DEFAULT_LABEL
,
196 CreateWorkspace
.DEFAULT_ICON
,
197 (isRepoElem
|| isDistribGroupElem
) && singleElement
198 && !isReadOnly
, params
);
200 // Register a remote repository
201 CommandUtils
.refreshCommand(menuManager
, window
,
202 RegisterRepository
.ID
,
203 RegisterRepository
.DEFAULT_LABEL
,
204 RegisterRepository
.DEFAULT_ICON
, isRepoElem
207 // Unregister a remote repository
208 params
= new HashMap
<String
, String
>();
209 params
.put(UnregisterRemoteRepo
.PARAM_REPO_PATH
, targetRepoPath
);
210 CommandUtils
.refreshParametrizedCommand(menuManager
, window
,
211 UnregisterRemoteRepo
.ID
,
212 UnregisterRemoteRepo
.DEFAULT_LABEL
,
213 UnregisterRemoteRepo
.DEFAULT_ICON
, isRepoElem
214 && !isHomeRepo
&& singleElement
, params
);
217 params
= new HashMap
<String
, String
>();
218 params
.put(Fetch
.PARAM_TARGET_REPO_PATH
, targetRepoPath
);
219 CommandUtils
.refreshParametrizedCommand(menuManager
, window
,
220 Fetch
.ID
, Fetch
.DEFAULT_LABEL
, Fetch
.DEFAULT_ICON
,
221 isRepoElem
&& isHomeRepo
&& singleElement
222 && !isReadOnly
, params
);
224 // Normalize workspace
225 params
= new HashMap
<String
, String
>();
226 params
.put(NormalizeWorkspace
.PARAM_TARGET_REPO_PATH
,
228 params
.put(NormalizeWorkspace
.PARAM_WORKSPACE_NAME
,
231 CommandUtils
.refreshParametrizedCommand(menuManager
, window
,
232 NormalizeWorkspace
.ID
, "Normalize...",
233 NormalizeWorkspace
.DEFAULT_ICON
, isDistribElem
234 && singleElement
&& !isReadOnly
, params
);
237 params
= new HashMap
<String
, String
>();
238 params
.put(CopyWorkspace
.PARAM_TARGET_REPO_PATH
, targetRepoPath
);
239 params
.put(CopyWorkspace
.PARAM_SOURCE_WORKSPACE_NAME
,
241 CommandUtils
.refreshParametrizedCommand(menuManager
, window
,
242 CopyWorkspace
.ID
, CopyWorkspace
.DEFAULT_LABEL
,
243 CopyWorkspace
.DEFAULT_ICON
, isDistribElem
244 && singleElement
, params
);
247 params
= new HashMap
<String
, String
>();
248 params
.put(DeleteWorkspace
.PARAM_TARGET_REPO_PATH
,
250 params
.put(DeleteWorkspace
.PARAM_WORKSPACE_NAME
, workspaceName
);
251 CommandUtils
.refreshParametrizedCommand(menuManager
, window
,
252 DeleteWorkspace
.ID
, DeleteWorkspace
.DEFAULT_LABEL
,
253 DeleteWorkspace
.DEFAULT_ICON
, isDistribElem
254 && singleElement
&& !isReadOnly
, params
);
257 MenuManager submenu
= new MenuManager("Advanced", DistPlugin
.ID
258 + ".advancedSubmenu");
259 IContributionItem ici
= menuManager
.find(DistPlugin
.ID
260 + ".advancedSubmenu");
262 menuManager
.remove(ici
);
264 // Normalize distribution (Legacy)
265 params
= new HashMap
<String
, String
>();
266 params
.put(NormalizeDistribution
.PARAM_TARGET_REPO_PATH
,
268 params
.put(NormalizeDistribution
.PARAM_WORKSPACE_NAME
,
270 CommandUtils
.refreshParametrizedCommand(submenu
, window
,
271 NormalizeDistribution
.ID
,
272 NormalizeDistribution
.DEFAULT_LABEL
,
273 NormalizeDistribution
.DEFAULT_ICON
, isDistribElem
274 && singleElement
&& !isReadOnly
, params
);
277 params
= new HashMap
<String
, String
>();
278 params
.put(PublishWorkspace
.PARAM_TARGET_REPO_PATH
,
280 params
.put(PublishWorkspace
.PARAM_WORKSPACE_NAME
, workspaceName
);
281 CommandUtils
.refreshParametrizedCommand(submenu
, window
,
282 PublishWorkspace
.ID
, PublishWorkspace
.DEFAULT_LABEL
,
283 PublishWorkspace
.DEFAULT_ICON
, isDistribElem
284 && singleElement
&& !isReadOnly
, params
);
286 if (submenu
.getSize() > 0)
287 menuManager
.add(submenu
);
289 // // Manage workspace authorizations
290 // params = new HashMap<String, String>();
291 // params.put(ManageWorkspaceAuth.PARAM_WORKSPACE_NAME, wsName);
292 // CommandHelpers.refreshParameterizedCommand(menuManager,
294 // ManageWorkspaceAuth.ID, ManageWorkspaceAuth.DEFAULT_LABEL,
295 // ManageWorkspaceAuth.DEFAULT_ICON_PATH, isDistribElem
296 // && singleElement && !isReadOnly, params);
298 } catch (RepositoryException e
) {
299 throw new SlcException("unexpected errror while "
300 + "building context menu", e
);
305 public void setFocus() {
306 viewer
.getTree().setFocus();
310 * Force refresh of the whole view
312 public void refresh() {
313 viewer
.setInput(nodeRepository
);
314 viewer
.expandToLevel(2);
317 /** Listens to drag */
318 class ViewDragListener
extends DragSourceAdapter
{
319 public void dragSetData(DragSourceEvent event
) {
320 IStructuredSelection selection
= (IStructuredSelection
) viewer
322 if (selection
.getFirstElement() instanceof WorkspaceElem
) {
323 WorkspaceElem de
= (WorkspaceElem
) selection
.getFirstElement();
324 if (TextTransfer
.getInstance().isSupportedType(event
.dataType
)) {
325 event
.data
= de
.getRepoElem().getUri() + "/"
326 + de
.getWorkspaceName();
332 /** Listens to drop */
333 class ViewDropListener
extends ViewerDropAdapter
{
335 public ViewDropListener(Viewer viewer
) {
340 public boolean performDrop(Object data
) {
341 WorkspaceElem sourceDist
= (WorkspaceElem
) getSelectedObject();
342 RepoElem targetRepo
= (RepoElem
) getCurrentTarget();
344 Boolean ok
= MessageDialog
.openConfirm(getSite().getShell(),
345 "Confirm distribution merge", "Do you want to merge "
346 + sourceDist
.getWorkspaceName() + " (from repo "
347 + sourceDist
.getRepoElem().getLabel()
348 + ") to repo " + targetRepo
.getLabel() + "?");
353 Map
<String
, String
> params
= new HashMap
<String
, String
>();
354 params
.put(MergeWorkspaces
.PARAM_TARGET_REPO_PATH
, targetRepo
355 .getRepoNode().getPath());
356 params
.put(MergeWorkspaces
.PARAM_SOURCE_REPO_PATH
, sourceDist
357 .getRepoElem().getRepoNode().getPath());
358 params
.put(MergeWorkspaces
.PARAM_SOURCE_WORKSPACE_NAME
,
359 sourceDist
.getWorkspaceName());
360 CommandHelpers
.callCommand(RefreshDistributionsView
.ID
, params
);
362 } catch (RepositoryException e
) {
363 throw new SlcException("Cannot process drop from " + sourceDist
364 + " to " + targetRepo
, e
);
369 public boolean validateDrop(Object target
, int operation
,
370 TransferData transferType
) {
371 if (target
instanceof RepoElem
) {
372 if (getSelectedObject() instanceof WorkspaceElem
) {
373 // check if not same repository
374 String srcRepoUri
= ((WorkspaceElem
) getSelectedObject())
375 .getRepoElem().getUri();
376 String targetRepoUri
= ((RepoElem
) target
).getUri();
377 return !targetRepoUri
.equals(srcRepoUri
);
385 * DEPENDENCY INJECTION
387 public void setNodeRepository(Repository repository
) {
388 this.nodeRepository
= repository
;
391 public void setTreeContentProvider(
392 DistTreeContentProvider treeContentProvider
) {
393 this.treeContentProvider
= treeContentProvider
;