]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/commands/RegisterRepository.java
Do not clean OSGi runtime
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / 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.wizard.WizardDialog;
32 import org.eclipse.ui.handlers.HandlerUtil;
33
34 /**
35 * Connect to a remote repository.
36 */
37 public class RegisterRepository extends AbstractHandler implements ArgeoNames,
38 SlcNames {
39
40 public final static String ID = DistPlugin.ID + ".registerRepository";
41 public final static String DEFAULT_LABEL = "Register a repository...";
42 public final static String DEFAULT_ICON_PATH = "icons/addRepo.gif";
43
44 /* DEPENDENCY INJECTION */
45 private RepositoryFactory repositoryFactory;
46 private Repository nodeRepository;
47 private Keyring keyring;
48
49 public Object execute(ExecutionEvent event) throws ExecutionException {
50 RegisterRepoWizard wizard = new RegisterRepoWizard(keyring,
51 repositoryFactory, nodeRepository);
52 WizardDialog dialog = new WizardDialog(
53 HandlerUtil.getActiveShell(event), wizard);
54 int result = dialog.open();
55 if (result == Dialog.OK)
56 CommandHelpers.callCommand(RefreshDistributionsView.ID);
57
58 // RemoteRepositoryLoginDialog dlg = new RemoteRepositoryLoginDialog(
59 // Display.getDefault().getActiveShell());
60 // if (dlg.open() == Dialog.OK) {
61 // }
62 return null;
63 }
64
65 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
66 this.repositoryFactory = repositoryFactory;
67 }
68
69 public void setKeyring(Keyring keyring) {
70 this.keyring = keyring;
71 }
72
73 public void setNodeRepository(Repository nodeRepository) {
74 this.nodeRepository = nodeRepository;
75 }
76
77 // class RemoteRepositoryLoginDialog extends TitleAreaDialog {
78 // private Text name;
79 // private Text uri;
80 // private Text username;
81 // private Text password;
82 // private Button saveInKeyring;
83 //
84 // public RemoteRepositoryLoginDialog(Shell parentShell) {
85 // super(parentShell);
86 // }
87 //
88 // protected Point getInitialSize() {
89 // return new Point(600, 400);
90 // }
91 //
92 // protected Control createDialogArea(Composite parent) {
93 // Composite dialogarea = (Composite) super.createDialogArea(parent);
94 // dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
95 // true));
96 // Composite composite = new Composite(dialogarea, SWT.NONE);
97 // composite.setLayout(new GridLayout(2, false));
98 // composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
99 // false));
100 // setMessage("Login to remote repository", IMessageProvider.NONE);
101 // name = createLT(composite, "Name", "Example Java Repository");
102 // uri = createLT(composite, "URI",
103 // "https://example.com/data/jcr/java");
104 // username = createLT(composite, "User", "");
105 // password = createLP(composite, "Password");
106 //
107 // saveInKeyring = createLC(composite, "Remember password", false);
108 // parent.pack();
109 // return composite;
110 // }
111 //
112 // @Override
113 // protected void createButtonsForButtonBar(Composite parent) {
114 // super.createButtonsForButtonBar(parent);
115 // Button test = createButton(parent, 2, "Test", false);
116 // test.addSelectionListener(new SelectionAdapter() {
117 // public void widgetSelected(SelectionEvent arg0) {
118 // testConnection();
119 // }
120 // });
121 // }
122 //
123 // void testConnection() {
124 // Session session = null;
125 // try {
126 // if (uri.getText().startsWith("http")) {// http, https
127 // URI checkedUri = new URI(uri.getText());
128 // String checkedUriStr = checkedUri.toString();
129 // Hashtable<String, String> params = new Hashtable<String, String>();
130 // params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI,
131 // checkedUriStr);
132 // Repository repository = ArgeoJcrUtils.getRepositoryByUri(
133 // repositoryFactory, checkedUriStr);
134 // if (username.getText().trim().equals("")) {// anonymous
135 // session = repository.login();
136 // } else {
137 // // FIXME use getTextChars() when upgrading to 3.7
138 // // see
139 // // https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
140 // char[] pwd = password.getText().toCharArray();
141 // SimpleCredentials sc = new SimpleCredentials(
142 // username.getText(), pwd);
143 // session = repository.login(sc);
144 // }
145 // } else {// alias
146 // Repository repository = ArgeoJcrUtils.getRepositoryByAlias(
147 // repositoryFactory, uri.getText());
148 // session = repository.login();
149 // }
150 // MessageDialog.openInformation(getParentShell(), "Success",
151 // "Connection to '" + uri.getText() + "' successful");
152 // } catch (Exception e) {
153 // ErrorFeedback.show(
154 // "Connection test failed for " + uri.getText(), e);
155 // } finally {
156 // JcrUtils.logoutQuietly(session);
157 // }
158 // }
159 //
160 // @Override
161 // protected void okPressed() {
162 // Session nodeSession = null;
163 // try {
164 // nodeSession = nodeRepository.login();
165 // String reposPath = UserJcrUtils.getUserHome(nodeSession)
166 // .getPath() + RepoConstants.REPOSITORIES_BASE_PATH;
167 //
168 // Node repos = nodeSession.getNode(reposPath);
169 // String repoNodeName = JcrUtils.replaceInvalidChars(name
170 // .getText());
171 // if (repos.hasNode(repoNodeName))
172 // throw new ArgeoException(
173 // "There is already a remote repository named "
174 // + repoNodeName);
175 // Node repoNode = repos.addNode(repoNodeName,
176 // ArgeoTypes.ARGEO_REMOTE_REPOSITORY);
177 // repoNode.setProperty(ARGEO_URI, uri.getText());
178 // repoNode.setProperty(ARGEO_USER_ID, username.getText());
179 // repoNode.addMixin(NodeType.MIX_TITLE);
180 // repoNode.setProperty(Property.JCR_TITLE, name.getText());
181 // nodeSession.save();
182 // if (saveInKeyring.getSelection()) {
183 // String pwdPath = repoNode.getPath() + '/' + ARGEO_PASSWORD;
184 // keyring.set(pwdPath, password.getText().toCharArray());
185 // nodeSession.save();
186 // }
187 // MessageDialog.openInformation(getParentShell(),
188 // "Repository Added",
189 // "Remote repository " + uri.getText() + "' added");
190 //
191 // super.okPressed();
192 // } catch (Exception e) {
193 // ErrorFeedback.show("Cannot add remote repository", e);
194 // } finally {
195 // JcrUtils.logoutQuietly(nodeSession);
196 // }
197 // }
198 //
199 // /** Creates label and text. */
200 // protected Text createLT(Composite parent, String label, String initial) {
201 // new Label(parent, SWT.NONE).setText(label);
202 // Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
203 // text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
204 // text.setText(initial);
205 // return text;
206 // }
207 //
208 // /** Creates label and check. */
209 // protected Button createLC(Composite parent, String label,
210 // Boolean initial) {
211 // new Label(parent, SWT.NONE).setText(label);
212 // Button check = new Button(parent, SWT.CHECK);
213 // check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
214 // check.setSelection(initial);
215 // return check;
216 // }
217 //
218 // protected Text createLP(Composite parent, String label) {
219 // new Label(parent, SWT.NONE).setText(label);
220 // Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
221 // | SWT.PASSWORD);
222 // text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
223 // return text;
224 // }
225 // }
226 }