A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / contentassist / TemplateProposal.java
index 57e4b4a..bb98859 100644 (file)
@@ -54,39 +54,51 @@ import org.eclipse.swt.graphics.Point;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.texteditor.link.EditorLinkedModeUI;
+
 /**
  * A template proposal.
  */
-public class TemplateProposal implements IPHPCompletionProposal, ICompletionProposalExtension2, ICompletionProposalExtension3 {
+public class TemplateProposal implements IPHPCompletionProposal,
+               ICompletionProposalExtension2, ICompletionProposalExtension3 {
 
        private final Template fTemplate;
+
        private final TemplateContext fContext;
+
        private final Image fImage;
+
        private IRegion fRegion;
+
        private int fRelevance;
 
        private IRegion fSelectedRegion; // initialized by apply()
+
        private String fDisplayString;
 
        /**
         * Creates a template proposal with a template and its context.
-        *
-        * @param template  the template
-        * @param context   the context in which the template was requested
-        * @param region        the region this proposal applies to
-        * @param image     the icon of the proposal
+        * 
+        * @param template
+        *            the template
+        * @param context
+        *            the context in which the template was requested
+        * @param region
+        *            the region this proposal applies to
+        * @param image
+        *            the icon of the proposal
         */
-       public TemplateProposal(Template template, TemplateContext context, IRegion region, Image image) {
+       public TemplateProposal(Template template, TemplateContext context,
+                       IRegion region, Image image) {
                Assert.isNotNull(template);
                Assert.isNotNull(context);
                Assert.isNotNull(region);
 
-               fTemplate= template;
-               fContext= context;
-               fImage= image;
-               fRegion= region;
+               fTemplate = template;
+               fContext = context;
+               fImage = image;
+               fRegion = region;
 
-               fDisplayString= null;
+               fDisplayString = null;
 
                if (context instanceof JavaContext) {
                        switch (((JavaContext) context).getCharacterBeforeStart()) {
@@ -95,13 +107,13 @@ public class TemplateProposal implements IPHPCompletionProposal, ICompletionProp
                        case '\r':
                        case '\n':
                        case '\t':
-                               fRelevance= 90;
+                               fRelevance = 90;
                                break;
                        default:
-                               fRelevance= 0;
+                               fRelevance = 0;
                        }
                } else {
-                       fRelevance= 90;
+                       fRelevance = 90;
                }
        }
 
@@ -113,101 +125,114 @@ public class TemplateProposal implements IPHPCompletionProposal, ICompletionProp
        }
 
        /*
-        * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
+        * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer,
+        *      char, int, int)
         */
-       public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
+       public void apply(ITextViewer viewer, char trigger, int stateMask,
+                       int offset) {
 
                try {
 
                        fContext.setReadOnly(false);
                        TemplateBuffer templateBuffer;
                        try {
-                               templateBuffer= fContext.evaluate(fTemplate);
+                               templateBuffer = fContext.evaluate(fTemplate);
                        } catch (TemplateException e1) {
-                               fSelectedRegion= fRegion;
+                               fSelectedRegion = fRegion;
                                return;
                        }
 
-                       int start= getReplaceOffset();
-                       int end= getReplaceEndOffset();
-                       end= Math.max(end, offset);
+                       int start = getReplaceOffset();
+                       int end = getReplaceEndOffset();
+                       end = Math.max(end, offset);
 
                        // insert template string
-                       IDocument document= viewer.getDocument();
-                       String templateString= templateBuffer.getString();
+                       IDocument document = viewer.getDocument();
+                       String templateString = templateBuffer.getString();
                        document.replace(start, end - start, templateString);
 
                        // translate positions
-                       LinkedModeModel model= new LinkedModeModel();
-                       TemplateVariable[] variables= templateBuffer.getVariables();
+                       LinkedModeModel model = new LinkedModeModel();
+                       TemplateVariable[] variables = templateBuffer.getVariables();
 
-                       MultiVariableGuess guess= fContext instanceof CompilationUnitContext ? ((CompilationUnitContext) fContext).getMultiVariableGuess() : null;
+                       MultiVariableGuess guess = fContext instanceof CompilationUnitContext ? ((CompilationUnitContext) fContext)
+                                       .getMultiVariableGuess()
+                                       : null;
 
-                       boolean hasPositions= false;
-                       for (int i= 0; i != variables.length; i++) {
-                               TemplateVariable variable= variables[i];
+                       boolean hasPositions = false;
+                       for (int i = 0; i != variables.length; i++) {
+                               TemplateVariable variable = variables[i];
 
                                if (variable.isUnambiguous())
                                        continue;
 
-                               LinkedPositionGroup group= new LinkedPositionGroup();
+                               LinkedPositionGroup group = new LinkedPositionGroup();
 
-                               int[] offsets= variable.getOffsets();
-                               int length= variable.getLength();
+                               int[] offsets = variable.getOffsets();
+                               int length = variable.getLength();
 
                                LinkedPosition first;
                                if (guess != null && variable instanceof MultiVariable) {
-                                       first= new VariablePosition(document, offsets[0] + start, length, guess, (MultiVariable) variable);
+                                       first = new VariablePosition(document, offsets[0] + start,
+                                                       length, guess, (MultiVariable) variable);
                                        guess.addSlave((VariablePosition) first);
                                } else {
-                                       String[] values= variable.getValues();
-                                       ICompletionProposal[] proposals= new ICompletionProposal[values.length];
-                                       for (int j= 0; j < values.length; j++) {
+                                       String[] values = variable.getValues();
+                                       ICompletionProposal[] proposals = new ICompletionProposal[values.length];
+                                       for (int j = 0; j < values.length; j++) {
                                                ensurePositionCategoryInstalled(document, model);
-                                               Position pos= new Position(offsets[0] + start, length);
+                                               Position pos = new Position(offsets[0] + start, length);
                                                document.addPosition(getCategory(), pos);
-                                               proposals[j]= new PositionBasedCompletionProposal(values[j], pos, length);
+                                               proposals[j] = new PositionBasedCompletionProposal(
+                                                               values[j], pos, length);
                                        }
 
                                        if (proposals.length > 1)
-                                               first= new ProposalPosition(document, offsets[0] + start, length, proposals);
+                                               first = new ProposalPosition(document, offsets[0]
+                                                               + start, length, proposals);
                                        else
-                                               first= new LinkedPosition(document, offsets[0] + start, length);
+                                               first = new LinkedPosition(document,
+                                                               offsets[0] + start, length);
                                }
 
-                               for (int j= 0; j != offsets.length; j++)
+                               for (int j = 0; j != offsets.length; j++)
                                        if (j == 0)
                                                group.addPosition(first);
                                        else
-                                               group.addPosition(new LinkedPosition(document, offsets[j] + start, length));
+                                               group.addPosition(new LinkedPosition(document,
+                                                               offsets[j] + start, length));
 
                                model.addGroup(group);
-                               hasPositions= true;
+                               hasPositions = true;
                        }
 
                        if (hasPositions) {
                                model.forceInstall();
-                               PHPEditor editor= getJavaEditor();
+                               PHPEditor editor = getJavaEditor();
                                if (editor != null) {
-                                       model.addLinkingListener(new EditorHighlightingSynchronizer(editor));
+                                       model
+                                                       .addLinkingListener(new EditorHighlightingSynchronizer(
+                                                                       editor));
                                }
 
-                               LinkedModeUI ui= new EditorLinkedModeUI(model, viewer);
-                               ui.setExitPosition(viewer, getCaretOffset(templateBuffer) + start, 0, Integer.MAX_VALUE);
+                               LinkedModeUI ui = new EditorLinkedModeUI(model, viewer);
+                               ui.setExitPosition(viewer, getCaretOffset(templateBuffer)
+                                               + start, 0, Integer.MAX_VALUE);
                                ui.enter();
 
-                               fSelectedRegion= ui.getSelectedRegion();
+                               fSelectedRegion = ui.getSelectedRegion();
                        } else
-                               fSelectedRegion= new Region(getCaretOffset(templateBuffer) + start, 0);
+                               fSelectedRegion = new Region(getCaretOffset(templateBuffer)
+                                               + start, 0);
 
                } catch (BadLocationException e) {
-                 PHPeclipsePlugin.log(e);
+                       PHPeclipsePlugin.log(e);
                        openErrorDialog(viewer.getTextWidget().getShell(), e);
-                       fSelectedRegion= fRegion;
+                       fSelectedRegion = fRegion;
                } catch (BadPositionCategoryException e) {
-                 PHPeclipsePlugin.log(e);
+                       PHPeclipsePlugin.log(e);
                        openErrorDialog(viewer.getTextWidget().getShell(), e);
-                       fSelectedRegion= fRegion;
+                       fSelectedRegion = fRegion;
                }
 
        }
@@ -215,11 +240,11 @@ public class TemplateProposal implements IPHPCompletionProposal, ICompletionProp
        /**
         * Returns the currently active java editor, or <code>null</code> if it
         * cannot be determined.
-        *
-        * @return  the currently active java editor, or <code>null</code>
+        * 
+        * @return the currently active java editor, or <code>null</code>
         */
        private PHPEditor getJavaEditor() {
-               IEditorPart part= PHPeclipsePlugin.getActivePage().getActiveEditor();
+               IEditorPart part = PHPeclipsePlugin.getActivePage().getActiveEditor();
                if (part instanceof PHPEditor)
                        return (PHPEditor) part;
                else
@@ -229,17 +254,17 @@ public class TemplateProposal implements IPHPCompletionProposal, ICompletionProp
        /**
         * Returns the offset of the range in the document that will be replaced by
         * applying this template.
-        *
+        * 
         * @return the offset of the range in the document that will be replaced by
         *         applying this template
         */
        private int getReplaceOffset() {
                int start;
                if (fContext instanceof DocumentTemplateContext) {
-                       DocumentTemplateContext docContext = (DocumentTemplateContext)fContext;
-                       start= docContext.getStart();
+                       DocumentTemplateContext docContext = (DocumentTemplateContext) fContext;
+                       start = docContext.getStart();
                } else {
-                       start= fRegion.getOffset();
+                       start = fRegion.getOffset();
                }
                return start;
        }
@@ -247,31 +272,34 @@ public class TemplateProposal implements IPHPCompletionProposal, ICompletionProp
        /**
         * Returns the end offset of the range in the document that will be replaced
         * by applying this template.
-        *
+        * 
         * @return the end offset of the range in the document that will be replaced
         *         by applying this template
         */
        private int getReplaceEndOffset() {
                int end;
                if (fContext instanceof DocumentTemplateContext) {
-                       DocumentTemplateContext docContext = (DocumentTemplateContext)fContext;
-                       end= docContext.getEnd();
+                       DocumentTemplateContext docContext = (DocumentTemplateContext) fContext;
+                       end = docContext.getEnd();
                } else {
-                       end= fRegion.getOffset() + fRegion.getLength();
+                       end = fRegion.getOffset() + fRegion.getLength();
                }
                return end;
        }
 
-       private void ensurePositionCategoryInstalled(final IDocument document, LinkedModeModel model) {
+       private void ensurePositionCategoryInstalled(final IDocument document,
+                       LinkedModeModel model) {
                if (!document.containsPositionCategory(getCategory())) {
                        document.addPositionCategory(getCategory());
-                       final InclusivePositionUpdater updater= new InclusivePositionUpdater(getCategory());
+                       final InclusivePositionUpdater updater = new InclusivePositionUpdater(
+                                       getCategory());
                        document.addPositionUpdater(updater);
 
                        model.addLinkingListener(new ILinkedModeListener() {
 
                                /*
-                                * @see org.eclipse.jface.text.link.ILinkedModeListener#left(org.eclipse.jface.text.link.LinkedModeModel, int)
+                                * @see org.eclipse.jface.text.link.ILinkedModeListener#left(org.eclipse.jface.text.link.LinkedModeModel,
+                                *      int)
                                 */
                                public void left(LinkedModeModel environment, int flags) {
                                        try {
@@ -282,8 +310,11 @@ public class TemplateProposal implements IPHPCompletionProposal, ICompletionProp
                                        document.removePositionUpdater(updater);
                                }
 
-                               public void suspend(LinkedModeModel environment) {}
-                               public void resume(LinkedModeModel environment, int flags) {}
+                               public void suspend(LinkedModeModel environment) {
+                               }
+
+                               public void resume(LinkedModeModel environment, int flags) {
+                               }
                        });
                }
        }
@@ -294,9 +325,9 @@ public class TemplateProposal implements IPHPCompletionProposal, ICompletionProp
 
        private int getCaretOffset(TemplateBuffer buffer) {
 
-           TemplateVariable[] variables= buffer.getVariables();
-               for (int i= 0; i != variables.length; i++) {
-                       TemplateVariable variable= variables[i];
+               TemplateVariable[] variables = buffer.getVariables();
+               for (int i = 0; i != variables.length; i++) {
+                       TemplateVariable variable = variables[i];
                        if (variable.getType().equals(GlobalTemplateVariables.Cursor.NAME))
                                return variable.getOffsets()[0];
                }
@@ -308,26 +339,29 @@ public class TemplateProposal implements IPHPCompletionProposal, ICompletionProp
         * @see ICompletionProposal#getSelection(IDocument)
         */
        public Point getSelection(IDocument document) {
-               return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength());
+               return new Point(fSelectedRegion.getOffset(), fSelectedRegion
+                               .getLength());
        }
 
        /*
         * @see ICompletionProposal#getAdditionalProposalInfo()
         */
        public String getAdditionalProposalInfo() {
-           try {
-                   fContext.setReadOnly(true);
+               try {
+                       fContext.setReadOnly(true);
                        TemplateBuffer templateBuffer;
                        try {
-                               templateBuffer= fContext.evaluate(fTemplate);
+                               templateBuffer = fContext.evaluate(fTemplate);
                        } catch (TemplateException e1) {
                                return null;
                        }
 
                        return templateBuffer.getString();
 
-           } catch (BadLocationException e) {
-                       handleException(PHPeclipsePlugin.getActiveWorkbenchShell(), new CoreException(new Status(IStatus.ERROR, PHPeclipsePlugin.getPluginId(), IStatus.OK, "", e))); //$NON-NLS-1$
+               } catch (BadLocationException e) {
+                       handleException(PHPeclipsePlugin.getActiveWorkbenchShell(),
+                                       new CoreException(new Status(IStatus.ERROR,
+                                                       PHPeclipsePlugin.getPluginId(), IStatus.OK, "", e))); //$NON-NLS-1$
                        return null;
                }
        }
@@ -337,13 +371,15 @@ public class TemplateProposal implements IPHPCompletionProposal, ICompletionProp
         */
        public String getDisplayString() {
                if (fDisplayString == null) {
-                       fDisplayString= fTemplate.getName() + TemplateContentAssistMessages.getString("TemplateProposal.delimiter") + fTemplate.getDescription(); //$NON-NLS-1$
+                       fDisplayString = fTemplate.getName()
+                                       + TemplateContentAssistMessages
+                                                       .getString("TemplateProposal.delimiter") + fTemplate.getDescription(); //$NON-NLS-1$
                }
                return fDisplayString;
        }
 
        public void setDisplayString(String displayString) {
-               fDisplayString= displayString;
+               fDisplayString = displayString;
        }
 
        /*
@@ -361,11 +397,13 @@ public class TemplateProposal implements IPHPCompletionProposal, ICompletionProp
        }
 
        private void openErrorDialog(Shell shell, Exception e) {
-               MessageDialog.openError(shell, TemplateContentAssistMessages.getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$
+               MessageDialog.openError(shell, TemplateContentAssistMessages
+                               .getString("TemplateEvaluator.error.title"), e.getMessage()); //$NON-NLS-1$
        }
 
        private void handleException(Shell shell, CoreException e) {
-               ExceptionHandler.handle(e, shell, TemplateContentAssistMessages.getString("TemplateEvaluator.error.title"), null); //$NON-NLS-1$
+               ExceptionHandler.handle(e, shell, TemplateContentAssistMessages
+                               .getString("TemplateEvaluator.error.title"), null); //$NON-NLS-1$
        }
 
        /*
@@ -376,7 +414,7 @@ public class TemplateProposal implements IPHPCompletionProposal, ICompletionProp
        }
 
        public void setRelevance(int relevance) {
-               fRelevance= relevance;
+               fRelevance = relevance;
        }
 
        public Template getTemplate() {
@@ -391,7 +429,8 @@ public class TemplateProposal implements IPHPCompletionProposal, ICompletionProp
        }
 
        /*
-        * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer, boolean)
+        * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer,
+        *      boolean)
         */
        public void selected(ITextViewer viewer, boolean smartToggle) {
        }
@@ -403,13 +442,15 @@ public class TemplateProposal implements IPHPCompletionProposal, ICompletionProp
        }
 
        /*
-        * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument, int, org.eclipse.jface.text.DocumentEvent)
+        * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument,
+        *      int, org.eclipse.jface.text.DocumentEvent)
         */
        public boolean validate(IDocument document, int offset, DocumentEvent event) {
                try {
-                       int replaceOffset= getReplaceOffset();
+                       int replaceOffset = getReplaceOffset();
                        if (offset >= replaceOffset) {
-                               String content= document.get(replaceOffset, offset - replaceOffset);
+                               String content = document.get(replaceOffset, offset
+                                               - replaceOffset);
                                return fTemplate.getName().startsWith(content);
                        }
                } catch (BadLocationException e) {
@@ -421,7 +462,8 @@ public class TemplateProposal implements IPHPCompletionProposal, ICompletionProp
        /*
         * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension3#getReplacementString()
         */
-       public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) {
+       public CharSequence getPrefixCompletionText(IDocument document,
+                       int completionOffset) {
                return fTemplate.getName();
        }