]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/javaeditor/CompoundEditExitStrategy.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / javaeditor / CompoundEditExitStrategy.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2005, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.javaeditor;
12
13
14import org.eclipse.swt.SWT;
15import org.eclipse.swt.custom.StyledText;
16import org.eclipse.swt.custom.VerifyKeyListener;
17import org.eclipse.swt.events.FocusEvent;
18import org.eclipse.swt.events.FocusListener;
19import org.eclipse.swt.events.MouseEvent;
20import org.eclipse.swt.events.MouseListener;
21import org.eclipse.swt.events.VerifyEvent;
22
23import org.eclipse.core.commands.ExecutionEvent;
24import org.eclipse.core.commands.ExecutionException;
25import org.eclipse.core.commands.IExecutionListener;
26import org.eclipse.core.commands.NotHandledException;
27
28import org.eclipse.core.runtime.ListenerList;
29
30import org.eclipse.jface.text.ITextViewer;
31
32import org.eclipse.ui.PlatformUI;
33import org.eclipse.ui.commands.ICommandService;
34
35import org.eclipse.jdt.internal.ui.JavaPlugin;
36
37/**
38 * Exit strategy for commands that want to fold repeated execution into one compound edit. See
39 * {@link org.eclipse.jface.text.IRewriteTarget#endCompoundChange() IRewriteTarget.endCompoundChange}.
40 * As long as a strategy is installed on an {@link ITextViewer}, it will detect the end of a
41 * compound operation when any of the following conditions becomes true:
42 * <ul>
43 * <li>the viewer's text widget loses the keyboard focus</li>
44 * <li>the mouse is clicked or double clicked inside the viewer's widget</li>
45 * <li>a command other than the ones specified is executed</li>
46 * <li>the viewer receives any key events that are not modifier combinations</li>
47 * </ul>
48 * <p>
49 * If the end of a compound edit is detected, any registered {@link ICompoundEditListener}s are
50 * notified and the strategy is disarmed (spring-loaded).
51 * </p>
52 *
53 * @since 3.1
54 */
55public final class CompoundEditExitStrategy {
56 /**
57 * Listens for events that may trigger the end of a compound edit.
58 */
59 private final class EventListener implements MouseListener, FocusListener, VerifyKeyListener, IExecutionListener {
60
61 /*
62 * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
63 */
64 public void mouseDoubleClick(MouseEvent e) {
65 // mouse actions end the compound change
66 fireEndCompoundEdit();
67 }
68
69 /*
70 * @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
71 */
72 public void mouseDown(MouseEvent e) {
73 // mouse actions end the compound change
74 fireEndCompoundEdit();
75 }
76
77 public void mouseUp(MouseEvent e) {}
78
79 public void focusGained(FocusEvent e) {}
80
81 /*
82 * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
83 */
84 public void focusLost(FocusEvent e) {
85 // losing focus ends the change
86 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 /*
96 * @see org.eclipse.core.commands.IExecutionListener#preExecute(java.lang.String, org.eclipse.core.commands.ExecutionEvent)
97 */
98 public void preExecute(String commandId, ExecutionEvent event) {
99 // any command other than the known ones end the compound change
100 for (int i= 0; i < fCommandIds.length; i++) {
101 if (commandId.equals(fCommandIds[i]))
102 return;
103 }
104 fireEndCompoundEdit();
105 }
106
107 /*
108 * @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent)
109 */
110 public void verifyKey(VerifyEvent event) {
111 // any key press that is not a modifier combo ends the compound change
112 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 public void generated_4289620665072051869(CompoundEditExitStrategy compoundeditexitstrategy) {
118 if (compoundeditexitstrategy.fWidgetEventSource != null) {
119 compoundeditexitstrategy.fWidgetEventSource.addVerifyKeyListener(this);
120 compoundeditexitstrategy.fWidgetEventSource.addMouseListener(this);
121 compoundeditexitstrategy.fWidgetEventSource.addFocusListener(this);
122 }
123
124 ICommandService commandService= (ICommandService)PlatformUI.getWorkbench().getAdapter(ICommandService.class);
125 if (commandService != null)
126 commandService.addExecutionListener(this);
127 }
128
129 public void generated_1342879145106809647(CompoundEditExitStrategy compoundeditexitstrategy) {
130 ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getAdapter(ICommandService.class);
131 if (commandService != null)
132 commandService.removeExecutionListener(this);
133
134 if (compoundeditexitstrategy.fWidgetEventSource != null) {
135 compoundeditexitstrategy.fWidgetEventSource.removeFocusListener(this);
136 compoundeditexitstrategy.fWidgetEventSource.removeMouseListener(this);
137 compoundeditexitstrategy.fWidgetEventSource.removeVerifyKeyListener(this);
138 compoundeditexitstrategy.fWidgetEventSource= null;
139 }
140 }
141
142 }
143
144 private final String[] fCommandIds;
145 private final EventListener fEventListener= new EventListener();
146 private final ListenerList fListenerList= new ListenerList(ListenerList.IDENTITY);
147
148 private ITextViewer fViewer;
149 private StyledText fWidgetEventSource;
150
151 /**
152 * Creates a new strategy, equivalent to calling
153 * {@linkplain #CompoundEditExitStrategy(String[]) CompoundEditExitStrategy(new String[] &#x7b; commandId &#x7d;)}.
154 *
155 * @param commandId the command id of the repeatable command
156 */
157 public CompoundEditExitStrategy(String commandId) {
158 if (commandId == null)
159 throw new NullPointerException("commandId"); //$NON-NLS-1$
160 fCommandIds= new String[] {commandId};
161 }
162
163 /**
164 * Creates a new strategy, ending upon execution of any command other than the ones
165 * specified.
166 *
167 * @param commandIds the ids of the repeatable commands
168 */
169 public CompoundEditExitStrategy(String[] commandIds) {
170 for (int i= 0; i < commandIds.length; i++) {
171 if (commandIds[i] == null)
172 throw new NullPointerException("commandIds[" + i + "]"); //$NON-NLS-1$ //$NON-NLS-2$
173 }
174 fCommandIds= new String[commandIds.length];
175 System.arraycopy(commandIds, 0, fCommandIds, 0, commandIds.length);
176 }
177
178 /**
179 * Installs the receiver on <code>viewer</code> and arms it. After this call returns, any
180 * registered listeners will be notified if a compound edit ends.
181 *
182 * @param viewer the viewer to install on
183 */
184 public void arm(ITextViewer viewer) {
185 disarm();
186 if (viewer == null)
187 throw new NullPointerException("editor"); //$NON-NLS-1$
188 fViewer= viewer;
189 addListeners();
190 }
191
192 /**
193 * Disarms the receiver. After this call returns, any registered listeners will be not be
194 * notified any more until <code>install</code> is called again. Note that the listeners are
195 * not removed.
196 * <p>
197 * Note that the receiver is automatically disarmed when the end of a compound edit has
198 * been detected and before the listeners are notified.
199 * </p>
200 */
201 public void disarm() {
202 if (isInstalled()) {
203 removeListeners();
204 fViewer= null;
205 }
206 }
207
208 private void addListeners() {
209 fWidgetEventSource= fViewer.getTextWidget();
210 fEventListener.generated_4289620665072051869(this);
211 }
212
213 private void removeListeners() {
214 fEventListener.generated_1342879145106809647(this);
215 }
216
217 private boolean isInstalled() {
218 return fViewer != null;
219 }
220
221 private void fireEndCompoundEdit() {
222 disarm();
223 Object[] listeners= fListenerList.getListeners();
224 for (int i= 0; i < listeners.length; i++) {
225 ICompoundEditListener listener= (ICompoundEditListener) listeners[i];
226 try {
227 listener.endCompoundEdit();
228 } catch (Exception e) {
229 JavaPlugin.log(e);
230 }
231 }
232 }
233
234 /**
235 * Adds a compound edit listener. Multiple registration is possible. Note that the receiver is
236 * automatically disarmed before the listeners are notified.
237 *
238 * @param listener the new listener
239 */
240 public void addCompoundListener(ICompoundEditListener listener) {
241 fListenerList.add(listener);
242 }
243
244 /**
245 * Removes a compound edit listener. If <code>listener</code> is registered multiple times, an
246 * arbitrary instance is removed. If <code>listener</code> is not currently registered,
247 * nothing happens.
248 *
249 * @param listener the listener to be removed.
250 */
251 public void removeCompoundListener(ICompoundEditListener listener) {
252 fListenerList.remove(listener);
253 }
254
255}