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