Source Code Cross Referenced for ExportPackageSection.java in  » IDE-Eclipse » Eclipse-plug-in-development » org » eclipse » pde » internal » ui » editor » plugin » 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.editor.plugin 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Copyright (c) 2005, 2007 IBM Corporation and others.
003:         * All rights reserved. This program and the accompanying materials
004:         * are made available under the terms of the Eclipse Public License v1.0
005:         * which accompanies this distribution, and is available at
006:         * http://www.eclipse.org/legal/epl-v10.html
007:         *
008:         * Contributors:
009:         *     IBM Corporation - initial API and implementation
010:         *     Remy Chi Jian Suen <remy.suen@gmail.com> - bug 200756
011:         *     Joern Dinkla <devnull@dinkla.com> - bug 200757
012:         *******************************************************************************/package org.eclipse.pde.internal.ui.editor.plugin;
013:
014:        import java.util.ArrayList;
015:        import java.util.Collection;
016:        import java.util.HashMap;
017:        import java.util.Vector;
018:
019:        import org.eclipse.core.resources.IProject;
020:        import org.eclipse.core.resources.IResource;
021:        import org.eclipse.core.runtime.CoreException;
022:        import org.eclipse.jdt.core.IJavaProject;
023:        import org.eclipse.jdt.core.IPackageFragment;
024:        import org.eclipse.jdt.core.JavaCore;
025:        import org.eclipse.jdt.core.JavaModelException;
026:        import org.eclipse.jdt.ui.JavaElementLabelProvider;
027:        import org.eclipse.jdt.ui.JavaUI;
028:        import org.eclipse.jdt.ui.actions.FindReferencesAction;
029:        import org.eclipse.jdt.ui.actions.ShowInPackageViewAction;
030:        import org.eclipse.jface.action.Action;
031:        import org.eclipse.jface.action.IMenuManager;
032:        import org.eclipse.jface.action.Separator;
033:        import org.eclipse.jface.viewers.ILabelProvider;
034:        import org.eclipse.jface.viewers.ISelection;
035:        import org.eclipse.jface.viewers.IStructuredSelection;
036:        import org.eclipse.jface.viewers.StructuredSelection;
037:        import org.eclipse.jface.viewers.TableViewer;
038:        import org.eclipse.jface.viewers.Viewer;
039:        import org.eclipse.jface.viewers.ViewerComparator;
040:        import org.eclipse.jface.window.Window;
041:        import org.eclipse.pde.core.IBaseModel;
042:        import org.eclipse.pde.core.IModelChangedEvent;
043:        import org.eclipse.pde.core.IModelChangedListener;
044:        import org.eclipse.pde.core.plugin.IPluginModelBase;
045:        import org.eclipse.pde.internal.core.ICoreConstants;
046:        import org.eclipse.pde.internal.core.bundle.BundlePluginBase;
047:        import org.eclipse.pde.internal.core.ibundle.IBundle;
048:        import org.eclipse.pde.internal.core.ibundle.IBundleModel;
049:        import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
050:        import org.eclipse.pde.internal.core.text.bundle.Bundle;
051:        import org.eclipse.pde.internal.core.text.bundle.ExportPackageHeader;
052:        import org.eclipse.pde.internal.core.text.bundle.ExportPackageObject;
053:        import org.eclipse.pde.internal.core.text.bundle.PackageObject;
054:        import org.eclipse.pde.internal.core.util.PDEJavaHelper;
055:        import org.eclipse.pde.internal.ui.PDEPlugin;
056:        import org.eclipse.pde.internal.ui.PDEUIMessages;
057:        import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
058:        import org.eclipse.pde.internal.ui.editor.PDEFormPage;
059:        import org.eclipse.pde.internal.ui.editor.TableSection;
060:        import org.eclipse.pde.internal.ui.editor.context.InputContextManager;
061:        import org.eclipse.pde.internal.ui.elements.DefaultTableProvider;
062:        import org.eclipse.pde.internal.ui.parts.ConditionalListSelectionDialog;
063:        import org.eclipse.pde.internal.ui.parts.TablePart;
064:        import org.eclipse.pde.internal.ui.search.dependencies.CalculateUsesAction;
065:        import org.eclipse.pde.internal.ui.util.SWTUtil;
066:        import org.eclipse.search.ui.NewSearchUI;
067:        import org.eclipse.swt.SWT;
068:        import org.eclipse.swt.custom.BusyIndicator;
069:        import org.eclipse.swt.layout.GridData;
070:        import org.eclipse.swt.widgets.Composite;
071:        import org.eclipse.swt.widgets.Display;
072:        import org.eclipse.swt.widgets.Table;
073:        import org.eclipse.ui.IViewPart;
074:        import org.eclipse.ui.PartInitException;
075:        import org.eclipse.ui.actions.ActionFactory;
076:        import org.eclipse.ui.forms.widgets.FormToolkit;
077:        import org.eclipse.ui.forms.widgets.Section;
078:        import org.osgi.framework.Constants;
079:
080:        public class ExportPackageSection extends TableSection implements 
081:                IModelChangedListener {
082:
083:            private static final int ADD_INDEX = 0;
084:            private static final int REMOVE_INDEX = 1;
085:            private static final int PROPERTIES_INDEX = 2;
086:            private static final int CALCULATE_USE_INDEX = 3;
087:
088:            class ExportPackageContentProvider extends DefaultTableProvider {
089:                public Object[] getElements(Object parent) {
090:                    if (fHeader == null) {
091:                        Bundle bundle = (Bundle) getBundle();
092:                        fHeader = (ExportPackageHeader) bundle
093:                                .getManifestHeader(getExportedPackageHeader());
094:                    }
095:                    return fHeader == null ? new Object[0] : fHeader
096:                            .getPackages();
097:                }
098:            }
099:
100:            private TableViewer fPackageViewer;
101:
102:            private Action fAddAction;
103:            private Action fGoToAction;
104:            private Action fRemoveAction;
105:            private Action fPropertiesAction;
106:            private ExportPackageHeader fHeader;
107:
108:            public ExportPackageSection(PDEFormPage page, Composite parent) {
109:                super (page, parent, Section.DESCRIPTION, new String[] {
110:                        PDEUIMessages.ExportPackageSection_add,
111:                        PDEUIMessages.ExportPackageSection_remove,
112:                        PDEUIMessages.ExportPackageSection_properties,
113:                        PDEUIMessages.ExportPackageSection_uses });
114:            }
115:
116:            private boolean isFragment() {
117:                IPluginModelBase model = (IPluginModelBase) getPage()
118:                        .getPDEEditor().getAggregateModel();
119:                return model.isFragmentModel();
120:            }
121:
122:            /*
123:             * (non-Javadoc)
124:             * 
125:             * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section,
126:             *      org.eclipse.ui.forms.widgets.FormToolkit)
127:             */
128:            protected void createClient(Section section, FormToolkit toolkit) {
129:                section.setText(PDEUIMessages.ExportPackageSection_title);
130:                if (isFragment())
131:                    section
132:                            .setDescription(PDEUIMessages.ExportPackageSection_descFragment);
133:                else
134:                    section
135:                            .setDescription(PDEUIMessages.ExportPackageSection_desc);
136:
137:                Composite container = createClientContainer(section, 2, toolkit);
138:                createViewerPartControl(container, SWT.MULTI, 2, toolkit);
139:                TablePart tablePart = getTablePart();
140:                fPackageViewer = tablePart.getTableViewer();
141:                fPackageViewer
142:                        .setContentProvider(new ExportPackageContentProvider());
143:                fPackageViewer.setLabelProvider(PDEPlugin.getDefault()
144:                        .getLabelProvider());
145:                fPackageViewer.setComparator(new ViewerComparator() {
146:                    public int compare(Viewer viewer, Object e1, Object e2) {
147:                        String s1 = e1.toString();
148:                        String s2 = e2.toString();
149:                        if (s1.indexOf(" ") != -1) //$NON-NLS-1$
150:                            s1 = s1.substring(0, s1.indexOf(" ")); //$NON-NLS-1$
151:                        if (s2.indexOf(" ") != -1) //$NON-NLS-1$
152:                            s2 = s2.substring(0, s2.indexOf(" ")); //$NON-NLS-1$
153:                        return super .compare(viewer, s1, s2);
154:                    }
155:                });
156:                toolkit.paintBordersFor(container);
157:                section.setClient(container);
158:                GridData gd = new GridData(GridData.FILL_BOTH);
159:                if (((ManifestEditor) getPage().getEditor()).isEquinox()) {
160:                    gd.verticalSpan = 2;
161:                    gd.minimumWidth = 300;
162:                }
163:                section.setLayout(FormLayoutFactory.createClearGridLayout(
164:                        false, 1));
165:                section.setLayoutData(gd);
166:                makeActions();
167:
168:                IBundleModel model = getBundleModel();
169:                fPackageViewer.setInput(model);
170:                model.addModelChangedListener(this );
171:                updateButtons();
172:            }
173:
174:            public boolean doGlobalAction(String actionId) {
175:
176:                if (!isEditable()) {
177:                    return false;
178:                }
179:
180:                if (actionId.equals(ActionFactory.DELETE.getId())) {
181:                    handleRemove();
182:                    return true;
183:                }
184:                if (actionId.equals(ActionFactory.CUT.getId())) {
185:                    // delete here and let the editor transfer
186:                    // the selection to the clipboard
187:                    handleRemove();
188:                    return false;
189:                }
190:                if (actionId.equals(ActionFactory.PASTE.getId())) {
191:                    doPaste();
192:                    return true;
193:                }
194:                return false;
195:            }
196:
197:            public void dispose() {
198:                IBundleModel model = getBundleModel();
199:                if (model != null)
200:                    model.removeModelChangedListener(this );
201:                super .dispose();
202:            }
203:
204:            /* (non-Javadoc)
205:             * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#canPaste(java.lang.Object, java.lang.Object[])
206:             */
207:            protected boolean canPaste(Object targetObject,
208:                    Object[] sourceObjects) {
209:                HashMap currentPackageFragments = null;
210:                // Only export package objects that represent existing package 
211:                // fragments within the Java project that this plugin.xml is stored
212:                // can be pasted
213:                for (int i = 0; i < sourceObjects.length; i++) {
214:                    // Only export package objects are allowed
215:                    if ((sourceObjects[i] instanceof  ExportPackageObject) == false) {
216:                        return false;
217:                    }
218:                    // Get the package fragments that are allowed and store them to 
219:                    // assist in searching
220:                    if (currentPackageFragments == null) {
221:                        currentPackageFragments = createCurrentExportPackageMap();
222:                    }
223:                    // Only export packages that are in the list of allowed package
224:                    // fragments are allowed
225:                    ExportPackageObject exportPackageObject = (ExportPackageObject) sourceObjects[i];
226:                    if (currentPackageFragments.containsKey(exportPackageObject
227:                            .getName()) == false) {
228:                        return false;
229:                    }
230:                }
231:                return true;
232:            }
233:
234:            /**
235:             * @return
236:             */
237:            private boolean canAddExportedPackages() {
238:                // Ensure model is editable
239:                if (isEditable() == false) {
240:                    return false;
241:                }
242:                // Get the model
243:                IPluginModelBase model = getModel();
244:                // Ensure model is defined
245:                if (model == null) {
246:                    return false;
247:                }
248:                // Get the underlying resource
249:                IResource resource = model.getUnderlyingResource();
250:                // Ensure resource is defined
251:                if (resource == null) {
252:                    return false;
253:                }
254:                // Get the project
255:                IProject project = resource.getProject();
256:                // Ensure the project is defined
257:                if (project == null) {
258:                    return false;
259:                }
260:                // Ensure the project is a Java project
261:                try {
262:                    if (project.hasNature(JavaCore.NATURE_ID) == false) {
263:                        return false;
264:                    }
265:                } catch (CoreException e) {
266:                    return false;
267:                }
268:                return true;
269:            }
270:
271:            /**
272:             * @return
273:             */
274:            private HashMap createCurrentExportPackageMap() {
275:                // Dummy hash map created in order to return a defined but empty map
276:                HashMap packageFragments = new HashMap(0);
277:                // Get the model
278:                IPluginModelBase model = getModel();
279:                // Ensure model is defined
280:                if (model == null) {
281:                    return packageFragments;
282:                }
283:                // Get the underlying resource
284:                IResource resource = model.getUnderlyingResource();
285:                // Ensure resource is defined
286:                if (resource == null) {
287:                    return packageFragments;
288:                }
289:                // Get the project
290:                IProject project = resource.getProject();
291:                // Ensure the project is defined
292:                if (project == null) {
293:                    return packageFragments;
294:                }
295:                // Ensure the project is a Java project
296:                try {
297:                    if (project.hasNature(JavaCore.NATURE_ID) == false) {
298:                        return packageFragments;
299:                    }
300:                } catch (CoreException e) {
301:                    return packageFragments;
302:                }
303:                // Get the Java project
304:                IJavaProject javaProject = JavaCore.create(project);
305:                // Ensure the Java project is defined
306:                if (javaProject == null) {
307:                    return packageFragments;
308:                }
309:                // Get the current packages associated with the export package header
310:                Vector currentExportPackages = null;
311:                if (fHeader == null) {
312:                    currentExportPackages = new Vector();
313:                } else {
314:                    currentExportPackages = fHeader.getPackageNames();
315:                }
316:                // Get a hashtable of all the package fragments that are allowed to
317:                // be added to the current export package header
318:                // Generally, all package fragments contained in the same Java project
319:                // as the plugin manifest file
320:                // No duplicates are allowed and all current packages are excluded
321:                return PDEJavaHelper.getPackageFragmentsHash(javaProject,
322:                        currentExportPackages, allowJavaPackages());
323:            }
324:
325:            /**
326:             * @return
327:             */
328:            private IPluginModelBase getModel() {
329:                return (IPluginModelBase) getPage().getModel();
330:            }
331:
332:            /**
333:             * @return
334:             */
335:            private boolean allowJavaPackages() {
336:                return "true".equals(getBundle().getHeader(ICoreConstants.ECLIPSE_JREBUNDLE)); //$NON-NLS-1$
337:            }
338:
339:            /* (non-Javadoc)
340:             * @see org.eclipse.pde.internal.ui.editor.StructuredViewerSection#doPaste()
341:             */
342:            protected void doPaste(Object targetObject, Object[] sourceObjects) {
343:                // Get the model
344:                IBundleModel model = getBundleModel();
345:                // Ensure the model is defined
346:                if (model == null) {
347:                    return;
348:                }
349:                // Get the bundle
350:                IBundle bundle = model.getBundle();
351:                // Paste all source objects
352:                for (int i = 0; i < sourceObjects.length; i++) {
353:                    Object sourceObject = sourceObjects[i];
354:                    if (sourceObject instanceof  ExportPackageObject) {
355:                        ExportPackageObject exportPackageObject = (ExportPackageObject) sourceObject;
356:                        // Export package object
357:                        // Adjust all the source object transient field values to
358:                        // acceptable values
359:                        exportPackageObject.reconnect(model, fHeader,
360:                                getVersionAttribute());
361:                        // Add the object to the header
362:                        if (fHeader == null) {
363:                            // Export package header not defined yet
364:                            // Define one
365:                            // Value will get inserted into a new export package object
366:                            // created by a factory
367:                            // Value needs to be empty string so no export package
368:                            // object is created as the initial value
369:                            bundle.setHeader(getExportedPackageHeader(), ""); //$NON-NLS-1$
370:                        }
371:                        // Add the export package to the header
372:                        fHeader.addPackage(exportPackageObject);
373:                    }
374:                }
375:            }
376:
377:            protected void selectionChanged(IStructuredSelection sel) {
378:                getPage().getPDEEditor().setSelection(sel);
379:                updateButtons();
380:            }
381:
382:            private void updateButtons() {
383:                Object[] selected = ((IStructuredSelection) fPackageViewer
384:                        .getSelection()).toArray();
385:
386:                TablePart tablePart = getTablePart();
387:                tablePart.setButtonEnabled(ADD_INDEX, canAddExportedPackages());
388:                tablePart.setButtonEnabled(REMOVE_INDEX, isEditable()
389:                        && selected.length > 0);
390:                tablePart.setButtonEnabled(PROPERTIES_INDEX,
391:                        shouldEnableProperties(selected));
392:                tablePart.setButtonEnabled(CALCULATE_USE_INDEX, isEditable()
393:                        && fPackageViewer.getTable().getItemCount() > 0);
394:
395:            }
396:
397:            private boolean shouldEnableProperties(Object[] selected) {
398:                if (selected.length == 0)
399:                    return false;
400:                if (selected.length == 1)
401:                    return true;
402:
403:                String version = ((ExportPackageObject) selected[0])
404:                        .getVersion();
405:                for (int i = 1; i < selected.length; i++) {
406:                    ExportPackageObject object = (ExportPackageObject) selected[i];
407:                    if (version == null) {
408:                        if (object.getVersion() != null) {
409:                            return false;
410:                        }
411:                    } else if (!version.equals(object.getVersion())) {
412:                        return false;
413:                    }
414:                }
415:                return true;
416:            }
417:
418:            protected void handleDoubleClick(IStructuredSelection selection) {
419:                handleGoToPackage(selection);
420:            }
421:
422:            private IPackageFragment getPackageFragment(ISelection sel) {
423:                if (sel instanceof  IStructuredSelection) {
424:                    IStructuredSelection selection = (IStructuredSelection) sel;
425:                    if (selection.size() != 1)
426:                        return null;
427:
428:                    IBaseModel model = getPage().getModel();
429:                    if (!(model instanceof  IPluginModelBase))
430:                        return null;
431:
432:                    return PDEJavaHelper.getPackageFragment(
433:                            ((PackageObject) selection.getFirstElement())
434:                                    .getName(), ((IPluginModelBase) model)
435:                                    .getPluginBase().getId(), getPage()
436:                                    .getPDEEditor().getCommonProject());
437:                }
438:                return null;
439:            }
440:
441:            private void handleGoToPackage(ISelection selection) {
442:                IPackageFragment frag = getPackageFragment(selection);
443:                if (frag != null)
444:                    try {
445:                        IViewPart part = PDEPlugin.getActivePage().showView(
446:                                JavaUI.ID_PACKAGES);
447:                        ShowInPackageViewAction action = new ShowInPackageViewAction(
448:                                part.getSite());
449:                        action.run(frag);
450:                    } catch (PartInitException e) {
451:                    }
452:            }
453:
454:            protected void buttonSelected(int index) {
455:                switch (index) {
456:                case ADD_INDEX:
457:                    handleAdd();
458:                    break;
459:                case REMOVE_INDEX:
460:                    handleRemove();
461:                    break;
462:                case PROPERTIES_INDEX:
463:                    handleOpenProperties();
464:                    break;
465:                case CALCULATE_USE_INDEX:
466:                    calculateUses();
467:                }
468:            }
469:
470:            private void handleOpenProperties() {
471:                Object[] selected = ((IStructuredSelection) fPackageViewer
472:                        .getSelection()).toArray();
473:                ExportPackageObject first = (ExportPackageObject) selected[0];
474:                DependencyPropertiesDialog dialog = new DependencyPropertiesDialog(
475:                        isEditable(), first);
476:                dialog.create();
477:                SWTUtil.setDialogSize(dialog, 400, -1);
478:                if (selected.length == 1)
479:                    dialog.setTitle(((ExportPackageObject) selected[0])
480:                            .getName());
481:                else
482:                    dialog.setTitle(PDEUIMessages.ExportPackageSection_props);
483:                if (dialog.open() == Window.OK && isEditable()) {
484:                    String newVersion = dialog.getVersion();
485:                    for (int i = 0; i < selected.length; i++) {
486:                        ExportPackageObject object = (ExportPackageObject) selected[i];
487:                        if (!newVersion.equals(object.getVersion()))
488:                            object.setVersion(newVersion);
489:                    }
490:                }
491:            }
492:
493:            private void handleRemove() {
494:                Object[] removed = ((IStructuredSelection) fPackageViewer
495:                        .getSelection()).toArray();
496:                for (int i = 0; i < removed.length; i++) {
497:                    fHeader.removePackage((PackageObject) removed[i]);
498:                }
499:            }
500:
501:            private void handleAdd() {
502:                IPluginModelBase model = (IPluginModelBase) getPage()
503:                        .getModel();
504:                final IProject project = model.getUnderlyingResource()
505:                        .getProject();
506:                try {
507:                    if (project.hasNature(JavaCore.NATURE_ID)) {
508:                        ILabelProvider labelProvider = new JavaElementLabelProvider();
509:                        final ConditionalListSelectionDialog dialog = new ConditionalListSelectionDialog(
510:                                PDEPlugin.getActiveWorkbenchShell(),
511:                                labelProvider,
512:                                PDEUIMessages.ExportPackageSection_dialogButtonLabel);
513:                        final Collection pckgs = fHeader == null ? new Vector()
514:                                : fHeader.getPackageNames();
515:                        final boolean allowJava = "true".equals(getBundle().getHeader(ICoreConstants.ECLIPSE_JREBUNDLE)); //$NON-NLS-1$
516:                        Runnable runnable = new Runnable() {
517:                            public void run() {
518:                                ArrayList elements = new ArrayList();
519:                                ArrayList conditional = new ArrayList();
520:                                IPackageFragment[] fragments = PDEJavaHelper
521:                                        .getPackageFragments(JavaCore
522:                                                .create(project), pckgs,
523:                                                allowJava);
524:                                for (int i = 0; i < fragments.length; i++) {
525:                                    try {
526:                                        if (fragments[i]
527:                                                .containsJavaResources()) {
528:                                            elements.add(fragments[i]);
529:                                        } else {
530:                                            conditional.add(fragments[i]);
531:                                        }
532:                                    } catch (JavaModelException e) {
533:                                    }
534:                                }
535:                                dialog.setElements(elements.toArray());
536:                                dialog.setConditionalElements(conditional
537:                                        .toArray());
538:                                dialog.setMultipleSelection(true);
539:                                dialog
540:                                        .setMessage(PDEUIMessages.PackageSelectionDialog_label);
541:                                dialog
542:                                        .setTitle(PDEUIMessages.ExportPackageSection_title);
543:                                dialog.create();
544:                                SWTUtil.setDialogSize(dialog, 400, 500);
545:                            }
546:                        };
547:                        BusyIndicator.showWhile(Display.getCurrent(), runnable);
548:                        if (dialog.open() == Window.OK) {
549:                            Object[] selected = dialog.getResult();
550:                            if (fHeader != null) {
551:                                for (int i = 0; i < selected.length; i++) {
552:                                    IPackageFragment candidate = (IPackageFragment) selected[i];
553:                                    fHeader.addPackage(new ExportPackageObject(
554:                                            fHeader, candidate,
555:                                            getVersionAttribute()));
556:                                }
557:                            } else {
558:                                getBundle().setHeader(
559:                                        getExportedPackageHeader(),
560:                                        getValue(selected));
561:                                // the way events get triggered, updateButtons isn't called
562:                                if (selected.length > 0)
563:                                    getTablePart().setButtonEnabled(
564:                                            CALCULATE_USE_INDEX, true);
565:                            }
566:                        }
567:                        labelProvider.dispose();
568:                    }
569:                } catch (CoreException e) {
570:                }
571:            }
572:
573:            private String getValue(Object[] objects) {
574:                StringBuffer buffer = new StringBuffer();
575:                for (int i = 0; i < objects.length; i++) {
576:                    IPackageFragment fragment = (IPackageFragment) objects[i];
577:                    if (buffer.length() > 0)
578:                        buffer.append("," + getLineDelimiter() + " "); //$NON-NLS-1$ //$NON-NLS-2$
579:                    buffer.append(fragment.getElementName());
580:                }
581:                return buffer.toString();
582:            }
583:
584:            public void modelChanged(IModelChangedEvent event) {
585:                if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
586:                    fHeader = null;
587:                    markStale();
588:                    return;
589:                }
590:
591:                if (getExportedPackageHeader().equals(
592:                        event.getChangedProperty())) {
593:                    refresh();
594:                    // Bug 171896
595:                    // Since the model sends a CHANGE event instead of
596:                    // an INSERT event on the very first addition to the empty table
597:                    // Selection should fire here to take this first insertion into account
598:                    Object lastElement = fPackageViewer
599:                            .getElementAt(fPackageViewer.getTable()
600:                                    .getItemCount() - 1);
601:                    if (lastElement != null) {
602:                        fPackageViewer.setSelection(new StructuredSelection(
603:                                lastElement));
604:                    }
605:
606:                    return;
607:                }
608:
609:                Object[] objects = event.getChangedObjects();
610:                for (int i = 0; i < objects.length; i++) {
611:                    if (objects[i] instanceof  ExportPackageObject) {
612:                        ExportPackageObject object = (ExportPackageObject) objects[i];
613:                        switch (event.getChangeType()) {
614:                        case IModelChangedEvent.INSERT:
615:                            fPackageViewer.add(object);
616:                            fPackageViewer
617:                                    .setSelection(new StructuredSelection(
618:                                            object));
619:                            fPackageViewer.getTable().setFocus();
620:                            break;
621:                        case IModelChangedEvent.REMOVE:
622:                            Table table = fPackageViewer.getTable();
623:                            int index = table.getSelectionIndex();
624:                            fPackageViewer.remove(object);
625:                            table
626:                                    .setSelection(index < table.getItemCount() ? index
627:                                            : table.getItemCount() - 1);
628:                            break;
629:                        default:
630:                            fPackageViewer.refresh(object);
631:                        }
632:                    }
633:                }
634:            }
635:
636:            public void refresh() {
637:                fPackageViewer.refresh();
638:                super .refresh();
639:            }
640:
641:            private void makeActions() {
642:                fAddAction = new Action(PDEUIMessages.RequiresSection_add) {
643:                    public void run() {
644:                        handleAdd();
645:                    }
646:                };
647:                fAddAction.setEnabled(isEditable());
648:                fGoToAction = new Action(
649:                        PDEUIMessages.ImportPackageSection_goToPackage) {
650:                    public void run() {
651:                        handleGoToPackage(fPackageViewer.getSelection());
652:                    }
653:                };
654:                fRemoveAction = new Action(PDEUIMessages.RequiresSection_delete) {
655:                    public void run() {
656:                        handleRemove();
657:                    }
658:                };
659:                fRemoveAction.setEnabled(isEditable());
660:
661:                fPropertiesAction = new Action(
662:                        PDEUIMessages.ExportPackageSection_propertyAction) {
663:                    public void run() {
664:                        handleOpenProperties();
665:                    }
666:                };
667:            }
668:
669:            protected void fillContextMenu(IMenuManager manager) {
670:                ISelection selection = fPackageViewer.getSelection();
671:                manager.add(fAddAction);
672:                boolean singleSelection = selection instanceof  IStructuredSelection
673:                        && ((IStructuredSelection) selection).size() == 1;
674:                if (singleSelection)
675:                    manager.add(fGoToAction);
676:                manager.add(new Separator());
677:                if (!selection.isEmpty())
678:                    manager.add(fRemoveAction);
679:                getPage().getPDEEditor().getContributor()
680:                        .contextMenuAboutToShow(manager);
681:                if (singleSelection)
682:                    manager.add(new Action(
683:                            PDEUIMessages.ExportPackageSection_findReferences) {
684:                        public void run() {
685:                            doSearch(fPackageViewer.getSelection());
686:                        }
687:                    });
688:                if (shouldEnableProperties(((IStructuredSelection) fPackageViewer
689:                        .getSelection()).toArray())) {
690:                    manager.add(new Separator());
691:                    manager.add(fPropertiesAction);
692:                }
693:            }
694:
695:            private void doSearch(ISelection sel) {
696:                IPackageFragment frag = getPackageFragment(sel);
697:                if (frag != null) {
698:                    FindReferencesAction action = new FindReferencesAction(
699:                            getPage().getEditorSite());
700:                    action.run(frag);
701:                } else if (sel instanceof  IStructuredSelection) {
702:                    IStructuredSelection selection = (IStructuredSelection) sel;
703:                    PackageObject exportObject = (PackageObject) selection
704:                            .getFirstElement();
705:                    NewSearchUI.runQueryInBackground(new BlankQuery(
706:                            exportObject));
707:                }
708:            }
709:
710:            private BundleInputContext getBundleContext() {
711:                InputContextManager manager = getPage().getPDEEditor()
712:                        .getContextManager();
713:                return (BundleInputContext) manager
714:                        .findContext(BundleInputContext.CONTEXT_ID);
715:            }
716:
717:            private IBundleModel getBundleModel() {
718:                BundleInputContext context = getBundleContext();
719:                return (context != null) ? (IBundleModel) context.getModel()
720:                        : null;
721:
722:            }
723:
724:            private String getLineDelimiter() {
725:                BundleInputContext inputContext = getBundleContext();
726:                if (inputContext != null) {
727:                    return inputContext.getLineDelimiter();
728:                }
729:                return System.getProperty("line.separator"); //$NON-NLS-1$
730:            }
731:
732:            private IBundle getBundle() {
733:                IBundleModel model = getBundleModel();
734:                return (model != null) ? model.getBundle() : null;
735:            }
736:
737:            private String getVersionAttribute() {
738:                int manifestVersion = BundlePluginBase
739:                        .getBundleManifestVersion(getBundle());
740:                return (manifestVersion < 2) ? ICoreConstants.PACKAGE_SPECIFICATION_VERSION
741:                        : Constants.VERSION_ATTRIBUTE;
742:            }
743:
744:            public String getExportedPackageHeader() {
745:                int manifestVersion = BundlePluginBase
746:                        .getBundleManifestVersion(getBundle());
747:                return (manifestVersion < 2) ? ICoreConstants.PROVIDE_PACKAGE
748:                        : Constants.EXPORT_PACKAGE;
749:            }
750:
751:            protected boolean createCount() {
752:                return true;
753:            }
754:
755:            private void calculateUses() {
756:                final IProject proj = getPage().getPDEEditor()
757:                        .getCommonProject();
758:                Action action = new CalculateUsesAction(proj,
759:                        (IBundlePluginModelBase) getPage().getModel());
760:                action.run();
761:            }
762:        }
w___ww__.___j___a___v___a2___s___.___co_m__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.