1 11 package org.eclipse.ui.internal.texteditor; 12 13 14 import org.eclipse.swt.SWT; 15 import org.eclipse.swt.custom.StyledText; 16 import org.eclipse.swt.custom.VerifyKeyListener; 17 import org.eclipse.swt.events.FocusEvent; 18 import org.eclipse.swt.events.FocusListener; 19 import org.eclipse.swt.events.MouseEvent; 20 import org.eclipse.swt.events.MouseListener; 21 import org.eclipse.swt.events.VerifyEvent; 22 23 import org.eclipse.core.commands.ExecutionEvent; 24 import org.eclipse.core.commands.ExecutionException; 25 import org.eclipse.core.commands.IExecutionListener; 26 import org.eclipse.core.commands.NotHandledException; 27 28 import org.eclipse.core.runtime.IStatus; 29 import org.eclipse.core.runtime.ListenerList; 30 import org.eclipse.core.runtime.Status; 31 32 import org.eclipse.jface.text.ITextViewer; 33 34 import org.eclipse.ui.PlatformUI; 35 import org.eclipse.ui.commands.ICommandService; 36 37 55 public final class CompoundEditExitStrategy { 56 59 private final class EventListener implements MouseListener, FocusListener, VerifyKeyListener, IExecutionListener { 60 61 64 public void mouseDoubleClick(MouseEvent e) { 65 fireEndCompoundEdit(); 67 } 68 69 72 public void mouseDown(MouseEvent e) { 73 fireEndCompoundEdit(); 75 } 76 77 public void mouseUp(MouseEvent e) {} 78 79 public void focusGained(FocusEvent e) {} 80 81 84 public void focusLost(FocusEvent e) { 85 fireEndCompoundEdit(); 87 } 88 89 public void notHandled(String commandId, NotHandledException exception) {} 90 91 public void postExecuteFailure(String commandId, ExecutionException exception) {} 92 93 public void postExecuteSuccess(String commandId, Object returnValue) {} 94 95 98 public void preExecute(String commandId, ExecutionEvent event) { 99 for (int i= 0; i < fCommandIds.length; i++) { 101 if (commandId.equals(fCommandIds[i])) 102 return; 103 } 104 fireEndCompoundEdit(); 105 } 106 107 110 public void verifyKey(VerifyEvent event) { 111 final int maskWithoutShift= SWT.MODIFIER_MASK & ~SWT.SHIFT; 113 if ((event.keyCode & SWT.MODIFIER_MASK) == 0 && (event.stateMask & maskWithoutShift) == 0) 114 fireEndCompoundEdit(); 115 } 116 117 } 118 119 private final String [] fCommandIds; 120 private final EventListener fEventListener= new EventListener(); 121 private final ListenerList fListenerList= new ListenerList(ListenerList.IDENTITY); 122 123 private ITextViewer fViewer; 124 private StyledText fWidgetEventSource; 125 126 132 public CompoundEditExitStrategy(String commandId) { 133 if (commandId == null) 134 throw new NullPointerException ("commandId"); fCommandIds= new String [] {commandId}; 136 } 137 138 144 public CompoundEditExitStrategy(String [] commandIds) { 145 for (int i= 0; i < commandIds.length; i++) { 146 if (commandIds[i] == null) 147 throw new NullPointerException ("commandIds[" + i + "]"); } 149 fCommandIds= new String [commandIds.length]; 150 System.arraycopy(commandIds, 0, fCommandIds, 0, commandIds.length); 151 } 152 153 159 public void arm(ITextViewer viewer) { 160 disarm(); 161 if (viewer == null) 162 throw new NullPointerException ("editor"); fViewer= viewer; 164 addListeners(fViewer); 165 } 166 167 176 public void disarm() { 177 if (isInstalled()) { 178 removeListeners(fViewer); 179 fViewer= null; 180 } 181 } 182 183 private void addListeners(ITextViewer viewer) { 184 fWidgetEventSource= viewer.getTextWidget(); 185 if (fWidgetEventSource != null) { 186 fWidgetEventSource.addVerifyKeyListener(fEventListener); 187 fWidgetEventSource.addMouseListener(fEventListener); 188 fWidgetEventSource.addFocusListener(fEventListener); 189 } 190 191 ICommandService commandService= (ICommandService)PlatformUI.getWorkbench().getAdapter(ICommandService.class); 192 if (commandService != null) 193 commandService.addExecutionListener(fEventListener); 194 } 195 196 private void removeListeners(ITextViewer editor) { 197 ICommandService commandService= (ICommandService)PlatformUI.getWorkbench().getAdapter(ICommandService.class); 198 if (commandService != null) 199 commandService.removeExecutionListener(fEventListener); 200 201 if (fWidgetEventSource != null) { 202 fWidgetEventSource.removeFocusListener(fEventListener); 203 fWidgetEventSource.removeMouseListener(fEventListener); 204 fWidgetEventSource.removeVerifyKeyListener(fEventListener); 205 fWidgetEventSource= null; 206 } 207 } 208 209 private boolean isInstalled() { 210 return fViewer != null; 211 } 212 213 private void fireEndCompoundEdit() { 214 disarm(); 215 Object [] listeners= fListenerList.getListeners(); 216 for (int i= 0; i < listeners.length; i++) { 217 ICompoundEditListener listener= (ICompoundEditListener) listeners[i]; 218 try { 219 listener.endCompoundEdit(); 220 } catch (Exception e) { 221 IStatus status= new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, IStatus.OK, "listener notification failed", e); TextEditorPlugin.getDefault().getLog().log(status); 223 } 224 } 225 } 226 227 233 public void addCompoundListener(ICompoundEditListener listener) { 234 fListenerList.add(listener); 235 } 236 237 244 public void removeCompoundListener(ICompoundEditListener listener) { 245 fListenerList.remove(listener); 246 } 247 248 } 249 | Popular Tags |