]> git.argeo.org Git - gpl/argeo-slc.git/blob - dist/commands/RegisterRepository.java
Prepare next development cycle
[gpl/argeo-slc.git] / dist / commands / RegisterRepository.java
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.RepositoryFactory;
20
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;
34
35 /**
36 * Connect to a remote repository.
37 */
38 public class RegisterRepository extends AbstractHandler implements ArgeoNames,
39 SlcNames {
40
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");
46
47
48 /* DEPENDENCY INJECTION */
49 private RepositoryFactory repositoryFactory;
50 private Repository nodeRepository;
51 private Keyring keyring;
52
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);
61
62 // RemoteRepositoryLoginDialog dlg = new RemoteRepositoryLoginDialog(
63 // Display.getDefault().getActiveShell());
64 // if (dlg.open() == Dialog.OK) {
65 // }
66 return null;
67 }
68
69 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
70 this.repositoryFactory = repositoryFactory;
71 }
72
73 public void setKeyring(Keyring keyring) {
74 this.keyring = keyring;
75 }
76
77 public void setNodeRepository(Repository nodeRepository) {
78 this.nodeRepository = nodeRepository;
79 }
80
81 // class RemoteRepositoryLoginDialog extends TitleAreaDialog {
82 // private Text name;
83 // private Text uri;
84 // private Text username;
85 // private Text password;
86 // private Button saveInKeyring;
87 //
88 // public RemoteRepositoryLoginDialog(Shell parentShell) {
89 // super(parentShell);
90 // }
91 //
92 // protected Point getInitialSize() {
93 // return new Point(600, 400);
94 // }
95 //
96 // protected Control createDialogArea(Composite parent) {
97 // Composite dialogarea = (Composite) super.createDialogArea(parent);
98 // dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
99 // 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,
103 // false));
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");
110 //
111 // saveInKeyring = createLC(composite, "Remember password", false);
112 // parent.pack();
113 // return composite;
114 // }
115 //
116 // @Override
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) {
122 // testConnection();
123 // }
124 // });
125 // }
126 //
127 // void testConnection() {
128 // Session session = null;
129 // try {
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,
135 // checkedUriStr);
136 // Repository repository = ArgeoJcrUtils.getRepositoryByUri(
137 // repositoryFactory, checkedUriStr);
138 // if (username.getText().trim().equals("")) {// anonymous
139 // session = repository.login();
140 // } else {
141 // // FIXME use getTextChars() when upgrading to 3.7
142 // // see
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);
148 // }
149 // } else {// alias
150 // Repository repository = ArgeoJcrUtils.getRepositoryByAlias(
151 // repositoryFactory, uri.getText());
152 // session = repository.login();
153 // }
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);
159 // } finally {
160 // JcrUtils.logoutQuietly(session);
161 // }
162 // }
163 //
164 // @Override
165 // protected void okPressed() {
166 // Session nodeSession = null;
167 // try {
168 // nodeSession = nodeRepository.login();
169 // String reposPath = UserJcrUtils.getUserHome(nodeSession)
170 // .getPath() + RepoConstants.REPOSITORIES_BASE_PATH;
171 //
172 // Node repos = nodeSession.getNode(reposPath);
173 // String repoNodeName = JcrUtils.replaceInvalidChars(name
174 // .getText());
175 // if (repos.hasNode(repoNodeName))
176 // throw new ArgeoException(
177 // "There is already a remote repository named "
178 // + repoNodeName);
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();
190 // }
191 // MessageDialog.openInformation(getParentShell(),
192 // "Repository Added",
193 // "Remote repository " + uri.getText() + "' added");
194 //
195 // super.okPressed();
196 // } catch (Exception e) {
197 // ErrorFeedback.show("Cannot add remote repository", e);
198 // } finally {
199 // JcrUtils.logoutQuietly(nodeSession);
200 // }
201 // }
202 //
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);
209 // return text;
210 // }
211 //
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);
219 // return check;
220 // }
221 //
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
225 // | SWT.PASSWORD);
226 // text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
227 // return text;
228 // }
229 // }
230 }