KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > visualcontent > ui > nodetree > SessionLoginAction


1 /**
2  * VC Browser - Visualizes the content of a JSR 170 compatible repository
3  * Copyright (C) 2006 Sandro Böhme
4  *
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.visualcontent.ui.nodetree;
19
20 import javax.jcr.Node;
21 import javax.jcr.RepositoryException;
22 import javax.jcr.Session;
23
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IConfigurationElement;
26 import org.eclipse.core.runtime.IExtensionRegistry;
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.ui.IViewActionDelegate;
31 import org.eclipse.ui.IViewPart;
32 import org.visualcontent.extensionpoints.RepositoryThrowable;
33 import org.visualcontent.extensionpoints.SessionThrowable;
34 import org.visualcontent.extensionpoints.VCRepository;
35 import org.visualcontent.ui.UiPlugin;
36 import org.visualcontent.ui.preferences.PreferenceConstants;
37
38 public class SessionLoginAction implements IViewActionDelegate {
39
40     NodeTreeViewPart nodeTreeView=null;
41
42     public void run(IAction action) {
43         SessionLogoutAction.logout(nodeTreeView);
44         this.initRepositoryAndSession();
45         if (nodeTreeView.getVcSession()!=null) {
46             Session session = null;
47             try {
48                 session = nodeTreeView.getVcSession().login(nodeTreeView.getVcRepository());
49             } catch (SessionThrowable e1) {
50                 UiPlugin.getDefault().showError("Could not login to the repository.",e1);
51             } catch (RepositoryThrowable e) {
52                 UiPlugin.getDefault().showError("Could not login to the repository.",e);
53             }
54             if (session!=null){
55                 try {
56                     Node rootNode = session.getRootNode();
57                     if (rootNode !=null){
58                         nodeTreeView.setInput(rootNode);
59                         nodeTreeView.setJcrSession(session);
60                     }
61                 } catch (RepositoryException e) {
62                     UiPlugin.getDefault().showError("Could not get the root node.",e);
63                 }
64             }
65         }
66     }
67
68     public void selectionChanged(IAction action, ISelection selection) {
69     }
70
71
72     private void initRepositoryAndSession(){
73         String JavaDoc repositoryId= org.visualcontent.ui.UiPlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.P_REPOSITORY_CHOICE);
74         IExtensionRegistry registry = Platform.getExtensionRegistry ();
75         IConfigurationElement[] repositoryConfigElements = registry.getConfigurationElementsFor("org.visualcontent.extensionpoints.VCRepository");
76         VCRepository repository = null;
77         for (int i = 0; i < repositoryConfigElements.length && repository==null; i++) {
78             try {
79                 boolean isChoosenRepository = repositoryId.equals(repositoryConfigElements[i].getAttribute("id"));
80                 boolean thereIsOnlyOneRepositoryPlugin = repositoryConfigElements.length == 1;
81                 if (isChoosenRepository || thereIsOnlyOneRepositoryPlugin){
82                     repository = (VCRepository) repositoryConfigElements[i].createExecutableExtension("class");
83                 }
84             } catch (CoreException e) {
85                 UiPlugin.getDefault().showError("Could not create the VCSession instance.",e);
86             } catch (Throwable JavaDoc t){
87                 UiPlugin.getDefault().showError("Could not create the VCSession instance.",t);
88             }
89         }
90         nodeTreeView.setVcRepository(repository);
91         nodeTreeView.setVcSession(repository.getCurrentVCSession());
92     }
93
94     public void init(IViewPart view) {
95         nodeTreeView = (NodeTreeViewPart) view;
96     }
97
98 }
99
Popular Tags