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
.commands
;
18 import javax
.jcr
.Repository
;
19 import javax
.jcr
.RepositoryFactory
;
21 import org
.argeo
.jcr
.ArgeoNames
;
22 import org
.argeo
.slc
.client
.ui
.dist
.DistPlugin
;
23 import org
.argeo
.slc
.client
.ui
.dist
.utils
.CommandHelpers
;
24 import org
.argeo
.slc
.client
.ui
.dist
.wizards
.RegisterRepoWizard
;
25 import org
.argeo
.slc
.jcr
.SlcNames
;
26 import org
.argeo
.util
.security
.Keyring
;
27 import org
.eclipse
.core
.commands
.AbstractHandler
;
28 import org
.eclipse
.core
.commands
.ExecutionEvent
;
29 import org
.eclipse
.core
.commands
.ExecutionException
;
30 import org
.eclipse
.jface
.dialogs
.Dialog
;
31 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
32 import org
.eclipse
.jface
.wizard
.WizardDialog
;
33 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
36 * Connect to a remote repository.
38 public class RegisterRepository
extends AbstractHandler
implements ArgeoNames
,
41 public final static String ID
= DistPlugin
.ID
+ ".registerRepository";
42 public final static String DEFAULT_LABEL
= "Register a repository...";
43 // public final static String DEFAULT_ICON_PATH = "icons/addRepo.gif";
44 public final static ImageDescriptor DEFAULT_ICON
= DistPlugin
45 .getImageDescriptor("icons/addRepo.gif");
48 /* DEPENDENCY INJECTION */
49 private RepositoryFactory repositoryFactory
;
50 private Repository nodeRepository
;
51 private Keyring keyring
;
53 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
54 RegisterRepoWizard wizard
= new RegisterRepoWizard(keyring
,
55 repositoryFactory
, nodeRepository
);
56 WizardDialog dialog
= new WizardDialog(
57 HandlerUtil
.getActiveShell(event
), wizard
);
58 int result
= dialog
.open();
59 if (result
== Dialog
.OK
)
60 CommandHelpers
.callCommand(RefreshDistributionsView
.ID
);
62 // RemoteRepositoryLoginDialog dlg = new RemoteRepositoryLoginDialog(
63 // Display.getDefault().getActiveShell());
64 // if (dlg.open() == Dialog.OK) {
69 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
70 this.repositoryFactory
= repositoryFactory
;
73 public void setKeyring(Keyring keyring
) {
74 this.keyring
= keyring
;
77 public void setNodeRepository(Repository nodeRepository
) {
78 this.nodeRepository
= nodeRepository
;
81 // class RemoteRepositoryLoginDialog extends TitleAreaDialog {
84 // private Text username;
85 // private Text password;
86 // private Button saveInKeyring;
88 // public RemoteRepositoryLoginDialog(Shell parentShell) {
89 // super(parentShell);
92 // protected Point getInitialSize() {
93 // return new Point(600, 400);
96 // protected Control createDialogArea(Composite parent) {
97 // Composite dialogarea = (Composite) super.createDialogArea(parent);
98 // dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
100 // Composite composite = new Composite(dialogarea, SWT.NONE);
101 // composite.setLayout(new GridLayout(2, false));
102 // composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
104 // setMessage("Login to remote repository", IMessageProvider.NONE);
105 // name = createLT(composite, "Name", "Example Java Repository");
106 // uri = createLT(composite, "URI",
107 // "https://example.com/data/jcr/java");
108 // username = createLT(composite, "User", "");
109 // password = createLP(composite, "Password");
111 // saveInKeyring = createLC(composite, "Remember password", false);
117 // protected void createButtonsForButtonBar(Composite parent) {
118 // super.createButtonsForButtonBar(parent);
119 // Button test = createButton(parent, 2, "Test", false);
120 // test.addSelectionListener(new SelectionAdapter() {
121 // public void widgetSelected(SelectionEvent arg0) {
127 // void testConnection() {
128 // Session session = null;
130 // if (uri.getText().startsWith("http")) {// http, https
131 // URI checkedUri = new URI(uri.getText());
132 // String checkedUriStr = checkedUri.toString();
133 // Hashtable<String, String> params = new Hashtable<String, String>();
134 // params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI,
136 // Repository repository = ArgeoJcrUtils.getRepositoryByUri(
137 // repositoryFactory, checkedUriStr);
138 // if (username.getText().trim().equals("")) {// anonymous
139 // session = repository.login();
141 // // FIXME use getTextChars() when upgrading to 3.7
143 // // https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
144 // char[] pwd = password.getText().toCharArray();
145 // SimpleCredentials sc = new SimpleCredentials(
146 // username.getText(), pwd);
147 // session = repository.login(sc);
150 // Repository repository = ArgeoJcrUtils.getRepositoryByAlias(
151 // repositoryFactory, uri.getText());
152 // session = repository.login();
154 // MessageDialog.openInformation(getParentShell(), "Success",
155 // "Connection to '" + uri.getText() + "' successful");
156 // } catch (Exception e) {
157 // ErrorFeedback.show(
158 // "Connection test failed for " + uri.getText(), e);
160 // JcrUtils.logoutQuietly(session);
165 // protected void okPressed() {
166 // Session nodeSession = null;
168 // nodeSession = nodeRepository.login();
169 // String reposPath = UserJcrUtils.getUserHome(nodeSession)
170 // .getPath() + RepoConstants.REPOSITORIES_BASE_PATH;
172 // Node repos = nodeSession.getNode(reposPath);
173 // String repoNodeName = JcrUtils.replaceInvalidChars(name
175 // if (repos.hasNode(repoNodeName))
176 // throw new ArgeoException(
177 // "There is already a remote repository named "
179 // Node repoNode = repos.addNode(repoNodeName,
180 // ArgeoTypes.ARGEO_REMOTE_REPOSITORY);
181 // repoNode.setProperty(ARGEO_URI, uri.getText());
182 // repoNode.setProperty(ARGEO_USER_ID, username.getText());
183 // repoNode.addMixin(NodeType.MIX_TITLE);
184 // repoNode.setProperty(Property.JCR_TITLE, name.getText());
185 // nodeSession.save();
186 // if (saveInKeyring.getSelection()) {
187 // String pwdPath = repoNode.getPath() + '/' + ARGEO_PASSWORD;
188 // keyring.set(pwdPath, password.getText().toCharArray());
189 // nodeSession.save();
191 // MessageDialog.openInformation(getParentShell(),
192 // "Repository Added",
193 // "Remote repository " + uri.getText() + "' added");
195 // super.okPressed();
196 // } catch (Exception e) {
197 // ErrorFeedback.show("Cannot add remote repository", e);
199 // JcrUtils.logoutQuietly(nodeSession);
203 // /** Creates label and text. */
204 // protected Text createLT(Composite parent, String label, String initial) {
205 // new Label(parent, SWT.NONE).setText(label);
206 // Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
207 // text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
208 // text.setText(initial);
212 // /** Creates label and check. */
213 // protected Button createLC(Composite parent, String label,
214 // Boolean initial) {
215 // new Label(parent, SWT.NONE).setText(label);
216 // Button check = new Button(parent, SWT.CHECK);
217 // check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
218 // check.setSelection(initial);
222 // protected Text createLP(Composite parent, String label) {
223 // new Label(parent, SWT.NONE).setText(label);
224 // Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
226 // text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));