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;
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;
45 * A user interface for <code>LinkedPositionManager</code>, using <code>ITextViewer</code>.
47 public class LinkedPositionUI implements LinkedPositionListener,
48 ITextInputListener, ModifyListener, VerifyListener, VerifyKeyListener, PaintListener, IPropertyChangeListener {
51 * A listener for notification when the user cancelled the edit operation.
53 public interface ExitListener {
54 void exit(boolean accept);
57 /** Preference key for linked position color */
58 // public final static String LINKED_POSITION_COLOR= "_linkedPositionColor"; //$NON-NLS-1$
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
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();
69 private final ITextViewer fViewer;
70 private final LinkedPositionManager fManager;
71 private Color fFrameColor;
73 private int fFinalCaretOffset= -1; // no final caret offset
75 private Position fFramePosition;
76 private int fCaretOffset;
78 private ExitListener fExitListener;
81 * Creates a user interface for <code>LinkedPositionManager</code>.
83 * @param viewer the text viewer.
84 * @param manager the <code>LinkedPositionManager</code> managing a <code>IDocument</code> of the <code>ITextViewer</code>.
86 public LinkedPositionUI(ITextViewer viewer, LinkedPositionManager manager) {
87 Assert.isNotNull(viewer);
88 Assert.isNotNull(manager);
93 fManager.setLinkedPositionListener(this);
95 initializeHighlightColor(viewer);
99 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
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);
109 private void initializeHighlightColor(ITextViewer viewer) {
111 if (fFrameColor != null)
112 fFrameColor.dispose();
114 StyledText text= viewer.getTextWidget();
116 Display display= text.getDisplay();
117 // fFrameColor= createColor(fgStore, CompilationUnitEditor.LINKED_POSITION_COLOR, display);
118 fFrameColor= createColor(fgStore, PHPeclipsePlugin.LINKED_POSITION_COLOR, display);
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.
126 private Color createColor(IPreferenceStore store, String key, Display display) {
130 if (store.contains(key)) {
132 if (store.isDefault(key))
133 rgb= PreferenceConverter.getDefaultColor(store, key);
135 rgb= PreferenceConverter.getColor(store, key);
138 return new Color(display, rgb);
145 * Sets the final position of the caret when the linked mode is exited
146 * successfully by leaving the last linked position using TAB.
148 public void setFinalCaretOffset(int offset) {
149 fFinalCaretOffset= offset;
153 * Sets a <code>CancelListener</code> which is notified if the linked mode
154 * is exited unsuccessfully by hitting ESC.
156 public void setCancelListener(ExitListener listener) {
157 fExitListener= listener;
161 * @see LinkedPositionManager.LinkedPositionListener#setCurrentPositions(Position, int)
163 public void setCurrentPosition(Position position, int caretOffset) {
164 if (!fFramePosition.equals(position)) {
166 fFramePosition= position;
169 fCaretOffset= caretOffset;
173 * Enters the linked mode. The linked mode can be left by calling
176 * @see #exit(boolean)
178 public void enter() {
180 IDocument document= fViewer.getDocument();
181 document.addPositionCategory(CARET_POSITION);
182 document.addPositionUpdater(fgUpdater);
184 if (fFinalCaretOffset != -1)
185 document.addPosition(CARET_POSITION, new Position(fFinalCaretOffset));
186 } catch (BadLocationException e) {
187 handleException(fViewer.getTextWidget().getShell(), e);
189 } catch (BadPositionCategoryException e) {
190 PHPeclipsePlugin.log(e);
191 Assert.isTrue(false);
194 fViewer.addTextInputListener(this);
196 ITextViewerExtension extension= (ITextViewerExtension) fViewer;
197 extension.prependVerifyKeyListener(this);
199 StyledText text= fViewer.getTextWidget();
200 text.addVerifyListener(this);
201 text.addModifyListener(this);
202 text.addPaintListener(this);
203 text.showSelection();
205 fFramePosition= fManager.getFirstPosition();
206 if (fFramePosition == null)
207 leave(UNINSTALL | COMMIT | UPDATE_CARET);
209 fgStore.addPropertyChangeListener(this);
213 * @see LinkedPositionManager.LinkedPositionListener#exit(boolean)
215 public void exit(boolean success) {
216 // no UNINSTALL since manager has already uninstalled itself
217 leave((success ? COMMIT : 0) | UPDATE_CARET);
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.
224 public IRegion getSelectedRegion() {
225 if (fFramePosition == null)
226 return new Region(fFinalCaretOffset, 0);
228 return new Region(fFramePosition.getOffset(), fFramePosition.getLength());
231 private void leave(int flags) {
232 if ((flags & UNINSTALL) != 0)
233 fManager.uninstall((flags & COMMIT) != 0);
235 fgStore.removePropertyChangeListener(this);
237 if (fFrameColor != null) {
238 fFrameColor.dispose();
242 StyledText text= fViewer.getTextWidget();
243 text.removePaintListener(this);
244 text.removeModifyListener(this);
245 text.removeVerifyListener(this);
247 ITextViewerExtension extension= (ITextViewerExtension) fViewer;
248 extension.removeVerifyKeyListener(this);
250 fViewer.removeTextInputListener(this);
253 IRegion region= fViewer.getVisibleRegion();
254 IDocument document= fViewer.getDocument();
256 if (((flags & COMMIT) != 0) &&
257 ((flags & DOCUMENT_CHANGED) == 0) &&
258 ((flags & UPDATE_CARET) != 0))
260 Position[] positions= document.getPositions(CARET_POSITION);
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);
269 document.removePositionUpdater(fgUpdater);
270 document.removePositionCategory(CARET_POSITION);
272 if (fExitListener != null)
274 ((flags & COMMIT) != 0) ||
275 ((flags & DOCUMENT_CHANGED) != 0));
277 } catch (BadPositionCategoryException e) {
278 PHPeclipsePlugin.log(e);
279 Assert.isTrue(false);
282 if ((flags & DOCUMENT_CHANGED) == 0)
286 private void next() {
289 fFramePosition= fManager.getNextPosition(fFramePosition.getOffset());
290 if (fFramePosition == null) {
291 leave(UNINSTALL | COMMIT | UPDATE_CARET);
298 private void previous() {
301 Position position= fManager.getPreviousPosition(fFramePosition.getOffset());
302 if (position == null) {
303 fViewer.getTextWidget().getDisplay().beep();
305 fFramePosition= position;
312 * @see VerifyKeyListener#verifyKey(VerifyEvent)
314 public void verifyKey(VerifyEvent event) {
315 switch (event.character) {
316 // [SHIFT-]TAB = hop between edit boxes
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;
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);
331 if (event.stateMask == SWT.SHIFT)
341 leave(UNINSTALL | COMMIT | UPDATE_CARET);
347 leave(UNINSTALL | COMMIT);
354 * @see VerifyListener#verifyText(VerifyEvent)
356 public void verifyText(VerifyEvent event) {
360 IRegion region= fViewer.getVisibleRegion();
362 int offset= event.start + region.getOffset();
363 int length= event.end - event.start;
365 // allow changes only within linked positions when coming through UI
366 if (!fManager.anyPositionIncludes(offset, length))
367 leave(UNINSTALL | COMMIT);
371 * @see PaintListener#paintControl(PaintEvent)
373 public void paintControl(PaintEvent event) {
374 if (fFramePosition == null)
377 IRegion region= fViewer.getVisibleRegion();
380 if (!includes(region, fFramePosition)) {
381 leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
385 int offset= fFramePosition.getOffset() - region.getOffset();
386 int length= fFramePosition.getLength();
388 StyledText text= fViewer.getTextWidget();
391 Point minLocation= getMinimumLocation(text, offset, length);
392 Point maxLocation= getMaximumLocation(text, offset, length);
394 int x1= minLocation.x;
395 int x2= minLocation.x + maxLocation.x - minLocation.x - 1;
396 int y= minLocation.y + text.getLineHeight() - 1;
399 gc.setForeground(fFrameColor);
400 gc.drawLine(x1, y, x2, y);
403 private static Point getMinimumLocation(StyledText text, int offset, int length) {
404 Point minLocation= new Point(Integer.MAX_VALUE, Integer.MAX_VALUE);
406 for (int i= 0; i <= length; i++) {
407 Point location= text.getLocationAtOffset(offset + i);
409 if (location.x < minLocation.x)
410 minLocation.x= location.x;
411 if (location.y < minLocation.y)
412 minLocation.y= location.y;
418 private static Point getMaximumLocation(StyledText text, int offset, int length) {
419 Point maxLocation= new Point(Integer.MIN_VALUE, Integer.MIN_VALUE);
421 for (int i= 0; i <= length; i++) {
422 Point location= text.getLocationAtOffset(offset + i);
424 if (location.x > maxLocation.x)
425 maxLocation.x= location.x;
426 if (location.y > maxLocation.y)
427 maxLocation.y= location.y;
433 private void redrawRegion() {
434 IRegion region= fViewer.getVisibleRegion();
436 if (!includes(region, fFramePosition)) {
437 leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
441 int offset= fFramePosition.getOffset() - region.getOffset();
442 int length= fFramePosition.getLength();
444 StyledText text= fViewer.getTextWidget();
445 if (text != null && !text.isDisposed())
446 text.redrawRange(offset, length, true);
449 private void selectRegion() {
450 IRegion region= fViewer.getVisibleRegion();
452 if (!includes(region, fFramePosition)) {
453 leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
457 int start= fFramePosition.getOffset() - region.getOffset();
458 int end= fFramePosition.getLength() + start;
460 StyledText text= fViewer.getTextWidget();
461 if (text != null && !text.isDisposed())
462 text.setSelection(start, end);
465 private void updateCaret() {
466 IRegion region= fViewer.getVisibleRegion();
468 if (!includes(region, fFramePosition)) {
469 leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
473 int offset= fFramePosition.getOffset() + fCaretOffset - region.getOffset();
475 if ((offset >= 0) && (offset <= region.getLength())) {
476 StyledText text= fViewer.getTextWidget();
477 if (text != null && !text.isDisposed())
478 text.setCaretOffset(offset);
483 * @see ModifyListener#modifyText(ModifyEvent)
485 public void modifyText(ModifyEvent e) {
486 // reposition caret after StyledText
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);
500 MessageDialog.openError(shell, title, e.getMessage());
501 PHPeclipsePlugin.log(e);
506 * @see ITextInputListener#inputDocumentAboutToBeChanged(IDocument, IDocument)
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);
515 * @see ITextInputListener#inputDocumentChanged(IDocument, IDocument)
517 public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
520 private static boolean includes(IRegion region, Position position) {
522 position.getOffset() >= region.getOffset() &&
523 position.getOffset() + position.getLength() <= region.getOffset() + region.getLength();