]> 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/DisplayRepoInformation.java
Prevent initalization of the UI to fail if a remote repo is not accessible.
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / DisplayRepoInformation.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.RepositoryException;
19 import javax.jcr.Session;
20
21 import org.argeo.jcr.JcrUtils;
22 import org.argeo.slc.SlcException;
23 import org.argeo.slc.client.ui.dist.DistPlugin;
24 import org.argeo.slc.client.ui.dist.model.RepoElem;
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.viewers.IStructuredSelection;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Shell;
40 import org.eclipse.swt.widgets.Text;
41 import org.eclipse.ui.handlers.HandlerUtil;
42
43 /**
44 * Create a new empty workspace in the current repository.
45 */
46
47 public class DisplayRepoInformation extends AbstractHandler {
48 public final static String ID = DistPlugin.ID + ".displayRepoInformation";
49 public final static String DEFAULT_LABEL = "Repository infos...";
50 public final static String DEFAULT_ICON_PATH = "icons/help.gif";
51
52 public Object execute(ExecutionEvent event) throws ExecutionException {
53
54 IStructuredSelection iss = (IStructuredSelection) HandlerUtil
55 .getActiveSite(event).getSelectionProvider().getSelection();
56
57 if (iss.getFirstElement() instanceof RepoElem) {
58 RepoElem re = (RepoElem) iss.getFirstElement();
59 InformationDialog inputDialog = new InformationDialog(HandlerUtil
60 .getActiveSite(event).getShell());
61 inputDialog.create();
62 Session session = null;
63 try {
64 session = re.getRepository().login(re.getCredentials());
65 inputDialog.loginTxt.setText(session.getUserID());
66 inputDialog.nameTxt.setText(re.getLabel());
67 inputDialog.uriTxt.setText(re.getUri());
68 inputDialog.readOnlyBtn.setSelection(re.isReadOnly());
69 } catch (RepositoryException e) {
70 throw new SlcException("Unexpected error while "
71 + "getting repository infos.", e);
72 } finally {
73 JcrUtils.logoutQuietly(session);
74 }
75 inputDialog.open();
76 }
77 return null;
78 }
79
80 public class InformationDialog extends Dialog {
81 Text nameTxt;
82 Text uriTxt;
83 Text loginTxt;
84 Button readOnlyBtn;
85
86 @Override
87 protected void createButtonsForButtonBar(Composite parent) {
88 // No Cancel button
89 createButton(parent, IDialogConstants.OK_ID, "OK", true);
90 }
91
92 public InformationDialog(Shell parentShell) {
93 super(parentShell);
94 }
95
96 protected Point getInitialSize() {
97 return new Point(500, 250);
98 }
99
100 protected Control createDialogArea(Composite parent) {
101
102 Composite dialogarea = (Composite) super.createDialogArea(parent);
103 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
104 true));
105 Composite composite = new Composite(dialogarea, SWT.NONE);
106 GridLayout layout = new GridLayout(2, false);
107 layout.horizontalSpacing = 15;
108 composite.setLayout(layout);
109 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
110 composite.setLayoutData(gd);
111
112 nameTxt = createLT(composite, "Name");
113 uriTxt = createLT(composite, "URI");
114 loginTxt = createLT(composite, "Logged as");
115 readOnlyBtn = createLC(composite, "Read only");
116 parent.pack();
117 return composite;
118 }
119
120 /** Creates label and text. */
121 protected Text createLT(Composite parent, String label) {
122 new Label(parent, SWT.NONE).setText(label);
123 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.NONE);
124 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
125 text.setEditable(false);
126 return text;
127 }
128
129 /** Creates label and check. */
130 protected Button createLC(Composite parent, String label) {
131 new Label(parent, SWT.NONE).setText(label);
132 Button check = new Button(parent, SWT.CHECK);
133 check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
134 check.setEnabled(false);
135 return check;
136 }
137
138 protected void configureShell(Shell shell) {
139 super.configureShell(shell);
140 shell.setText("Repository information");
141 }
142 }
143 }