KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > refactoring > LaunchConfigTypeChange


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 /*
12  * Created on Apr 11, 2004
13  *
14  * To change the template for this generated file go to
15  * Window - Preferences - Java - Code Generation - Code and Comments
16  */

17 package org.eclipse.jdt.internal.junit.refactoring;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.debug.core.DebugPlugin;
22 import org.eclipse.debug.core.ILaunchConfiguration;
23 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
24 import org.eclipse.jdt.core.IType;
25 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
26 import org.eclipse.ltk.core.refactoring.Change;
27 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
28
29
30 class LaunchConfigTypeChange extends Change {
31
32     private IType fType;
33     private ILaunchConfiguration fConfig;
34     private String JavaDoc fNewName;
35
36     public LaunchConfigTypeChange(IType type, ILaunchConfiguration config, String JavaDoc newName) {
37         fType= type;
38         fConfig= config;
39         fNewName= newName;
40     }
41     
42     /**
43      * {@inheritDoc}
44      */

45     public String JavaDoc getName() {
46         return fConfig.getName();
47     }
48     
49     /**
50      * {@inheritDoc}
51      */

52     public void initializeValidationData(IProgressMonitor pm) {
53         // must be implemented to decide correct value of isValid
54
}
55
56     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
57         return new RefactoringStatus();
58     }
59     
60     /* (non-Javadoc)
61      * @see org.eclipse.jdt.internal.corext.refactoring.base.IChange#perform(org.eclipse.jdt.internal.corext.refactoring.base.ChangeContext, org.eclipse.core.runtime.IProgressMonitor)
62      */

63     public Change perform(IProgressMonitor pm) throws CoreException {
64         pm.beginTask("", 1); //$NON-NLS-1$
65
String JavaDoc current= fConfig.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String JavaDoc)null);
66         int index= current.lastIndexOf('.');
67         String JavaDoc newTypeName;
68         if (index == -1) {
69             newTypeName= fNewName;
70         } else {
71             newTypeName= current.substring(0, index + 1) + fNewName;
72         }
73         ILaunchConfigurationWorkingCopy copy= fConfig.getWorkingCopy();
74         copy.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, newTypeName);
75         
76         // generate the new configuration name
77
String JavaDoc launchConfigurationName= fConfig.getName();
78         
79         if (launchConfigurationName.equals(current)) {
80             if (!DebugPlugin.getDefault().getLaunchManager().isExistingLaunchConfigurationName(fNewName))
81                 copy.rename(fNewName);
82         }
83         copy.doSave();
84         pm.worked(1);
85         return new LaunchConfigTypeChange(fType, fConfig, (index == -1) ? current : current.substring(index + 1));
86     }
87
88     /* (non-Javadoc)
89      * @see org.eclipse.jdt.internal.corext.refactoring.base.IChange#getModifiedLanguageElement()
90      */

91     public Object JavaDoc getModifiedElement() {
92         return fConfig;
93     }
94 }
Popular Tags