KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > target > TargetDefinitionFromPlatformOperation


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.pde.internal.ui.wizards.target;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.runtime.Preferences;
18 import org.eclipse.pde.core.plugin.IPluginModelBase;
19 import org.eclipse.pde.core.plugin.PluginRegistry;
20 import org.eclipse.pde.internal.core.ICoreConstants;
21 import org.eclipse.pde.internal.core.PDECore;
22 import org.eclipse.pde.internal.core.itarget.IAdditionalLocation;
23 import org.eclipse.pde.internal.core.itarget.IArgumentsInfo;
24 import org.eclipse.pde.internal.core.itarget.IEnvironmentInfo;
25 import org.eclipse.pde.internal.core.itarget.IImplicitDependenciesInfo;
26 import org.eclipse.pde.internal.core.itarget.ILocationInfo;
27 import org.eclipse.pde.internal.core.itarget.ITarget;
28 import org.eclipse.pde.internal.core.itarget.ITargetJRE;
29 import org.eclipse.pde.internal.core.itarget.ITargetModel;
30 import org.eclipse.pde.internal.core.itarget.ITargetModelFactory;
31 import org.eclipse.pde.internal.core.itarget.ITargetPlugin;
32
33 public class TargetDefinitionFromPlatformOperation extends BaseTargetDefinitionOperation {
34
35     public TargetDefinitionFromPlatformOperation(IFile file) {
36         super(file);
37     }
38     
39     protected void initializeTarget(ITargetModel model) {
40         ITarget target = model.getTarget();
41         ITargetModelFactory factory = model.getFactory();
42         Preferences preferences = PDECore.getDefault().getPluginPreferences();
43         
44         initializeArgumentsInfo(preferences, target, factory);
45         initializeEnvironmentInfo(preferences, target, factory);
46         initializeImplicitInfo(preferences, target, factory);
47         initializeLocationInfo(preferences, target, factory);
48         initializeAdditionalLocsInfo(preferences, target, factory);
49         initializeJREInfo(target, factory);
50         initializePluginContent(preferences, target, factory);
51         
52     }
53         
54     protected void initializeArgumentsInfo(Preferences preferences, ITarget target, ITargetModelFactory factory) {
55         String JavaDoc progArgs = preferences.getString(ICoreConstants.PROGRAM_ARGS);
56         String JavaDoc vmArgs = preferences.getString(ICoreConstants.VM_ARGS);
57         if (progArgs.length() + vmArgs.length() > 0) {
58             IArgumentsInfo info = factory.createArguments();
59             info.setProgramArguments(progArgs);
60             info.setVMArguments(vmArgs);
61             target.setArguments(info);
62         }
63     }
64     
65     protected void initializeEnvironmentInfo(Preferences preferences, ITarget target, ITargetModelFactory factory) {
66         IEnvironmentInfo info = factory.createEnvironment();
67         info.setOS(preferences.getString(ICoreConstants.OS));
68         info.setWS(preferences.getString(ICoreConstants.WS));
69         info.setNL(preferences.getString(ICoreConstants.NL));
70         info.setArch(preferences.getString(ICoreConstants.ARCH));
71         target.setEnvironment(info);
72     }
73     
74     protected void initializeImplicitInfo(Preferences preferences, ITarget target, ITargetModelFactory factory) {
75         String JavaDoc value = preferences.getString(ICoreConstants.IMPLICIT_DEPENDENCIES);
76         if (value.length() > 0) {
77             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(value, ","); //$NON-NLS-1$
78
ITargetPlugin[] plugins = new ITargetPlugin[tokenizer.countTokens()];
79             int i = 0;
80             while(tokenizer.hasMoreTokens()) {
81                 String JavaDoc id = tokenizer.nextToken();
82                 ITargetPlugin plugin = factory.createPlugin();
83                 plugin.setId(id);
84                 plugins[i++] = plugin;
85             }
86             IImplicitDependenciesInfo info = factory.createImplicitPluginInfo();
87             info.addPlugins(plugins);
88             target.setImplicitPluginsInfo(info);
89         }
90     }
91     
92     protected void initializeLocationInfo(Preferences preferences, ITarget target, ITargetModelFactory factory) {
93         ILocationInfo info = factory.createLocation();
94         boolean useThis = preferences.getString(ICoreConstants.TARGET_MODE).equals(ICoreConstants.VALUE_USE_THIS);
95         info.setDefault(useThis);
96         if (!useThis)
97             info.setPath(preferences.getString(ICoreConstants.PLATFORM_PATH));
98         target.setLocationInfo(info);
99     }
100     
101     protected void initializeAdditionalLocsInfo(Preferences preferences, ITarget target, ITargetModelFactory factory) {
102         String JavaDoc additional = preferences.getString(ICoreConstants.ADDITIONAL_LOCATIONS);
103         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(additional, ","); //$NON-NLS-1$
104
int size = tokenizer.countTokens();
105         if (size > 0) {
106             IAdditionalLocation[] locations = new IAdditionalLocation[size];
107             int i = 0;
108             while (tokenizer.hasMoreTokens()) {
109                 IAdditionalLocation location = factory.createAdditionalLocation();
110                 location.setPath(tokenizer.nextToken().trim());
111                 locations[i++] = location;
112             }
113             target.addAdditionalDirectories(locations);
114         }
115     }
116     
117     protected void initializeJREInfo(ITarget target, ITargetModelFactory factory) {
118         ITargetJRE info = factory.createJREInfo();
119         info.setDefaultJRE();
120         target.setTargetJREInfo(info);
121     }
122     
123     protected void initializePluginContent(Preferences preferences, ITarget target, ITargetModelFactory factory) {
124         String JavaDoc value = preferences.getString(ICoreConstants.CHECKED_PLUGINS);
125         if (value.length() == 0 || value.equals(ICoreConstants.VALUE_SAVED_NONE))
126             return;
127         if (value.equals(ICoreConstants.VALUE_SAVED_ALL)) {
128             target.setUseAllPlugins(true);
129         } else {
130             IPluginModelBase [] models = PluginRegistry.getExternalModels();
131             ArrayList JavaDoc list = new ArrayList JavaDoc(models.length);
132             for (int i = 0; i < models.length; i++) {
133                 if (models[i].isEnabled()) {
134                     ITargetPlugin plugin = factory.createPlugin();
135                     String JavaDoc id = models[i].getPluginBase().getId();
136                     if (id != null)
137                         plugin.setId(id);
138                     list.add(plugin);
139                 }
140             }
141             if (list.size() > 0)
142                 target.addPlugins((ITargetPlugin[]) list.toArray(new ITargetPlugin[list.size()]));
143         }
144             
145     }
146
147 }
148
Popular Tags