summaryrefslogtreecommitdiff
path: root/core/source/org/openoffice/ide/eclipse/core/wizards/pages/UnoPackageExportPage.java
blob: b95364be25f4a40797da9229ec632d361d5e2b3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*************************************************************************
 *
 * The Contents of this file are made available subject to the terms of
 * the GNU Lesser General Public License Version 2.1
 *
 * GNU Lesser General Public License Version 2.1
 * =============================================
 * Copyright 2009 by Cédric Bosdonnat
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 * 
 * The Initial Developer of the Original Code is: Cédric Bosdonnat.
 *
 * Copyright: 2009 by Cédric Bosdonnat
 *
 * All Rights Reserved.
 * 
 ************************************************************************/
package org.openoffice.ide.eclipse.core.wizards.pages;

import java.io.File;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.internal.ide.DialogUtil;
import org.eclipse.ui.internal.ide.dialogs.ResourceTreeAndListGroup;
import org.eclipse.ui.model.WorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.openoffice.ide.eclipse.core.OOEclipsePlugin;
import org.openoffice.ide.eclipse.core.PluginLogger;
import org.openoffice.ide.eclipse.core.i18n.ImagesConstants;
import org.openoffice.ide.eclipse.core.internal.helpers.UnoidlProjectHelper;
import org.openoffice.ide.eclipse.core.model.IUnoidlProject;
import org.openoffice.ide.eclipse.core.model.ProjectsManager;
import org.openoffice.ide.eclipse.core.model.config.IOOo;
import org.openoffice.ide.eclipse.core.model.language.ILanguageBuilder;
import org.openoffice.ide.eclipse.core.model.utils.SystemHelper;
import org.openoffice.ide.eclipse.core.utils.FilesFinder;
import org.openoffice.ide.eclipse.core.wizards.Messages;
import org.openoffice.plugin.core.model.UnoPackage;

/**
 * First page of the new UNO extension export wizard.
 * 
 * @author Cédric Bosdonnat
 *
 */
@SuppressWarnings("restriction")
public class UnoPackageExportPage extends WizardPage {

    private static final int DESTINATION_PART_COLS = 3;

    private static final String OVERWRITE_FILES = "overwrite.files"; //$NON-NLS-1$
    private static final String AUTODEPLOY = "autodeploy"; //$NON-NLS-1$
    private static final String DESTINATION_HISTORY = "destination.history"; //$NON-NLS-1$
    
    private static final int MAX_DESTINATION_STORED = 5;

    private Combo mProjectsList;
    private ResourceTreeAndListGroup mResourceGroup;
    private Combo mDestinationCombo;
    private Button mOverwriteBox;
    private Button mAutodeployBox;
    
    private IUnoidlProject mSelectedProject;

    private ManifestExportPage mManifestPage;

    /**
     * Constructor.
     * 
     * @param pPageName the page id
     * @param pPrj the project to export
     * @param pManifestPage the manifest page of the wizard
     */
    public UnoPackageExportPage( String pPageName, IUnoidlProject pPrj, ManifestExportPage pManifestPage ) {
        super(pPageName);
        
        setTitle( Messages.getString("UnoPackageExportPage.Title") ); //$NON-NLS-1$
        setDescription( Messages.getString("UnoPackageExportPage.Description") ); //$NON-NLS-1$
        setImageDescriptor( OOEclipsePlugin.getImageDescriptor( ImagesConstants.PACKAGE_EXPORT_WIZ ) );
        
        mSelectedProject = pPrj;
        mManifestPage = pManifestPage;
    }

    /**
     * {@inheritDoc}
     */
    public void createControl(Composite pParent) {
        Composite body = new Composite( pParent, SWT.NONE );
        body.setLayout( new GridLayout( ) );
        body.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
        setControl( body );
        
        createProjectSelection( );
        createResourcesGroup( );
        createDestinationGroup( );
        createOptionsGroup( );
        
        setPageComplete( checkPageCompletion() );
        
        // Load the data into the fields
        loadData( );
    }

    
    
    /**
     * Loads the data in the different controls of the page.
     */
    private void loadData() {
        // Select the project
        String[] items = mProjectsList.getItems();
        int i = 0;
        boolean selected = false;
        while ( mSelectedProject != null && i < items.length && !selected ) {
            if ( items[i].equals( mSelectedProject.getName() ) ) {
                mProjectsList.select( i );
                selected = true;
            }
            i++;
        }
        
        // Select the XCU / XCS files by default
        IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject( mSelectedProject.getName() );
        FilesFinder finder = new FilesFinder( new String[] { IUnoidlProject.XCU_EXTENSION, IUnoidlProject.XCS_EXTENSION } );
        try {
        	finder.addExclude(mSelectedProject.getDistFolder().getFullPath());
            prj.accept( finder );
        } catch (CoreException e) {
            PluginLogger.error("Could not visit the project's content.", e);
        }
        
        ArrayList< IFile > files = finder.getResults();
        for (IFile file : files) {
            mResourceGroup.initialCheckListItem( file );
            mResourceGroup.initialCheckTreeItem( file );
        }
        
        restoreWidgetValues();
    }

    /**
     * Creates the project selection part of the dialog.
     */
    private void createProjectSelection() {
        Composite body = (Composite)getControl();
        Composite selectionBody = new Composite( body, SWT.NONE );
        selectionBody.setLayout( new GridLayout( 2, false ) );
        selectionBody.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, true, false ) );
        
        Label lbl = new Label( selectionBody, SWT.NORMAL );
        lbl.setText( Messages.getString("UnoPackageExportPage.Project") ); //$NON-NLS-1$
        lbl.setLayoutData( new GridData( SWT.BEGINNING, SWT.CENTER, false, false ) );
        
        IUnoidlProject[] prjs = ProjectsManager.getProjects();
        String[] prjNames = new String[prjs.length];
        for (int i = 0; i < prjs.length; i++) {
            IUnoidlProject prj = prjs[i];
            prjNames[i] = prj.getName();
        }
        
        mProjectsList = new Combo( selectionBody, SWT.DROP_DOWN | SWT.READ_ONLY );
        mProjectsList.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) );
        mProjectsList.setItems( prjNames );
        
        mProjectsList.addModifyListener( new ModifyListener() {
            
            public void modifyText(ModifyEvent pE) {
                int id = mProjectsList.getSelectionIndex();
                IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject( mSelectedProject.getName() );
                if ( id != -1 ) {
                    String name = mProjectsList.getItem( id );
                    IUnoidlProject unoprj = ProjectsManager.getProject( name );
                    mSelectedProject = unoprj;
                    
                    // Change the project in the manifest page
                    mManifestPage.setProject( unoprj );
                    
                    mResourceGroup.setRoot( prj );
                }
                
                setPageComplete( checkPageCompletion() );
            }
        });
    }

    /**
     * Creates the project's resources selection part of the dialog.
     */
    private void createResourcesGroup() {
        Composite body = (Composite)getControl();
        Composite selectionBody = new Composite( body, SWT.NONE );
        selectionBody.setLayout( new GridLayout( 2, false ) );
        selectionBody.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, true, false ) );
        
        mResourceGroup = new ResourceTreeAndListGroup(selectionBody, new ArrayList<Object>(),
                getResourceProvider(IResource.FOLDER | IResource.FILE),
                WorkbenchLabelProvider.getDecoratingWorkbenchLabelProvider(),
                getResourceProvider(IResource.FILE), WorkbenchLabelProvider
                        .getDecoratingWorkbenchLabelProvider(), SWT.NONE,
                DialogUtil.inRegularFontMode(selectionBody));
    }
    
    /**
     * Creates the package destination part of the dialog.
     */
    private void createDestinationGroup() {
        Composite body = (Composite)getControl();
        Composite groupBody = new Composite( body, SWT.NONE );
        groupBody.setLayout( new GridLayout( ) );
        groupBody.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, true, false ) );
        
        Label titleLbl = new Label( groupBody, SWT.NONE );
        titleLbl.setText( Messages.getString("UnoPackageExportPage.SelectDestination") ); //$NON-NLS-1$
        titleLbl.setLayoutData( new GridData( SWT.BEGINNING, SWT.BEGINNING, false, false ) );
        
        Composite rowBody = new Composite( groupBody, SWT.NONE );
        rowBody.setLayout( new GridLayout( DESTINATION_PART_COLS, false ) );
        rowBody.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, true, false ) );
        
        Label lbl = new Label( rowBody, SWT.None );
        lbl.setText( Messages.getString("UnoPackageExportPage.OxtFile") ); //$NON-NLS-1$
        lbl.setLayoutData( new GridData( SWT.BEGINNING, SWT.CENTER, false, false ) );
        
        mDestinationCombo = new Combo( rowBody, SWT.DROP_DOWN );
        mDestinationCombo.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) );
        mDestinationCombo.addModifyListener( new ModifyListener() {
            
            public void modifyText(ModifyEvent pE) {
                setPageComplete( checkPageCompletion() );
            }
        });
        
        Button btn = new Button( rowBody, SWT.PUSH );
        btn.setText( Messages.getString("UnoPackageExportPage.Browse") ); //$NON-NLS-1$
        btn.setLayoutData( new GridData( SWT.BEGINNING, SWT.CENTER, false, false ) );
        btn.addSelectionListener( new SelectionListener() {
            
            public void widgetSelected(SelectionEvent pE) {
                FileDialog dlg = new FileDialog( getShell(), SWT.SAVE );
                String path = dlg.open();
                if ( path != null ) {
                    mDestinationCombo.setText( path );
                }
            }
            
            public void widgetDefaultSelected(SelectionEvent pE) {
                widgetSelected( pE );
            }
        });
    }
    
    /**
     * Creates the options part of the dialog (the one at the bottom).
     */
    private void createOptionsGroup() {
        Composite body = (Composite)getControl();
        Composite groupBody = new Composite( body, SWT.NONE );
        groupBody.setLayout( new GridLayout( ) );
        groupBody.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, true, false ) );
        
        Label titleLbl = new Label( groupBody, SWT.NONE );
        titleLbl.setText( Messages.getString("UnoPackageExportPage.Options") ); //$NON-NLS-1$
        titleLbl.setLayoutData( new GridData( SWT.BEGINNING, SWT.BEGINNING, false, false ) );
        
        Composite rowsBody = new Composite( groupBody, SWT.NONE );
        rowsBody.setLayout( new GridLayout( ) );
        rowsBody.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, true, false ) );
        
        mOverwriteBox = new Button( rowsBody, SWT.CHECK );
        mOverwriteBox.setText( Messages.getString("UnoPackageExportPage.OverwriteWithoutWarning") ); //$NON-NLS-1$
        mOverwriteBox.setLayoutData( new GridData( SWT.BEGINNING, SWT.BEGINNING, false, false ) );
        
        mAutodeployBox = new Button( rowsBody, SWT.CHECK );
        mAutodeployBox.setText( Messages.getString("UnoPackageExportPage.AutoDeploy") ); //$NON-NLS-1$
        mAutodeployBox.setLayoutData( new GridData( SWT.BEGINNING, SWT.BEGINNING, false, false ) );
    }
    
    /**
     * @return <code>true</code> if the page is complete, <code>false</code> otherwise.
     */
    private boolean checkPageCompletion() {
        return !(0 == mDestinationCombo.getText().length()) && mProjectsList.getSelectionIndex() != -1;
    }
    
    /*
     * Data handling and filtering methods
     */
    
    /**
     * @param pRes the resource to be checked
     * 
     * @return <code>true</code> if the resource is hidden in the lists, <code>false</code>
     *      otherwise.
     */
    private boolean isHiddenResource( IResource pRes ) {
        boolean hidden = false;
        
        // Hide the binaries: they are always included from somewhere else
        IUnoidlProject unoprj = ProjectsManager.getProject( pRes.getProject().getName() );
        hidden |= unoprj.getFolder( unoprj.getBuildPath() ).equals( pRes );
        
        IFolder[] bins = unoprj.getBinFolders();
        for (IFolder bin : bins) {
            hidden |= bin.equals( pRes );
        }
        
        // Hide the hidden files
        hidden |= pRes.getName().startsWith( "." ); //$NON-NLS-1$
        
        // Hide files which are always included in the package
        hidden |= pRes.getName().equals( IUnoidlProject.DESCRIPTION_FILENAME );
        hidden |= pRes.getName().equals( "MANIFEST.MF" ); //$NON-NLS-1$
        hidden |= pRes.getName().equals( "manifest.xml" ); //$NON-NLS-1$
        hidden |= pRes.getName().equals( "types.rdb" ); //$NON-NLS-1$
        
        return hidden;
    }

    /**
     * @param pResourceType the type of the resources to return by the provider.
     * 
     * @return a content provider for <code>IResource</code>s that returns 
     * only children of the given resource type.
     */
    private ITreeContentProvider getResourceProvider( final int pResourceType ) {
        return new WorkbenchContentProvider() {
            public Object[] getChildren( Object pObject ) {
                ArrayList<IResource> results = new ArrayList<IResource>();
                
                if (pObject instanceof ArrayList<?>) {
                    ArrayList<?> objs = (ArrayList<?>)pObject;
                    for (Object o : objs) {
                        if ( o instanceof IResource ) {
                            results.add( ( IResource ) o );
                        }
                    }
                } else if (pObject instanceof IContainer) {
                    IResource[] members = null;
                    try {
                        members = ((IContainer) pObject).members();

                        //filter out the desired resource types
                        for (int i = 0; i < members.length; i++) {
                            //And the test bits with the resource types to see if they are what we want
                            if ((members[i].getType() & pResourceType) > 0 && !isHiddenResource( members[i] ) ) {
                                results.add(members[i]);
                            }
                        }
                    } catch (CoreException e) {
                    }
                }
                return results.toArray( );
            }
        };
    }

    /**
     * Stores the controls values for the next instance of the page.
     */
    public void saveWidgetValues() {
        IDialogSettings settings = getDialogSettings();
        if ( settings != null ) {
            settings.put( OVERWRITE_FILES, mOverwriteBox.getSelection() );
            settings.put( AUTODEPLOY, mAutodeployBox.getSelection() );
            
            String[] topItems = new String[ MAX_DESTINATION_STORED ];
            String firstItem = mDestinationCombo.getText().trim();
            topItems[0] = firstItem;
            int items_i = 0;
            int top_i = 0;
            int count = mDestinationCombo.getItemCount();
            while ( top_i < MAX_DESTINATION_STORED - 1 && items_i < count ) {
                String item = mDestinationCombo.getItem( items_i ).trim( );
                if ( mDestinationCombo.getSelectionIndex() != items_i ) {
                    topItems[ top_i + 1 ] = item;
                    top_i++;
                }
                items_i++;
            }
            settings.put( DESTINATION_HISTORY, topItems );
        }
    }
    
    /**
     * Loads the saved values of the controls states.
     */
    public void restoreWidgetValues() {
        IDialogSettings settings = getDialogSettings();
        if ( settings != null ) {
            mOverwriteBox.setSelection( settings.getBoolean( OVERWRITE_FILES ) );
            mAutodeployBox.setSelection( settings.getBoolean( AUTODEPLOY ) );
            String[] items = settings.getArray( DESTINATION_HISTORY );
            for (String item : items) {
                if ( item != null && !(0 == item.length()) ) {
                    mDestinationCombo.add( item );
                }
            }
        }
    }

    /**
     * @return the package model built from the data provided by the user or <code>null</code> if 
     *          something blocked the process.
     */
    public UnoPackage getPackageModel() {
        UnoPackage pack = null;
        
        try {
            // Test the existence of the destination: warning may be needed
            boolean doit = true;
            File destFile = new File( mDestinationCombo.getText() );
            if ( destFile.exists() && !mOverwriteBox.getSelection() ) {
                String msg = MessageFormat.format( 
                        Messages.getString("UnoPackageExportPage.OverwriteQuestion"), //$NON-NLS-1$ 
                        destFile.getPath() );
                doit = MessageDialog.openQuestion( getShell(), getTitle(), msg);
            }

            if ( doit ) {
                File prjFile = SystemHelper.getFile( mSelectedProject );
                
                // Export the library
                IFile library = null;
                ILanguageBuilder langBuilder = mSelectedProject.getLanguage().getLanguageBuidler();
                library = langBuilder.createLibrary( mSelectedProject );

                // Create the package model
                pack = UnoidlProjectHelper.createMinimalUnoPackage( mSelectedProject, destFile );
                pack.addToClean( SystemHelper.getFile( library ) );
                
                IFile descrFile = mSelectedProject.getFile( IUnoidlProject.DESCRIPTION_FILENAME );
                if ( descrFile.exists() ) {
                    File resFile = SystemHelper.getFile( descrFile );
                    pack.addContent( UnoPackage.getPathRelativeToBase( resFile, prjFile ),
                                    resFile );
                }

                // Add the additional content to the package
                List<?> items = mResourceGroup.getAllWhiteCheckedItems();
                for (Object item : items) {
                    if ( item instanceof IResource ) {
                        File resFile = SystemHelper.getFile( (IResource)item );
                        pack.addContent( UnoPackage.getPathRelativeToBase( resFile, prjFile ),
                                        resFile );
                    }
                }
                
                // Run the deployer
                if ( mAutodeployBox.getSelection() ) {
                    DeployerJob job = new DeployerJob( mSelectedProject.getOOo(), destFile );
                    
                    Display.getDefault().asyncExec( job );
                }
            }
        } catch ( Exception e ) {
            PluginLogger.error( Messages.getString("UnoPackageExportPage.LibraryCreationError"), e ); //$NON-NLS-1$
        }
        
        return pack;
    }
    
    /**
     * Refresh the selected project.
     */
    public void refreshProject() {
        try {
            // Refresh the project and return the status
            String prjName = mSelectedProject.getName();
            IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject( prjName );
            prj.refreshLocal( IResource.DEPTH_INFINITE, null );
        } catch (CoreException e) {
        }
    }
    
    /**
     * Thread performing the package deployment into OpenOffice.org.
     * 
     * @author Cédric Bosdonnat
     *
     */
    class DeployerJob implements Runnable {
        
        private IOOo mOOo;
        private File mDest;
        
        /**
         * Constructor.
         * 
         * @param pOoo the OpenOffice.org where to deploy
         * @param pDest the package to deploy
         */
        DeployerJob(IOOo pOoo, File pDest) {
            mOOo = pOoo;
            mDest = pDest;
        }
        
        /**
         * {@inheritDoc}
         */
        public void run() {
            if (mOOo.canManagePackages()) {
                mOOo.updatePackage(mDest, null);
            }
        }
    }
}