KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > views > NewExtensionLocationAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.ui.views;
12
13 import java.io.*;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.jface.action.*;
17 import org.eclipse.jface.dialogs.*;
18 import org.eclipse.jface.resource.*;
19 import org.eclipse.swt.*;
20 import org.eclipse.swt.widgets.*;
21 import org.eclipse.update.configuration.*;
22 import org.eclipse.update.core.*;
23 import org.eclipse.update.internal.ui.*;
24 import org.eclipse.update.operations.*;
25
26 public class NewExtensionLocationAction extends Action {
27     private Shell shell;
28     
29     public NewExtensionLocationAction(Shell shell, String JavaDoc text, ImageDescriptor desc) {
30         super(text, desc);
31         this.shell = shell;
32     }
33
34     public void run() {
35         
36         IStatus status = OperationsManager.getValidator().validatePlatformConfigValid();
37         if (status != null) {
38             ErrorDialog.openError(shell, null, null, status);
39             return;
40         }
41         
42         DirectoryDialog dialog =
43             new DirectoryDialog(shell, SWT.APPLICATION_MODAL);
44         dialog.setMessage(UpdateUIMessages.NewExtensionLocationAction_selectExtLocation);
45
46         String JavaDoc dir = dialog.open();
47         while (dir != null) {
48             File dirFile = getExtensionSite(new File(dir));
49             if (dirFile != null) {
50                 if (addExtensionLocation(dirFile))
51                     return;
52                 else {
53                     // re-open the directory dialog
54
dialog.setFilterPath(dir);
55                     dir = dialog.open();
56                 }
57             } else {
58                 MessageDialog.openInformation(
59                     shell,
60                     UpdateUIMessages.NewExtensionLocationAction_extInfoTitle,
61                     UpdateUIMessages.NewExtensionLocationAction_extInfoMessage);
62                 // re-open the directory dialog
63
dialog.setFilterPath(dir);
64                 dir = dialog.open();
65             }
66         }
67     }
68
69     /**
70      * @param directory
71      * @return the site file (including "eclipse" path) when directory is an eclipse exstension, null otherwise
72      */

73     static File getExtensionSite(File directory) {
74         // Check the eclipse folder
75
if (directory.getName().equals("eclipse")) { //$NON-NLS-1$
76
// if we picked up the eclipse directory, check if its parent is a site
77
File site = getExtensionSite(directory.getParentFile());
78             if (site != null)
79                 return directory;
80             // otherwise, fall through
81
}
82         
83         File eclipse = new File(directory, "eclipse"); //$NON-NLS-1$
84
if (!eclipse.exists() || !eclipse.isDirectory())
85             return null;
86
87         // check the marker
88
File marker = new File(eclipse, ".eclipseextension"); //$NON-NLS-1$
89
if (!marker.exists() || marker.isDirectory())
90             return null;
91         return eclipse;
92     }
93
94     private boolean addExtensionLocation(File dir) {
95         try {
96             IInstallConfiguration config = SiteManager.getLocalSite().getCurrentConfiguration();
97             IConfiguredSite csite = config.createLinkedConfiguredSite(dir);
98             csite.verifyUpdatableStatus();
99             config.addConfiguredSite(csite);
100             boolean restartNeeded = SiteManager.getLocalSite().save();
101             UpdateUI.requestRestart(restartNeeded);
102             return true;
103         } catch (CoreException e) {
104             String JavaDoc title = UpdateUIMessages.InstallWizard_TargetPage_location_error_title;
105             ErrorDialog.openError(shell, title, null, e.getStatus());
106             UpdateUI.logException(e,false);
107             return false;
108         }
109     }
110
111 }
112
Popular Tags