2 * Copyright (C) 2007-2012 Mathieu Baudier
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
;
23 import javax
.jcr
.Session
;
25 import org
.apache
.commons
.logging
.Log
;
26 import org
.apache
.commons
.logging
.LogFactory
;
27 import org
.argeo
.eclipse
.ui
.AbstractTreeContentProvider
;
28 import org
.argeo
.eclipse
.ui
.ErrorFeedback
;
29 import org
.argeo
.eclipse
.ui
.TreeParent
;
30 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
31 import org
.argeo
.slc
.client
.ui
.dist
.commands
.CopyWorkspace
;
32 import org
.argeo
.slc
.client
.ui
.dist
.commands
.CreateWorkspace
;
33 import org
.argeo
.slc
.client
.ui
.dist
.commands
.DeleteWorkspace
;
34 import org
.argeo
.slc
.client
.ui
.dist
.commands
.ManageWorkspaceAuth
;
35 import org
.argeo
.slc
.client
.ui
.dist
.commands
.NormalizeDistribution
;
36 import org
.argeo
.slc
.client
.ui
.dist
.editors
.DistributionEditor
;
37 import org
.argeo
.slc
.client
.ui
.dist
.editors
.DistributionEditorInput
;
38 import org
.argeo
.slc
.client
.ui
.dist
.utils
.CommandHelpers
;
39 import org
.argeo
.slc
.jcr
.SlcNames
;
40 import org
.eclipse
.jface
.action
.IMenuListener
;
41 import org
.eclipse
.jface
.action
.IMenuManager
;
42 import org
.eclipse
.jface
.action
.MenuManager
;
43 import org
.eclipse
.jface
.viewers
.ColumnLabelProvider
;
44 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
45 import org
.eclipse
.jface
.viewers
.IDoubleClickListener
;
46 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
47 import org
.eclipse
.jface
.viewers
.TreeViewer
;
48 import org
.eclipse
.jface
.viewers
.TreeViewerColumn
;
49 import org
.eclipse
.swt
.SWT
;
50 import org
.eclipse
.swt
.widgets
.Composite
;
51 import org
.eclipse
.swt
.widgets
.Menu
;
52 import org
.eclipse
.swt
.widgets
.Tree
;
53 import org
.eclipse
.ui
.IWorkbenchWindow
;
54 import org
.eclipse
.ui
.PartInitException
;
55 import org
.eclipse
.ui
.part
.ViewPart
;
58 * Browse and manipulate distributions (like merge, rename, etc.). Only support
59 * one single repository currently.
62 public class DistributionsView
extends ViewPart
implements SlcNames
{
63 private final static Log log
= LogFactory
.getLog(DistributionsView
.class);
64 public final static String ID
= DistPlugin
.ID
+ ".distributionsView";
66 private Repository repository
;
68 private TreeViewer viewer
;
71 public void createPartControl(Composite parent
) {
72 // Define the TableViewer
73 viewer
= new TreeViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
74 | SWT
.FULL_SELECTION
| SWT
.BORDER
);
76 TreeViewerColumn col
= new TreeViewerColumn(viewer
, SWT
.NONE
);
77 col
.getColumn().setWidth(200);
78 col
.getColumn().setText("Workspace");
79 col
.setLabelProvider(new ColumnLabelProvider() {
81 public String
getText(Object element
) {
82 return element
.toString();
86 final Tree table
= viewer
.getTree();
87 table
.setHeaderVisible(true);
88 table
.setLinesVisible(true);
90 viewer
.setContentProvider(new DistributionsContentProvider());
91 viewer
.addDoubleClickListener(new DistributionsDCL());
93 MenuManager menuManager
= new MenuManager();
94 Menu menu
= menuManager
.createContextMenu(viewer
.getTree());
95 menuManager
.addMenuListener(new IMenuListener() {
96 public void menuAboutToShow(IMenuManager manager
) {
97 contextMenuAboutToShow(manager
);
100 viewer
.getTree().setMenu(menu
);
101 getSite().registerContextMenu(menuManager
, viewer
);
103 viewer
.setInput(getSite());
108 public void setFocus() {
109 viewer
.getTree().setFocus();
113 * Force refresh of the whole view
115 public void refresh() {
116 viewer
.setContentProvider(new DistributionsContentProvider());
119 public void setRepository(Repository repository
) {
120 this.repository
= repository
;
123 /** Programatically configure the context menu */
124 protected void contextMenuAboutToShow(IMenuManager menuManager
) {
125 IWorkbenchWindow window
= DistPlugin
.getDefault().getWorkbench()
126 .getActiveWorkbenchWindow();
127 // Get Current selected item :
128 Object firstElement
= ((IStructuredSelection
) viewer
.getSelection())
131 if (firstElement
instanceof TreeParent
) {
132 TreeParent tp
= (TreeParent
) firstElement
;
133 String wsName
= tp
.getName();
135 // Build conditions depending on element type (repo or distribution
136 // for the time being)
137 boolean isDistribElem
= false; // , isRepoElem = false;
139 // if (tp instanceof RepositoryElem){
140 // isRepoElem = true;
142 if (tp
instanceof DistributionElem
) {
143 isDistribElem
= true;
147 CommandHelpers
.refreshCommand(menuManager
, window
,
148 CreateWorkspace
.ID
, CreateWorkspace
.DEFAULT_LABEL
,
149 CreateWorkspace
.DEFAULT_ICON_PATH
,
150 tp
instanceof RepositoryElem
);
152 // Normalize workspace
153 Map
<String
, String
> params
= new HashMap
<String
, String
>();
154 params
.put(NormalizeDistribution
.PARAM_WORKSPACE
, wsName
);
155 CommandHelpers
.refreshParameterizedCommand(menuManager
, window
,
156 NormalizeDistribution
.ID
,
157 NormalizeDistribution
.DEFAULT_LABEL
,
158 NormalizeDistribution
.DEFAULT_ICON_PATH
, isDistribElem
,
162 params
= new HashMap
<String
, String
>();
163 params
.put(CopyWorkspace
.PARAM_WORKSPACE_NAME
, wsName
);
164 CommandHelpers
.refreshParameterizedCommand(menuManager
, window
,
165 CopyWorkspace
.ID
, CopyWorkspace
.DEFAULT_LABEL
,
166 CopyWorkspace
.DEFAULT_ICON_PATH
, isDistribElem
, params
);
169 params
= new HashMap
<String
, String
>();
170 params
.put(DeleteWorkspace
.PARAM_WORKSPACE_NAME
, wsName
);
171 CommandHelpers
.refreshParameterizedCommand(menuManager
, window
,
172 DeleteWorkspace
.ID
, DeleteWorkspace
.DEFAULT_LABEL
,
173 DeleteWorkspace
.DEFAULT_ICON_PATH
, isDistribElem
, params
);
175 // Manage workspace authorizations
176 params
= new HashMap
<String
, String
>();
177 params
.put(ManageWorkspaceAuth
.PARAM_WORKSPACE_NAME
, wsName
);
178 CommandHelpers
.refreshParameterizedCommand(menuManager
, window
,
179 ManageWorkspaceAuth
.ID
, ManageWorkspaceAuth
.DEFAULT_LABEL
,
180 ManageWorkspaceAuth
.DEFAULT_ICON_PATH
, isDistribElem
,
185 private class DistributionsContentProvider
extends
186 AbstractTreeContentProvider
{
188 public Object
[] getElements(Object arg0
) {
189 return new Object
[] { new RepositoryElem("java", repository
) };
194 private static class RepositoryElem
extends TreeParent
{
195 // private final Repository repository;
196 private Session defaultSession
;
198 public RepositoryElem(String name
, Repository repository
) {
200 // this.repository = repository;
202 defaultSession
= repository
.login();
203 String
[] workspaceNames
= defaultSession
.getWorkspace()
204 .getAccessibleWorkspaceNames();
205 for (String workspace
: workspaceNames
)
206 addChild(new DistributionElem(repository
, workspace
));
207 } catch (RepositoryException e
) {
208 ErrorFeedback
.show("Cannot log to repository", e
);
213 public synchronized void dispose() {
214 if (log
.isTraceEnabled())
215 log
.trace("Disposing RepositoryElement");
216 if (defaultSession
!= null)
217 defaultSession
.logout();
222 private static class DistributionElem
extends TreeParent
{
223 private final String workspaceName
;
224 private final Repository repository
;
226 public DistributionElem(Repository repository
, String workspaceName
) {
227 super(workspaceName
);
228 this.workspaceName
= workspaceName
;
229 this.repository
= repository
;
232 public String
getWorkspaceName() {
233 return workspaceName
;
236 public Repository
getRepository() {
242 public void dispose() {
246 private class DistributionsDCL
implements IDoubleClickListener
{
248 public void doubleClick(DoubleClickEvent event
) {
249 if (event
.getSelection() == null || event
.getSelection().isEmpty())
251 Object obj
= ((IStructuredSelection
) event
.getSelection())
253 if (obj
instanceof DistributionElem
) {
254 DistributionElem distributionElem
= (DistributionElem
) obj
;
255 DistributionEditorInput dei
= new DistributionEditorInput(
256 distributionElem
.getRepository(),
257 distributionElem
.getWorkspaceName());
259 DistPlugin
.getDefault().getWorkbench()
260 .getActiveWorkbenchWindow().getActivePage()
261 .openEditor(dei
, DistributionEditor
.ID
);
262 } catch (PartInitException e
) {
263 ErrorFeedback
.show("Cannot open editor for "
264 + distributionElem
.getWorkspaceName(), e
);