KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > jres > JREsUpdater


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.jdt.internal.debug.ui.jres;
12
13 import java.io.IOException JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 import javax.xml.parsers.ParserConfigurationException JavaDoc;
17 import javax.xml.transform.TransformerException JavaDoc;
18
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
21 import org.eclipse.jdt.internal.launching.VMDefinitionsContainer;
22 import org.eclipse.jdt.launching.IVMInstall;
23 import org.eclipse.jdt.launching.IVMInstallType;
24 import org.eclipse.jdt.launching.JavaRuntime;
25 import org.eclipse.jface.operation.IRunnableWithProgress;
26
27 /**
28  * Processes add/removed/changed VMs.
29  */

30 public class JREsUpdater {
31     
32     // the VMs defined when this updated is instantiated
33
private VMDefinitionsContainer fOriginalVMs;
34     
35     /**
36      * Contstructs a new VM updater to update VM install settings.
37      */

38     public JREsUpdater() {
39         fOriginalVMs = new VMDefinitionsContainer();
40         IVMInstall def = JavaRuntime.getDefaultVMInstall();
41         if (def != null) {
42             fOriginalVMs.setDefaultVMInstallCompositeID(JavaRuntime.getCompositeIdFromVM(def));
43         }
44     
45         IVMInstallType[] types = JavaRuntime.getVMInstallTypes();
46         for (int i = 0; i < types.length; i++) {
47             IVMInstall[] vms = types[i].getVMInstalls();
48             for (int j = 0; j < vms.length; j++) {
49                 fOriginalVMs.addVM(vms[j]);
50             }
51         }
52     }
53     
54     /**
55      * Updates VM settings and returns whether the update was successful.
56      *
57      * @param jres new installed JREs
58      * @param defaultJRE new default VM
59      * @return whether the update was successful
60      */

61     public boolean updateJRESettings(IVMInstall[] jres, IVMInstall defaultJRE) {
62         
63         // Create a VM definition container
64
VMDefinitionsContainer vmContainer = new VMDefinitionsContainer();
65         
66         // Set the default VM Id on the container
67
String JavaDoc defaultVMId = JavaRuntime.getCompositeIdFromVM(defaultJRE);
68         vmContainer.setDefaultVMInstallCompositeID(defaultVMId);
69         
70         // Set the VMs on the container
71
for (int i = 0; i < jres.length; i++) {
72             vmContainer.addVM(jres[i]);
73         }
74         
75         
76         // Generate XML for the VM defs and save it as the new value of the VM preference
77
saveVMDefinitions(vmContainer);
78         
79         return true;
80     }
81     
82     private void saveVMDefinitions(final VMDefinitionsContainer container) {
83         IRunnableWithProgress runnable = new IRunnableWithProgress() {
84             public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
85                 try {
86                     monitor.beginTask(JREMessages.JREsUpdater_0, 100);
87                     String JavaDoc vmDefXML = container.getAsXML();
88                     monitor.worked(40);
89                     JavaRuntime.getPreferences().setValue(JavaRuntime.PREF_VM_XML, vmDefXML);
90                     monitor.worked(30);
91                     JavaRuntime.savePreferences();
92                     monitor.worked(30);
93                 } catch (IOException JavaDoc ioe) {
94                     JDIDebugUIPlugin.log(ioe);
95                 } catch (ParserConfigurationException JavaDoc e) {
96                     JDIDebugUIPlugin.log(e);
97                 } catch (TransformerException JavaDoc e) {
98                     JDIDebugUIPlugin.log(e);
99                 } finally {
100                     monitor.done();
101                 }
102                 
103             }
104         };
105         try {
106             JDIDebugUIPlugin.getDefault().getWorkbench().getProgressService().busyCursorWhile(runnable);
107         } catch (InvocationTargetException JavaDoc e) {
108             JDIDebugUIPlugin.log(e);
109         } catch (InterruptedException JavaDoc e) {
110             JDIDebugUIPlugin.log(e);
111         }
112     }
113 }
114
Popular Tags