Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html /
  
  package org.eclipse.xtext.ui.refactoring.ui;
  
 import  org.eclipse.core.runtime.IProgressMonitor;
 import  org.eclipse.core.runtime.IStatus;
 import  org.eclipse.core.runtime.Status;
 import  org.eclipse.core.runtime.jobs.Job;
 import  org.eclipse.jface.action.Action;
 import  org.eclipse.jface.action.IAction;
 import  org.eclipse.jface.action.IMenuListener2;
 import  org.eclipse.jface.action.IMenuManager;
 import  org.eclipse.jface.action.MenuManager;
 import  org.eclipse.jface.bindings.keys.IKeyLookup;
 import  org.eclipse.jface.bindings.keys.KeyLookupFactory;
 import  org.eclipse.jface.bindings.keys.KeyStroke;
 import  org.eclipse.jface.text.ITextViewerExtension5;
 import  org.eclipse.jface.text.IViewportListener;
 import  org.eclipse.jface.text.IWidgetTokenKeeper;
 import  org.eclipse.jface.text.IWidgetTokenKeeperExtension;
 import  org.eclipse.jface.text.IWidgetTokenOwner;
 import  org.eclipse.jface.text.IWidgetTokenOwnerExtension;
 import  org.eclipse.jface.text.link.LinkedPosition;
 import  org.eclipse.jface.text.source.ISourceViewer;
 import  org.eclipse.jface.util.Geometry;
 import  org.eclipse.jface.util.Util;
 import  org.eclipse.swt.SWT;
 import  org.eclipse.swt.custom.StyleRange;
 import  org.eclipse.swt.custom.StyledText;
 import  org.eclipse.swt.events.ControlEvent;
 import  org.eclipse.swt.events.ControlListener;
 import  org.eclipse.swt.events.DisposeEvent;
 import  org.eclipse.swt.events.DisposeListener;
 import  org.eclipse.swt.events.KeyEvent;
 import  org.eclipse.swt.events.KeyListener;
 import  org.eclipse.swt.events.MouseAdapter;
 import  org.eclipse.swt.events.MouseEvent;
 import  org.eclipse.swt.events.MouseListener;
 import  org.eclipse.swt.events.PaintEvent;
 import  org.eclipse.swt.events.PaintListener;
 import  org.eclipse.swt.events.SelectionAdapter;
 import  org.eclipse.swt.events.SelectionEvent;
 import  org.eclipse.swt.events.ShellAdapter;
 import  org.eclipse.swt.events.ShellEvent;
 import  org.eclipse.swt.graphics.Color;
 import  org.eclipse.swt.graphics.Image;
 import  org.eclipse.swt.graphics.Point;
 import  org.eclipse.swt.graphics.Rectangle;
 import  org.eclipse.swt.graphics.Region;
 import  org.eclipse.swt.layout.GridLayout;
 import  org.eclipse.swt.widgets.Composite;
 import  org.eclipse.swt.widgets.Control;
 import  org.eclipse.swt.widgets.Display;
 import  org.eclipse.swt.widgets.Menu;
 import  org.eclipse.swt.widgets.Shell;
 import  org.eclipse.swt.widgets.ToolBar;
 import  org.eclipse.swt.widgets.ToolItem;
 import  org.eclipse.ui.IPartListener2;
 import  org.eclipse.ui.IWorkbenchPart;
 import  org.eclipse.ui.IWorkbenchPartReference;
 import  org.eclipse.ui.PlatformUI;
 import  org.eclipse.ui.keys.IBindingService;
 import  org.eclipse.ui.progress.UIJob;

Author(s):
Holger Schill - Initial contribution and API
 
 public class RenameRefactoringPopup implements IWidgetTokenKeeper, IWidgetTokenKeeperExtension {

Offset of info hover arrow from the left or right side.
 
 	private static final int HAO = 10;

Width of info hover arrow.
 
 	private static final int HAW = 8;

Height of info hover arrow.
 
 	private static final int HAH = 10;

Gap between linked position and popup.
 
 	private static final int GAP = 2;
 
 	private XtextEditor editor;
	private Region region;
	private static final int WIDGET_PRIORITY = 1000;
	private static boolean MAC = Util.isMac();
	private boolean delayJobFinished = false;
	private static final int POPUP_VISIBILITY_DELAY = 300;
	private Shell popup;
	private GridLayout popupLayout;
	private ToolBar toolBar;
	private Image menuImage;
	private MenuManager menuManager;
	private boolean iSMenuUp = false;
	public RenameRefactoringPopup(XtextEditor editorRenameRefactoringController controllerRenameLinkedMode renameLinkedMode) {
		this. = editor;
		this. = controller;
		this. = renameLinkedMode;
	}
	protected void updateVisibility() {
		if ( != null && !.isDisposed() && ) {
			boolean visible = false;
				StyledText textWidget = .getInternalSourceViewer().getTextWidget();
				Rectangle eArea = Geometry.toDisplay(textWidgettextWidget.getClientArea());
				Rectangle pBounds = .getBounds();
				pBounds.x -= ;
				pBounds.y -= ;
				pBounds.width += 2 * ;
				pBounds.height += 2 * ;
				if (eArea.intersects(pBounds)) {
					visible = true;
				}
			}
			if (visible && !.isVisible()) {
				ISourceViewer viewer = .getInternalSourceViewer();
				if (viewer instanceof IWidgetTokenOwnerExtension) {
					IWidgetTokenOwnerExtension widgetTokenOwnerExtension = (IWidgetTokenOwnerExtension) viewer;
					widgetTokenOwnerExtension.requestWidgetToken(this);
				}
else if (!visible && .isVisible()) {
			}
			.setVisible(visible);
		}
	}
	protected void releaseWidgetToken() {
		ISourceViewer viewer = .getInternalSourceViewer();
		if (viewer instanceof IWidgetTokenOwner) {
			IWidgetTokenOwner widgetTokenOwner = (IWidgetTokenOwner) viewer;
			widgetTokenOwner.releaseWidgetToken(this);
		}
	}
	public void open() {
		// Must cache here, since editor context is not available in menu from popup shell:
		Shell workbenchShell = .getSite().getShell();
		final Display display = workbenchShell.getDisplay();
		 = new Shell(workbenchShell, SWT.ON_TOP | SWT.NO_TRIM | SWT.TOOL);
		 = new GridLayout(2, false);
		.setLayout();
		// Leave linked mode when popup loses focus
		// (except when focus goes back to workbench window or menu is open):
		.addShellListener(new ShellAdapter() {
			public void shellDeactivated(ShellEvent e) {
					return;
				final Shell editorShell = .getSite().getShell();
				display.asyncExec(new Runnable() {
					// post to UI thread since editor shell only gets activated after popup has lost focus
					public void run() {
						Shell activeShell = display.getActiveShell();
						if (activeShell != editorShell) {
						}
					}
				});
			}
		});
		if (!) { // carbon and cocoa draw their own border...
			.addPaintListener(new PaintListener() {
				public void paintControl(PaintEvent pe) {
					pe.gc.drawPolygon(getPolygon(true));
				}
			});
		}
		UIJob delayJob = new UIJob(display"Delayed RenameInformationPopup") {
			public IStatus runInUIThread(IProgressMonitor monitor) {
				if ( != null && !.isDisposed()) {
				}
				return Status.OK_STATUS;
			}
		};
		delayJob.setSystem(true);
		delayJob.setPriority(Job.INTERACTIVE);
		delayJob.schedule();
	}
	protected void createContent(Composite parent) {
		Display display = parent.getDisplay();
		Color foreground = display.getSystemColor(SWT.COLOR_INFO_FOREGROUND);
		Color background = display.getSystemColor(SWT.COLOR_INFO_BACKGROUND);
		StyledText hint = new StyledText(, SWT.READ_ONLY | SWT.SINGLE);
		String enterKeyName = getEnterBinding();
		String hintTemplate = "Enter new name, press {0} to refactor";
		hint.setText(Messages.format(hintTemplateenterKeyName));
		hint.setForeground(foreground);
		hint.setStyleRange(new StyleRange(hintTemplate.indexOf("{0}"), enterKeyName.length(), nullnull, SWT.BOLD)); //$NON-NLS-1$
		hint.setEnabled(false); // text must not be selectable
		addViewMenu(parent);
		recursiveSetBackgroundColor(parentbackground);
	}
	protected static void recursiveSetBackgroundColor(Control control, Color color) {
		control.setBackground(color);
		if (control instanceof Composite) {
			Control[] children = ((Composite) control).getChildren();
			for (int i = 0; i < children.length; i++) {
				recursiveSetBackgroundColor(children[i], color);
			}
		}
	}
	protected ToolBar addViewMenu(final Composite parent) {
		 = new ToolBar(parent, SWT.FLAT);
		final ToolItem menuButton = new ToolItem(, SWT.PUSH, 0);
		 = Activator.getImageDescriptor("icons/elcl16/view_menu.gif").createImage();
		menuButton.setImage();
		menuButton.setToolTipText("Menu");
		.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
			}
		});
		menuButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
			}
		});
		.pack();
		return ;
	}
	protected void showMenu(ToolBar toolBar) {
		Menu menu = getMenuManager().createContextMenu(toolBar);
		menu.setLocation(toolBar.toDisplay(0, toolBar.getSize().y));
		 = true;
		menu.setVisible(true);
	}
	protected MenuManager getMenuManager() {
		if ( != null) {
			return ;
		}
		 = new MenuManager();
		.setRemoveAllWhenShown(true);
		.addMenuListener(new IMenuListener2() {
			public void menuAboutToHide(IMenuManager manager) {
				 = false;
			}
			public void menuAboutToShow(IMenuManager manager) {
				boolean canRefactor = .isCurrentNameValid();
				IAction refactorAction = new Action("Rename...") {
					public void run() {
					}
				};
				refactorAction.setAccelerator(SWT.CR);
				refactorAction.setEnabled(canRefactor);
				manager.add(refactorAction);
				IAction previewAction = new Action("Preview...") {
					public void run() {
					}
				};
				previewAction.setAccelerator(SWT.CTRL | SWT.CR);
				previewAction.setEnabled(canRefactor);
				manager.add(previewAction);
				IAction openDialogAction = new Action("Open Rename Dialog..." + '\t' + ) {
					public void run() {
					}
				};
				manager.add(openDialogAction);
			}
		});
		return ;
	}
	protected static String getEnterBinding() {
		return KeyStroke.getInstance(KeyLookupFactory.getDefault().formalKeyLookup(IKeyLookup.CR_NAME)).format();
	}
	protected Point computePopupLocation() {
		if ( == null || .isDisposed())
			return null;
		LinkedPosition position = .getCurrentLinkedPosition();
		if (position == null)
			return null;
		ISourceViewer viewer = .getInternalSourceViewer();
		ITextViewerExtension5 viewer5 = (ITextViewerExtension5) viewer;
		int widgetOffset = viewer5.modelOffset2WidgetOffset(position.offset);
		StyledText textWidget = viewer.getTextWidget();
		Point pos = textWidget.getLocationAtOffset(widgetOffset);
		Point pSize = getExtent();
		pSize.y +=  + 1;
		pos.x -= ;
		pos.y += textWidget.getLineHeight(widgetOffset);
		Point dPos = textWidget.toDisplay(pos);
		Rectangle displayBounds = textWidget.getDisplay().getClientArea();
		Rectangle dPopupRect = Geometry.createRectangle(dPospSize);
		Geometry.moveInside(dPopupRectdisplayBounds);
		return new Point(dPopupRect.x, dPopupRect.y);
	}
	protected Point getExtent() {
		Point e = .getSize();
		e.y -= ;
		return e;
	}
	protected void updatePopupLocation() {
		Point loc = computePopupLocation();
		if (loc != null && !loc.equals(.getLocation())) {
			.setLocation(loc);
		}
	}
	protected void packPopup() {
		.pack();
		Region oldRegion = ;
		 = new Region();
		.add(getPolygon(false));
		.setRegion();
		Rectangle bounds = .getBounds();
		.setSize(bounds.width, bounds.height + 2);
		if (oldRegion != null) {
			oldRegion.dispose();
		}
	}
	protected int[] getPolygon(boolean border) {
		Point e = getExtent();
		int b = border ? 1 : 0;
		boolean isRTL = (.getStyle() & SWT.RIGHT_TO_LEFT) != 0;
		int ha1 = isRTL ? e.x -  :  + ;
		int ha2 = isRTL ? e.x -  -  / 2 :  +  / 2;
		int ha3 = isRTL ? e.x -  -  : ;
		int[] poly;
		poly = new int[] { 0, ha3 + bha2bha1 - be.x - be.x - be.y +  - b, 0,
				e.y +  - b, 0,  };
		return poly;
	}

WARNING: only works in workbench window context!

Returns:
the keybinding for Refactor > Rename
	protected static String getOpenDialogBinding() {
		IBindingService bindingService = (IBindingService) PlatformUI.getWorkbench().getAdapter(IBindingService.class);
		if (bindingService == null)
			return ""//$NON-NLS-1$
		String binding = bindingService
				.getBestActiveBindingFormattedFor("org.eclipse.xtext.ui.refactoring.RenameElement");
		return binding == null ? "" : binding//$NON-NLS-1$
	}
	public boolean requestWidgetToken(IWidgetTokenOwner ownerint priority) {
		return false;
	}
	public boolean setFocus(IWidgetTokenOwner owner) {
		if ( != null && !.isDisposed())
		return true;
	}
	public boolean requestWidgetToken(IWidgetTokenOwner owner) {
		return false;
	}
	public boolean ownsFocusShell() {
			return true;
		if ( == null || .isDisposed())
			return false;
		Shell activeShell = .getDisplay().getActiveShell();
		if ( == activeShell)
			return true;
		return false;
	}
	protected void activateEditor() {
		.getSite().getShell().setActive();
	}
	public void close() {
		if ( != null) {
			if (!.isDisposed()) {
				.close();
			}
			 = null;
		}
		if ( != null) {
			if (!.isDisposed()) {
				.dispose();
			}
		}
	}
	protected class PopupVisibilityManager implements IPartListener2, ControlListener, MouseListener, KeyListener, IViewportListener {
		protected void start() {
			.getSite().getWorkbenchWindow().getPartService().addPartListener(this);
			final ISourceViewer viewer = .getInternalSourceViewer();
			final StyledText textWidget = viewer.getTextWidget();
			textWidget.addControlListener(this);
			textWidget.addMouseListener(this);
			textWidget.addKeyListener(this);
			.getSite().getShell().addControlListener(this);
			viewer.addViewportListener(this);
			.addDisposeListener(new DisposeListener() {
				public void widgetDisposed(DisposeEvent e) {
					.getSite().getWorkbenchWindow().getPartService()
							.removePartListener(PopupVisibilityManager.this);
					if (!textWidget.isDisposed()) {
						textWidget.removeControlListener(PopupVisibilityManager.this);
						textWidget.removeMouseListener(PopupVisibilityManager.this);
						textWidget.removeKeyListener(PopupVisibilityManager.this);
					}
					.getSite().getShell().removeControlListener(PopupVisibilityManager.this);
					viewer.removeViewportListener(PopupVisibilityManager.this);
					if ( != null) {
						.dispose();
						 = null;
					}
					if ( != null) {
						.dispose();
						 = null;
					}
				}
			});
		}
		public void partActivated(IWorkbenchPartReference partRef) {
			IWorkbenchPart fPart = .getEditorSite().getPart();
			if (partRef.getPart(false) == fPart) {
			}
		}
		public void partDeactivated(IWorkbenchPartReference partRef) {
			IWorkbenchPart fPart = .getEditorSite().getPart();
			if ( != null && !.isDisposed() && partRef.getPart(false) == fPart) {
				.setVisible(false);
			}
		}
		public void viewportChanged(int verticalOffset) {
		}
		public void mouseUp(MouseEvent e) {
		}
		public void keyPressed(KeyEvent e) {
		}
		public void controlMoved(ControlEvent e) {
		}
		public void controlResized(ControlEvent e) {
		}
		public void partBroughtToTop(IWorkbenchPartReference partRef) {
		}
		public void partClosed(IWorkbenchPartReference partRef) {
		}
		public void partHidden(IWorkbenchPartReference partRef) {
		}
		public void partInputChanged(IWorkbenchPartReference partRef) {
		}
		public void partOpened(IWorkbenchPartReference partRef) {
		}
		public void partVisible(IWorkbenchPartReference partRef) {
		}
		public void mouseDoubleClick(MouseEvent e) {
		}
		public void mouseDown(MouseEvent e) {
		}
		public void keyReleased(KeyEvent e) {
		}
	}
New to GrepCode? Check out our FAQ X