2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.text.link;
7 import java.lang.reflect.InvocationTargetException;
9 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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;
46 * A user interface for <code>LinkedPositionManager</code>, using <code>ITextViewer</code>.
48 public class LinkedPositionUI implements LinkedPositionListener,
49 ITextInputListener, ModifyListener, VerifyListener, VerifyKeyListener, PaintListener, IPropertyChangeListener {
52 * A listener for notification when the user cancelled the edit operation.
54 public interface ExitListener {
55 void exit(boolean accept);
58 /** Preference key for linked position color */
59 // public final static String LINKED_POSITION_COLOR= "_linkedPositionColor"; //$NON-NLS-1$
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
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();
70 private final ITextViewer fViewer;
71 private final LinkedPositionManager fManager;
72 private Color fFrameColor;
74 private int fFinalCaretOffset= -1; // no final caret offset
76 private Position fFramePosition;
77 private int fCaretOffset;
79 private ExitListener fExitListener;
82 * Creates a user interface for <code>LinkedPositionManager</code>.
84 * @param viewer the text viewer.
85 * @param manager the <code>LinkedPositionManager</code> managing a <code>IDocument</code> of the <code>ITextViewer</code>.
87 public LinkedPositionUI(ITextViewer viewer, LinkedPositionManager manager) {
88 Assert.isNotNull(viewer);
89 Assert.isNotNull(manager);
94 fManager.setLinkedPositionListener(this);
96 initializeHighlightColor(viewer);
100 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
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);
110 private void initializeHighlightColor(ITextViewer viewer) {
112 if (fFrameColor != null)
113 fFrameColor.dispose();
115 StyledText text= viewer.getTextWidget();
117 Display display= text.getDisplay();
118 // fFrameColor= createColor(fgStore, CompilationUnitEditor.LINKED_POSITION_COLOR, display);
119 fFrameColor= createColor(fgStore, PHPeclipsePlugin.LINKED_POSITION_COLOR, display);
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.
127 private Color createColor(IPreferenceStore store, String key, Display display) {
131 if (store.contains(key)) {
133 if (store.isDefault(key))
134 rgb= PreferenceConverter.getDefaultColor(store, key);
136 rgb= PreferenceConverter.getColor(store, key);
139 return new Color(display, rgb);
146 * Sets the final position of the caret when the linked mode is exited
147 * successfully by leaving the last linked position using TAB.
149 public void setFinalCaretOffset(int offset) {
150 fFinalCaretOffset= offset;
154 * Sets a <code>CancelListener</code> which is notified if the linked mode
155 * is exited unsuccessfully by hitting ESC.
157 public void setCancelListener(ExitListener listener) {
158 fExitListener= listener;
162 * @see LinkedPositionManager.LinkedPositionListener#setCurrentPositions(Position, int)
164 public void setCurrentPosition(Position position, int caretOffset) {
165 if (!fFramePosition.equals(position)) {
167 fFramePosition= position;
170 fCaretOffset= caretOffset;
174 * Enters the linked mode. The linked mode can be left by calling
177 * @see #exit(boolean)
179 public void enter() {
181 IDocument document= fViewer.getDocument();
182 document.addPositionCategory(CARET_POSITION);
183 document.addPositionUpdater(fgUpdater);
185 if (fFinalCaretOffset != -1)
186 document.addPosition(CARET_POSITION, new Position(fFinalCaretOffset));
187 } catch (BadLocationException e) {
188 handleException(fViewer.getTextWidget().getShell(), e);
190 } catch (BadPositionCategoryException e) {
191 PHPeclipsePlugin.log(e);
192 Assert.isTrue(false);
195 fViewer.addTextInputListener(this);
197 ITextViewerExtension extension= (ITextViewerExtension) fViewer;
198 extension.prependVerifyKeyListener(this);
200 StyledText text= fViewer.getTextWidget();
201 text.addVerifyListener(this);
202 text.addModifyListener(this);
203 text.addPaintListener(this);
204 text.showSelection();
206 fFramePosition= fManager.getFirstPosition();
207 if (fFramePosition == null)
208 leave(UNINSTALL | COMMIT | UPDATE_CARET);
210 fgStore.addPropertyChangeListener(this);
214 * @see LinkedPositionManager.LinkedPositionListener#exit(boolean)
216 public void exit(boolean success) {
217 // no UNINSTALL since manager has already uninstalled itself
218 leave((success ? COMMIT : 0) | UPDATE_CARET);
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.
225 public IRegion getSelectedRegion() {
226 if (fFramePosition == null)
227 return new Region(fFinalCaretOffset, 0);
229 return new Region(fFramePosition.getOffset(), fFramePosition.getLength());
232 private void leave(int flags) {
233 if ((flags & UNINSTALL) != 0)
234 fManager.uninstall((flags & COMMIT) != 0);
236 fgStore.removePropertyChangeListener(this);
238 if (fFrameColor != null) {
239 fFrameColor.dispose();
243 StyledText text= fViewer.getTextWidget();
244 text.removePaintListener(this);
245 text.removeModifyListener(this);
246 text.removeVerifyListener(this);
248 ITextViewerExtension extension= (ITextViewerExtension) fViewer;
249 extension.removeVerifyKeyListener(this);
251 fViewer.removeTextInputListener(this);
254 IRegion region= fViewer.getVisibleRegion();
255 IDocument document= fViewer.getDocument();
257 if (((flags & COMMIT) != 0) &&
258 ((flags & DOCUMENT_CHANGED) == 0) &&
259 ((flags & UPDATE_CARET) != 0))
261 Position[] positions= document.getPositions(CARET_POSITION);
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);
270 document.removePositionUpdater(fgUpdater);
271 document.removePositionCategory(CARET_POSITION);
273 if (fExitListener != null)
275 ((flags & COMMIT) != 0) ||
276 ((flags & DOCUMENT_CHANGED) != 0));
278 } catch (BadPositionCategoryException e) {
279 PHPeclipsePlugin.log(e);
280 Assert.isTrue(false);
283 if ((flags & DOCUMENT_CHANGED) == 0)
287 private void next() {
290 fFramePosition= fManager.getNextPosition(fFramePosition.getOffset());
291 if (fFramePosition == null) {
292 leave(UNINSTALL | COMMIT | UPDATE_CARET);
299 private void previous() {
302 Position position= fManager.getPreviousPosition(fFramePosition.getOffset());
303 if (position == null) {
304 fViewer.getTextWidget().getDisplay().beep();
306 fFramePosition= position;
313 * @see VerifyKeyListener#verifyKey(VerifyEvent)
315 public void verifyKey(VerifyEvent event) {
316 switch (event.character) {
317 // [SHIFT-]TAB = hop between edit boxes
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;
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);
332 if (event.stateMask == SWT.SHIFT)
342 leave(UNINSTALL | COMMIT | UPDATE_CARET);
348 leave(UNINSTALL | COMMIT);
355 * @see VerifyListener#verifyText(VerifyEvent)
357 public void verifyText(VerifyEvent event) {
361 IRegion region= fViewer.getVisibleRegion();
363 int offset= event.start + region.getOffset();
364 int length= event.end - event.start;
366 // allow changes only within linked positions when coming through UI
367 if (!fManager.anyPositionIncludes(offset, length))
368 leave(UNINSTALL | COMMIT);
372 * @see PaintListener#paintControl(PaintEvent)
374 public void paintControl(PaintEvent event) {
375 if (fFramePosition == null)
378 IRegion region= fViewer.getVisibleRegion();
381 if (!includes(region, fFramePosition)) {
382 leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
386 int offset= fFramePosition.getOffset() - region.getOffset();
387 int length= fFramePosition.getLength();
389 StyledText text= fViewer.getTextWidget();
392 Point minLocation= getMinimumLocation(text, offset, length);
393 Point maxLocation= getMaximumLocation(text, offset, length);
395 int x1= minLocation.x;
396 int x2= minLocation.x + maxLocation.x - minLocation.x - 1;
397 int y= minLocation.y + text.getLineHeight() - 1;
400 gc.setForeground(fFrameColor);
401 gc.drawLine(x1, y, x2, y);
404 private static Point getMinimumLocation(StyledText text, int offset, int length) {
405 Point minLocation= new Point(Integer.MAX_VALUE, Integer.MAX_VALUE);
407 for (int i= 0; i <= length; i++) {
408 Point location= text.getLocationAtOffset(offset + i);
410 if (location.x < minLocation.x)
411 minLocation.x= location.x;
412 if (location.y < minLocation.y)
413 minLocation.y= location.y;
419 private static Point getMaximumLocation(StyledText text, int offset, int length) {
420 Point maxLocation= new Point(Integer.MIN_VALUE, Integer.MIN_VALUE);
422 for (int i= 0; i <= length; i++) {
423 Point location= text.getLocationAtOffset(offset + i);
425 if (location.x > maxLocation.x)
426 maxLocation.x= location.x;
427 if (location.y > maxLocation.y)
428 maxLocation.y= location.y;
434 private void redrawRegion() {
435 IRegion region= fViewer.getVisibleRegion();
437 if (!includes(region, fFramePosition)) {
438 leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
442 int offset= fFramePosition.getOffset() - region.getOffset();
443 int length= fFramePosition.getLength();
445 StyledText text= fViewer.getTextWidget();
446 if (text != null && !text.isDisposed())
447 text.redrawRange(offset, length, true);
450 private void selectRegion() {
451 IRegion region= fViewer.getVisibleRegion();
453 if (!includes(region, fFramePosition)) {
454 leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
458 int start= fFramePosition.getOffset() - region.getOffset();
459 int end= fFramePosition.getLength() + start;
461 StyledText text= fViewer.getTextWidget();
462 if (text != null && !text.isDisposed())
463 text.setSelection(start, end);
466 private void updateCaret() {
467 IRegion region= fViewer.getVisibleRegion();
469 if (!includes(region, fFramePosition)) {
470 leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
474 int offset= fFramePosition.getOffset() + fCaretOffset - region.getOffset();
476 if ((offset >= 0) && (offset <= region.getLength())) {
477 StyledText text= fViewer.getTextWidget();
478 if (text != null && !text.isDisposed())
479 text.setCaretOffset(offset);
484 * @see ModifyListener#modifyText(ModifyEvent)
486 public void modifyText(ModifyEvent e) {
487 // reposition caret after StyledText
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);
501 MessageDialog.openError(shell, title, e.getMessage());
502 PHPeclipsePlugin.log(e);
507 * @see ITextInputListener#inputDocumentAboutToBeChanged(IDocument, IDocument)
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);
516 * @see ITextInputListener#inputDocumentChanged(IDocument, IDocument)
518 public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
521 private static boolean includes(IRegion region, Position position) {
523 position.getOffset() >= region.getOffset() &&
524 position.getOffset() + position.getLength() <= region.getOffset() + region.getLength();