View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
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
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
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.
15   */
16  package org.argeo.slc.client.ui.dist.commands;
17  
18  import javax.jcr.Repository;
19  import javax.jcr.Session;
20  
21  import org.argeo.jcr.JcrUtils;
22  import org.argeo.slc.client.ui.dist.DistPlugin;
23  import org.argeo.slc.client.ui.dist.model.RepoElem;
24  import org.argeo.slc.repo.RepoService;
25  import org.eclipse.core.commands.AbstractHandler;
26  import org.eclipse.core.commands.ExecutionEvent;
27  import org.eclipse.core.commands.ExecutionException;
28  import org.eclipse.jface.dialogs.Dialog;
29  import org.eclipse.jface.dialogs.IDialogConstants;
30  import org.eclipse.jface.resource.ImageDescriptor;
31  import org.eclipse.jface.viewers.IStructuredSelection;
32  import org.eclipse.swt.SWT;
33  import org.eclipse.swt.graphics.Point;
34  import org.eclipse.swt.layout.GridData;
35  import org.eclipse.swt.layout.GridLayout;
36  import org.eclipse.swt.widgets.Button;
37  import org.eclipse.swt.widgets.Composite;
38  import org.eclipse.swt.widgets.Control;
39  import org.eclipse.swt.widgets.Label;
40  import org.eclipse.swt.widgets.Shell;
41  import org.eclipse.swt.widgets.Text;
42  import org.eclipse.ui.handlers.HandlerUtil;
43  
44  /**
45   * Open a dialog that displays various information on the current repository.
46   */
47  public class DisplayRepoInformation extends AbstractHandler {
48  	public final static String ID = DistPlugin.PLUGIN_ID + ".displayRepoInformation";
49  	public final static String DEFAULT_LABEL = "Information";
50  	public final static ImageDescriptor DEFAULT_ICON = DistPlugin
51  			.getImageDescriptor("icons/help.gif");
52  
53  	/* DEPENDENCY INJECTION */
54  	private RepoService repoService;
55  	private Repository nodeRepository;
56  
57  	public Object execute(ExecutionEvent event) throws ExecutionException {
58  		IStructuredSelection iss = (IStructuredSelection) HandlerUtil
59  				.getActiveSite(event).getSelectionProvider().getSelection();
60  		if (iss.getFirstElement() instanceof RepoElem) {
61  			RepoElem re = (RepoElem) iss.getFirstElement();
62  
63  			Session defaultSession = null;
64  			try {
65  				defaultSession = repoService.getRemoteSession(re.getRepoNodePath(),
66  						re.getUri(), null);
67  
68  				InformationDialog inputDialog = new InformationDialog(
69  						HandlerUtil.getActiveSite(event).getShell());
70  				inputDialog.create();
71  				// TODO add more information.
72  				inputDialog.loginTxt.setText(defaultSession.getUserID());
73  				inputDialog.nameTxt.setText(re.getLabel());
74  				inputDialog.uriTxt.setText(re.getUri());
75  				inputDialog.readOnlyBtn.setSelection(re.isReadOnly());
76  
77  				inputDialog.open();
78  				// } catch (RepositoryException e) {
79  				// throw new SlcException("Unexpected error while "
80  				// + "getting repository information.", e);
81  			} finally {
82  				JcrUtils.logoutQuietly(defaultSession);
83  			}
84  		}
85  		return null;
86  	}
87  
88  	private class InformationDialog extends Dialog {
89  		Text nameTxt;
90  		Text uriTxt;
91  		Text loginTxt;
92  		Button readOnlyBtn;
93  
94  		@Override
95  		protected void createButtonsForButtonBar(Composite parent) {
96  			// No Cancel button
97  			createButton(parent, IDialogConstants.OK_ID, "OK", true);
98  		}
99  
100 		public InformationDialog(Shell parentShell) {
101 			super(parentShell);
102 		}
103 
104 		protected Point getInitialSize() {
105 			return new Point(500, 250);
106 		}
107 
108 		protected Control createDialogArea(Composite parent) {
109 			Composite dialogarea = (Composite) super.createDialogArea(parent);
110 			dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
111 					true));
112 			Composite composite = new Composite(dialogarea, SWT.NONE);
113 			GridLayout layout = new GridLayout(2, false);
114 			layout.horizontalSpacing = 15;
115 			composite.setLayout(layout);
116 			GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
117 			composite.setLayoutData(gd);
118 
119 			nameTxt = createLT(composite, "Name");
120 			uriTxt = createLT(composite, "URI");
121 			loginTxt = createLT(composite, "Logged as");
122 			readOnlyBtn = createLC(composite, "Read only");
123 			parent.pack();
124 			return composite;
125 		}
126 
127 		/** Creates label and text. */
128 		protected Text createLT(Composite parent, String label) {
129 			new Label(parent, SWT.RIGHT).setText(label);
130 			Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.NONE);
131 			text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
132 			text.setEditable(false);
133 			return text;
134 		}
135 
136 		/** Creates label and check. */
137 		protected Button createLC(Composite parent, String label) {
138 			new Label(parent, SWT.RIGHT).setText(label);
139 			Button check = new Button(parent, SWT.CHECK);
140 			check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
141 			check.setEnabled(false);
142 			return check;
143 		}
144 
145 		protected void configureShell(Shell shell) {
146 			super.configureShell(shell);
147 			shell.setText("Repository information");
148 		}
149 	}
150 
151 	/* DEPENDENCY INJECTION */
152 	public void setRepoService(RepoService repoService) {
153 		this.repoService = repoService;
154 	}
155 
156 	public void setNodeRepository(Repository nodeRepository) {
157 		this.nodeRepository = nodeRepository;
158 	}
159 
160 }