Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 157935 Details for
Bug 301330
OSX Cocoa - Things get slower over time when running long sequence of tests
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
Test class that uses readAndDispatch()
GuiUtils.java (text/plain), 29.35 KB, created by
Francis Upton IV
on 2010-02-02 13:25:21 EST
(
hide
)
Description:
Test class that uses readAndDispatch()
Filename:
MIME Type:
Creator:
Francis Upton IV
Created:
2010-02-02 13:25:21 EST
Size:
29.35 KB
patch
obsolete
>/* > * Copyright 2004 oakland software, incorporated. All rights Reserved. > */ >package com.oaklandsw.gui; > >import java.io.ByteArrayInputStream; >import java.nio.charset.Charset; >import java.util.ArrayList; >import java.util.Iterator; >import java.util.List; >import java.util.SortedMap; >import java.util.regex.Matcher; >import java.util.regex.Pattern; > >import org.eclipse.core.internal.registry.ExtensionRegistry; >import org.eclipse.core.resources.IFile; >import org.eclipse.core.resources.IFolder; >import org.eclipse.core.resources.IProject; >import org.eclipse.core.resources.IResource; >import org.eclipse.core.runtime.ContributorFactoryOSGi; >import org.eclipse.core.runtime.IContributor; >import org.eclipse.core.runtime.IExtensionRegistry; >import org.eclipse.core.runtime.ILog; >import org.eclipse.core.runtime.IPath; >import org.eclipse.core.runtime.IStatus; >import org.eclipse.core.runtime.Platform; >import org.eclipse.core.runtime.Status; >import org.eclipse.jface.action.IContributionItem; >import org.eclipse.jface.viewers.TreePath; >import org.eclipse.jface.viewers.Viewer; >import org.eclipse.jface.viewers.ViewerFilter; >import org.eclipse.osgi.util.NLS; >import org.eclipse.swt.SWT; >import org.eclipse.swt.custom.ScrolledComposite; >import org.eclipse.swt.dnd.DND; >import org.eclipse.swt.events.SelectionAdapter; >import org.eclipse.swt.events.SelectionEvent; >import org.eclipse.swt.events.VerifyEvent; >import org.eclipse.swt.events.VerifyListener; >import org.eclipse.swt.graphics.Font; >import org.eclipse.swt.graphics.FontData; >import org.eclipse.swt.graphics.FontMetrics; >import org.eclipse.swt.graphics.GC; >import org.eclipse.swt.graphics.Point; >import org.eclipse.swt.graphics.Rectangle; >import org.eclipse.swt.layout.FillLayout; >import org.eclipse.swt.widgets.Button; >import org.eclipse.swt.widgets.Composite; >import org.eclipse.swt.widgets.Control; >import org.eclipse.swt.widgets.Display; >import org.eclipse.swt.widgets.Event; >import org.eclipse.swt.widgets.Link; >import org.eclipse.swt.widgets.Listener; >import org.eclipse.swt.widgets.Shell; >import org.eclipse.swt.widgets.Text; >import org.eclipse.ui.IWorkbenchWindow; >import org.eclipse.ui.PlatformUI; >import org.eclipse.ui.menus.CommandContributionItem; >import org.eclipse.ui.menus.CommandContributionItemParameter; > >import com.oaklandsw.base.AppEditor; >import com.oaklandsw.base.AppRuntime; >import com.oaklandsw.base.BaseNode; >import com.oaklandsw.base.BasePlugin; >import com.oaklandsw.base.BaseTypeNode; >import com.oaklandsw.base.BaseUtils; >import com.oaklandsw.base.Cap; >import com.oaklandsw.base.LoggerSupport; >import com.oaklandsw.base.ResourceHelper; >import com.oaklandsw.util.SystemUtils; >import com.oaklandsw.util.Util; >import com.oaklandsw.utillog.Log; > >public class GuiUtils >{ > static > { > GuiImpossibleHandler gih = new GuiImpossibleHandler(); > Util._impossibleHandler = gih; > } > > private static final Log _log = LoggerSupport.makeLogger(); > > private static class GuiImpossibleHandler implements Util.ImpossibleHandler > { > public void handleImpossible(String message, Throwable ex) > { > guiImpossible(message, ex); > } > } > > public static void getClassLoaded() > { > // noop to get the class loaded to register the handler for impossible > } > > /** > * For when we want to be sure and open a bug report in cases where the > * exception might be lost. > */ > public static void guiImpossible(String message, Throwable ex) > { > try > { > IStatus status = new Status(IStatus.ERROR, > BasePlugin.ID, > BaseUtils.IMPOSSIBLE_MESSAGE_PREFIX > + message, > ex); > > ILog eclipseLog = Platform > .getLog(Platform.getBundle(BasePlugin.ID)); > eclipseLog.log(status); > > BugReportDialog.openBugReport(Messages.GuiUtils_BugReport, > status, > (GuiAppEditor)BasePlugin > .getAppEditor()); > } > catch (Exception e) > { > // Prevent an infinite loop if something goes wrong with the > // bug reporting > System.err.println("FATAL: exception during bug reporting"); //$NON-NLS-1$ > ex.printStackTrace(System.err); > } > > } > > public static int DIALOG_STYLE = SWT.RESIZE > | SWT.APPLICATION_MODAL > | SWT.DIALOG_TRIM; > > // Add XML as a contribution as if it were present in the plugin.xml file > public static void addContribution(AppEditor editor, String contribution) > { > ByteArrayInputStream is = new ByteArrayInputStream(contribution > .toString().getBytes()); > > IExtensionRegistry reg = Platform.getExtensionRegistry(); > Object ut = ((ExtensionRegistry)reg).getTemporaryUserToken(); > > IContributor cont = ContributorFactoryOSGi.createContributor(Platform > .getBundle(editor.getPluginId())); > > if (false) > System.out.println(contribution); > if (!reg.addContribution(is, cont, false, null, null, ut)) > Util.impossible("Contribution not added due to error"); //$NON-NLS-1$ > > } > > public static String createWizardElement(String name, > String shortName, > String executorClass, > Cap cap) > { > return "<wizard" + " category='" //$NON-NLS-1$ //$NON-NLS-2$ > + cap.getCategoryId() + "'" //$NON-NLS-1$ > + " icon='images/" //$NON-NLS-1$ > + shortName > + ".gif'" //$NON-NLS-1$ > + " descriptionImage='images/" //$NON-NLS-1$ > + shortName > + ".gif'" //$NON-NLS-1$ > + " class='" //$NON-NLS-1$ > + executorClass > + "'" //$NON-NLS-1$ > + " id='com.oaklandsw.transform.wizard.new" //$NON-NLS-1$ > + shortName > + "'" //$NON-NLS-1$ > + " name='" //$NON-NLS-1$ > + name > + "'>\n" //$NON-NLS-1$ > + "<description>\n" //$NON-NLS-1$ > + Messages.GuiUtils_CreateA > + name > + "</description>\n" //$NON-NLS-1$ > + " </wizard>\n"; //$NON-NLS-1$ > > } > > public static int operationToTransferAction(int operation) > { > if ((operation & DND.DROP_COPY) != 0) > return BaseNode.TRANSFER_COPY; > if ((operation & DND.DROP_MOVE) != 0) > return BaseNode.TRANSFER_MOVE; > Util.impossible("Unsupported operation: " + operation); //$NON-NLS-1$ > return -1; > } > > public static Font deriveFont(Font initialFont, int style) > { > FontData[] fontData = initialFont.getFontData(); > for (int i = 0; i < fontData.length; i++) > { > fontData[i].setStyle(style); > } > return new Font(Display.getCurrent(), fontData); > } > > private static Font _monoFont; > > public static Font getMonospaceFont() > { > if (_monoFont == null) > { > Display display = Display.getCurrent(); > FontData fds[] = display.getSystemFont().getFontData(); > // FIXME - When Eclipse bug 48055 is resolved, update this with a > // better > // abstraction > fds[0].setName("Courier"); //$NON-NLS-1$ > _monoFont = new Font(display, fds[0]); > } > return _monoFont; > } > > public static final boolean FORCE = true; > > // Make the dialog no wider than 3/4 the width and height of the parent > public static void adjustDialogSize(Shell dialogShell, > Shell parentShell, > boolean force) > { > if (parentShell == null) > return; > int parentWidth = parentShell.getSize().x; > int dialogWidth = dialogShell.getSize().x; > int parentHeight = parentShell.getSize().y; > int dialogHeight = dialogShell.getSize().y; > if (dialogWidth > parentWidth * .75 || force) > dialogWidth = (int)(parentWidth * .75); > if (dialogHeight > parentHeight || force) > dialogHeight = (int)(parentHeight * .75); > dialogShell.setSize(dialogWidth, dialogHeight); > > adjustDialogLocation(dialogShell, parentShell, force); > } > > // Make the dialog no wider than 3/4 the width and height of the parent > public static void adjustDialogLocation(Shell dialogShell, > Shell parentShell, > boolean force) > { > // Now fix the location to make sure it is correctly centered > // on the parent > Rectangle parentSize = parentShell.getBounds(); > Rectangle mySize = dialogShell.getBounds(); > > int locationX, locationY; > locationX = (parentSize.width - mySize.width) / 2 + parentSize.x; > locationY = (parentSize.height - mySize.height) / 2 + parentSize.y; > > dialogShell.setLocation(new Point(locationX, locationY)); > } > > public static Composite createInsetComposite(Composite parent, int inset) > { > Composite c = new Composite(parent, SWT.NONE); > FillLayout fl = new FillLayout(); > fl.marginHeight = 10; > fl.marginWidth = 10; > c.setLayout(fl); > return c; > } > > public static void setTextColumns(Text text, int columns) > { > GC gc = new GC(text); > FontMetrics fm = gc.getFontMetrics(); > int width = columns * fm.getAverageCharWidth(); > int height = fm.getHeight(); > gc.dispose(); > text.setSize(text.computeSize(width, height)); > } > > public static Text createNumericText(Composite parent) > { > Text text = new Text(parent, SWT.TRAIL | SWT.BORDER); > text.addVerifyListener(new VerifyListener() > { > public void verifyText(VerifyEvent e) > { > Pattern pattern = Pattern.compile("[\\-0-9]*"); //$NON-NLS-1$ > Matcher m = pattern.matcher(e.text); > if (!m.matches()) > e.doit = false; > } > }); > return text; > } > > // Used to force a component to be a fixed width > public static void setCompWidth(Control comp, int width) > { > Point size = comp.getSize(); > size.x = width; > comp.setSize(size); > } > > // Handles making sure only one of the buttons is selected > public static Listener getToggleButtonListener(final Composite composite) > { > Listener listener = new Listener() > { > public void handleEvent(Event e) > { > Control[] children = composite.getChildren(); > for (int i = 0; i < children.length; i++) > { > Control child = children[i]; > if (e.widget != child > && child instanceof Button > && (child.getStyle() & SWT.RADIO) != 0) > { > ((Button)child).setSelection(false); > } > } > ((Button)e.widget).setSelection(true); > } > }; > return listener; > } > > // Used with a ScrolledComposite to calculate the proper > // size, this is called from the Listener below, and if > // anything changes in the contained Composite > public static void computeResize(final ScrolledComposite sc, > final Composite composite) > { > Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT); > sc.setMinSize(size); > composite.layout(); > if (false) > { > System.out.println("computeResize: new client area: " //$NON-NLS-1$ > + sc.getClientArea() > + " origin: " //$NON-NLS-1$ > + sc.getOrigin() > + " tree bounds: " //$NON-NLS-1$ > + composite.getBounds()); > } > } > > // Used to handle a SWT.Resize event with a ScrolledComposite > // containing a Composite (the typical pattern for scrollbars) > // This allows scrolling on both axes; to prevent scrolling > // on one or the other, remove the "setMinHeight/Width" call > // above (you will need to refactor this to handle that case) > public static Listener getResizeListener(final ScrolledComposite composite, > final Composite control) > { > Listener l = new Listener() > { > public void handleEvent(Event e) > { > computeResize(composite, control); > } > }; > > return l; > } > > public static Link getHelpLink(Composite parent, > final GuiAppEditor editor, > String text, > String helpContext) > { > final Link link = new Link(parent, SWT.WRAP | SWT.NO_FOCUS); > PlatformUI.getWorkbench().getHelpSystem().setHelp(link, helpContext); > link.setText("<a>" + text + "</a>"); //$NON-NLS-1$ //$NON-NLS-2$ > link.addSelectionListener(new SelectionAdapter() > { > @Override > public void widgetSelected(SelectionEvent e) > { > if (editor.getShell() != null) > { > Control c = link; > while (c != null) > { > if (c.isListening(SWT.Help)) > { > c.notifyListeners(SWT.Help, new Event()); > break; > } > c = c.getParent(); > } > } > } > }); > return link; > > } > > // The first time a dialog is used, it can take longer > public static final int INITIAL_WAIT_TIME = 5000; > // For subsequent times > // Windows seems to be much slower rendering the windows so we need > // the extra time > public static final int WAIT_TIME = SystemUtils.IS_OS_WINDOWS > ? 750 > : 200; > > public static void pushButton(Button b) > { > final Display d = Display.getCurrent(); > final Point p = d.map(b, null, 10, 10); > > Event event = new Event(); > event.x = p.x; > event.y = p.y; > event.type = SWT.MouseMove; > d.post(event); > > event = new Event(); > event.type = SWT.MouseDown; > event.button = 1; > d.post(event); > > event = new Event(); > event.type = SWT.MouseUp; > event.button = 1; > d.post(event); > > processEvents(); > } > > public static void sendOk(Display display) > { > sendOkAccel('O', display); > } > > // Sends OK as an accelerator key > public static void sendOkAccel(char letter, Display display) > { > if (SystemUtils.IS_OS_MAC_OSX) > sendEnter(display); > else > GuiUtils.sendKey(letter, SWT.ALT, display); > } > > public static void sendEnter(Display display) > { > GuiUtils.sendKey('\r', display); > } > > public static void sendEscape(Display display) > { > GuiUtils.sendKey(SWT.ESC, display); > } > > public static void waitAndPushOK(final Display display, final int waitTime) > { > // Alter the value and press the right key > new Thread(new Runnable() > { > public void run() > { > _log.debug("waitAndpushOK start"); //$NON-NLS-1$ > // Wait for property sheet to become open > if (waitTime == -1) > GuiUtils.sleep(WAIT_TIME); > else > GuiUtils.sleep(waitTime); > > _log.debug("waitAndpushOK wake"); //$NON-NLS-1$ > > display.syncExec(new Runnable() > { > public void run() > { > CompositeDialog cd = (CompositeDialog)AppEditorImpl._testDialog; > GuiUtils.pushButton(cd.getOkButton()); > _log.debug("waitAndpushOK sent"); //$NON-NLS-1$ > } > }); > } > }).start(); > > } > > public static void sendKey(final int keyCode, final Display display) > { > sendKey(keyCode, 0, display); > } > > // Used mainly for testing, emits a keystroke > public static void sendKey(final int keyCode, > final int stateMask, > final Display display) > { > // Send enter event > display.syncExec(new Runnable() > { > public void run() > { > Event event; > > // Note: use Snippet25 to determine what happens when > // the desired keys are pressed. This code does exactly > // the same thing as the events that were seen. No other > // combinations will work correctly. > > if (stateMask != 0) > { > event = new Event(); > event.type = SWT.KeyDown; > event.keyCode = stateMask; > display.post(event); > } > > event = new Event(); > event.type = SWT.KeyDown; > event.keyCode = keyCode; > event.stateMask = stateMask; > event.character = (char)keyCode; > display.post(event); > > event = new Event(); > event.type = SWT.KeyUp; > event.keyCode = keyCode; > event.stateMask = stateMask; > event.character = (char)keyCode; > display.post(event); > > if (stateMask != 0) > { > event = new Event(); > event.type = SWT.KeyUp; > event.keyCode = stateMask; > display.post(event); > } > } > }); > > } > > // Process any outstanding events and return when there are no more > public static void processEvents() > { > processEvents(null, 1); > } > > // Process any outstanding events and return when there are no more > public static void processEvents(int loopCount) > { > processEvents(null, loopCount); > } > > // The display is the display to use the sync exec > public static void processEvents(Display d, final int loopCount) > { > if (Display.getCurrent() != null) > { > // We are on the UI thread > runProcessEvents(loopCount); > } > else > { > if (d != null) > { > d.syncExec(new Runnable() > { > public void run() > { > runProcessEvents(loopCount); > } > }); > } > } > } > > // Process any outstanding events and return when there are no more > public static void runProcessEvents(int loopCount) > { > for (int i = 0; i < loopCount; i++) > { > if (_log.isDebugEnabled()) > _log.debug("processEvents - start: " + i + " of: " + loopCount); //$NON-NLS-1$ //$NON-NLS-2$ > int count = 0; > > Display display = Display.getCurrent(); > while (display.readAndDispatch()) > count++; > display.update(); > while (display.readAndDispatch()) > count++; > if (false) > { > System.out.println("processed: " //$NON-NLS-1$ > + count > + " at loop: " //$NON-NLS-1$ > + i > + " of: " //$NON-NLS-1$ > + loopCount); > } > if (_log.isDebugEnabled()) > _log.debug("processEvents - end processed: " + count); //$NON-NLS-1$ > } > } > > public static void blockAndProcess() > { > while (true) > Display.getCurrent().readAndDispatch(); > } > > private static class Wake > { > boolean _waitThreadDone = false; > boolean _sleeping = true; > } > > public static void sleep(final int time) > { > // This can be called on a non-display thread > final Display d = Display.getCurrent(); > > if (d == null) > { > try > { > Thread.sleep(time); > } > catch (InterruptedException e) > { > } > return; > } > sleep(time, d); > } > > public static void sleep(final int time, final Display d) > { > d.syncExec(new Runnable() > { > public void run() > { > long wakeTime = System.currentTimeMillis() + time; > final Wake wake = new Wake(); > > Thread wakeThread = null; > > wakeThread = new Thread(new Runnable() > { > public void run() > { > try > { > Thread.sleep(time); > } > catch (InterruptedException e) > { > } > > synchronized (wake) > { > if (!wake._sleeping) > { > if (false) > { > System.out > .println("GuiUtils.sleep() wake thread returned (no wake): " //$NON-NLS-1$ > + System.currentTimeMillis()); > } > return; > } > > if (false) > { > System.out > .println("GuiUtils.sleep() waking display thread: " //$NON-NLS-1$ > + System.currentTimeMillis()); > } > d.wake(); > wake._waitThreadDone = true; > } > } > }); > > wakeThread.start(); > > if (_log.isDebugEnabled()) > _log.debug("start sleep: " + time); //$NON-NLS-1$ > if (false) > { > System.out.println("GuiUtils.sleep() - start " //$NON-NLS-1$ > + time > + " wake: " //$NON-NLS-1$ > + wakeTime > + " current: " //$NON-NLS-1$ > + System.currentTimeMillis()); > } > > while (System.currentTimeMillis() < wakeTime) > { > // Queue this even to wake up if the wake thread > // has stopped while sleeping, this has to be checked > // inside of the sleep to eliminate a window in case > // the wake thread terminates before the sleep > d.syncExec(new Runnable() > { > public void run() > { > synchronized (wake) > { > if (wake._waitThreadDone) > d.wake(); > } > } > }); > > d.readAndDispatch(); > d.sleep(); > if (false) > { > System.out.println("GuiUtils.sleep() - after sleep " //$NON-NLS-1$ > + wakeTime > + " current: " //$NON-NLS-1$ > + System.currentTimeMillis()); > > } > } > > synchronized (wake) > { > // Make sure the wait thread does not try to wake the next > // guy > wake._sleeping = false; > } > > if (false) > { > System.out.println("GuiUtils.sleep() - end sleep: target: " //$NON-NLS-1$ > + wakeTime > + " actual: " //$NON-NLS-1$ > + System.currentTimeMillis()); > } > _log.debug("end sleep"); //$NON-NLS-1$ > } > }); > > } > > public static IContributionItem createContributionItem(IWorkbenchWindow window, > String id) > { > return new CommandContributionItem(new CommandContributionItemParameter(window, > id, > id, > 0)); > } > > // Returns a ViewerFilter for the specified repos objec type (and PROJECT) > // this is used in places where nodes are selected > public static final boolean DIR_ONLY = true; > > public static final boolean ALLOW_WRITE = true; > > public static ViewerFilter getReposObjectFilter(final AppRuntime runtime, > final String reposType, > final boolean dirOnly, > final boolean allowWrite) > { > return new ViewerFilter() > { > @Override > public boolean select(Viewer viewer, > Object parentElement, > Object element) > { > if (false) > { > System.out.println("parent:" //$NON-NLS-1$ > + parentElement > + " elem: " //$NON-NLS-1$ > + element); > } > > if (element instanceof BaseNode) > { > BaseTypeNode nodeType = ((BaseNode)element) > .getBaseTypeNode(); > if (dirOnly) > { > return false; > } > String checkReposType = nodeType.getRepositoryType(); > if (checkReposType != null > && (checkReposType.equals(reposType))) > { > return true; > } > } > else > { > IResource res = (IResource)element; > > if (res instanceof IFile) > { > BaseTypeNode type = runtime.getResourceType(res); > if (!dirOnly > && type != null > && type.getRepositoryType().equals(reposType)) > return true; > } > > if (res instanceof IProject) > { > IProject proj = (IProject)res; > if (!runtime.isProjectSupported(proj)) > return false; > if (allowWrite && ResourceHelper.isReadOnly(proj)) > return false; > return true; > } > > if (res instanceof IFolder) > { > IPath path = res.getProjectRelativePath(); > if (path.segment(0).equals(reposType)) > return true; > } > > return false; > } > > return false; > } > }; > > } > > // Gets the encodings for this platform in the array form > // understood by the combo adaptor > > public static Object[] getEncodingsForCombo() > { > List output = new ArrayList(); > > output.add(Messages.GuiUtils_PlaftformDefault); > output.add(""); //$NON-NLS-1$ > > SortedMap charsets = Charset.availableCharsets(); > Iterator it = charsets.keySet().iterator(); > while (it.hasNext()) > { > String encoding = (String)it.next(); > // Add twice, the first string is displayed, the > // second is the value > output.add(encoding); > output.add(encoding); > } > return output.toArray(); > } > > public static String limitOutput(AppEditor editor, String output, int limit) > { > if (output == null) > return null; > String retString; > > // If it's too much, takes forever to layout > if (output.length() > limit && limit != -1) > { > editor.showWarning(NLS.bind(Messages.GuiUtils_OutputLength, output > .length(), limit)); > retString = output.substring(0, limit); > } > else > { > retString = output; > } > > return retString; > } > > // For JFace TreePaths > public static String treePathToString(TreePath path) > { > StringBuffer sb = new StringBuffer(200); > for (int i = 0, len = path.getSegmentCount(); i < len; i++) > { > if (i > 0) > sb.append(","); //$NON-NLS-1$ > sb.append(path.getSegment(i)); > } > return sb.toString(); > } > >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 301330
:
157688
|
157689
| 157935 |
231185
|
231506