Source Code Cross Referenced for WizardProjectsImportPage.java in  » IDE-Eclipse » ui-ide » org » eclipse » ui » internal » wizards » datatransfer » 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 » ui ide » org.eclipse.ui.internal.wizards.datatransfer 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*******************************************************************************
0002:         * Copyright (c) 2004, 2007 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:         *     Red Hat, Inc - extensive changes to allow importing of Archive Files
0011:         *     Philippe Ombredanne (pombredanne@nexb.com)
0012:         *     		- Bug 101180 [Import/Export] Import Existing Project into Workspace default widget is back button , should be text field
0013:         *     Martin Oberhuber (martin.oberhuber@windriver.com)
0014:         *     		- Bug 187318[Wizards] "Import Existing Project" loops forever with cyclic symbolic links
0015:         *******************************************************************************/package org.eclipse.ui.internal.wizards.datatransfer;
0016:
0017:        import java.io.File;
0018:        import java.io.IOException;
0019:        import java.io.InputStream;
0020:        import java.lang.reflect.InvocationTargetException;
0021:        import java.net.URI;
0022:        import java.util.ArrayList;
0023:        import java.util.Collection;
0024:        import java.util.HashSet;
0025:        import java.util.Iterator;
0026:        import java.util.List;
0027:        import java.util.Set;
0028:        import java.util.zip.ZipException;
0029:        import java.util.zip.ZipFile;
0030:
0031:        import org.eclipse.core.resources.IProject;
0032:        import org.eclipse.core.resources.IProjectDescription;
0033:        import org.eclipse.core.resources.IResource;
0034:        import org.eclipse.core.resources.IWorkspace;
0035:        import org.eclipse.core.resources.ResourcesPlugin;
0036:        import org.eclipse.core.runtime.CoreException;
0037:        import org.eclipse.core.runtime.IPath;
0038:        import org.eclipse.core.runtime.IProgressMonitor;
0039:        import org.eclipse.core.runtime.IStatus;
0040:        import org.eclipse.core.runtime.OperationCanceledException;
0041:        import org.eclipse.core.runtime.Path;
0042:        import org.eclipse.core.runtime.Platform;
0043:        import org.eclipse.core.runtime.Status;
0044:        import org.eclipse.core.runtime.SubProgressMonitor;
0045:        import org.eclipse.jface.dialogs.Dialog;
0046:        import org.eclipse.jface.dialogs.ErrorDialog;
0047:        import org.eclipse.jface.dialogs.IDialogConstants;
0048:        import org.eclipse.jface.dialogs.IDialogSettings;
0049:        import org.eclipse.jface.dialogs.MessageDialog;
0050:        import org.eclipse.jface.operation.IRunnableWithProgress;
0051:        import org.eclipse.jface.viewers.CheckStateChangedEvent;
0052:        import org.eclipse.jface.viewers.CheckboxTreeViewer;
0053:        import org.eclipse.jface.viewers.ICheckStateListener;
0054:        import org.eclipse.jface.viewers.ITreeContentProvider;
0055:        import org.eclipse.jface.viewers.LabelProvider;
0056:        import org.eclipse.jface.viewers.Viewer;
0057:        import org.eclipse.jface.viewers.ViewerComparator;
0058:        import org.eclipse.jface.wizard.WizardPage;
0059:        import org.eclipse.osgi.util.NLS;
0060:        import org.eclipse.swt.SWT;
0061:        import org.eclipse.swt.events.FocusAdapter;
0062:        import org.eclipse.swt.events.SelectionAdapter;
0063:        import org.eclipse.swt.events.SelectionEvent;
0064:        import org.eclipse.swt.events.TraverseEvent;
0065:        import org.eclipse.swt.events.TraverseListener;
0066:        import org.eclipse.swt.layout.GridData;
0067:        import org.eclipse.swt.layout.GridLayout;
0068:        import org.eclipse.swt.widgets.Button;
0069:        import org.eclipse.swt.widgets.Composite;
0070:        import org.eclipse.swt.widgets.DirectoryDialog;
0071:        import org.eclipse.swt.widgets.FileDialog;
0072:        import org.eclipse.swt.widgets.Label;
0073:        import org.eclipse.swt.widgets.Text;
0074:        import org.eclipse.ui.actions.WorkspaceModifyOperation;
0075:        import org.eclipse.ui.dialogs.IOverwriteQuery;
0076:        import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
0077:        import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
0078:        import org.eclipse.ui.internal.ide.StatusUtil;
0079:        import org.eclipse.ui.statushandlers.StatusManager;
0080:        import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider;
0081:        import org.eclipse.ui.wizards.datatransfer.ImportOperation;
0082:
0083:        /**
0084:         * The WizardProjectsImportPage is the page that allows the user to import
0085:         * projects from a particular location.
0086:         */
0087:        public class WizardProjectsImportPage extends WizardPage implements 
0088:                IOverwriteQuery {
0089:
0090:            /**
0091:             * The name of the folder containing metadata information for the workspace.
0092:             */
0093:            public static final String METADATA_FOLDER = ".metadata"; //$NON-NLS-1$
0094:
0095:            /**
0096:             * Class declared public only for test suite.
0097:             * 
0098:             */
0099:            public class ProjectRecord {
0100:                File projectSystemFile;
0101:
0102:                Object projectArchiveFile;
0103:
0104:                String projectName;
0105:
0106:                Object parent;
0107:
0108:                int level;
0109:
0110:                IProjectDescription description;
0111:
0112:                ILeveledImportStructureProvider provider;
0113:
0114:                /**
0115:                 * Create a record for a project based on the info in the file.
0116:                 * 
0117:                 * @param file
0118:                 */
0119:                ProjectRecord(File file) {
0120:                    projectSystemFile = file;
0121:                    setProjectName();
0122:                }
0123:
0124:                /**
0125:                 * @param file
0126:                 *            The Object representing the .project file
0127:                 * @param parent
0128:                 *            The parent folder of the .project file
0129:                 * @param level
0130:                 *            The number of levels deep in the provider the file is
0131:                 * @param entryProvider
0132:                 *            The provider for the archive file that contains it
0133:                 */
0134:                ProjectRecord(Object file, Object parent, int level,
0135:                        ILeveledImportStructureProvider entryProvider) {
0136:                    this .projectArchiveFile = file;
0137:                    this .parent = parent;
0138:                    this .level = level;
0139:                    this .provider = entryProvider;
0140:                    setProjectName();
0141:                }
0142:
0143:                /**
0144:                 * Set the name of the project based on the projectFile.
0145:                 */
0146:                private void setProjectName() {
0147:                    IProjectDescription newDescription = null;
0148:                    try {
0149:                        if (projectArchiveFile != null) {
0150:                            InputStream stream = provider
0151:                                    .getContents(projectArchiveFile);
0152:                            if (stream != null) {
0153:                                newDescription = IDEWorkbenchPlugin
0154:                                        .getPluginWorkspace()
0155:                                        .loadProjectDescription(stream);
0156:                                stream.close();
0157:                            }
0158:                        } else {
0159:                            IPath path = new Path(projectSystemFile.getPath());
0160:                            // if the file is in the default location, use the directory
0161:                            // name as the project name
0162:                            if (isDefaultLocation(path)) {
0163:                                projectName = path
0164:                                        .segment(path.segmentCount() - 2);
0165:                                newDescription = IDEWorkbenchPlugin
0166:                                        .getPluginWorkspace()
0167:                                        .newProjectDescription(projectName);
0168:                            } else {
0169:                                newDescription = IDEWorkbenchPlugin
0170:                                        .getPluginWorkspace()
0171:                                        .loadProjectDescription(path);
0172:                            }
0173:                        }
0174:                    } catch (CoreException e) {
0175:                        // no good couldn't get the name
0176:                    } catch (IOException e) {
0177:                        // no good couldn't get the name
0178:                    }
0179:
0180:                    if (newDescription == null) {
0181:                        this .description = null;
0182:                        projectName = ""; //$NON-NLS-1$
0183:                    } else {
0184:                        this .description = newDescription;
0185:                        projectName = this .description.getName();
0186:                    }
0187:                }
0188:
0189:                /**
0190:                 * Returns whether the given project description file path is in the
0191:                 * default location for a project
0192:                 * 
0193:                 * @param path
0194:                 *            The path to examine
0195:                 * @return Whether the given path is the default location for a project
0196:                 */
0197:                private boolean isDefaultLocation(IPath path) {
0198:                    // The project description file must at least be within the project,
0199:                    // which is within the workspace location
0200:                    if (path.segmentCount() < 2)
0201:                        return false;
0202:                    return path.removeLastSegments(2).toFile().equals(
0203:                            Platform.getLocation().toFile());
0204:                }
0205:
0206:                /**
0207:                 * Get the name of the project
0208:                 * 
0209:                 * @return String
0210:                 */
0211:                public String getProjectName() {
0212:                    return projectName;
0213:                }
0214:            }
0215:
0216:            // dialog store id constants
0217:            private final static String STORE_COPY_PROJECT_ID = "WizardProjectsImportPage.STORE_COPY_PROJECT_ID"; //$NON-NLS-1$
0218:
0219:            private final static String STORE_ARCHIVE_SELECTED = "WizardProjectsImportPage.STORE_ARCHIVE_SELECTED"; //$NON-NLS-1$
0220:
0221:            private Text directoryPathField;
0222:
0223:            private CheckboxTreeViewer projectsList;
0224:
0225:            private Button copyCheckbox;
0226:
0227:            private boolean copyFiles = false;
0228:
0229:            private ProjectRecord[] selectedProjects = new ProjectRecord[0];
0230:
0231:            // Keep track of the directory that we browsed to last time
0232:            // the wizard was invoked.
0233:            private static String previouslyBrowsedDirectory = ""; //$NON-NLS-1$
0234:
0235:            // Keep track of the archive that we browsed to last time
0236:            // the wizard was invoked.
0237:            private static String previouslyBrowsedArchive = ""; //$NON-NLS-1$
0238:
0239:            private Button projectFromDirectoryRadio;
0240:
0241:            private Button projectFromArchiveRadio;
0242:
0243:            private Text archivePathField;
0244:
0245:            private Button browseDirectoriesButton;
0246:
0247:            private Button browseArchivesButton;
0248:
0249:            private IProject[] wsProjects;
0250:
0251:            // constant from WizardArchiveFileResourceImportPage1
0252:            private static final String[] FILE_IMPORT_MASK = {
0253:                    "*.jar;*.zip;*.tar;*.tar.gz;*.tgz", "*.*" }; //$NON-NLS-1$ //$NON-NLS-2$
0254:
0255:            // The last selected path to mimize searches
0256:            private String lastPath;
0257:
0258:            /**
0259:             * Creates a new project creation wizard page.
0260:             * 
0261:             */
0262:            public WizardProjectsImportPage() {
0263:                this ("wizardExternalProjectsPage"); //$NON-NLS-1$
0264:            }
0265:
0266:            /**
0267:             * Create a new instance of the receiver.
0268:             * 
0269:             * @param pageName
0270:             */
0271:            public WizardProjectsImportPage(String pageName) {
0272:                super (pageName);
0273:                setPageComplete(false);
0274:                setTitle(DataTransferMessages.WizardProjectsImportPage_ImportProjectsTitle);
0275:                setDescription(DataTransferMessages.WizardProjectsImportPage_ImportProjectsDescription);
0276:            }
0277:
0278:            /*
0279:             * (non-Javadoc)
0280:             * 
0281:             * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
0282:             */
0283:            public void createControl(Composite parent) {
0284:
0285:                initializeDialogUnits(parent);
0286:
0287:                Composite workArea = new Composite(parent, SWT.NONE);
0288:                setControl(workArea);
0289:
0290:                workArea.setLayout(new GridLayout());
0291:                workArea.setLayoutData(new GridData(GridData.FILL_BOTH
0292:                        | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL));
0293:
0294:                createProjectsRoot(workArea);
0295:                createProjectsList(workArea);
0296:                createOptionsArea(workArea);
0297:                restoreWidgetValues();
0298:                Dialog.applyDialogFont(workArea);
0299:
0300:            }
0301:
0302:            /**
0303:             * Create the area with the extra options.
0304:             * 
0305:             * @param workArea
0306:             */
0307:            private void createOptionsArea(Composite workArea) {
0308:                Composite optionsGroup = new Composite(workArea, SWT.NONE);
0309:                optionsGroup.setLayout(new GridLayout());
0310:                optionsGroup.setLayoutData(new GridData(
0311:                        GridData.FILL_HORIZONTAL));
0312:
0313:                copyCheckbox = new Button(optionsGroup, SWT.CHECK);
0314:                copyCheckbox
0315:                        .setText(DataTransferMessages.WizardProjectsImportPage_CopyProjectsIntoWorkspace);
0316:                copyCheckbox.setLayoutData(new GridData(
0317:                        GridData.FILL_HORIZONTAL));
0318:                copyCheckbox.addSelectionListener(new SelectionAdapter() {
0319:                    public void widgetSelected(SelectionEvent e) {
0320:                        copyFiles = copyCheckbox.getSelection();
0321:                    }
0322:                });
0323:            }
0324:
0325:            /**
0326:             * Create the checkbox list for the found projects.
0327:             * 
0328:             * @param workArea
0329:             */
0330:            private void createProjectsList(Composite workArea) {
0331:
0332:                Label title = new Label(workArea, SWT.NONE);
0333:                title
0334:                        .setText(DataTransferMessages.WizardProjectsImportPage_ProjectsListTitle);
0335:
0336:                Composite listComposite = new Composite(workArea, SWT.NONE);
0337:                GridLayout layout = new GridLayout();
0338:                layout.numColumns = 2;
0339:                layout.marginWidth = 0;
0340:                layout.makeColumnsEqualWidth = false;
0341:                listComposite.setLayout(layout);
0342:
0343:                listComposite.setLayoutData(new GridData(
0344:                        GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL
0345:                                | GridData.FILL_BOTH));
0346:
0347:                projectsList = new CheckboxTreeViewer(listComposite, SWT.BORDER);
0348:                GridData listData = new GridData(GridData.GRAB_HORIZONTAL
0349:                        | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
0350:                projectsList.getControl().setLayoutData(listData);
0351:
0352:                projectsList.setContentProvider(new ITreeContentProvider() {
0353:
0354:                    /*
0355:                     * (non-Javadoc)
0356:                     * 
0357:                     * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
0358:                     */
0359:                    public Object[] getChildren(Object parentElement) {
0360:                        return null;
0361:                    }
0362:
0363:                    /*
0364:                     * (non-Javadoc)
0365:                     * 
0366:                     * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
0367:                     */
0368:                    public Object[] getElements(Object inputElement) {
0369:                        return getValidProjects();
0370:                    }
0371:
0372:                    /*
0373:                     * (non-Javadoc)
0374:                     * 
0375:                     * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
0376:                     */
0377:                    public boolean hasChildren(Object element) {
0378:                        return false;
0379:                    }
0380:
0381:                    /*
0382:                     * (non-Javadoc)
0383:                     * 
0384:                     * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
0385:                     */
0386:                    public Object getParent(Object element) {
0387:                        return null;
0388:                    }
0389:
0390:                    /*
0391:                     * (non-Javadoc)
0392:                     * 
0393:                     * @see org.eclipse.jface.viewers.IContentProvider#dispose()
0394:                     */
0395:                    public void dispose() {
0396:
0397:                    }
0398:
0399:                    /*
0400:                     * (non-Javadoc)
0401:                     * 
0402:                     * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
0403:                     *      java.lang.Object, java.lang.Object)
0404:                     */
0405:                    public void inputChanged(Viewer viewer, Object oldInput,
0406:                            Object newInput) {
0407:                    }
0408:
0409:                });
0410:
0411:                projectsList.setLabelProvider(new LabelProvider() {
0412:                    /*
0413:                     * (non-Javadoc)
0414:                     * 
0415:                     * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
0416:                     */
0417:                    public String getText(Object element) {
0418:                        return ((ProjectRecord) element).getProjectName();
0419:                    }
0420:                });
0421:
0422:                projectsList.addCheckStateListener(new ICheckStateListener() {
0423:                    /*
0424:                     * (non-Javadoc)
0425:                     * 
0426:                     * @see org.eclipse.jface.viewers.ICheckStateListener#checkStateChanged(org.eclipse.jface.viewers.CheckStateChangedEvent)
0427:                     */
0428:                    public void checkStateChanged(CheckStateChangedEvent event) {
0429:                        setPageComplete(projectsList.getCheckedElements().length > 0);
0430:                    }
0431:                });
0432:
0433:                projectsList.setInput(this );
0434:                projectsList.setComparator(new ViewerComparator());
0435:                createSelectionButtons(listComposite);
0436:            }
0437:
0438:            /**
0439:             * Create the selection buttons in the listComposite.
0440:             * 
0441:             * @param listComposite
0442:             */
0443:            private void createSelectionButtons(Composite listComposite) {
0444:                Composite buttonsComposite = new Composite(listComposite,
0445:                        SWT.NONE);
0446:                GridLayout layout = new GridLayout();
0447:                layout.marginWidth = 0;
0448:                layout.marginHeight = 0;
0449:                buttonsComposite.setLayout(layout);
0450:
0451:                buttonsComposite.setLayoutData(new GridData(
0452:                        GridData.VERTICAL_ALIGN_BEGINNING));
0453:
0454:                Button selectAll = new Button(buttonsComposite, SWT.PUSH);
0455:                selectAll.setText(DataTransferMessages.DataTransfer_selectAll);
0456:                selectAll.addSelectionListener(new SelectionAdapter() {
0457:                    public void widgetSelected(SelectionEvent e) {
0458:                        projectsList.setCheckedElements(selectedProjects);
0459:                        setPageComplete(projectsList.getCheckedElements().length > 0);
0460:                    }
0461:                });
0462:                Dialog.applyDialogFont(selectAll);
0463:                setButtonLayoutData(selectAll);
0464:
0465:                Button deselectAll = new Button(buttonsComposite, SWT.PUSH);
0466:                deselectAll
0467:                        .setText(DataTransferMessages.DataTransfer_deselectAll);
0468:                deselectAll.addSelectionListener(new SelectionAdapter() {
0469:                    /*
0470:                     * (non-Javadoc)
0471:                     * 
0472:                     * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
0473:                     */
0474:                    public void widgetSelected(SelectionEvent e) {
0475:
0476:                        projectsList.setCheckedElements(new Object[0]);
0477:                        setPageComplete(false);
0478:                    }
0479:                });
0480:                Dialog.applyDialogFont(deselectAll);
0481:                setButtonLayoutData(deselectAll);
0482:
0483:                Button refresh = new Button(buttonsComposite, SWT.PUSH);
0484:                refresh.setText(DataTransferMessages.DataTransfer_refresh);
0485:                refresh.addSelectionListener(new SelectionAdapter() {
0486:                    /*
0487:                     * (non-Javadoc)
0488:                     * 
0489:                     * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
0490:                     */
0491:                    public void widgetSelected(SelectionEvent e) {
0492:                        if (projectFromDirectoryRadio.getSelection()) {
0493:                            updateProjectsList(directoryPathField.getText()
0494:                                    .trim());
0495:                        } else {
0496:                            updateProjectsList(archivePathField.getText()
0497:                                    .trim());
0498:                        }
0499:                    }
0500:                });
0501:                Dialog.applyDialogFont(refresh);
0502:                setButtonLayoutData(refresh);
0503:            }
0504:
0505:            /**
0506:             * Create the area where you select the root directory for the projects.
0507:             * 
0508:             * @param workArea
0509:             *            Composite
0510:             */
0511:            private void createProjectsRoot(Composite workArea) {
0512:
0513:                // project specification group
0514:                Composite projectGroup = new Composite(workArea, SWT.NONE);
0515:                GridLayout layout = new GridLayout();
0516:                layout.numColumns = 3;
0517:                layout.makeColumnsEqualWidth = false;
0518:                layout.marginWidth = 0;
0519:                projectGroup.setLayout(layout);
0520:                projectGroup.setLayoutData(new GridData(
0521:                        GridData.FILL_HORIZONTAL));
0522:
0523:                // new project from directory radio button
0524:                projectFromDirectoryRadio = new Button(projectGroup, SWT.RADIO);
0525:                projectFromDirectoryRadio
0526:                        .setText(DataTransferMessages.WizardProjectsImportPage_RootSelectTitle);
0527:
0528:                // project location entry field
0529:                this .directoryPathField = new Text(projectGroup, SWT.BORDER);
0530:
0531:                this .directoryPathField.setLayoutData(new GridData(
0532:                        GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));
0533:
0534:                // browse button
0535:                browseDirectoriesButton = new Button(projectGroup, SWT.PUSH);
0536:                browseDirectoriesButton
0537:                        .setText(DataTransferMessages.DataTransfer_browse);
0538:                setButtonLayoutData(browseDirectoriesButton);
0539:
0540:                // new project from archive radio button
0541:                projectFromArchiveRadio = new Button(projectGroup, SWT.RADIO);
0542:                projectFromArchiveRadio
0543:                        .setText(DataTransferMessages.WizardProjectsImportPage_ArchiveSelectTitle);
0544:
0545:                // project location entry field
0546:                archivePathField = new Text(projectGroup, SWT.BORDER);
0547:
0548:                archivePathField.setLayoutData(new GridData(
0549:                        GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL));
0550:                // browse button
0551:                browseArchivesButton = new Button(projectGroup, SWT.PUSH);
0552:                browseArchivesButton
0553:                        .setText(DataTransferMessages.DataTransfer_browse);
0554:                setButtonLayoutData(browseArchivesButton);
0555:
0556:                projectFromDirectoryRadio.setSelection(true);
0557:                archivePathField.setEnabled(false);
0558:                browseArchivesButton.setEnabled(false);
0559:
0560:                browseDirectoriesButton
0561:                        .addSelectionListener(new SelectionAdapter() {
0562:                            /*
0563:                             * (non-Javadoc)
0564:                             * 
0565:                             * @see org.eclipse.swt.events.SelectionAdapter#widgetS
0566:                             *      elected(org.eclipse.swt.events.SelectionEvent)
0567:                             */
0568:                            public void widgetSelected(SelectionEvent e) {
0569:                                handleLocationDirectoryButtonPressed();
0570:                            }
0571:
0572:                        });
0573:
0574:                browseArchivesButton
0575:                        .addSelectionListener(new SelectionAdapter() {
0576:                            /*
0577:                             * (non-Javadoc)
0578:                             * 
0579:                             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
0580:                             */
0581:                            public void widgetSelected(SelectionEvent e) {
0582:                                handleLocationArchiveButtonPressed();
0583:                            }
0584:
0585:                        });
0586:
0587:                directoryPathField.addTraverseListener(new TraverseListener() {
0588:
0589:                    /*
0590:                     * (non-Javadoc)
0591:                     * 
0592:                     * @see org.eclipse.swt.events.TraverseListener#keyTraversed(org.eclipse.swt.events.TraverseEvent)
0593:                     */
0594:                    public void keyTraversed(TraverseEvent e) {
0595:                        if (e.detail == SWT.TRAVERSE_RETURN) {
0596:                            e.doit = false;
0597:                            updateProjectsList(directoryPathField.getText()
0598:                                    .trim());
0599:                        }
0600:                    }
0601:
0602:                });
0603:
0604:                directoryPathField.addFocusListener(new FocusAdapter() {
0605:
0606:                    /*
0607:                     * (non-Javadoc)
0608:                     * 
0609:                     * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
0610:                     */
0611:                    public void focusLost(org.eclipse.swt.events.FocusEvent e) {
0612:                        updateProjectsList(directoryPathField.getText().trim());
0613:                    }
0614:
0615:                });
0616:
0617:                archivePathField.addTraverseListener(new TraverseListener() {
0618:
0619:                    /*
0620:                     * (non-Javadoc)
0621:                     * 
0622:                     * @see org.eclipse.swt.events.TraverseListener#keyTraversed(org.eclipse.swt.events.TraverseEvent)
0623:                     */
0624:                    public void keyTraversed(TraverseEvent e) {
0625:                        if (e.detail == SWT.TRAVERSE_RETURN) {
0626:                            e.doit = false;
0627:                            updateProjectsList(archivePathField.getText()
0628:                                    .trim());
0629:                        }
0630:                    }
0631:
0632:                });
0633:
0634:                archivePathField.addFocusListener(new FocusAdapter() {
0635:                    /*
0636:                     * (non-Javadoc)
0637:                     * 
0638:                     * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
0639:                     */
0640:                    public void focusLost(org.eclipse.swt.events.FocusEvent e) {
0641:                        updateProjectsList(archivePathField.getText().trim());
0642:                    }
0643:                });
0644:
0645:                projectFromDirectoryRadio
0646:                        .addSelectionListener(new SelectionAdapter() {
0647:                            /*
0648:                             * (non-Javadoc)
0649:                             * 
0650:                             * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
0651:                             */
0652:                            public void widgetSelected(SelectionEvent e) {
0653:                                directoryRadioSelected();
0654:                            }
0655:                        });
0656:
0657:                projectFromArchiveRadio
0658:                        .addSelectionListener(new SelectionAdapter() {
0659:                            /*
0660:                             * (non-Javadoc)
0661:                             * 
0662:                             * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
0663:                             */
0664:                            public void widgetSelected(SelectionEvent e) {
0665:                                archiveRadioSelected();
0666:                            }
0667:                        });
0668:            }
0669:
0670:            private void archiveRadioSelected() {
0671:                if (projectFromArchiveRadio.getSelection()) {
0672:                    directoryPathField.setEnabled(false);
0673:                    browseDirectoriesButton.setEnabled(false);
0674:                    archivePathField.setEnabled(true);
0675:                    browseArchivesButton.setEnabled(true);
0676:                    updateProjectsList(archivePathField.getText());
0677:                    archivePathField.setFocus();
0678:                    copyCheckbox.setSelection(true);
0679:                    copyCheckbox.setEnabled(false);
0680:                }
0681:            }
0682:
0683:            private void directoryRadioSelected() {
0684:                if (projectFromDirectoryRadio.getSelection()) {
0685:                    directoryPathField.setEnabled(true);
0686:                    browseDirectoriesButton.setEnabled(true);
0687:                    archivePathField.setEnabled(false);
0688:                    browseArchivesButton.setEnabled(false);
0689:                    updateProjectsList(directoryPathField.getText());
0690:                    directoryPathField.setFocus();
0691:                    copyCheckbox.setEnabled(true);
0692:                    copyCheckbox.setSelection(copyFiles);
0693:                }
0694:            }
0695:
0696:            /*
0697:             * (non-Javadoc) Method declared on IDialogPage. Set the focus on path
0698:             * fields when page becomes visible.
0699:             */
0700:            public void setVisible(boolean visible) {
0701:                super .setVisible(visible);
0702:                if (visible && this .projectFromDirectoryRadio.getSelection()) {
0703:                    this .directoryPathField.setFocus();
0704:                }
0705:                if (visible && this .projectFromArchiveRadio.getSelection()) {
0706:                    this .archivePathField.setFocus();
0707:                }
0708:            }
0709:
0710:            /**
0711:             * Update the list of projects based on path. Method declared public only
0712:             * for test suite.
0713:             * 
0714:             * @param path
0715:             */
0716:            public void updateProjectsList(final String path) {
0717:
0718:                if (path.equals(lastPath)) {
0719:                    return;
0720:                }
0721:
0722:                lastPath = path;
0723:
0724:                // on an empty path empty selectedProjects
0725:                if (path == null || path.length() == 0) {
0726:                    selectedProjects = new ProjectRecord[0];
0727:                    projectsList.refresh(true);
0728:                    projectsList.setCheckedElements(selectedProjects);
0729:                    setPageComplete(projectsList.getCheckedElements().length > 0);
0730:                    return;
0731:                }
0732:                // We can't access the radio button from the inner class so get the
0733:                // status beforehand
0734:                final boolean dirSelected = this .projectFromDirectoryRadio
0735:                        .getSelection();
0736:                try {
0737:                    getContainer().run(true, true, new IRunnableWithProgress() {
0738:
0739:                        /*
0740:                         * (non-Javadoc)
0741:                         * 
0742:                         * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
0743:                         */
0744:                        public void run(IProgressMonitor monitor) {
0745:
0746:                            monitor
0747:                                    .beginTask(
0748:                                            DataTransferMessages.WizardProjectsImportPage_SearchingMessage,
0749:                                            100);
0750:                            File directory = new File(path);
0751:                            selectedProjects = new ProjectRecord[0];
0752:                            Collection files = new ArrayList();
0753:                            monitor.worked(10);
0754:                            if (!dirSelected
0755:                                    && ArchiveFileManipulations.isTarFile(path)) {
0756:                                TarFile sourceTarFile = getSpecifiedTarSourceFile(path);
0757:                                if (sourceTarFile == null) {
0758:                                    return;
0759:                                }
0760:
0761:                                TarLeveledStructureProvider provider = ArchiveFileManipulations
0762:                                        .getTarStructureProvider(sourceTarFile,
0763:                                                getContainer().getShell());
0764:                                Object child = provider.getRoot();
0765:
0766:                                if (!collectProjectFilesFromProvider(files,
0767:                                        provider, child, 0, monitor)) {
0768:                                    return;
0769:                                }
0770:                                Iterator filesIterator = files.iterator();
0771:                                selectedProjects = new ProjectRecord[files
0772:                                        .size()];
0773:                                int index = 0;
0774:                                monitor.worked(50);
0775:                                monitor
0776:                                        .subTask(DataTransferMessages.WizardProjectsImportPage_ProcessingMessage);
0777:                                while (filesIterator.hasNext()) {
0778:                                    selectedProjects[index++] = (ProjectRecord) filesIterator
0779:                                            .next();
0780:                                }
0781:                            } else if (!dirSelected
0782:                                    && ArchiveFileManipulations.isZipFile(path)) {
0783:                                ZipFile sourceFile = getSpecifiedZipSourceFile(path);
0784:                                if (sourceFile == null) {
0785:                                    return;
0786:                                }
0787:                                ZipLeveledStructureProvider provider = ArchiveFileManipulations
0788:                                        .getZipStructureProvider(sourceFile,
0789:                                                getContainer().getShell());
0790:                                Object child = provider.getRoot();
0791:
0792:                                if (!collectProjectFilesFromProvider(files,
0793:                                        provider, child, 0, monitor)) {
0794:                                    return;
0795:                                }
0796:                                Iterator filesIterator = files.iterator();
0797:                                selectedProjects = new ProjectRecord[files
0798:                                        .size()];
0799:                                int index = 0;
0800:                                monitor.worked(50);
0801:                                monitor
0802:                                        .subTask(DataTransferMessages.WizardProjectsImportPage_ProcessingMessage);
0803:                                while (filesIterator.hasNext()) {
0804:                                    selectedProjects[index++] = (ProjectRecord) filesIterator
0805:                                            .next();
0806:                                }
0807:                            }
0808:
0809:                            else if (dirSelected && directory.isDirectory()) {
0810:
0811:                                if (!collectProjectFilesFromDirectory(files,
0812:                                        directory, null, monitor)) {
0813:                                    return;
0814:                                }
0815:                                Iterator filesIterator = files.iterator();
0816:                                selectedProjects = new ProjectRecord[files
0817:                                        .size()];
0818:                                int index = 0;
0819:                                monitor.worked(50);
0820:                                monitor
0821:                                        .subTask(DataTransferMessages.WizardProjectsImportPage_ProcessingMessage);
0822:                                while (filesIterator.hasNext()) {
0823:                                    File file = (File) filesIterator.next();
0824:                                    selectedProjects[index] = new ProjectRecord(
0825:                                            file);
0826:                                    index++;
0827:                                }
0828:                            } else {
0829:                                monitor.worked(60);
0830:                            }
0831:                            monitor.done();
0832:                        }
0833:
0834:                    });
0835:                } catch (InvocationTargetException e) {
0836:                    IDEWorkbenchPlugin.log(e.getMessage(), e);
0837:                } catch (InterruptedException e) {
0838:                    // Nothing to do if the user interrupts.
0839:                }
0840:
0841:                projectsList.refresh(true);
0842:                projectsList.setCheckedElements(getValidProjects());
0843:                setPageComplete(projectsList.getCheckedElements().length > 0);
0844:            }
0845:
0846:            /**
0847:             * Answer a handle to the zip file currently specified as being the source.
0848:             * Return null if this file does not exist or is not of valid format.
0849:             */
0850:            private ZipFile getSpecifiedZipSourceFile(String fileName) {
0851:                if (fileName.length() == 0) {
0852:                    return null;
0853:                }
0854:
0855:                try {
0856:                    return new ZipFile(fileName);
0857:                } catch (ZipException e) {
0858:                    displayErrorDialog(DataTransferMessages.ZipImport_badFormat);
0859:                } catch (IOException e) {
0860:                    displayErrorDialog(DataTransferMessages.ZipImport_couldNotRead);
0861:                }
0862:
0863:                archivePathField.setFocus();
0864:                return null;
0865:            }
0866:
0867:            /**
0868:             * Answer a handle to the zip file currently specified as being the source.
0869:             * Return null if this file does not exist or is not of valid format.
0870:             */
0871:            private TarFile getSpecifiedTarSourceFile(String fileName) {
0872:                if (fileName.length() == 0) {
0873:                    return null;
0874:                }
0875:
0876:                try {
0877:                    return new TarFile(fileName);
0878:                } catch (TarException e) {
0879:                    displayErrorDialog(DataTransferMessages.TarImport_badFormat);
0880:                } catch (IOException e) {
0881:                    displayErrorDialog(DataTransferMessages.ZipImport_couldNotRead);
0882:                }
0883:
0884:                archivePathField.setFocus();
0885:                return null;
0886:            }
0887:
0888:            /**
0889:             * Display an error dialog with the specified message.
0890:             * 
0891:             * @param message
0892:             *            the error message
0893:             */
0894:            protected void displayErrorDialog(String message) {
0895:                MessageDialog.openError(getContainer().getShell(),
0896:                        getErrorDialogTitle(), message);
0897:            }
0898:
0899:            /**
0900:             * Get the title for an error dialog. Subclasses should override.
0901:             */
0902:            protected String getErrorDialogTitle() {
0903:                return IDEWorkbenchMessages.WizardExportPage_internalErrorTitle;
0904:            }
0905:
0906:            /**
0907:             * Collect the list of .project files that are under directory into files.
0908:             * 
0909:             * @param files
0910:             * @param directory
0911:             * @param directoriesVisited
0912:             *            Set of canonical paths of directories, used as recursion guard
0913:             * @param monitor
0914:             *            The monitor to report to
0915:             * @return boolean <code>true</code> if the operation was completed.
0916:             */
0917:            private boolean collectProjectFilesFromDirectory(Collection files,
0918:                    File directory, Set directoriesVisited,
0919:                    IProgressMonitor monitor) {
0920:
0921:                if (monitor.isCanceled()) {
0922:                    return false;
0923:                }
0924:                monitor
0925:                        .subTask(NLS
0926:                                .bind(
0927:                                        DataTransferMessages.WizardProjectsImportPage_CheckingMessage,
0928:                                        directory.getPath()));
0929:                File[] contents = directory.listFiles();
0930:                if (contents == null)
0931:                    return false;
0932:
0933:                // Initialize recursion guard for recursive symbolic links
0934:                if (directoriesVisited == null) {
0935:                    directoriesVisited = new HashSet();
0936:                    try {
0937:                        directoriesVisited.add(directory.getCanonicalPath());
0938:                    } catch (IOException exception) {
0939:                        StatusManager.getManager().handle(
0940:                                StatusUtil.newStatus(IStatus.ERROR, exception
0941:                                        .getLocalizedMessage(), exception));
0942:                    }
0943:                }
0944:
0945:                // first look for project description files
0946:                final String dotProject = IProjectDescription.DESCRIPTION_FILE_NAME;
0947:                for (int i = 0; i < contents.length; i++) {
0948:                    File file = contents[i];
0949:                    if (file.isFile() && file.getName().equals(dotProject)) {
0950:                        files.add(file);
0951:                        // don't search sub-directories since we can't have nested
0952:                        // projects
0953:                        return true;
0954:                    }
0955:                }
0956:                // no project description found, so recurse into sub-directories
0957:                for (int i = 0; i < contents.length; i++) {
0958:                    if (contents[i].isDirectory()) {
0959:                        if (!contents[i].getName().equals(METADATA_FOLDER)) {
0960:                            try {
0961:                                String canonicalPath = contents[i]
0962:                                        .getCanonicalPath();
0963:                                if (!directoriesVisited.add(canonicalPath)) {
0964:                                    // already been here --> do not recurse
0965:                                    continue;
0966:                                }
0967:                            } catch (IOException exception) {
0968:                                StatusManager
0969:                                        .getManager()
0970:                                        .handle(
0971:                                                StatusUtil
0972:                                                        .newStatus(
0973:                                                                IStatus.ERROR,
0974:                                                                exception
0975:                                                                        .getLocalizedMessage(),
0976:                                                                exception));
0977:
0978:                            }
0979:                            collectProjectFilesFromDirectory(files,
0980:                                    contents[i], directoriesVisited, monitor);
0981:                        }
0982:                    }
0983:                }
0984:                return true;
0985:            }
0986:
0987:            /**
0988:             * Collect the list of .project files that are under directory into files.
0989:             * 
0990:             * @param files
0991:             * @param monitor
0992:             *            The monitor to report to
0993:             * @return boolean <code>true</code> if the operation was completed.
0994:             */
0995:            private boolean collectProjectFilesFromProvider(Collection files,
0996:                    ILeveledImportStructureProvider provider, Object entry,
0997:                    int level, IProgressMonitor monitor) {
0998:
0999:                if (monitor.isCanceled()) {
1000:                    return false;
1001:                }
1002:                monitor
1003:                        .subTask(NLS
1004:                                .bind(
1005:                                        DataTransferMessages.WizardProjectsImportPage_CheckingMessage,
1006:                                        provider.getLabel(entry)));
1007:                List children = provider.getChildren(entry);
1008:                if (children == null) {
1009:                    children = new ArrayList(1);
1010:                }
1011:                Iterator childrenEnum = children.iterator();
1012:                while (childrenEnum.hasNext()) {
1013:                    Object child = childrenEnum.next();
1014:                    if (provider.isFolder(child)) {
1015:                        collectProjectFilesFromProvider(files, provider, child,
1016:                                level + 1, monitor);
1017:                    }
1018:                    String elementLabel = provider.getLabel(child);
1019:                    if (elementLabel
1020:                            .equals(IProjectDescription.DESCRIPTION_FILE_NAME)) {
1021:                        files.add(new ProjectRecord(child, entry, level,
1022:                                provider));
1023:                    }
1024:                }
1025:                return true;
1026:            }
1027:
1028:            /**
1029:             * The browse button has been selected. Select the location.
1030:             */
1031:            protected void handleLocationDirectoryButtonPressed() {
1032:
1033:                DirectoryDialog dialog = new DirectoryDialog(directoryPathField
1034:                        .getShell());
1035:                dialog
1036:                        .setMessage(DataTransferMessages.WizardProjectsImportPage_SelectDialogTitle);
1037:
1038:                String dirName = directoryPathField.getText().trim();
1039:                if (dirName.length() == 0) {
1040:                    dirName = previouslyBrowsedDirectory;
1041:                }
1042:
1043:                if (dirName.length() == 0) {
1044:                    dialog.setFilterPath(IDEWorkbenchPlugin
1045:                            .getPluginWorkspace().getRoot().getLocation()
1046:                            .toOSString());
1047:                } else {
1048:                    File path = new File(dirName);
1049:                    if (path.exists()) {
1050:                        dialog.setFilterPath(new Path(dirName).toOSString());
1051:                    }
1052:                }
1053:
1054:                String selectedDirectory = dialog.open();
1055:                if (selectedDirectory != null) {
1056:                    previouslyBrowsedDirectory = selectedDirectory;
1057:                    directoryPathField.setText(previouslyBrowsedDirectory);
1058:                    updateProjectsList(selectedDirectory);
1059:                }
1060:
1061:            }
1062:
1063:            /**
1064:             * The browse button has been selected. Select the location.
1065:             */
1066:            protected void handleLocationArchiveButtonPressed() {
1067:
1068:                FileDialog dialog = new FileDialog(archivePathField.getShell());
1069:                dialog.setFilterExtensions(FILE_IMPORT_MASK);
1070:                dialog
1071:                        .setText(DataTransferMessages.WizardProjectsImportPage_SelectArchiveDialogTitle);
1072:
1073:                String fileName = archivePathField.getText().trim();
1074:                if (fileName.length() == 0) {
1075:                    fileName = previouslyBrowsedArchive;
1076:                }
1077:
1078:                if (fileName.length() == 0) {
1079:                    dialog.setFilterPath(IDEWorkbenchPlugin
1080:                            .getPluginWorkspace().getRoot().getLocation()
1081:                            .toOSString());
1082:                } else {
1083:                    File path = new File(fileName);
1084:                    if (path.exists()) {
1085:                        dialog.setFilterPath(new Path(fileName).toOSString());
1086:                    }
1087:                }
1088:
1089:                String selectedArchive = dialog.open();
1090:                if (selectedArchive != null) {
1091:                    previouslyBrowsedArchive = selectedArchive;
1092:                    archivePathField.setText(previouslyBrowsedArchive);
1093:                    updateProjectsList(selectedArchive);
1094:                }
1095:
1096:            }
1097:
1098:            /**
1099:             * Create the selected projects
1100:             * 
1101:             * @return boolean <code>true</code> if all project creations were
1102:             *         successful.
1103:             */
1104:            public boolean createProjects() {
1105:                saveWidgetValues();
1106:                final Object[] selected = projectsList.getCheckedElements();
1107:                WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
1108:                    protected void execute(IProgressMonitor monitor)
1109:                            throws InvocationTargetException,
1110:                            InterruptedException {
1111:                        try {
1112:                            monitor.beginTask("", selected.length); //$NON-NLS-1$
1113:                            if (monitor.isCanceled()) {
1114:                                throw new OperationCanceledException();
1115:                            }
1116:                            for (int i = 0; i < selected.length; i++) {
1117:                                createExistingProject(
1118:                                        (ProjectRecord) selected[i],
1119:                                        new SubProgressMonitor(monitor, 1));
1120:                            }
1121:                        } finally {
1122:                            monitor.done();
1123:                        }
1124:                    }
1125:                };
1126:                // run the new project creation operation
1127:                try {
1128:                    getContainer().run(true, true, op);
1129:                } catch (InterruptedException e) {
1130:                    return false;
1131:                } catch (InvocationTargetException e) {
1132:                    // one of the steps resulted in a core exception
1133:                    Throwable t = e.getTargetException();
1134:                    String message = DataTransferMessages.WizardExternalProjectImportPage_errorMessage;
1135:                    IStatus status;
1136:                    if (t instanceof  CoreException) {
1137:                        status = ((CoreException) t).getStatus();
1138:                    } else {
1139:                        status = new Status(IStatus.ERROR,
1140:                                IDEWorkbenchPlugin.IDE_WORKBENCH, 1, message, t);
1141:                    }
1142:                    ErrorDialog.openError(getShell(), message, null, status);
1143:                    return false;
1144:                }
1145:                return true;
1146:            }
1147:
1148:            /**
1149:             * Performs clean-up if the user cancels the wizard without doing anything
1150:             */
1151:            public void performCancel() {
1152:                ArchiveFileManipulations.clearProviderCache(getContainer()
1153:                        .getShell());
1154:            }
1155:
1156:            /**
1157:             * Create the project described in record. If it is successful return true.
1158:             * 
1159:             * @param record
1160:             * @return boolean <code>true</code> if successful
1161:             * @throws InterruptedException
1162:             */
1163:            private boolean createExistingProject(final ProjectRecord record,
1164:                    IProgressMonitor monitor) throws InvocationTargetException,
1165:                    InterruptedException {
1166:                String projectName = record.getProjectName();
1167:                final IWorkspace workspace = ResourcesPlugin.getWorkspace();
1168:                final IProject project = workspace.getRoot().getProject(
1169:                        projectName);
1170:                if (record.description == null) {
1171:                    // error case
1172:                    record.description = workspace
1173:                            .newProjectDescription(projectName);
1174:                    IPath locationPath = new Path(record.projectSystemFile
1175:                            .getAbsolutePath());
1176:
1177:                    // If it is under the root use the default location
1178:                    if (Platform.getLocation().isPrefixOf(locationPath)) {
1179:                        record.description.setLocation(null);
1180:                    } else {
1181:                        record.description.setLocation(locationPath);
1182:                    }
1183:                } else {
1184:                    record.description.setName(projectName);
1185:                }
1186:                if (record.projectArchiveFile != null) {
1187:                    // import from archive
1188:                    List fileSystemObjects = record.provider
1189:                            .getChildren(record.parent);
1190:                    record.provider.setStrip(record.level);
1191:                    ImportOperation operation = new ImportOperation(project
1192:                            .getFullPath(), record.provider.getRoot(),
1193:                            record.provider, this , fileSystemObjects);
1194:                    operation.setContext(getShell());
1195:                    operation.run(monitor);
1196:                    return true;
1197:                }
1198:                // import from file system
1199:                File importSource = null;
1200:                if (copyFiles) {
1201:                    // import project from location copying files - use default project
1202:                    // location for this workspace
1203:                    URI locationURI = record.description.getLocationURI();
1204:                    // if location is null, project already exists in this location or
1205:                    // some error condition occured.
1206:                    if (locationURI != null) {
1207:                        importSource = new File(locationURI);
1208:                        IProjectDescription desc = workspace
1209:                                .newProjectDescription(projectName);
1210:                        desc.setBuildSpec(record.description.getBuildSpec());
1211:                        desc.setComment(record.description.getComment());
1212:                        desc.setDynamicReferences(record.description
1213:                                .getDynamicReferences());
1214:                        desc.setNatureIds(record.description.getNatureIds());
1215:                        desc.setReferencedProjects(record.description
1216:                                .getReferencedProjects());
1217:                        record.description = desc;
1218:                    }
1219:                }
1220:
1221:                try {
1222:                    monitor
1223:                            .beginTask(
1224:                                    DataTransferMessages.WizardProjectsImportPage_CreateProjectsTask,
1225:                                    100);
1226:                    project.create(record.description, new SubProgressMonitor(
1227:                            monitor, 30));
1228:                    project.open(IResource.BACKGROUND_REFRESH,
1229:                            new SubProgressMonitor(monitor, 70));
1230:                } catch (CoreException e) {
1231:                    throw new InvocationTargetException(e);
1232:                } finally {
1233:                    monitor.done();
1234:                }
1235:
1236:                // import operation to import project files if copy checkbox is selected
1237:                if (copyFiles && importSource != null) {
1238:                    List filesToImport = FileSystemStructureProvider.INSTANCE
1239:                            .getChildren(importSource);
1240:                    ImportOperation operation = new ImportOperation(project
1241:                            .getFullPath(), importSource,
1242:                            FileSystemStructureProvider.INSTANCE, this ,
1243:                            filesToImport);
1244:                    operation.setContext(getShell());
1245:                    operation.setOverwriteResources(true); // need to overwrite
1246:                    // .project, .classpath
1247:                    // files
1248:                    operation.setCreateContainerStructure(false);
1249:                    operation.run(monitor);
1250:                }
1251:
1252:                return true;
1253:            }
1254:
1255:            /**
1256:             * The <code>WizardDataTransfer</code> implementation of this
1257:             * <code>IOverwriteQuery</code> method asks the user whether the existing
1258:             * resource at the given path should be overwritten.
1259:             * 
1260:             * @param pathString
1261:             * @return the user's reply: one of <code>"YES"</code>, <code>"NO"</code>,
1262:             *         <code>"ALL"</code>, or <code>"CANCEL"</code>
1263:             */
1264:            public String queryOverwrite(String pathString) {
1265:
1266:                Path path = new Path(pathString);
1267:
1268:                String messageString;
1269:                // Break the message up if there is a file name and a directory
1270:                // and there are at least 2 segments.
1271:                if (path.getFileExtension() == null || path.segmentCount() < 2) {
1272:                    messageString = NLS
1273:                            .bind(
1274:                                    IDEWorkbenchMessages.WizardDataTransfer_existsQuestion,
1275:                                    pathString);
1276:                } else {
1277:                    messageString = NLS
1278:                            .bind(
1279:                                    IDEWorkbenchMessages.WizardDataTransfer_overwriteNameAndPathQuestion,
1280:                                    path.lastSegment(), path
1281:                                            .removeLastSegments(1).toOSString());
1282:                }
1283:
1284:                final MessageDialog dialog = new MessageDialog(getContainer()
1285:                        .getShell(), IDEWorkbenchMessages.Question, null,
1286:                        messageString, MessageDialog.QUESTION, new String[] {
1287:                                IDialogConstants.YES_LABEL,
1288:                                IDialogConstants.YES_TO_ALL_LABEL,
1289:                                IDialogConstants.NO_LABEL,
1290:                                IDialogConstants.NO_TO_ALL_LABEL,
1291:                                IDialogConstants.CANCEL_LABEL }, 0);
1292:                String[] response = new String[] { YES, ALL, NO, NO_ALL, CANCEL };
1293:                // run in syncExec because callback is from an operation,
1294:                // which is probably not running in the UI thread.
1295:                getControl().getDisplay().syncExec(new Runnable() {
1296:                    public void run() {
1297:                        dialog.open();
1298:                    }
1299:                });
1300:                return dialog.getReturnCode() < 0 ? CANCEL : response[dialog
1301:                        .getReturnCode()];
1302:            }
1303:
1304:            /**
1305:             * Method used for test suite.
1306:             * 
1307:             * @return Button the Import from Directory RadioButton
1308:             */
1309:            public Button getProjectFromDirectoryRadio() {
1310:                return projectFromDirectoryRadio;
1311:            }
1312:
1313:            /**
1314:             * Method used for test suite.
1315:             * 
1316:             * @return CheckboxTreeViewer the viewer containing all the projects found
1317:             */
1318:            public CheckboxTreeViewer getProjectsList() {
1319:                return projectsList;
1320:            }
1321:
1322:            /**
1323:             * Retrieve all the projects in the current workspace.
1324:             * 
1325:             * @return IProject[] array of IProject in the current workspace
1326:             */
1327:            private IProject[] getProjectsInWorkspace() {
1328:                if (wsProjects == null) {
1329:                    wsProjects = IDEWorkbenchPlugin.getPluginWorkspace()
1330:                            .getRoot().getProjects();
1331:                }
1332:                return wsProjects;
1333:            }
1334:
1335:            /**
1336:             * Get the array of valid project records that can be imported from the
1337:             * source workspace or archive, selected by the user. If a project with the
1338:             * same name exists in both the source workspace and the current workspace,
1339:             * it will not appear in the list of projects to import and thus cannot be
1340:             * selected for import.
1341:             * 
1342:             * Method declared public for test suite.
1343:             * 
1344:             * @return ProjectRecord[] array of projects that can be imported into the
1345:             *         workspace
1346:             */
1347:            public ProjectRecord[] getValidProjects() {
1348:                List validProjects = new ArrayList();
1349:                for (int i = 0; i < selectedProjects.length; i++) {
1350:                    if (!isProjectInWorkspace(selectedProjects[i]
1351:                            .getProjectName())) {
1352:                        validProjects.add(selectedProjects[i]);
1353:                    }
1354:                }
1355:                return (ProjectRecord[]) validProjects
1356:                        .toArray(new ProjectRecord[validProjects.size()]);
1357:            }
1358:
1359:            /**
1360:             * Determine if the project with the given name is in the current workspace.
1361:             * 
1362:             * @param projectName
1363:             *            String the project name to check
1364:             * @return boolean true if the project with the given name is in this
1365:             *         workspace
1366:             */
1367:            private boolean isProjectInWorkspace(String projectName) {
1368:                if (projectName == null) {
1369:                    return false;
1370:                }
1371:                IProject[] workspaceProjects = getProjectsInWorkspace();
1372:                for (int i = 0; i < workspaceProjects.length; i++) {
1373:                    if (projectName.equals(workspaceProjects[i].getName())) {
1374:                        return true;
1375:                    }
1376:                }
1377:                return false;
1378:            }
1379:
1380:            /**
1381:             * Use the dialog store to restore widget values to the values that they
1382:             * held last time this wizard was used to completion.
1383:             * 
1384:             * Method declared public only for use of tests.
1385:             */
1386:            public void restoreWidgetValues() {
1387:                IDialogSettings settings = getDialogSettings();
1388:                if (settings != null) {
1389:                    // checkbox
1390:                    copyFiles = settings.getBoolean(STORE_COPY_PROJECT_ID);
1391:                    copyCheckbox.setSelection(copyFiles);
1392:
1393:                    // radio selection
1394:                    boolean archiveSelected = settings
1395:                            .getBoolean(STORE_ARCHIVE_SELECTED);
1396:                    projectFromDirectoryRadio.setSelection(!archiveSelected);
1397:                    projectFromArchiveRadio.setSelection(archiveSelected);
1398:                    if (archiveSelected) {
1399:                        archiveRadioSelected();
1400:                    } else {
1401:                        directoryRadioSelected();
1402:                    }
1403:                }
1404:            }
1405:
1406:            /**
1407:             * Since Finish was pressed, write widget values to the dialog store so that
1408:             * they will persist into the next invocation of this wizard page.
1409:             * 
1410:             * Method declared public only for use of tests.
1411:             */
1412:            public void saveWidgetValues() {
1413:                IDialogSettings settings = getDialogSettings();
1414:                if (settings != null) {
1415:                    settings.put(STORE_COPY_PROJECT_ID, copyCheckbox
1416:                            .getSelection());
1417:
1418:                    settings.put(STORE_ARCHIVE_SELECTED,
1419:                            projectFromArchiveRadio.getSelection());
1420:                }
1421:            }
1422:
1423:            /**
1424:             * Method used for test suite.
1425:             * 
1426:             * @return Button copy checkbox
1427:             */
1428:            public Button getCopyCheckbox() {
1429:                return copyCheckbox;
1430:            }
1431:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.