1 11 package org.eclipse.pde.internal.ui.launcher; 12 13 import java.io.File ; 14 15 import org.eclipse.core.resources.IFile; 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.resources.IWorkspaceRoot; 18 import org.eclipse.core.resources.ResourcesPlugin; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.core.runtime.Status; 23 import org.eclipse.core.variables.IStringVariableManager; 24 import org.eclipse.core.variables.VariablesPlugin; 25 import org.eclipse.debug.core.ILaunchConfiguration; 26 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 27 import org.eclipse.jface.window.Window; 28 import org.eclipse.pde.internal.ui.PDEPlugin; 29 import org.eclipse.pde.internal.ui.PDEUIMessages; 30 import org.eclipse.pde.internal.ui.util.FileNameFilter; 31 import org.eclipse.pde.ui.launcher.AbstractLauncherTab; 32 import org.eclipse.pde.ui.launcher.IPDELauncherConstants; 33 import org.eclipse.swt.SWT; 34 import org.eclipse.swt.events.SelectionAdapter; 35 import org.eclipse.swt.events.SelectionEvent; 36 import org.eclipse.swt.layout.GridData; 37 import org.eclipse.swt.layout.GridLayout; 38 import org.eclipse.swt.widgets.Button; 39 import org.eclipse.swt.widgets.Composite; 40 import org.eclipse.swt.widgets.FileDialog; 41 import org.eclipse.swt.widgets.Group; 42 import org.eclipse.swt.widgets.Label; 43 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 44 import org.eclipse.ui.dialogs.ISelectionStatusValidator; 45 import org.eclipse.ui.model.WorkbenchContentProvider; 46 import org.eclipse.ui.model.WorkbenchLabelProvider; 47 48 public class ConfigurationTemplateBlock extends BaseBlock { 49 50 private Button fGenerateFileButton; 51 private Button fUseTemplateButton; 52 53 public ConfigurationTemplateBlock(AbstractLauncherTab tab) { 54 super(tab); 55 } 56 57 public void createControl(Composite parent) { 58 Group group = new Group(parent, SWT.NONE); 59 group.setText(PDEUIMessages.ConfigurationTab_configFileGroup); 60 group.setLayout(new GridLayout(2, false)); 61 group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 62 63 fGenerateFileButton = new Button(group, SWT.RADIO); 64 fGenerateFileButton.setText(PDEUIMessages.ConfigurationTab_defaultConfigIni); 65 GridData gd = new GridData(); 66 gd.horizontalSpan = 2; 67 fGenerateFileButton.setLayoutData(gd); 68 fGenerateFileButton.addSelectionListener(new SelectionAdapter() { 69 public void widgetSelected(SelectionEvent e) { 70 enableBrowseSection(!fGenerateFileButton.getSelection()); 71 } 72 }); 73 74 fUseTemplateButton = new Button(group, SWT.RADIO); 75 fUseTemplateButton.setText(PDEUIMessages.ConfigurationTab_existingConfigIni); 76 gd = new GridData(); 77 gd.horizontalSpan = 2; 78 fUseTemplateButton.setLayoutData(gd); 79 80 createText(group, PDEUIMessages.ConfigurationTab_templateLoc, 20); 81 82 Composite buttons = new Composite(group, SWT.NONE); 83 GridLayout layout = new GridLayout(4, false); 84 layout.marginHeight = layout.marginWidth = 0; 85 buttons.setLayout(layout); 86 gd = new GridData(GridData.FILL_HORIZONTAL); 87 gd.horizontalSpan = 2; 88 buttons.setLayoutData(gd); 89 90 Label label = new Label(buttons, SWT.NONE); 91 label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 92 93 createButtons(buttons, new String [] { 94 PDEUIMessages.BaseBlock_workspaceS, PDEUIMessages.BaseBlock_filesystemS, PDEUIMessages.BaseBlock_variablesS 95 }); 96 } 97 98 public void initializeFrom(ILaunchConfiguration configuration) throws CoreException { 99 boolean generateDefault = configuration.getAttribute(IPDELauncherConstants.CONFIG_GENERATE_DEFAULT, true); 100 fGenerateFileButton.setSelection(generateDefault); 101 fUseTemplateButton.setSelection(!generateDefault); 102 enableBrowseSection(!generateDefault); 103 fLocationText.setText(configuration.getAttribute(IPDELauncherConstants.CONFIG_TEMPLATE_LOCATION, "")); } 105 106 public void performApply(ILaunchConfigurationWorkingCopy configuration) { 107 configuration.setAttribute(IPDELauncherConstants.CONFIG_GENERATE_DEFAULT, fGenerateFileButton.getSelection()); 108 if (!fGenerateFileButton.getSelection()) 109 configuration.setAttribute(IPDELauncherConstants.CONFIG_TEMPLATE_LOCATION, fLocationText.getText().trim()); 110 } 111 112 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) { 113 configuration.setAttribute(IPDELauncherConstants.CONFIG_GENERATE_DEFAULT, true); 114 configuration.setAttribute(IPDELauncherConstants.CONFIG_TEMPLATE_LOCATION, 115 "${target_home}" + File.separatorChar + "configuration" + File.separatorChar + "config.ini"); } 117 118 119 protected String getName() { 120 return PDEUIMessages.ConfigurationTemplateBlock_name; 121 } 122 123 protected void handleBrowseWorkspace() { 124 ElementTreeSelectionDialog dialog = 125 new ElementTreeSelectionDialog( 126 fTab.getControl().getShell(), 127 new WorkbenchLabelProvider(), 128 new WorkbenchContentProvider()); 129 130 IFile file = getFile(); 131 if (file != null) 132 dialog.setInitialSelection(file); 133 dialog.setInput(PDEPlugin.getWorkspace().getRoot()); 134 dialog.addFilter(new FileNameFilter("config.ini")); dialog.setAllowMultiple(false); 136 dialog.setTitle(PDEUIMessages.ConfigurationTab_fileSelection); 137 dialog.setMessage(PDEUIMessages.ConfigurationTab_fileDialogMessage); 138 dialog.setValidator(new ISelectionStatusValidator() { 139 public IStatus validate(Object [] selection) { 140 if (selection.length > 0 && selection[0] instanceof IFile) 141 return new Status(IStatus.OK, PDEPlugin.getPluginId(), 142 IStatus.OK, "", null); 144 return new Status(IStatus.ERROR, PDEPlugin.getPluginId(), 145 IStatus.ERROR, "", null); } 147 }); 148 if (dialog.open() == Window.OK) { 149 file = (IFile) dialog.getFirstResult(); 150 fLocationText.setText("${workspace_loc:" + file.getFullPath().makeRelative() + "}"); } 152 } 153 154 protected IFile getFile() { 155 String path = getLocation(); 156 if (path.length() > 0) { 157 IResource res = null; 158 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 159 if (path.startsWith("${workspace_loc:")) { IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager(); 161 try { 162 path = manager.performStringSubstitution(path, false); 163 IFile[] containers = root.findFilesForLocation(new Path(path)); 164 if (containers.length > 0) 165 res = containers[0]; 166 } catch (CoreException e) { 167 } 168 } else { 169 res = root.findMember(path); 170 } 171 if (res instanceof IFile) { 172 return (IFile)res; 173 } 174 } 175 return null; 176 } 177 178 179 protected void handleBrowseFileSystem() { 180 FileDialog dialog = new FileDialog(fTab.getControl().getShell()); 181 dialog.setFilterExtensions(new String [] {"*.ini"}); dialog.setFilterPath(getLocation()); 183 dialog.setText(PDEUIMessages.ConfigurationTab_configLocMessage); 184 String res = dialog.open(); 185 if (res != null) 186 fLocationText.setText(res); 187 } 188 189 protected String getLocation() { 190 String path = fLocationText.getText().trim(); 191 IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager(); 192 try { 193 return manager.performStringSubstitution(path, false); 194 } catch (CoreException e) { 195 return path; 196 } 197 } 198 199 } 200 | Popular Tags |