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
.editors
.DistributionEditor
;
36 import org
.argeo
.slc
.client
.ui
.dist
.editors
.DistributionEditorInput
;
37 import org
.argeo
.slc
.client
.ui
.dist
.utils
.CommandHelpers
;
38 import org
.argeo
.slc
.jcr
.SlcNames
;
39 import org
.eclipse
.jface
.action
.IMenuListener
;
40 import org
.eclipse
.jface
.action
.IMenuManager
;
41 import org
.eclipse
.jface
.action
.MenuManager
;
42 import org
.eclipse
.jface
.viewers
.ColumnLabelProvider
;
43 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
44 import org
.eclipse
.jface
.viewers
.IDoubleClickListener
;
45 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
46 import org
.eclipse
.jface
.viewers
.TreeViewer
;
47 import org
.eclipse
.jface
.viewers
.TreeViewerColumn
;
48 import org
.eclipse
.swt
.SWT
;
49 import org
.eclipse
.swt
.widgets
.Composite
;
50 import org
.eclipse
.swt
.widgets
.Menu
;
51 import org
.eclipse
.swt
.widgets
.Tree
;
52 import org
.eclipse
.ui
.IWorkbenchWindow
;
53 import org
.eclipse
.ui
.PartInitException
;
54 import org
.eclipse
.ui
.part
.ViewPart
;
57 * Browse and manipulate distributions (like merge, rename, etc.). Only support
58 * one single repository currently.
61 public class DistributionsView
extends ViewPart
implements SlcNames
{
62 private final static Log log
= LogFactory
.getLog(DistributionsView
.class);
63 public final static String ID
= DistPlugin
.ID
+ ".distributionsView";
65 private Repository repository
;
67 private TreeViewer viewer
;
70 public void createPartControl(Composite parent
) {
71 // Define the TableViewer
72 viewer
= new TreeViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
73 | SWT
.FULL_SELECTION
| SWT
.BORDER
);
75 TreeViewerColumn col
= new TreeViewerColumn(viewer
, SWT
.NONE
);
76 col
.getColumn().setWidth(200);
77 col
.getColumn().setText("Workspace");
78 col
.setLabelProvider(new ColumnLabelProvider() {
80 public String
getText(Object element
) {
81 return element
.toString();
85 final Tree table
= viewer
.getTree();
86 table
.setHeaderVisible(true);
87 table
.setLinesVisible(true);
89 viewer
.setContentProvider(new DistributionsContentProvider());
90 viewer
.addDoubleClickListener(new DistributionsDCL());
92 MenuManager menuManager
= new MenuManager();
93 Menu menu
= menuManager
.createContextMenu(viewer
.getTree());
94 menuManager
.addMenuListener(new IMenuListener() {
95 public void menuAboutToShow(IMenuManager manager
) {
96 contextMenuAboutToShow(manager
);
99 viewer
.getTree().setMenu(menu
);
100 getSite().registerContextMenu(menuManager
, viewer
);
102 viewer
.setInput(getSite());
107 public void setFocus() {
108 viewer
.getTree().setFocus();
112 * Force refresh of the whole view
114 public void refresh() {
115 viewer
.setContentProvider(new DistributionsContentProvider());
118 public void setRepository(Repository repository
) {
119 this.repository
= repository
;
122 /** Programatically configure the context menu */
123 protected void contextMenuAboutToShow(IMenuManager menuManager
) {
124 IWorkbenchWindow window
= DistPlugin
.getDefault().getWorkbench()
125 .getActiveWorkbenchWindow();
126 // Get Current selected item :
127 Object firstElement
= ((IStructuredSelection
) viewer
.getSelection())
130 if (firstElement
instanceof TreeParent
) {
131 TreeParent tp
= (TreeParent
) firstElement
;
132 String wsName
= tp
.getName();
134 // Build conditions depending on element type (repo or distribution
135 // for the time being)
136 boolean isDistribElem
= false; // , isRepoElem = false;
138 // if (tp instanceof RepositoryElem){
139 // isRepoElem = true;
141 if (tp
instanceof DistributionElem
) {
142 isDistribElem
= true;
146 CommandHelpers
.refreshCommand(menuManager
, window
,
147 CreateWorkspace
.ID
, CreateWorkspace
.DEFAULT_LABEL
,
148 CreateWorkspace
.DEFAULT_ICON_PATH
, true);
151 Map
<String
, String
> params
= new HashMap
<String
, String
>();
152 params
.put(CopyWorkspace
.PARAM_WORKSPACE_NAME
, wsName
);
153 CommandHelpers
.refreshParameterizedCommand(menuManager
, window
,
154 CopyWorkspace
.ID
, CopyWorkspace
.DEFAULT_LABEL
,
155 CopyWorkspace
.DEFAULT_ICON_PATH
, isDistribElem
, params
);
158 params
= new HashMap
<String
, String
>();
159 params
.put(DeleteWorkspace
.PARAM_WORKSPACE_NAME
, wsName
);
160 CommandHelpers
.refreshParameterizedCommand(menuManager
, window
,
161 DeleteWorkspace
.ID
, DeleteWorkspace
.DEFAULT_LABEL
,
162 DeleteWorkspace
.DEFAULT_ICON_PATH
, isDistribElem
, params
);
164 // Manage workspace authorizations
165 params
= new HashMap
<String
, String
>();
166 params
.put(ManageWorkspaceAuth
.PARAM_WORKSPACE_NAME
, wsName
);
167 CommandHelpers
.refreshParameterizedCommand(menuManager
, window
,
168 ManageWorkspaceAuth
.ID
, ManageWorkspaceAuth
.DEFAULT_LABEL
,
169 ManageWorkspaceAuth
.DEFAULT_ICON_PATH
, isDistribElem
,
174 private class DistributionsContentProvider
extends
175 AbstractTreeContentProvider
{
177 public Object
[] getElements(Object arg0
) {
178 return new Object
[] { new RepositoryElem("java", repository
) };
183 private static class RepositoryElem
extends TreeParent
{
184 // private final Repository repository;
185 private Session defaultSession
;
187 public RepositoryElem(String name
, Repository repository
) {
189 // this.repository = repository;
191 defaultSession
= repository
.login();
192 String
[] workspaceNames
= defaultSession
.getWorkspace()
193 .getAccessibleWorkspaceNames();
194 for (String workspace
: workspaceNames
)
195 addChild(new DistributionElem(repository
, workspace
));
196 } catch (RepositoryException e
) {
197 ErrorFeedback
.show("Cannot log to repository", e
);
202 public synchronized void dispose() {
203 if (log
.isTraceEnabled())
204 log
.trace("Disposing RepositoryElement");
205 if (defaultSession
!= null)
206 defaultSession
.logout();
211 private static class DistributionElem
extends TreeParent
{
212 private final String workspaceName
;
213 private final Repository repository
;
215 public DistributionElem(Repository repository
, String workspaceName
) {
216 super(workspaceName
);
217 this.workspaceName
= workspaceName
;
218 this.repository
= repository
;
221 public String
getWorkspaceName() {
222 return workspaceName
;
225 public Repository
getRepository() {
231 public void dispose() {
235 private class DistributionsDCL
implements IDoubleClickListener
{
237 public void doubleClick(DoubleClickEvent event
) {
238 if (event
.getSelection() == null || event
.getSelection().isEmpty())
240 Object obj
= ((IStructuredSelection
) event
.getSelection())
242 if (obj
instanceof DistributionElem
) {
243 DistributionElem distributionElem
= (DistributionElem
) obj
;
244 DistributionEditorInput dei
= new DistributionEditorInput(
245 distributionElem
.getRepository(),
246 distributionElem
.getWorkspaceName());
248 DistPlugin
.getDefault().getWorkbench()
249 .getActiveWorkbenchWindow().getActivePage()
250 .openEditor(dei
, DistributionEditor
.ID
);
251 } catch (PartInitException e
) {
252 ErrorFeedback
.show("Cannot open editor for "
253 + distributionElem
.getWorkspaceName(), e
);