a9389eaf9bd89f1dbeef5a98760d4e3f5511ed72
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / link / LinkedPositionUI.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.ui.text.link;
6
7 import java.lang.reflect.InvocationTargetException;
8
9 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.jface.dialogs.MessageDialog;
12 import org.eclipse.jface.preference.IPreferenceStore;
13 import org.eclipse.jface.preference.PreferenceConverter;
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.BadPositionCategoryException;
16 import org.eclipse.jface.text.DefaultPositionUpdater;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.IPositionUpdater;
19 import org.eclipse.jface.text.IRegion;
20 import org.eclipse.jface.text.ITextInputListener;
21 import org.eclipse.jface.text.ITextViewer;
22 import org.eclipse.jface.text.ITextViewerExtension;
23 import org.eclipse.jface.text.Position;
24 import org.eclipse.jface.text.Region;
25 import org.eclipse.jface.util.Assert;
26 import org.eclipse.jface.util.IPropertyChangeListener;
27 import org.eclipse.jface.util.PropertyChangeEvent;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.custom.StyledText;
30 import org.eclipse.swt.custom.VerifyKeyListener;
31 import org.eclipse.swt.events.ModifyEvent;
32 import org.eclipse.swt.events.ModifyListener;
33 import org.eclipse.swt.events.PaintEvent;
34 import org.eclipse.swt.events.PaintListener;
35 import org.eclipse.swt.events.VerifyEvent;
36 import org.eclipse.swt.events.VerifyListener;
37 import org.eclipse.swt.graphics.Color;
38 import org.eclipse.swt.graphics.GC;
39 import org.eclipse.swt.graphics.Point;
40 import org.eclipse.swt.graphics.RGB;
41 import org.eclipse.swt.widgets.Display;
42 import org.eclipse.swt.widgets.Shell;
43
44 /**
45  * A user interface for <code>LinkedPositionManager</code>, using <code>ITextViewer</code>.
46  */
47 public class LinkedPositionUI implements LinkedPositionListener,
48         ITextInputListener, ModifyListener, VerifyListener, VerifyKeyListener, PaintListener, IPropertyChangeListener {
49
50         /**
51          * A listener for notification when the user cancelled the edit operation.
52          */
53         public interface ExitListener {
54                 void exit(boolean accept);
55         }
56         
57   /** Preference key for linked position color */
58  // public final static String LINKED_POSITION_COLOR= "_linkedPositionColor"; //$NON-NLS-1$
59         // leave flags
60         private static final int UNINSTALL= 1;                  // uninstall linked position manager
61         private static final int COMMIT= 2;                             // commit changes
62         private static final int DOCUMENT_CHANGED= 4;   // document has changed
63         private static final int UPDATE_CARET= 8;               // update caret
64
65         private static final String CARET_POSITION= "LinkedPositionUI.caret.position"; //$NON-NLS-1$
66         private static final IPositionUpdater fgUpdater= new DefaultPositionUpdater(CARET_POSITION);
67         private static final IPreferenceStore fgStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
68         
69         private final ITextViewer fViewer;
70         private final LinkedPositionManager fManager;   
71         private Color fFrameColor;
72
73         private int fFinalCaretOffset= -1; // no final caret offset
74
75         private Position fFramePosition;
76         private int fCaretOffset;
77         
78         private ExitListener fExitListener;
79         
80         /**
81          * Creates a user interface for <code>LinkedPositionManager</code>.
82          * 
83          * @param viewer  the text viewer.
84          * @param manager the <code>LinkedPositionManager</code> managing a <code>IDocument</code> of the <code>ITextViewer</code>.
85          */
86         public LinkedPositionUI(ITextViewer viewer, LinkedPositionManager manager) {
87                 Assert.isNotNull(viewer);
88                 Assert.isNotNull(manager);
89                 
90                 fViewer= viewer;
91                 fManager= manager;
92                 
93                 fManager.setLinkedPositionListener(this);
94
95                 initializeHighlightColor(viewer);
96         }
97
98         /*
99          * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
100          */
101         public void propertyChange(PropertyChangeEvent event) {
102         //      if (event.getProperty().equals(CompilationUnitEditor.LINKED_POSITION_COLOR)) {
103     if (event.getProperty().equals(PHPeclipsePlugin.LINKED_POSITION_COLOR)) {
104                         initializeHighlightColor(fViewer);
105                         redrawRegion();
106                 }
107         }
108
109         private void initializeHighlightColor(ITextViewer viewer) {
110
111                 if (fFrameColor != null)
112                         fFrameColor.dispose();
113
114                 StyledText text= viewer.getTextWidget();
115                 if (text != null) {
116                         Display display= text.getDisplay();
117         //              fFrameColor= createColor(fgStore, CompilationUnitEditor.LINKED_POSITION_COLOR, display);
118                   fFrameColor= createColor(fgStore, PHPeclipsePlugin.LINKED_POSITION_COLOR, display);
119     }
120         }
121
122         /**
123          * Creates a color from the information stored in the given preference store.
124          * Returns <code>null</code> if there is no such information available.
125          */
126         private Color createColor(IPreferenceStore store, String key, Display display) {
127         
128                 RGB rgb= null;          
129                 
130                 if (store.contains(key)) {
131                         
132                         if (store.isDefault(key))
133                                 rgb= PreferenceConverter.getDefaultColor(store, key);
134                         else
135                                 rgb= PreferenceConverter.getColor(store, key);
136                 
137                         if (rgb != null)
138                                 return new Color(display, rgb);
139                 }
140                 
141                 return null;
142         }
143         
144         /**
145          * Sets the final position of the caret when the linked mode is exited
146          * successfully by leaving the last linked position using TAB.
147          */
148         public void setFinalCaretOffset(int offset) {
149                 fFinalCaretOffset= offset;      
150         }
151
152         /**
153          * Sets a <code>CancelListener</code> which is notified if the linked mode
154          * is exited unsuccessfully by hitting ESC.
155          */
156         public void setCancelListener(ExitListener listener) {
157                 fExitListener= listener;
158         }
159
160         /*
161          * @see LinkedPositionManager.LinkedPositionListener#setCurrentPositions(Position, int)
162          */
163         public void setCurrentPosition(Position position, int caretOffset) {
164                 if (!fFramePosition.equals(position)) {
165                         redrawRegion();
166                         fFramePosition= position;
167                 }
168
169                 fCaretOffset= caretOffset;
170         }
171
172         /**
173          * Enters the linked mode. The linked mode can be left by calling
174          * <code>exit</code>.
175          * 
176          * @see #exit(boolean)
177          */
178         public void enter() {
179                 // track final caret
180                 IDocument document= fViewer.getDocument();
181                 document.addPositionCategory(CARET_POSITION);
182                 document.addPositionUpdater(fgUpdater);
183                 try {
184                         if (fFinalCaretOffset != -1)
185                                 document.addPosition(CARET_POSITION, new Position(fFinalCaretOffset));
186                 } catch (BadLocationException e) {
187                         handleException(fViewer.getTextWidget().getShell(), e);
188
189                 } catch (BadPositionCategoryException e) {
190                         PHPeclipsePlugin.log(e);
191                         Assert.isTrue(false);
192                 }
193
194                 fViewer.addTextInputListener(this);
195                                 
196                 ITextViewerExtension extension= (ITextViewerExtension) fViewer;
197                 extension.prependVerifyKeyListener(this);
198
199                 StyledText text= fViewer.getTextWidget();                       
200                 text.addVerifyListener(this);
201                 text.addModifyListener(this);
202                 text.addPaintListener(this);
203                 text.showSelection();
204
205                 fFramePosition= fManager.getFirstPosition();
206                 if (fFramePosition == null)
207                         leave(UNINSTALL | COMMIT | UPDATE_CARET);
208
209                 fgStore.addPropertyChangeListener(this);
210         }
211
212         /*
213          * @see LinkedPositionManager.LinkedPositionListener#exit(boolean)
214          */
215         public void exit(boolean success) {
216                 // no UNINSTALL since manager has already uninstalled itself
217                 leave((success ? COMMIT : 0) | UPDATE_CARET);
218         }
219
220         /**
221          * Returns the cursor selection, after having entered the linked mode.
222          * <code>enter()</code> must be called prior to a call to this method.
223          */
224         public IRegion getSelectedRegion() {
225                 if (fFramePosition == null)
226                         return new Region(fFinalCaretOffset, 0);
227                 else
228                         return new Region(fFramePosition.getOffset(), fFramePosition.getLength());
229         }
230         
231         private void leave(int flags) {
232                 if ((flags & UNINSTALL) != 0)
233                         fManager.uninstall((flags & COMMIT) != 0);
234
235                 fgStore.removePropertyChangeListener(this);
236                 
237                 if (fFrameColor != null) {
238                         fFrameColor.dispose();
239                         fFrameColor= null;
240                 }                       
241                 
242                 StyledText text= fViewer.getTextWidget();       
243                 text.removePaintListener(this);
244                 text.removeModifyListener(this);
245                 text.removeVerifyListener(this);
246
247                 ITextViewerExtension extension= (ITextViewerExtension) fViewer;
248                 extension.removeVerifyKeyListener(this);
249
250                 fViewer.removeTextInputListener(this);
251                 
252                 try {
253                         IRegion region= fViewer.getVisibleRegion();
254                         IDocument document= fViewer.getDocument();
255
256                         if (((flags & COMMIT) != 0) &&
257                                 ((flags & DOCUMENT_CHANGED) == 0) &&
258                                 ((flags & UPDATE_CARET) != 0))
259                         {
260                                 Position[] positions= document.getPositions(CARET_POSITION);
261
262                                 if ((positions != null) && (positions.length != 0)) {
263                                         int offset= positions[0].getOffset() - region.getOffset();              
264                                         if ((offset >= 0) && (offset <= region.getLength()))
265                                                 text.setSelection(offset, offset);
266                                 }
267                         }
268
269                         document.removePositionUpdater(fgUpdater);
270                         document.removePositionCategory(CARET_POSITION);
271                         
272                         if (fExitListener != null)
273                                 fExitListener.exit(
274                                         ((flags & COMMIT) != 0) ||
275                                         ((flags & DOCUMENT_CHANGED) != 0));
276
277                 } catch (BadPositionCategoryException e) {
278                         PHPeclipsePlugin.log(e);
279                         Assert.isTrue(false);
280                 }
281
282                 if ((flags & DOCUMENT_CHANGED) == 0)
283                         text.redraw();
284         }
285
286         private void next() {
287                 redrawRegion();
288                 
289                 fFramePosition= fManager.getNextPosition(fFramePosition.getOffset());
290                 if (fFramePosition == null) {
291                         leave(UNINSTALL | COMMIT | UPDATE_CARET);
292                 } else {
293                         selectRegion();
294                         redrawRegion();
295                 }
296         }
297         
298         private void previous() {
299                 redrawRegion();
300                 
301                 Position position= fManager.getPreviousPosition(fFramePosition.getOffset());
302                 if (position == null) {
303                         fViewer.getTextWidget().getDisplay().beep();
304                 } else {
305                         fFramePosition= position;
306                         selectRegion();
307                         redrawRegion();
308                 }                               
309         }
310
311         /*
312          * @see VerifyKeyListener#verifyKey(VerifyEvent)
313          */
314         public void verifyKey(VerifyEvent event) {
315                 switch (event.character) {
316                 // [SHIFT-]TAB = hop between edit boxes
317                 case 0x09:
318                         {
319                                 Point selection= fViewer.getTextWidget().getSelection();
320                                 IRegion region= fViewer.getVisibleRegion();
321                                 int offset= selection.x + region.getOffset();
322                                 int length= selection.y - selection.x;
323                                 
324                                 // if tab was treated as a document change, would it exceed variable range?
325                                 if (!LinkedPositionManager.includes(fFramePosition, offset, length)) {
326                                         leave(UNINSTALL | COMMIT | UPDATE_CARET);
327                                         return;
328                                 }
329                         }
330                 
331                         if (event.stateMask == SWT.SHIFT)
332                                 previous();
333                         else 
334                                 next();                 
335                         
336                         event.doit= false;
337                         break;
338
339                 // ENTER
340                 case 0x0D:
341                         leave(UNINSTALL | COMMIT | UPDATE_CARET);
342                         event.doit= false;
343                         break;
344
345                 // ESC
346                 case 0x1B:
347                         leave(UNINSTALL | COMMIT);
348                         event.doit= false;
349                         break;
350                 }
351         }
352
353         /*
354          * @see VerifyListener#verifyText(VerifyEvent)
355          */
356         public void verifyText(VerifyEvent event) {
357                 if (!event.doit)
358                         return;
359
360                 IRegion region= fViewer.getVisibleRegion();
361
362                 int offset= event.start + region.getOffset();
363                 int length= event.end - event.start;
364
365                 // allow changes only within linked positions when coming through UI
366                 if (!fManager.anyPositionIncludes(offset, length))
367                         leave(UNINSTALL | COMMIT);
368         }
369
370         /*
371          * @see PaintListener#paintControl(PaintEvent)
372          */
373         public void paintControl(PaintEvent event) {    
374                 if (fFramePosition == null)
375                         return;
376
377                 IRegion region= fViewer.getVisibleRegion();
378                 
379                 // #6824
380                 if (!includes(region, fFramePosition)) {
381                         leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
382                         return;             
383                 }
384                 
385                 int offset= fFramePosition.getOffset() -  region.getOffset();
386                 int length= fFramePosition.getLength();
387                         
388                 StyledText text= fViewer.getTextWidget();
389                 
390                 // support for bidi
391                 Point minLocation= getMinimumLocation(text, offset, length);
392                 Point maxLocation= getMaximumLocation(text, offset, length);
393
394                 int x1= minLocation.x;
395                 int x2= minLocation.x + maxLocation.x - minLocation.x - 1;
396                 int y= minLocation.y + text.getLineHeight() - 1;
397                 
398                 GC gc= event.gc;
399                 gc.setForeground(fFrameColor);
400                 gc.drawLine(x1, y, x2, y);
401         }
402
403         private static Point getMinimumLocation(StyledText text, int offset, int length) {
404                 Point minLocation= new Point(Integer.MAX_VALUE, Integer.MAX_VALUE);
405
406                 for (int i= 0; i <= length; i++) {
407                         Point location= text.getLocationAtOffset(offset + i);
408                         
409                         if (location.x < minLocation.x)
410                                 minLocation.x= location.x;                      
411                         if (location.y < minLocation.y)
412                                 minLocation.y= location.y;                      
413                 }       
414                 
415                 return minLocation;
416         }
417
418         private static Point getMaximumLocation(StyledText text, int offset, int length) {
419                 Point maxLocation= new Point(Integer.MIN_VALUE, Integer.MIN_VALUE);
420
421                 for (int i= 0; i <= length; i++) {
422                         Point location= text.getLocationAtOffset(offset + i);
423                         
424                         if (location.x > maxLocation.x)
425                                 maxLocation.x= location.x;                      
426                         if (location.y > maxLocation.y)
427                                 maxLocation.y= location.y;                      
428                 }       
429                 
430                 return maxLocation;
431         }
432
433         private void redrawRegion() {
434                 IRegion region= fViewer.getVisibleRegion();
435                 
436                 if (!includes(region, fFramePosition)) {
437                         leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
438                         return;             
439                 }
440
441                 int offset= fFramePosition.getOffset() -  region.getOffset();
442                 int length= fFramePosition.getLength();
443
444                 StyledText text= fViewer.getTextWidget();
445                 if (text != null && !text.isDisposed())
446                         text.redrawRange(offset, length, true);
447         }
448
449         private void selectRegion() {
450                 IRegion region= fViewer.getVisibleRegion();
451
452                 if (!includes(region, fFramePosition)) {
453                         leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
454                         return;   
455                 }
456
457                 int start= fFramePosition.getOffset() - region.getOffset();
458                 int end= fFramePosition.getLength() + start;    
459
460                 StyledText text= fViewer.getTextWidget();
461                 if (text != null && !text.isDisposed())
462                         text.setSelection(start, end);
463         }
464
465         private void updateCaret() {
466                 IRegion region= fViewer.getVisibleRegion();             
467
468                 if (!includes(region, fFramePosition)) {
469                         leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
470                         return;   
471                 }
472
473                 int offset= fFramePosition.getOffset() + fCaretOffset - region.getOffset();
474                 
475                 if ((offset >= 0) && (offset <= region.getLength())) {
476                         StyledText text= fViewer.getTextWidget();
477                         if (text != null && !text.isDisposed())
478                                 text.setCaretOffset(offset);
479                 }
480         }
481
482         /*
483          * @see ModifyListener#modifyText(ModifyEvent)
484          */      
485         public void modifyText(ModifyEvent e) {
486                 // reposition caret after StyledText
487                 redrawRegion();
488                 updateCaret();
489         }
490
491         private static void handleException(Shell shell, Exception e) {
492                 String title= LinkedPositionMessages.getString("LinkedPositionUI.error.title"); //$NON-NLS-1$
493                 if (e instanceof CoreException)
494                         PHPeclipsePlugin.log(e);
495    //   ExceptionHandler.handle((CoreException)e, shell, title, null);
496                 else if (e instanceof InvocationTargetException)
497                         PHPeclipsePlugin.log(e);
498     //  ExceptionHandler.handle((InvocationTargetException)e, shell, title, null);
499                 else {
500                         MessageDialog.openError(shell, title, e.getMessage());
501                         PHPeclipsePlugin.log(e);
502                 }
503         }
504
505         /*
506          * @see ITextInputListener#inputDocumentAboutToBeChanged(IDocument, IDocument)
507          */
508         public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) {
509                 // 5326: leave linked mode on document change
510                 int flags= UNINSTALL | COMMIT | (oldInput.equals(newInput) ? 0 : DOCUMENT_CHANGED);
511                 leave(flags);
512         }
513
514         /*
515          * @see ITextInputListener#inputDocumentChanged(IDocument, IDocument)
516          */
517         public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
518         }
519
520         private static boolean includes(IRegion region, Position position) {
521                 return
522                         position.getOffset() >= region.getOffset() &&
523                         position.getOffset() + position.getLength() <= region.getOffset() + region.getLength();
524         }
525
526 }