001: /**
002: *
003: */package gui.actions;
004:
005: import java.util.Iterator;
006: import java.util.List;
007:
008: import org.eclipse.core.resources.IResource;
009: import org.eclipse.core.resources.IWorkspaceRoot;
010: import org.eclipse.core.resources.ResourcesPlugin;
011: import org.eclipse.core.runtime.IPath;
012: import org.eclipse.jdt.core.ICompilationUnit;
013: import org.eclipse.jdt.core.dom.FieldDeclaration;
014: import org.eclipse.jface.text.ITextSelection;
015: import org.eclipse.swt.widgets.Shell;
016: import org.eclipse.ui.IEditorInput;
017: import org.eclipse.ui.IEditorPart;
018: import org.eclipse.ui.IPathEditorInput;
019: import org.eclipse.ui.PlatformUI;
020: import org.eclipse.ui.ide.IDE;
021:
022: import core.CompilationUnitParser;
023: import core.Util;
024:
025: /**
026: * A helper class to initialize the Rename Dialog.
027: * @author sh
028: */
029: public class ActionUtil {
030:
031: /**
032: * This method scans all java source files in the workspace if one
033: * contains the given simple type. If a match has been found a
034: * FieldDeclaration instance and its compilation unit will be returned
035: * by a map.
036: *
037: * @param selection the text selection of the type in the active editor
038: * @return a field declaration instance of the selected type
039: */
040: public static FieldDeclaration scanForSimpleType(
041: ITextSelection selection) {
042: if (selection == null || selection.isEmpty()
043: || selection.getText().equals(""))
044: return null;
045:
046: // find the Java Source file beginning from the active editor
047: IEditorPart part = PlatformUI.getWorkbench()
048: .getActiveWorkbenchWindow().getActivePage()
049: .getActiveEditor();
050:
051: // get the input from the active editor
052: IEditorInput input = part.getEditorInput();
053: if (input instanceof IPathEditorInput) {
054: IPathEditorInput pathInput = (IPathEditorInput) input;
055: IPath filePath = pathInput.getPath();
056: filePath = filePath.removeLastSegments(1);
057:
058: // get the filename of the file currently opened in the active editor
059: String currentFile = pathInput.getName();
060:
061: // get the package which the file contains
062: String packageName = filePath.lastSegment();
063:
064: // get the compilation units of the workspace
065: List<ICompilationUnit> units = Util.getICompilationUnits();
066:
067: // seach the compilation unit of the concerning file
068: ICompilationUnit targetUnit = null;
069: for (Iterator<ICompilationUnit> iterator = units.iterator(); iterator
070: .hasNext();) {
071: ICompilationUnit unit = (ICompilationUnit) iterator
072: .next();
073:
074: // check if the cu has the filename which is
075: // selected and if it is the correct package
076: String cuPackage = Util.classname(unit.getParent()
077: .getElementName());
078: String cuClassName = unit.getElementName();
079: if (currentFile.equals(cuClassName)
080: && packageName.equals(cuPackage))
081: targetUnit = unit;
082: }
083:
084: // scan for field definitions matching the selection
085: if (((ITextSelection) selection).getText().isEmpty()
086: && targetUnit == null)
087: return null;
088:
089: CompilationUnitParser unitParser = new CompilationUnitParser();
090: String sel = ((ITextSelection) selection).getText();
091: return unitParser.parseFieldDeclarations(targetUnit, sel);
092: }
093: return null;
094: }
095:
096: /**
097: * helper method to get a shell.
098: * @return a shell
099: */
100: public static Shell getShell() {
101: return PlatformUI.getWorkbench().getActiveWorkbenchWindow()
102: .getShell();
103: }
104:
105: /**
106: * helper method which saves all modified resources.
107: * @return
108: */
109: public static boolean saveAll() {
110: IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace()
111: .getRoot();
112: return IDE.saveAllEditors(new IResource[] { workspaceRoot },
113: false);
114: }
115: }
|