001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package org.terracotta.dso.launch;
005:
006: import org.apache.commons.lang.StringUtils;
007: import org.eclipse.core.resources.IFile;
008: import org.eclipse.core.resources.IFolder;
009: import org.eclipse.core.resources.IProject;
010: import org.eclipse.core.resources.IResource;
011: import org.eclipse.core.resources.ResourcesPlugin;
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.Path;
014: import org.eclipse.core.variables.IStringVariableManager;
015: import org.eclipse.core.variables.VariablesPlugin;
016: import org.eclipse.debug.core.ILaunchConfiguration;
017: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
018: import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
019: import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
020: import org.eclipse.jface.dialogs.IDialogConstants;
021: import org.eclipse.jface.viewers.Viewer;
022: import org.eclipse.jface.viewers.ViewerFilter;
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.events.ModifyEvent;
025: import org.eclipse.swt.events.ModifyListener;
026: import org.eclipse.swt.events.SelectionAdapter;
027: import org.eclipse.swt.events.SelectionEvent;
028: import org.eclipse.swt.layout.GridData;
029: import org.eclipse.swt.layout.GridLayout;
030: import org.eclipse.swt.widgets.Button;
031: import org.eclipse.swt.widgets.Composite;
032: import org.eclipse.swt.widgets.Group;
033: import org.eclipse.swt.widgets.Text;
034: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
035: import org.eclipse.ui.model.WorkbenchContentProvider;
036: import org.eclipse.ui.model.WorkbenchLabelProvider;
037: import org.eclipse.ui.views.navigator.ResourceSorter;
038: import org.terracotta.dso.TcPlugin;
039: import org.terracotta.ui.util.SWTUtil;
040:
041: public class ConfigurationTab extends AbstractLaunchConfigurationTab
042: implements IDSOLaunchConfigurationConstants {
043: private Text fServerText;
044: private Button fConfigServerButton;
045: private Text fConfigServerText;
046: private Button fConfigFileButton;
047: private Text fConfigFileText;
048: private Button fConfigBrowseButton;
049:
050: private ModifyListener fBasicModifyListener = new ModifyListener() {
051: public void modifyText(ModifyEvent evt) {
052: updateLaunchConfigurationDialog();
053: }
054: };
055:
056: private SelectionAdapter fConfigSelector = new SelectionAdapter() {
057: public void widgetSelected(SelectionEvent e) {
058: if (e.widget == fConfigServerButton) {
059: fConfigServerText.setEnabled(true);
060: fConfigFileText.setEnabled(false);
061: } else {
062: fConfigServerText.setEnabled(false);
063: fConfigFileText.setEnabled(true);
064: }
065: }
066: };
067:
068: public void createControl(Composite parent) {
069: Composite comp = new Composite(parent, SWT.NONE);
070: setControl(comp);
071: comp.setLayout(new GridLayout());
072: Group group = new Group(comp, SWT.NONE);
073: group.setText("Server specification");
074: group.setLayout(new GridLayout());
075: group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
076: fServerText = new Text(group, SWT.BORDER | SWT.SINGLE);
077: fServerText
078: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
079: fServerText.addModifyListener(fBasicModifyListener);
080:
081: group = new Group(comp, SWT.NONE);
082: group.setText("Configuration specification");
083: group.setLayout(new GridLayout(3, false));
084: group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
085: fConfigServerButton = new Button(group, SWT.RADIO);
086: fConfigServerButton.setText("Server");
087: fConfigServerButton.addSelectionListener(fConfigSelector);
088: fConfigServerText = new Text(group, SWT.BORDER | SWT.SINGLE);
089: fConfigServerText.addModifyListener(fBasicModifyListener);
090: GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
091: gridData.horizontalSpan = 2;
092: fConfigServerText.setLayoutData(gridData);
093: fConfigFileButton = new Button(group, SWT.RADIO);
094: fConfigFileButton.setText("File");
095: fConfigFileButton.addSelectionListener(fConfigSelector);
096: fConfigFileText = new Text(group, SWT.BORDER | SWT.SINGLE);
097: fConfigFileText.setLayoutData(new GridData(
098: GridData.FILL_HORIZONTAL));
099: fConfigFileText.addModifyListener(fBasicModifyListener);
100: fConfigBrowseButton = new Button(group, SWT.PUSH);
101: fConfigBrowseButton.setText("Browse...");
102: SWTUtil.applyDefaultButtonSize(fConfigBrowseButton);
103: fConfigBrowseButton
104: .addSelectionListener(new SelectionAdapter() {
105: public void widgetSelected(SelectionEvent e) {
106: ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
107: getShell(),
108: new WorkbenchLabelProvider(),
109: new WorkbenchContentProvider());
110: dialog.setTitle("Terracotta");
111: dialog
112: .setMessage("Locate Terracotta configuration file");
113: dialog.setInput(ResourcesPlugin.getWorkspace()
114: .getRoot());
115: dialog.addFilter(new ViewerFilter() {
116: public boolean select(Viewer viewer,
117: Object parentElement, Object element) {
118: if (element instanceof IProject
119: || element instanceof IFolder) {
120: return true;
121: }
122: if (element instanceof IFile) {
123: IFile file = (IFile) element;
124: return file
125: .getProjectRelativePath()
126: .getFileExtension().equals(
127: "xml");
128: }
129: return false;
130: }
131: });
132: dialog.setSorter(new ResourceSorter(
133: ResourceSorter.NAME));
134: if (dialog.open() == IDialogConstants.OK_ID) {
135: IResource resource = (IResource) dialog
136: .getFirstResult();
137: String arg = resource.getFullPath()
138: .toString();
139: IStringVariableManager variableManager = VariablesPlugin
140: .getDefault()
141: .getStringVariableManager();
142: String fileLoc = variableManager
143: .generateVariableExpression(
144: "workspace_loc", arg); //$NON-NLS-1$
145: fConfigFileText.setText(fileLoc);
146: }
147: }
148: });
149: }
150:
151: public boolean isValid(ILaunchConfiguration config) {
152: setMessage(null);
153: setErrorMessage(null);
154:
155: return validateServer() | validateConfigSpec();
156: }
157:
158: private boolean validateServer() {
159: return validateServerSpec(fServerText.getText());
160: }
161:
162: private boolean validateConfigSpec() {
163: if (fConfigServerButton.getSelection()) {
164: return validateServerSpec(fConfigServerText.getText());
165: } else {
166: String configSpec = fConfigFileText.getText().trim();
167: if (configSpec != null && !configSpec.startsWith("$")) {
168: try {
169: IStringVariableManager variableManager = VariablesPlugin
170: .getDefault().getStringVariableManager();
171: variableManager.validateStringVariables(configSpec);
172: configSpec = variableManager
173: .performStringSubstitution(configSpec);
174: } catch (CoreException ce) {
175: setErrorMessage(ce.getMessage());
176: updateLaunchConfigurationDialog();
177: return false;
178: }
179: if (configSpec == null || configSpec.length() == 0) {
180: setErrorMessage("Configuration file path is empty.");
181: return false;
182: }
183: Path configSpecPath = new Path(configSpec);
184: if (!configSpecPath.toFile().exists()) {
185: setErrorMessage("File '" + configSpec
186: + "' does not exist.");
187: return false;
188: }
189: }
190: }
191: return true;
192: }
193:
194: /*private*/IResource getResource(String path) {
195: Path containerPath = new Path(path);
196: return ResourcesPlugin.getWorkspace().getRoot().findMember(
197: containerPath);
198: }
199:
200: private boolean validateServerSpec(String text) {
201: if (text != null && text.length() > 0) {
202: String[] elems = StringUtils.split(text, ",");
203:
204: if (elems != null && elems.length > 0) {
205: for (int i = 0; i < elems.length; i++) {
206: String elem = elems[i];
207: int colonIndex = elem.indexOf(':');
208: String host = colonIndex == -1 ? elem : elem
209: .substring(0, colonIndex);
210:
211: if (host == null || host.length() == 0) {
212: setErrorMessage("Malformed server list");
213: return false;
214: }
215: if (colonIndex != -1) {
216: String port = "";
217:
218: try {
219: port = elem.substring(colonIndex + 1);
220: if (port != null && port.length() > 0) {
221: Integer.parseInt(port);
222: }
223: } catch (NumberFormatException nfe) {
224: setErrorMessage("Cannot parse port '"
225: + port + "'");
226: return false;
227: }
228: }
229: }
230: }
231: }
232:
233: return true;
234: }
235:
236: public void setDefaults(
237: ILaunchConfigurationWorkingCopy configuration) {
238: try {
239: IProject project = getProject(configuration);
240: IFile configFile = TcPlugin.getDefault()
241: .getConfigurationFile(project);
242:
243: if (configFile != null) {
244: String arg = configFile.getFullPath().toString();
245: IStringVariableManager variableManager = VariablesPlugin
246: .getDefault().getStringVariableManager();
247: String configSpec = variableManager
248: .generateVariableExpression(
249: "workspace_loc", arg); //$NON-NLS-1$
250: configuration.setAttribute(ID_CONFIG_FILE_SPEC,
251: configSpec);
252: }
253: } catch (CoreException ce) {
254: /**/
255: }
256: }
257:
258: public void initializeFrom(ILaunchConfiguration configuration) {
259: try {
260: String serverSpec = configuration.getAttribute(
261: ID_SERVER_SPEC, "");
262: fServerText.setText(serverSpec);
263: } catch (CoreException ce) {
264: /**/
265: }
266:
267: try {
268: String configFileSpec = configuration.getAttribute(
269: ID_CONFIG_FILE_SPEC, (String) null);
270: if (configFileSpec != null) {
271: fConfigFileText.setText(configFileSpec);
272: fConfigFileButton.setSelection(true);
273: }
274: } catch (CoreException ce) {
275: /**/
276: }
277:
278: try {
279: String configServerSpec = configuration.getAttribute(
280: ID_CONFIG_SERVER_SPEC, (String) null);
281: if (configServerSpec != null) {
282: fConfigServerText.setText(configServerSpec);
283: fConfigServerButton.setSelection(true);
284: }
285: } catch (CoreException ce) {
286: /**/
287: }
288: }
289:
290: private IProject getProject(ILaunchConfiguration configuration)
291: throws CoreException {
292: String projectName = configuration.getAttribute(
293: IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
294: (String) null);
295: if (projectName != null) {
296: projectName = projectName.trim();
297: if (projectName.length() > 0) {
298: return ResourcesPlugin.getWorkspace().getRoot()
299: .getProject(projectName);
300: }
301: }
302: return null;
303: }
304:
305: public void performApply(
306: ILaunchConfigurationWorkingCopy configuration) {
307: String serverSpec = fServerText.getText().trim();
308: if (serverSpec != null && serverSpec.length() > 0) {
309: configuration.setAttribute(ID_SERVER_SPEC, serverSpec);
310: } else {
311: configuration.setAttribute(ID_SERVER_SPEC, (String) null);
312: }
313:
314: if (fConfigFileButton.getSelection()) {
315: String configSpec = fConfigFileText.getText().trim();
316: if (configSpec != null && configSpec.length() > 0) {
317: configuration.setAttribute(ID_CONFIG_FILE_SPEC,
318: configSpec);
319: } else {
320: configuration.setAttribute(ID_CONFIG_FILE_SPEC,
321: (String) null);
322: }
323: } else {
324: String configSpec = fConfigServerText.getText().trim();
325: if (configSpec != null && configSpec.length() > 0) {
326: configuration.setAttribute(ID_CONFIG_SERVER_SPEC,
327: configSpec);
328: } else {
329: configuration.setAttribute(ID_CONFIG_SERVER_SPEC,
330: (String) null);
331: }
332: }
333: }
334:
335: public String getName() {
336: return "Terracotta";
337: }
338: }
|