001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.wizards.target;
011:
012: import java.util.ArrayList;
013: import java.util.StringTokenizer;
014:
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.core.runtime.Preferences;
017: import org.eclipse.pde.core.plugin.IPluginModelBase;
018: import org.eclipse.pde.core.plugin.PluginRegistry;
019: import org.eclipse.pde.internal.core.ICoreConstants;
020: import org.eclipse.pde.internal.core.PDECore;
021: import org.eclipse.pde.internal.core.itarget.IAdditionalLocation;
022: import org.eclipse.pde.internal.core.itarget.IArgumentsInfo;
023: import org.eclipse.pde.internal.core.itarget.IEnvironmentInfo;
024: import org.eclipse.pde.internal.core.itarget.IImplicitDependenciesInfo;
025: import org.eclipse.pde.internal.core.itarget.ILocationInfo;
026: import org.eclipse.pde.internal.core.itarget.ITarget;
027: import org.eclipse.pde.internal.core.itarget.ITargetJRE;
028: import org.eclipse.pde.internal.core.itarget.ITargetModel;
029: import org.eclipse.pde.internal.core.itarget.ITargetModelFactory;
030: import org.eclipse.pde.internal.core.itarget.ITargetPlugin;
031:
032: public class TargetDefinitionFromPlatformOperation extends
033: BaseTargetDefinitionOperation {
034:
035: public TargetDefinitionFromPlatformOperation(IFile file) {
036: super (file);
037: }
038:
039: protected void initializeTarget(ITargetModel model) {
040: ITarget target = model.getTarget();
041: ITargetModelFactory factory = model.getFactory();
042: Preferences preferences = PDECore.getDefault()
043: .getPluginPreferences();
044:
045: initializeArgumentsInfo(preferences, target, factory);
046: initializeEnvironmentInfo(preferences, target, factory);
047: initializeImplicitInfo(preferences, target, factory);
048: initializeLocationInfo(preferences, target, factory);
049: initializeAdditionalLocsInfo(preferences, target, factory);
050: initializeJREInfo(target, factory);
051: initializePluginContent(preferences, target, factory);
052:
053: }
054:
055: protected void initializeArgumentsInfo(Preferences preferences,
056: ITarget target, ITargetModelFactory factory) {
057: String progArgs = preferences
058: .getString(ICoreConstants.PROGRAM_ARGS);
059: String vmArgs = preferences.getString(ICoreConstants.VM_ARGS);
060: if (progArgs.length() + vmArgs.length() > 0) {
061: IArgumentsInfo info = factory.createArguments();
062: info.setProgramArguments(progArgs);
063: info.setVMArguments(vmArgs);
064: target.setArguments(info);
065: }
066: }
067:
068: protected void initializeEnvironmentInfo(Preferences preferences,
069: ITarget target, ITargetModelFactory factory) {
070: IEnvironmentInfo info = factory.createEnvironment();
071: info.setOS(preferences.getString(ICoreConstants.OS));
072: info.setWS(preferences.getString(ICoreConstants.WS));
073: info.setNL(preferences.getString(ICoreConstants.NL));
074: info.setArch(preferences.getString(ICoreConstants.ARCH));
075: target.setEnvironment(info);
076: }
077:
078: protected void initializeImplicitInfo(Preferences preferences,
079: ITarget target, ITargetModelFactory factory) {
080: String value = preferences
081: .getString(ICoreConstants.IMPLICIT_DEPENDENCIES);
082: if (value.length() > 0) {
083: StringTokenizer tokenizer = new StringTokenizer(value, ","); //$NON-NLS-1$
084: ITargetPlugin[] plugins = new ITargetPlugin[tokenizer
085: .countTokens()];
086: int i = 0;
087: while (tokenizer.hasMoreTokens()) {
088: String id = tokenizer.nextToken();
089: ITargetPlugin plugin = factory.createPlugin();
090: plugin.setId(id);
091: plugins[i++] = plugin;
092: }
093: IImplicitDependenciesInfo info = factory
094: .createImplicitPluginInfo();
095: info.addPlugins(plugins);
096: target.setImplicitPluginsInfo(info);
097: }
098: }
099:
100: protected void initializeLocationInfo(Preferences preferences,
101: ITarget target, ITargetModelFactory factory) {
102: ILocationInfo info = factory.createLocation();
103: boolean useThis = preferences.getString(
104: ICoreConstants.TARGET_MODE).equals(
105: ICoreConstants.VALUE_USE_THIS);
106: info.setDefault(useThis);
107: if (!useThis)
108: info.setPath(preferences
109: .getString(ICoreConstants.PLATFORM_PATH));
110: target.setLocationInfo(info);
111: }
112:
113: protected void initializeAdditionalLocsInfo(
114: Preferences preferences, ITarget target,
115: ITargetModelFactory factory) {
116: String additional = preferences
117: .getString(ICoreConstants.ADDITIONAL_LOCATIONS);
118: StringTokenizer tokenizer = new StringTokenizer(additional, ","); //$NON-NLS-1$
119: int size = tokenizer.countTokens();
120: if (size > 0) {
121: IAdditionalLocation[] locations = new IAdditionalLocation[size];
122: int i = 0;
123: while (tokenizer.hasMoreTokens()) {
124: IAdditionalLocation location = factory
125: .createAdditionalLocation();
126: location.setPath(tokenizer.nextToken().trim());
127: locations[i++] = location;
128: }
129: target.addAdditionalDirectories(locations);
130: }
131: }
132:
133: protected void initializeJREInfo(ITarget target,
134: ITargetModelFactory factory) {
135: ITargetJRE info = factory.createJREInfo();
136: info.setDefaultJRE();
137: target.setTargetJREInfo(info);
138: }
139:
140: protected void initializePluginContent(Preferences preferences,
141: ITarget target, ITargetModelFactory factory) {
142: String value = preferences
143: .getString(ICoreConstants.CHECKED_PLUGINS);
144: if (value.length() == 0
145: || value.equals(ICoreConstants.VALUE_SAVED_NONE))
146: return;
147: if (value.equals(ICoreConstants.VALUE_SAVED_ALL)) {
148: target.setUseAllPlugins(true);
149: } else {
150: IPluginModelBase[] models = PluginRegistry
151: .getExternalModels();
152: ArrayList list = new ArrayList(models.length);
153: for (int i = 0; i < models.length; i++) {
154: if (models[i].isEnabled()) {
155: ITargetPlugin plugin = factory.createPlugin();
156: String id = models[i].getPluginBase().getId();
157: if (id != null)
158: plugin.setId(id);
159: list.add(plugin);
160: }
161: }
162: if (list.size() > 0)
163: target.addPlugins((ITargetPlugin[]) list
164: .toArray(new ITargetPlugin[list.size()]));
165: }
166:
167: }
168:
169: }
|