Ignore:
Timestamp:
09/08/13 23:16:15 (11 years ago)
Author:
masberg
Message:

Bugfix: NullPointerException? when trying to open extensionless in MDT project. Formatting fixes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/org.modelica.mdt.ui/src/org/modelica/mdt/ui/refactoring/ui/actions/RenameFunction.java

    r694 r1811  
    2020
    2121/** <p>action that is triggered from the editor context menu.</p>
    22   *
    23   * <p>This action is declared in the <code>plugin.xml</code>.</p>
    24   *
    25   * @author Leif Frenzel
    26   */
     22 *
     23 * <p>This action is declared in the <code>plugin.xml</code>.</p>
     24 *
     25 * @author Leif Frenzel
     26 */
    2727public class RenameFunction implements IEditorActionDelegate {
    2828
    29   private static final String EXT_MO = "mo"; //$NON-NLS-1$
    30  
    31   private ISelection selection;
    32   private IEditorPart targetEditor;
    33   private boolean onPropertiesFile;
     29    private static final String EXT_MO = "mo"; //$NON-NLS-1$
    3430
    35   private RenameFunctionInfo info = new RenameFunctionInfo();
    36  
    37  
    38   // interface methods of IEditorActionDelegate
    39   /////////////////////////////////////////////
     31    private ISelection selection;
     32    private IEditorPart targetEditor;
     33    private boolean onPropertiesFile;
    4034
    41   public void setActiveEditor( final IAction action,
    42                                final IEditorPart targetEditor ) {
    43     this.targetEditor = targetEditor;
    44     onPropertiesFile = false;
    45     IFile file = getFile();
    46     if(    file != null
    47         && file.getFileExtension().equals( EXT_MO ) ) {
    48       onPropertiesFile = true;
    49     }                           
    50   }
     35    private RenameFunctionInfo info = new RenameFunctionInfo();
    5136
    52   public void run( final IAction action ) {
    53     if( !onPropertiesFile ) {
    54       refuse();
    55     } else {
    56       if( selection != null && selection instanceof ITextSelection ) {
    57         applySelection( ( ITextSelection )selection );
    58         if( saveAll() ) {
    59           openWizard();
    60         }
    61       }
    62     }
    63   }
    6437
    65   public void selectionChanged( final IAction action,
    66                                 final ISelection selection ) {
    67     this.selection = selection;
    68   }
    69  
    70  
    71   // helping methods
    72   //////////////////
     38    // interface methods of IEditorActionDelegate
     39    /////////////////////////////////////////////
    7340
    74   private void applySelection( final ITextSelection textSelection ) {
    75     info.setOldName( textSelection.getText() );
    76     info.setNewName( textSelection.getText() );
    77     info.setOffset( textSelection.getOffset() );
    78     info.setSourceFile( getFile() );
    79   }
     41    public void setActiveEditor(final IAction action, final IEditorPart targetEditor) {
     42        this.targetEditor = targetEditor;
     43        onPropertiesFile = false;
     44        IFile file = getFile();
     45        if (file != null) {
     46            String fileExtension = file.getFileExtension();
     47            if (fileExtension != null && fileExtension.equals(EXT_MO)) {
     48                onPropertiesFile = true;
     49            }
     50        }                           
     51    }
    8052
    81   private void refuse() {
    82     String title = UITexts.renameFunction_refuseDlg_title;
    83     String message = UITexts.renameFunction_refuseDlg_message;
    84     MessageDialog.openInformation( getShell(), title, message );
    85   }
     53    public void run(final IAction action) {
     54        if (!onPropertiesFile) {
     55            refuse();
     56        }
     57        else {
     58            if (selection != null && selection instanceof ITextSelection) {
     59                applySelection((ITextSelection)selection);
     60                if (saveAll()) {
     61                    openWizard();
     62                }
     63            }
     64        }
     65    }
    8666
    87   private static boolean saveAll() {
    88     IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    89     return IDE.saveAllEditors( new IResource[] { workspaceRoot }, false );
    90   }
    91  
    92   private void openWizard() {
    93     RefactoringProcessor processor = new RenameFunctionProcessor( info );
    94     RenameFunctionRefactoring ref = new RenameFunctionRefactoring( processor );
    95     RenameFunctionWizard wizard = new RenameFunctionWizard( ref, info );
    96     RefactoringWizardOpenOperation op
    97       = new RefactoringWizardOpenOperation( wizard );
    98     try {
    99       String titleForFailedChecks = ""; //$NON-NLS-1$
    100       op.run( getShell(), titleForFailedChecks );
    101     } catch( final InterruptedException irex ) {
    102       // operation was cancelled
    103     }
    104   }
     67    public void selectionChanged(final IAction action, final ISelection selection) {
     68        this.selection = selection;
     69    }
    10570
    106   private Shell getShell() {
    107     Shell result = null;
    108     if( targetEditor != null ) {
    109       result = targetEditor.getSite().getShell();
    110     } else {
    111       result = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
    112     }
    113     return result;
    114   }
    115  
    116   private final IFile getFile() {
    117     IFile result = null;
    118     if( targetEditor instanceof ITextEditor )  {
    119       ITextEditor editor = ( ITextEditor )targetEditor;
    120       IEditorInput input = editor.getEditorInput();
    121       if( input instanceof IFileEditorInput ) {
    122         result = ( ( IFileEditorInput )input ).getFile();
    123       }
    124     }
    125     return result;
    126   }
     71
     72    // helper methods
     73    //////////////////
     74
     75    private void applySelection(final ITextSelection textSelection) {
     76        info.setOldName(textSelection.getText());
     77        info.setNewName(textSelection.getText());
     78        info.setOffset(textSelection.getOffset());
     79        info.setSourceFile(getFile());
     80    }
     81
     82    private void refuse() {
     83        String title = UITexts.renameFunction_refuseDlg_title;
     84        String message = UITexts.renameFunction_refuseDlg_message;
     85        MessageDialog.openInformation(getShell(), title, message);
     86    }
     87
     88    private static boolean saveAll() {
     89        IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
     90        return IDE.saveAllEditors(new IResource[] { workspaceRoot }, false);
     91    }
     92
     93    private void openWizard() {
     94        RefactoringProcessor processor = new RenameFunctionProcessor(info);
     95        RenameFunctionRefactoring ref = new RenameFunctionRefactoring(processor);
     96        RenameFunctionWizard wizard = new RenameFunctionWizard(ref, info);
     97        RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard);
     98        try {
     99            String titleForFailedChecks = ""; //$NON-NLS-1$
     100            op.run( getShell(), titleForFailedChecks );
     101        }
     102        catch (final InterruptedException irex) {
     103            // operation was cancelled
     104        }
     105    }
     106
     107    private Shell getShell() {
     108        Shell result = null;
     109        if (targetEditor != null) {
     110            result = targetEditor.getSite().getShell();
     111        }
     112        else {
     113            result = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
     114        }
     115        return result;
     116    }
     117
     118    private final IFile getFile() {
     119        IFile result = null;
     120        if (targetEditor instanceof ITextEditor)  {
     121            ITextEditor editor = (ITextEditor)targetEditor;
     122            IEditorInput input = editor.getEditorInput();
     123            if (input instanceof IFileEditorInput) {
     124                result = ((IFileEditorInput)input).getFile();
     125            }
     126        }
     127        return result;
     128    }
    127129}
Note: See TracChangeset for help on using the changeset viewer.