Source Code Cross Referenced for MacroUtil.java in  » IDE-Eclipse » Eclipse-plug-in-development » org » eclipse » pde » internal » ui » tests » macro » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Eclipse » Eclipse plug in development » org.eclipse.pde.internal.ui.tests.macro 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*******************************************************************************
0002:         * Copyright (c) 2000, 2005 IBM Corporation and others.
0003:         * All rights reserved. This program and the accompanying materials
0004:         * are made available under the terms of the Eclipse Public License v1.0
0005:         * which accompanies this distribution, and is available at
0006:         * http://www.eclipse.org/legal/epl-v10.html
0007:         * 
0008:         * Contributors:
0009:         *     IBM Corporation - initial API and implementation
0010:         *******************************************************************************/package org.eclipse.pde.internal.ui.tests.macro;
0011:
0012:        import java.util.ArrayList;
0013:        import java.util.Iterator;
0014:
0015:        import org.eclipse.core.runtime.CoreException;
0016:        import org.eclipse.core.runtime.IPath;
0017:        import org.eclipse.core.runtime.IStatus;
0018:        import org.eclipse.core.runtime.Path;
0019:        import org.eclipse.core.runtime.Status;
0020:        import org.eclipse.jface.action.ActionContributionItem;
0021:        import org.eclipse.jface.action.CoolBarManager;
0022:        import org.eclipse.jface.action.IAction;
0023:        import org.eclipse.jface.action.IContributionItem;
0024:        import org.eclipse.jface.action.ICoolBarManager;
0025:        import org.eclipse.jface.action.IToolBarManager;
0026:        import org.eclipse.jface.action.MenuManager;
0027:        import org.eclipse.jface.action.ToolBarContributionItem;
0028:        import org.eclipse.jface.action.ToolBarManager;
0029:        import org.eclipse.jface.window.ApplicationWindow;
0030:        import org.eclipse.jface.window.Window;
0031:        import org.eclipse.jface.wizard.IWizardPage;
0032:        import org.eclipse.jface.wizard.WizardDialog;
0033:        import org.eclipse.swt.SWT;
0034:        import org.eclipse.swt.widgets.Button;
0035:        import org.eclipse.swt.widgets.Composite;
0036:        import org.eclipse.swt.widgets.Control;
0037:        import org.eclipse.swt.widgets.CoolBar;
0038:        import org.eclipse.swt.widgets.Display;
0039:        import org.eclipse.swt.widgets.Event;
0040:        import org.eclipse.swt.widgets.Menu;
0041:        import org.eclipse.swt.widgets.MenuItem;
0042:        import org.eclipse.swt.widgets.Shell;
0043:        import org.eclipse.swt.widgets.ToolBar;
0044:        import org.eclipse.swt.widgets.ToolItem;
0045:        import org.eclipse.swt.widgets.Widget;
0046:        import org.eclipse.ui.IEditorPart;
0047:        import org.eclipse.ui.IEditorReference;
0048:        import org.eclipse.ui.IEditorSite;
0049:        import org.eclipse.ui.IPluginContribution;
0050:        import org.eclipse.ui.IViewPart;
0051:        import org.eclipse.ui.IWorkbenchPage;
0052:        import org.eclipse.ui.IWorkbenchPart;
0053:        import org.eclipse.ui.IWorkbenchPartSite;
0054:        import org.eclipse.ui.IWorkbenchWindow;
0055:        import org.eclipse.ui.internal.EditorSite;
0056:        import org.eclipse.ui.internal.PartPane;
0057:        import org.eclipse.ui.internal.PartSite;
0058:        import org.eclipse.ui.keys.SWTKeySupport;
0059:        import org.w3c.dom.Node;
0060:
0061:        public class MacroUtil {
0062:            private static final String EMPTY_STRING = ""; //$NON-NLS-1$
0063:
0064:            /**
0065:             * Returns the path where counters of the given event are stored, or null if
0066:             * we are not keeping records of the given event.
0067:             * 
0068:             * @param event
0069:             * @return
0070:             */
0071:            public static WidgetIdentifier getWidgetIdentifier(Widget widget) {
0072:                if (widget instanceof  MenuItem) {
0073:
0074:                    MenuItem menuItem = (MenuItem) widget;
0075:
0076:                    if (onMenubar(menuItem)) {
0077:                        return new WidgetIdentifier(
0078:                                new Path("menus"), new Path(getActionId(menuItem))); //$NON-NLS-1$
0079:                    }
0080:                    Control c = widget.getDisplay().getFocusControl();
0081:                    WidgetIdentifier ci = getControlIdentifier(c);
0082:                    if (ci == null)
0083:                        return null;
0084:                    return new WidgetIdentifier(
0085:                            new Path("popup").append(ci.getFullyQualifiedPath()), new Path(getActionId(menuItem))); //$NON-NLS-1$
0086:                } else if (widget instanceof  ToolItem) {
0087:                    ToolItem toolItem = (ToolItem) widget;
0088:
0089:                    if (onToolbar(toolItem))
0090:                        return new WidgetIdentifier(
0091:                                new Path("toolbar"), new Path(getActionId(toolItem))); //$NON-NLS-1$
0092:                    // local toolbar somewhere - locate the parent
0093:                    // first
0094:                    ToolBar toolBar = toolItem.getParent();
0095:                    WidgetIdentifier controlId = getControlIdentifier(toolBar);
0096:                    IPath localPath = controlId.getFullyQualifiedPath();
0097:                    return new WidgetIdentifier(new Path("local-toolbar")
0098:                            .append(localPath), new Path(getActionId(toolItem)));
0099:                } else if (widget instanceof  Shell) {
0100:                    return new WidgetIdentifier(new Path("shell"),
0101:                            getShellId((Shell) widget));
0102:                } else if (widget instanceof  Control) {
0103:                    return getControlIdentifier((Control) widget);
0104:                } else if (widget instanceof  Menu) {
0105:                    return new WidgetIdentifier(new Path("menu"), new Path(
0106:                            getActionId((Menu) widget)));
0107:                }
0108:                return null;
0109:            }
0110:
0111:            public static IPath getShellId(Shell shell) {
0112:                Object data = shell.getData();
0113:                String id = "";
0114:                if (data instanceof  WizardDialog) {
0115:                    id = data.getClass().getName().toString();
0116:                } else if (data instanceof  Window) {
0117:                    id = data.getClass().getName().toString();
0118:                }
0119:                return new Path(id);
0120:            }
0121:
0122:            public static WidgetIdentifier getControlIdentifier(Control control) {
0123:                Shell shell = control.getShell();
0124:                Object data = shell.getData();
0125:                if (data instanceof  WizardDialog) {
0126:                    // in wizard
0127:                    WizardDialog wd = (WizardDialog) data;
0128:                    IWizardPage page = wd.getCurrentPage();
0129:                    if (page == null)
0130:                        return null;
0131:                    Control pageControl = page.getControl();
0132:                    String relativePath = computeRelativePath(
0133:                            (Composite) pageControl, null, control);
0134:                    if (relativePath != null) {
0135:                        IPath path = new Path("wizard-page").append(page
0136:                                .getName());
0137:                        return new WidgetIdentifier(path,
0138:                                new Path(relativePath));
0139:                    }
0140:                    // check for wizard buttons
0141:                    if (control instanceof  Button) {
0142:                        relativePath = computeRelativePath(shell,
0143:                                (Composite) pageControl, control);
0144:                        return new WidgetIdentifier(new Path("wizard"),
0145:                                new Path(relativePath));
0146:                    }
0147:                    return null;
0148:                } else if (data instanceof  IWorkbenchWindow) {
0149:                    IWorkbenchWindow window = (IWorkbenchWindow) data;
0150:                    IWorkbenchPage page = window.getActivePage();
0151:                    IWorkbenchPart part = page.getActivePart();
0152:                    IWorkbenchPartSite site = part.getSite();
0153:                    IPath path;
0154:                    if (part instanceof  IViewPart)
0155:                        path = new Path("view").append(site.getId());
0156:                    else if (part instanceof  IEditorPart) {
0157:                        String inputName = ((IEditorPart) part)
0158:                                .getEditorInput().getName();
0159:                        path = new Path("editor").append(site.getId()).append(
0160:                                inputName);
0161:                    } else
0162:                        return null;
0163:                    PartSite partSite = (PartSite) site;
0164:                    PartPane pane = partSite.getPane();
0165:                    Composite paneComposite = (Composite) pane.getControl();
0166:                    // If the control we are looking for is a local tool bar,
0167:                    // go up one level
0168:                    if (part instanceof  IViewPart && control instanceof  ToolBar)
0169:                        paneComposite = paneComposite.getParent();
0170:                    String relativePath = computeRelativePath(paneComposite,
0171:                            null, control);
0172:                    if (relativePath != null) {
0173:                        return new WidgetIdentifier(path,
0174:                                new Path(relativePath));
0175:                    }
0176:                } else {
0177:                    // unknown shell - fetch controls starting from the shell
0178:                    String relativePath = computeRelativePath(shell, null,
0179:                            control);
0180:                    return new WidgetIdentifier(new Path("shell"), new Path(
0181:                            relativePath));
0182:                }
0183:                return null;
0184:            }
0185:
0186:            private static String computeRelativePath(Composite parent,
0187:                    Composite skip, Control control) {
0188:                int[] counter = new int[1];
0189:                counter[0] = 0;
0190:                boolean result = computeControlToken(parent, skip, control,
0191:                        counter);
0192:                if (!result && skip == null)
0193:                    return null;
0194:                int index = result ? counter[0] : 0;
0195:                return getControlId(control, index);
0196:            }
0197:
0198:            private static String getControlId(Control control, int index) {
0199:                MacroManager recorder = MacroPlugin.getDefault()
0200:                        .getMacroManager();
0201:                String controlId = recorder.resolveWidget(control);
0202:                if (controlId == null)
0203:                    controlId = index + "";
0204:                return control.getClass().getName() + "#" + controlId;
0205:            }
0206:
0207:            private static boolean computeControlToken(Composite parent,
0208:                    Composite skip, Control control, int[] counter) {
0209:                Control[] children = parent.getChildren();
0210:                for (int i = 0; i < children.length; i++) {
0211:                    Control child = children[i];
0212:
0213:                    if (!child.isVisible())
0214:                        continue;
0215:
0216:                    if (child.getClass().equals(control.getClass())) {
0217:                        // same type - increment counter
0218:                        counter[0]++;
0219:                        if (control.equals(child)) {
0220:                            // bingo
0221:                            return true;
0222:                        }
0223:                    } else if (child instanceof  Composite) {
0224:                        if (skip != null && child.equals(skip))
0225:                            continue;
0226:                        boolean status = computeControlToken((Composite) child,
0227:                                skip, control, counter);
0228:                        if (status)
0229:                            return true;
0230:                    }
0231:                }
0232:                return false;
0233:            }
0234:
0235:            public static boolean isInputControl(Control control) {
0236:                return true;
0237:            }
0238:
0239:            /**
0240:             * @param menuItem
0241:             * @return
0242:             */
0243:            private static boolean onMenubar(MenuItem menuItem) {
0244:                Menu parent = menuItem.getParent();
0245:                MenuItem parentItem = parent.getParentItem();
0246:
0247:                if (parentItem != null) {
0248:                    return onMenubar(parentItem);
0249:                }
0250:
0251:                Shell theShell = parent.getShell();
0252:
0253:                return parent == theShell.getMenuBar();
0254:            }
0255:
0256:            private static boolean onToolbar(ToolItem toolItem) {
0257:                ToolBar toolBar = toolItem.getParent();
0258:                Shell shell = toolBar.getShell();
0259:                Object data = shell.getData();
0260:                if (data instanceof  ApplicationWindow) {
0261:                    ApplicationWindow window = (ApplicationWindow) data;
0262:                    ToolBarManager mng = window.getToolBarManager();
0263:                    if (mng != null) {
0264:                        if (mng.getControl() != null
0265:                                && mng.getControl() == toolBar)
0266:                            return true;
0267:                    }
0268:                    CoolBarManager cmng = window.getCoolBarManager();
0269:                    if (cmng != null) {
0270:                        CoolBar cbar = cmng.getControl();
0271:                        Composite parent = toolBar.getParent();
0272:                        while (parent != null) {
0273:                            if (parent == cbar)
0274:                                return true;
0275:                            parent = parent.getParent();
0276:                        }
0277:                    }
0278:                }
0279:                return false;
0280:            }
0281:
0282:            /**
0283:             * @param toolItem
0284:             * @return
0285:             */
0286:            private static String getActionId(ToolItem toolItem) {
0287:                Object data = toolItem.getData();
0288:                if (data != null && (data instanceof  IContributionItem)) {
0289:                    String result = getActionId((IContributionItem) data);
0290:                    if (!result.equals(EMPTY_STRING)) {
0291:                        return result;
0292:                    }
0293:                }
0294:
0295:                return "readablename/" + getDisplayName(toolItem); //$NON-NLS-1$
0296:            }
0297:
0298:            /**
0299:             * @param toolItem
0300:             * @return
0301:             */
0302:            private static String getDisplayName(ToolItem toolItem) {
0303:                String name = toolItem.getText();
0304:
0305:                if (name != null && !name.equals(EMPTY_STRING)) {
0306:                    return name;
0307:                }
0308:
0309:                name = toolItem.getToolTipText();
0310:
0311:                if (name != null) {
0312:                    return name;
0313:                }
0314:
0315:                return "unknown"; //$NON-NLS-1$
0316:            }
0317:
0318:            /**
0319:             * Returns an identifier for the given MenuItem, based on its user-readable
0320:             * strings
0321:             * 
0322:             * @param menuItem
0323:             * @return
0324:             */
0325:            private static String getDisplayName(MenuItem menuItem) {
0326:
0327:                if (menuItem.getParent() == null
0328:                        || menuItem.getParent().getParentItem() == null) {
0329:                    return removeChar(menuItem.getText(), '&');
0330:                }
0331:
0332:                return getDisplayName(menuItem.getParent()) + "/" //$NON-NLS-1$
0333:                        + removeChar(menuItem.getText(), '&');
0334:            }
0335:
0336:            /**
0337:             * Returns an identifier for the given Menu, based on its user-readable
0338:             * strings
0339:             * 
0340:             * @param menu
0341:             * @return
0342:             */
0343:            private static String getDisplayName(Menu menu) {
0344:
0345:                MenuItem parentItem = menu.getParentItem();
0346:
0347:                if (parentItem == null) {
0348:                    return EMPTY_STRING;
0349:                }
0350:
0351:                return getDisplayName(parentItem);
0352:            }
0353:
0354:            protected String getContribId(MenuItem menuItem) {
0355:                Object data = menuItem.getData();
0356:                if (data != null && (data instanceof  IContributionItem)) {
0357:                    String result = ((IContributionItem) data).getId();
0358:
0359:                    if (result != null) {
0360:                        return result;
0361:                    }
0362:                }
0363:
0364:                return EMPTY_STRING;
0365:            }
0366:
0367:            /**
0368:             * @param menuItem
0369:             * @return
0370:             */
0371:            private static String getActionId(MenuItem menuItem) {
0372:                Object data = menuItem.getData();
0373:                if (data != null && (data instanceof  IContributionItem)) {
0374:                    String result = getActionId((IContributionItem) data);
0375:                    if (!result.equals(EMPTY_STRING)) {
0376:                        return result;
0377:                    }
0378:                }
0379:
0380:                // return EMPTY_STRING;
0381:
0382:                return "readablename/" + getDisplayName(menuItem); //$NON-NLS-1$
0383:            }
0384:
0385:            private static String getActionId(Menu menu) {
0386:                Object data = menu.getData();
0387:                if (data != null && (data instanceof  IContributionItem)) {
0388:                    String result = getActionId((IContributionItem) data);
0389:                    if (!result.equals(EMPTY_STRING)) {
0390:                        return result;
0391:                    }
0392:                }
0393:
0394:                // return EMPTY_STRING;
0395:
0396:                return "readablename/" + getDisplayName(menu); //$NON-NLS-1$
0397:            }
0398:
0399:            private static String getActionId(IContributionItem contrib) {
0400:                String id = null;
0401:
0402:                if (contrib instanceof  IPluginContribution) {
0403:                    id = ((IPluginContribution) contrib).getLocalId();
0404:                }
0405:                if (id == null)
0406:                    id = contrib.getId();
0407:
0408:                if (id != null) {
0409:                    return "contribid/" + id; //$NON-NLS-1$
0410:                }
0411:
0412:                if (contrib instanceof  ActionContributionItem) {
0413:                    ActionContributionItem actionItem = (ActionContributionItem) contrib;
0414:
0415:                    id = actionItem.getId();
0416:
0417:                    if (id != null) {
0418:                        return "actionid/" + id; //$NON-NLS-1$
0419:                    }
0420:
0421:                    IAction action = actionItem.getAction();
0422:
0423:                    id = action.getActionDefinitionId();
0424:
0425:                    if (id != null) {
0426:                        return "defid/" + id; //$NON-NLS-1$
0427:                    }
0428:
0429:                    return "actionclass/" + action.getClass().getName(); //$NON-NLS-1$
0430:                }
0431:                return "contribclass/" + contrib.getClass().getName(); //$NON-NLS-1$
0432:            }
0433:
0434:            /*
0435:             * (non-Javadoc)
0436:             * 
0437:             * @see org.eclipse.instrumentation.IDataProvider#getDefaultValue(org.eclipse.core.runtime.IPath)
0438:             */
0439:            public Object getDefaultValue(IPath node) throws CoreException {
0440:                return null;
0441:            }
0442:
0443:            private MenuItem getMenuItem(Menu menu, IPath menuPath) {
0444:                if (menuPath.isEmpty()) {
0445:                    return null;
0446:                }
0447:
0448:                String toFind = menuPath.segment(0);
0449:                MenuItem[] items = menu.getItems();
0450:
0451:                for (int idx = 0; idx < items.length; idx++) {
0452:                    MenuItem item = items[idx];
0453:
0454:                    String itemName = removeChar(item.getText(), '&');
0455:
0456:                    if (itemName.equals(toFind)) {
0457:                        return getMenuItem(item, menuPath
0458:                                .removeFirstSegments(1));
0459:                    }
0460:                }
0461:
0462:                return null;
0463:            }
0464:
0465:            private MenuItem getMenuItem(MenuItem menu, IPath menuPath) {
0466:                if (menuPath.isEmpty()) {
0467:                    return menu;
0468:                }
0469:
0470:                Menu subMenu = menu.getMenu();
0471:                if (subMenu == null) {
0472:                    return null;
0473:                }
0474:
0475:                return getMenuItem(subMenu, menuPath);
0476:
0477:            }
0478:
0479:            public static String removeChar(String input, char toRemove) {
0480:                StringBuffer buf = new StringBuffer(input.length());
0481:
0482:                int last = 0;
0483:                for (int pos = input.indexOf(toRemove); pos != -1; pos = input
0484:                        .indexOf(toRemove, last)) {
0485:                    buf.append(input.substring(last, pos));
0486:                    last = pos + 1;
0487:                }
0488:
0489:                buf.append(input.substring(last, input.length()));
0490:
0491:                return buf.toString();
0492:            }
0493:
0494:            public static String getAttribute(Node node, String name) {
0495:                Node value = node.getAttributes().getNamedItem(name);
0496:                if (value != null)
0497:                    return value.getNodeValue();
0498:                return null;
0499:            }
0500:
0501:            public static String getNormalizedText(String source) {
0502:                if (source == null)
0503:                    return "";
0504:                //String result = source.replace('\t', ' ');
0505:                String result = source;
0506:                result = result.trim();
0507:                return result;
0508:            }
0509:
0510:            public static String getWritableText(String input) {
0511:                String result = input.trim();
0512:                StringBuffer buf = new StringBuffer();
0513:                for (int i = 0; i < result.length(); i++) {
0514:                    char c = result.charAt(i);
0515:                    switch (c) {
0516:                    case '<':
0517:                        buf.append("&lt;"); //$NON-NLS-1$
0518:                        break;
0519:                    case '>':
0520:                        buf.append("&gt;"); //$NON-NLS-1$
0521:                        break;
0522:                    case '&':
0523:                        buf.append("&amp;"); //$NON-NLS-1$
0524:                        break;
0525:                    case '\'':
0526:                        buf.append("&apos;"); //$NON-NLS-1$
0527:                        break;
0528:                    case '\"':
0529:                        buf.append("&quot;"); //$NON-NLS-1$
0530:                        break;
0531:                    default:
0532:                        buf.append(c);
0533:                    }
0534:                }
0535:                return buf.toString();
0536:            }
0537:
0538:            public static CommandTarget locateCommandTarget(Composite parent,
0539:                    WidgetIdentifier wid, int line) throws CoreException {
0540:                return locateCommandTarget(parent, wid, null, line);
0541:            }
0542:
0543:            public static CommandTarget locateCommandTarget(Composite parent,
0544:                    WidgetIdentifier wid, ArrayList parents, int line)
0545:                    throws CoreException {
0546:                Shell shell = (Shell) parent;
0547:
0548:                String firstToken = wid.contextPath.segment(0);
0549:                IPath wpath = wid.widgetPath;
0550:                Iterator iter = parents != null ? parents.iterator() : null;
0551:                if (firstToken.equals("menus"))
0552:                    return locateMenuBarItem(shell, wpath, iter, line);
0553:                if (firstToken.equals("popup"))
0554:                    return locatePopupMenuItem(shell, wid, iter, line);
0555:                if (firstToken.equals("toolbar"))
0556:                    return locateToolItem(shell, wpath, line);
0557:                if (firstToken.equals("local-toolbar"))
0558:                    return locateLocalToolItem(shell, wid, line);
0559:                if (firstToken.equals("wizard"))
0560:                    return locateWizardControl(shell, wpath, line);
0561:                if (firstToken.equals("shell"))
0562:                    return locateShellControl(shell, wpath, line);
0563:
0564:                String id = wid.contextPath.segment(1);
0565:                if (firstToken.equals("wizard-page"))
0566:                    return locateWizardPageControl(shell, id, wpath, line);
0567:                if (firstToken.equals("view"))
0568:                    return locateViewControl(shell, id, wpath, line);
0569:                if (firstToken.equals("editor")) {
0570:                    String inputName = wid.contextPath.segment(2);
0571:                    return locateEditorControl(shell, id, inputName, wpath,
0572:                            line);
0573:                }
0574:                return null;
0575:            }
0576:
0577:            private static CommandTarget locateMenuBarItem(Shell shell,
0578:                    IPath path, Iterator parents, int line)
0579:                    throws CoreException {
0580:                MenuItem item = null;
0581:                Object data = shell.getData();
0582:                Menu menuBar = shell.getMenuBar();
0583:
0584:                if (data instanceof  ApplicationWindow && parents != null) {
0585:                    ApplicationWindow window = (ApplicationWindow) data;
0586:                    MenuManager manager = window.getMenuBarManager();
0587:                    item = locateMenuItem(manager, path.toString(), parents,
0588:                            line);
0589:                } else {
0590:                    item = locateMenuItem(menuBar, path.toString(), line);
0591:                }
0592:                if (item != null)
0593:                    return new CommandTarget(item, menuBar);
0594:                throwCoreException("Cannot locate menu item: "
0595:                        + path.toString(), line);
0596:                return null;
0597:            }
0598:
0599:            private static MenuItem locateMenuItem(Menu menu, String id,
0600:                    int line) {
0601:                MenuItem[] items = menu.getItems();
0602:
0603:                for (int i = 0; i < items.length; i++) {
0604:                    MenuItem item = items[i];
0605:
0606:                    Menu submenu = item.getMenu();
0607:                    if (submenu != null) {
0608:                        MenuItem hit = locateMenuItem(submenu, id, line);
0609:                        if (hit != null)
0610:                            return hit;
0611:                    } else {
0612:                        String itemId = getActionId(item);
0613:                        if (itemId != null && id.equals(itemId))
0614:                            return item;
0615:                    }
0616:                }
0617:                return null;
0618:            }
0619:
0620:            private static MenuItem locateMenuItem(MenuManager mng, String id,
0621:                    Iterator parents, int line) {
0622:                IContributionItem[] items = mng.getItems();
0623:
0624:                String parentId = null;
0625:                if (parents.hasNext())
0626:                    parentId = (String) parents.next();
0627:
0628:                for (int i = 0; i < items.length; i++) {
0629:                    IContributionItem citem = items[i];
0630:
0631:                    if (citem instanceof  MenuManager) {
0632:                        MenuManager submenu = (MenuManager) citem;
0633:                        String subId = submenu.getId();
0634:
0635:                        if (subId.equals(parentId)) {
0636:                            // show this menu to force dynamic items
0637:                            // to show
0638:                            Menu menu = submenu.getMenu();
0639:                            forceMenuOpen(null, menu);
0640:
0641:                            MenuItem hit = locateMenuItem(submenu, id, parents,
0642:                                    line);
0643:                            forceMenuClosed(menu);
0644:                            if (hit != null)
0645:                                return hit;
0646:                        }
0647:                    } else {
0648:                        String itemId = getActionId(citem);
0649:                        if (itemId != null && id.equals(itemId)) {
0650:                            MenuItem hit = locateMenuItem(mng.getMenu(), id,
0651:                                    line);
0652:                            if (hit != null)
0653:                                return hit;
0654:                        }
0655:                    }
0656:                }
0657:                return null;
0658:            }
0659:
0660:            private static void forceMenuOpen(Control c, Menu menu) {
0661:                Event e = new Event();
0662:                e.type = SWT.Show;
0663:                e.widget = menu;
0664:                /*
0665:                if (c!=null) {
0666:                	Point midpoint = c.getSize();
0667:                	midpoint.x /= 2;
0668:                	midpoint.y /= 2;
0669:                	midpoint = c.toDisplay(midpoint);
0670:                	menu.setLocation(midpoint);
0671:                }
0672:                 */
0673:                //menu.setVisible(true);
0674:                menu.notifyListeners(e.type, e);
0675:                processDisplayEvents(menu.getDisplay());
0676:            }
0677:
0678:            private static void forceMenuClosed(Menu menu) {
0679:                Event e = new Event();
0680:                e.type = SWT.Hide;
0681:                //menu.setVisible(false);
0682:                e.widget = menu;
0683:                menu.notifyListeners(e.type, e);
0684:                processDisplayEvents(menu.getDisplay());
0685:            }
0686:
0687:            public static void processDisplayEvents(Display display) {
0688:                for (;;) {
0689:                    if (!display.readAndDispatch())
0690:                        break;
0691:                }
0692:            }
0693:
0694:            private static CommandTarget locatePopupMenuItem(Shell shell,
0695:                    WidgetIdentifier wid, Iterator parents, int line)
0696:                    throws CoreException {
0697:                IPath contextPath = wid.contextPath.removeFirstSegments(1);
0698:                IPath wpath = new Path(contextPath.lastSegment());
0699:                contextPath = contextPath.removeLastSegments(1);
0700:                CommandTarget target = locateCommandTarget(shell,
0701:                        new WidgetIdentifier(contextPath, wpath), line);
0702:                if (target != null) {
0703:                    Control control = (Control) target.getWidget();
0704:                    Menu popupMenu = control.getMenu();
0705:                    if (popupMenu != null) {
0706:                        forceMenuOpen(control, popupMenu);
0707:                        MenuItem menuItem = locateMenuItem(popupMenu, wid
0708:                                .getWidgetId(), line);
0709:                        forceMenuClosed(popupMenu);
0710:                        if (menuItem != null) {
0711:                            return new CommandTarget(menuItem, control);
0712:                        }
0713:                    }
0714:                }
0715:                throwCoreException("Cannot locate pop-up menu item: "
0716:                        + wid.getWidgetId(), line);
0717:                return null;
0718:            }
0719:
0720:            private static CommandTarget locateToolItem(Shell shell,
0721:                    IPath path, int line) throws CoreException {
0722:                Object data = shell.getData();
0723:                CommandTarget target = null;
0724:                if (data instanceof  ApplicationWindow) {
0725:                    ApplicationWindow window = (ApplicationWindow) data;
0726:                    CoolBarManager coolMng = window.getCoolBarManager();
0727:                    if (coolMng != null) {
0728:                        target = locateToolItem(coolMng, path.toString(), line);
0729:                    }
0730:                    ToolBarManager toolMng = window.getToolBarManager();
0731:                    if (toolMng != null) {
0732:                        target = locateToolItem(toolMng, path.toString(), line);
0733:                    }
0734:                }
0735:                if (target == null)
0736:                    throwCoreException("Cannot locate pop-up menu item: "
0737:                            + path.toString(), line);
0738:                return target;
0739:            }
0740:
0741:            private static CommandTarget locateToolItem(
0742:                    ICoolBarManager coolMng, String id, int line) {
0743:                IContributionItem[] items = coolMng.getItems();
0744:                for (int i = 0; i < items.length; i++) {
0745:                    if (items[i] instanceof  ToolBarContributionItem) {
0746:                        ToolBarContributionItem item = (ToolBarContributionItem) items[i];
0747:                        IToolBarManager toolMng = item.getToolBarManager();
0748:                        CommandTarget target = locateToolItem(
0749:                                (ToolBarManager) toolMng, id, line);
0750:                        if (target != null)
0751:                            return target;
0752:                    }
0753:                }
0754:                return null;
0755:            }
0756:
0757:            private static CommandTarget locateToolItem(ToolBarManager toolMng,
0758:                    String id, int line) {
0759:                return locateToolItem(toolMng.getControl(), id, line);
0760:            }
0761:
0762:            private static CommandTarget locateToolItem(ToolBar toolBar,
0763:                    String id, int line) {
0764:                ToolItem[] items = toolBar.getItems();
0765:                for (int i = 0; i < items.length; i++) {
0766:                    ToolItem item = items[i];
0767:                    String itemId = getActionId(item);
0768:                    if (itemId != null && itemId.equals(id))
0769:                        return new CommandTarget(item, toolBar);
0770:                }
0771:                return null;
0772:            }
0773:
0774:            private static CommandTarget locateLocalToolItem(Shell shell,
0775:                    WidgetIdentifier wid, int line) throws CoreException {
0776:                IPath wpath = wid.contextPath.removeFirstSegments(1);
0777:                String firstToken = wpath.segment(0);
0778:
0779:                if (firstToken.equals("view")) {
0780:                    String id = wpath.segment(1);
0781:                    IViewPart view = locateView(shell, id, line);
0782:                    if (view != null) {
0783:                        PartPane pane = getPartPane(view);
0784:                        processDisplayEvents(shell.getDisplay());
0785:                        Composite parent = pane.getControl().getParent();
0786:                        Control c = locateVisibleChild((Composite) parent,
0787:                                null, wpath.removeFirstSegments(2));
0788:                        if (c != null) {
0789:                            //TODO bad cast
0790:                            ToolBarManager mng = (ToolBarManager) view
0791:                                    .getViewSite().getActionBars()
0792:                                    .getToolBarManager();
0793:                            CommandTarget target = locateToolItem(mng, wid
0794:                                    .getWidgetId(), line);
0795:                            if (target != null)
0796:                                return target;
0797:                        }
0798:                    }
0799:                }
0800:                throwCoreException("Cannot locate local tool bar item: "
0801:                        + wid.getFullyQualifiedId().toString(), line);
0802:                return null;
0803:            }
0804:
0805:            private static WizardCommandTarget locateWizardControl(Shell shell,
0806:                    IPath wpath, int line) throws CoreException {
0807:                WizardDialog wdialog = (WizardDialog) shell.getData();
0808:                IWizardPage page = wdialog.getCurrentPage();
0809:                Composite pparent = (Composite) page.getControl();
0810:                Control control = locateVisibleChild(shell, pparent, wpath);
0811:                if (control == null)
0812:                    throwCoreException("Cannot locate wizard control: "
0813:                            + wpath.toString(), line);
0814:                if (control.isDisposed())
0815:                    throwCoreException("Wizard control is disposed: "
0816:                            + wpath.toString(), line);
0817:                return new WizardCommandTarget(control, wdialog);
0818:            }
0819:
0820:            private static WindowCommandTarget locateShellControl(Shell shell,
0821:                    IPath wpath, int line) throws CoreException {
0822:                Window window = (Window) shell.getData();
0823:                Control control = locateVisibleChild(shell, null, wpath);
0824:                if (control == null)
0825:                    throwCoreException("Cannot locate shell control: "
0826:                            + wpath.toString(), line);
0827:                if (control.isDisposed())
0828:                    throwCoreException("Shell control is disposed: "
0829:                            + wpath.toString(), line);
0830:                return new WindowCommandTarget(control, window);
0831:            }
0832:
0833:            private static WizardCommandTarget locateWizardPageControl(
0834:                    Shell shell, String id, IPath wpath, int line)
0835:                    throws CoreException {
0836:                Control control = null;
0837:                Object data = shell.getData();
0838:                if (data instanceof  WizardDialog) {
0839:                    WizardDialog wdialog = (WizardDialog) data;
0840:                    IWizardPage page = wdialog.getCurrentPage();
0841:                    String pname = page.getName();
0842:                    // assert page
0843:                    if (pname.equals(id) == false)
0844:                        throwCoreException("Unexpected wizard page: " + pname
0845:                                + ", expected " + id, line);
0846:                    Composite pparent = (Composite) page.getControl();
0847:                    control = locateVisibleChild(pparent, null, wpath);
0848:                    if (control != null)
0849:                        return new WizardCommandTarget(control, wdialog);
0850:                }
0851:                if (control == null)
0852:                    throwCoreException("Cannot locate wizard page control: "
0853:                            + wpath.toString(), line);
0854:                return null;
0855:            }
0856:
0857:            private static IViewPart locateView(Shell shell, String id, int line)
0858:                    throws CoreException {
0859:                Object data = shell.getData();
0860:
0861:                if (data instanceof  IWorkbenchWindow) {
0862:                    IWorkbenchWindow window = (IWorkbenchWindow) data;
0863:                    IWorkbenchPage page = window.getActivePage();
0864:                    if (page != null) {
0865:                        IViewPart view = page.showView(id);
0866:                        return view;
0867:                    }
0868:                }
0869:                throwCoreException("Cannot locate view: " + id, line);
0870:                return null;
0871:            }
0872:
0873:            private static PartPane getPartPane(IViewPart part) {
0874:                IWorkbenchPartSite site = part.getSite();
0875:                PartPane pane = ((PartSite) site).getPane();
0876:                return pane;
0877:            }
0878:
0879:            private static ViewCommandTarget locateViewControl(Shell shell,
0880:                    String id, IPath wpath, int line) throws CoreException {
0881:                Control control = null;
0882:
0883:                IViewPart view = locateView(shell, id, line);
0884:                if (view != null) {
0885:                    PartPane pane = getPartPane(view);
0886:                    Control c = pane.getControl();
0887:                    control = locateVisibleChild((Composite) c, null, wpath);
0888:                    if (control != null) {
0889:                        return new ViewCommandTarget(control, view);
0890:                    }
0891:                }
0892:                throwCoreException("Cannot locate view control: "
0893:                        + wpath.toString(), line);
0894:                return null;
0895:            }
0896:
0897:            private static EditorCommandTarget locateEditorControl(Shell shell,
0898:                    String id, String inputName, IPath wpath, int line)
0899:                    throws CoreException {
0900:                Control control = null;
0901:
0902:                Object data = shell.getData();
0903:
0904:                if (data instanceof  IWorkbenchWindow) {
0905:                    IWorkbenchWindow window = (IWorkbenchWindow) data;
0906:                    IWorkbenchPage page = window.getActivePage();
0907:                    if (page != null) {
0908:                        IEditorReference[] erefs = page.getEditorReferences();
0909:                        IEditorPart editor = null;
0910:                        for (int i = 0; i < erefs.length; i++) {
0911:                            IEditorReference eref = erefs[i];
0912:                            if (eref.getId().equals(id)) {
0913:                                // check the input
0914:                                IEditorPart part = eref.getEditor(true);
0915:                                if (part.getEditorInput().getName().equals(
0916:                                        inputName)) {
0917:                                    editor = part;
0918:                                    break;
0919:                                }
0920:                            }
0921:                        }
0922:                        if (editor != null) {
0923:                            IEditorSite site = editor.getEditorSite();
0924:                            PartPane pane = ((EditorSite) site).getPane();
0925:                            Control c = pane.getControl();
0926:                            control = locateVisibleChild((Composite) c, null,
0927:                                    wpath);
0928:                            if (control != null) {
0929:                                return new EditorCommandTarget(control, editor);
0930:                            }
0931:                        }
0932:                    }
0933:                }
0934:                if (control == null)
0935:                    throwCoreException("Cannot locate editor control: "
0936:                            + wpath.toString(), line);
0937:                return null;
0938:            }
0939:
0940:            private static Control locateVisibleChild(Composite parent,
0941:                    Composite skip, IPath wpath) {
0942:                int[] counter = new int[1];
0943:                counter[0] = 0;
0944:                String wid = wpath.toString();
0945:                int sloc = wid.lastIndexOf('#');
0946:                if (sloc == -1)
0947:                    return null;
0948:                String wclassName = wid.substring(0, sloc);
0949:                return locateVisibleChild(parent, skip, wid, wclassName,
0950:                        counter);
0951:            }
0952:
0953:            private static Control locateVisibleChild(Composite parent,
0954:                    Composite skip, String id, String wclassName, int[] counter) {
0955:                Control[] children = parent.getChildren();
0956:                for (int i = 0; i < children.length; i++) {
0957:                    Control child = children[i];
0958:
0959:                    if (child.getClass().getName().equals(wclassName)) {
0960:                        // same type - increment counter
0961:                        if (child.isVisible() == false)
0962:                            continue;
0963:                        counter[0]++;
0964:                        String cid = getControlId(child, counter[0]);
0965:                        if (cid.equals(id)) {
0966:                            // bingo
0967:                            return child;
0968:                        }
0969:                    } else if (child instanceof  Composite) {
0970:                        if (skip != null && child.equals(skip))
0971:                            continue;
0972:                        if (!child.isVisible())
0973:                            continue;
0974:                        Control c = locateVisibleChild((Composite) child, skip,
0975:                                id, wclassName, counter);
0976:                        if (c != null)
0977:                            return c;
0978:                    }
0979:                }
0980:                return null;
0981:            }
0982:
0983:            public static void throwCoreException(String message, int line)
0984:                    throws CoreException {
0985:                throwCoreException(message, line, null);
0986:            }
0987:
0988:            public static void throwCoreException(String message, int line,
0989:                    Throwable t) throws CoreException {
0990:                if (line > 0)
0991:                    message = "Line " + line + ": " + message;
0992:                Status s = new Status(IStatus.ERROR, "org.eclipse.ui.macro",
0993:                        IStatus.OK, message, t);
0994:                throw new CoreException(s);
0995:            }
0996:
0997:            public static java.util.List generatePossibleKeyStrokes(Event event) {
0998:                final java.util.List keyStrokes = new ArrayList(3);
0999:
1000:                /*
1001:                 * If this is not a keyboard event, then there are no key strokes. This
1002:                 * can happen if we are listening to focus traversal events.
1003:                 */
1004:                if ((event.stateMask == 0) && (event.keyCode == 0)
1005:                        && (event.character == 0)) {
1006:                    return keyStrokes;
1007:                }
1008:
1009:                // Add each unique key stroke to the list for consideration.
1010:                final int firstAccelerator = SWTKeySupport
1011:                        .convertEventToUnmodifiedAccelerator(event);
1012:                keyStrokes.add(SWTKeySupport
1013:                        .convertAcceleratorToKeyStroke(firstAccelerator));
1014:
1015:                // We shouldn't allow delete to undergo shift resolution.
1016:                if (event.character == SWT.DEL) {
1017:                    return keyStrokes;
1018:                }
1019:
1020:                final int secondAccelerator = SWTKeySupport
1021:                        .convertEventToUnshiftedModifiedAccelerator(event);
1022:                if (secondAccelerator != firstAccelerator) {
1023:                    keyStrokes.add(SWTKeySupport
1024:                            .convertAcceleratorToKeyStroke(secondAccelerator));
1025:                }
1026:
1027:                final int thirdAccelerator = SWTKeySupport
1028:                        .convertEventToModifiedAccelerator(event);
1029:                if ((thirdAccelerator != secondAccelerator)
1030:                        && (thirdAccelerator != firstAccelerator)) {
1031:                    keyStrokes.add(SWTKeySupport
1032:                            .convertAcceleratorToKeyStroke(thirdAccelerator));
1033:                }
1034:
1035:                return keyStrokes;
1036:            }
1037:        }
w_w_w___.___j___av___a___2___s.___co_m__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.