summaryrefslogtreecommitdiff
path: root/core/source/org/openoffice/ide/eclipse/core/wizards/pages/ManifestExportPage.java
blob: f29f55c9e974d9dacc11fe0682bbc47972c3f8dc (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
/*************************************************************************
 *
 * 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 org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
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.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
import org.eclipse.ui.dialogs.ISelectionStatusValidator;
import org.eclipse.ui.dialogs.SaveAsDialog;
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.model.IUnoidlProject;
import org.openoffice.ide.eclipse.core.model.language.LanguageExportPart;
import org.openoffice.ide.eclipse.core.model.pack.UnoPackage;
import org.openoffice.ide.eclipse.core.wizards.Messages;

/**
 * Second page of the new OXT package export wizard.
 * 
 * @author Cédric Bosdonnat
 *
 */
public class ManifestExportPage extends WizardPage {

    public static final int HORIZONTAL_INDENT = 20;
    
    public static final String MANIFEST_FILENAME = "manifest.xml"; //$NON-NLS-1$
    
    private static final int LAYOUT_COLS = 3;
    
    private IUnoidlProject mProject;
    private LanguageExportPart mLangPart;

    private Button mGenerateManifestBtn;
    private Button mReuseManifestBtn;

    private Button mSaveManifestBtn;
    private Label mSaveRowLbl;
    private Text mSaveRowTxt;
    private Button mSaveRowBtn;
    private Label mLoadRowLbl;
    private Text mLoadRowTxt;
    private Button mLoadRowBtn;

    /**
     * Constructor.
     * 
     * @param pPageName the page name
     * @param pProject the project to export 
     */
    public ManifestExportPage( String pPageName, IUnoidlProject pProject ) {
        super(pPageName);
        setTitle( Messages.getString("ManifestExportPage.Title") ); //$NON-NLS-1$
        setDescription( Messages.getString("ManifestExportPage.Description") ); //$NON-NLS-1$
        
        mProject = pProject;
    }
    
    /**
     * @param pProject the UNO project selected for the wizard.
     */
    public void setProject( IUnoidlProject pProject ) {
        mProject = pProject;
        reloadLanguagePart();
    }

    /**
     * @return the UNO project to export as a package
     */
    public IUnoidlProject getProject( ) {
        return mProject;
    }
    
    /**
     * Set the proper manifest.xml file to the package model from the user selection.
     * 
     * @param pModel the model to change
     */
    public void configureManifest(UnoPackage pModel) {
        IFile saveFile = getSaveManifestFile( );
        if ( saveFile != null ) {
            pModel.setSaveManifestFile( saveFile );
        }
        
        IFile readFile = getReadManifestFile( );
        if ( readFile != null ) {
            pModel.setReadManifestFile( readFile );
        }
    }

    /**
     * Create the build scripts for the package model if required by the user.
     * 
     * @param pModel the model to export
     */
    public void createBuildScripts(UnoPackage pModel) {
        mLangPart.doFinish( pModel );
    }
    
    
    /**
     * Define the manifest file to generate and force the dialog to this value.
     * 
     * @param pFile the file to set or <code>null</code> to remove the current 
     *      existing value
     */
    public void setManifestPath(IFile pFile) {
        // FIXME Ugly hack due to some bug in the GTK buttons events
        if ( pFile != null && !pFile.exists() ) {
            mSaveManifestBtn.setSelection( true );
            mReuseManifestBtn.setSelection( false );
            mGenerateManifestBtn.setSelection( true );
            mSaveRowTxt.setText( pFile.getFullPath().toString() );
            setSaveRowEnabled( true );
            setLoadRowEnabled( false );
        } else if ( pFile != null && pFile.exists() ) {
            mGenerateManifestBtn.setSelection( false );
            mReuseManifestBtn.setSelection( true );
            mSaveManifestBtn.setSelection( false );
            mLoadRowTxt.setText( pFile.getFullPath().toString() );
            setLoadRowEnabled( true );
            setSaveRowEnabled( false );
        } else {
            mGenerateManifestBtn.setSelection( true );
            mReuseManifestBtn.setSelection( false );
            mSaveManifestBtn.setSelection( true );
            mSaveRowTxt.setText( new String( ) );
            setSaveRowEnabled( true );
            setLoadRowEnabled( false );
        }
    }
    
    /**
     * {@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 );
        
        // Add the controls
        Label title = new Label( body, SWT.NONE );
        title.setText( Messages.getString("ManifestExportPage.DefineManifestText") ); //$NON-NLS-1$
        
        createOptionsGroup( body );
        
        // Add the language specific controls
        reloadLanguagePart();
        
        // Load the default values
        mGenerateManifestBtn.setSelection( true );
    }

    /**
     * Enables the manifest save row.
     * 
     * @param pEnabled <code>true</code> to enable all the controls of the row
     *      <code>false</code> otherwise.
     */
    private void setSaveRowEnabled( boolean pEnabled ) {
        mSaveRowLbl.setEnabled( pEnabled );
        mSaveRowTxt.setEnabled( pEnabled );
        mSaveRowBtn.setEnabled( pEnabled );
    }
    
    /**
     * Enables the manifest load row.
     * 
     * @param pEnabled <code>true</code> to enable all the controls of the row
     *      <code>false</code> otherwise.
     */
    private void setLoadRowEnabled( boolean pEnabled ) {
        mLoadRowLbl.setEnabled( pEnabled );
        mLoadRowTxt.setEnabled( pEnabled );
        mLoadRowBtn.setEnabled( pEnabled );
    }
    
    /**
     * Create the manifest save/reuse options.
     * 
     * @param pParent the parent composite where to create the controls
     */
    private void createOptionsGroup( Composite pParent ) {
        Composite body = new Composite( pParent, SWT.NONE );
        body.setLayout( new GridLayout( ) );
        GridData gd = new GridData( SWT.FILL, SWT.BEGINNING, true, false );
        gd.horizontalIndent = HORIZONTAL_INDENT;
        body.setLayoutData( gd );
        
        mGenerateManifestBtn = new Button( body, SWT.RADIO );
        mGenerateManifestBtn.setText( Messages.getString("ManifestExportPage.GenerateManifestText") ); //$NON-NLS-1$
        mGenerateManifestBtn.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, true, false ) );
        mGenerateManifestBtn.addSelectionListener( new SelectionListener() {
            
            public void widgetSelected(SelectionEvent pE) {
                boolean selection = mGenerateManifestBtn.getSelection();
                mSaveManifestBtn.setEnabled( selection );
                
                boolean saveSelection = mSaveManifestBtn.getSelection();
                setSaveRowEnabled( selection && saveSelection );
            }
            
            public void widgetDefaultSelected(SelectionEvent pE) {
                widgetSelected( pE );
            }
        });
        
        // Create the controls for the manifest generation
        Composite saveOptions = new Composite( body, SWT.NONE );
        saveOptions.setLayout( new GridLayout( LAYOUT_COLS, false ) );
        gd = new GridData( SWT.FILL, SWT.BEGINNING, true, false );
        gd.horizontalIndent = HORIZONTAL_INDENT;
        saveOptions.setLayoutData( gd );
        
        createManifestSaveOptions( saveOptions );
        
        mReuseManifestBtn = new Button( body, SWT.RADIO );
        mReuseManifestBtn.setText( Messages.getString("ManifestExportPage.UserManifestText") ); //$NON-NLS-1$
        mReuseManifestBtn.setLayoutData( new GridData( SWT.FILL, SWT.BEGINNING, true, false ) );
        mReuseManifestBtn.addSelectionListener( new SelectionListener() {
            
            public void widgetSelected(SelectionEvent pE) {
                boolean enabled = mReuseManifestBtn.getSelection();
                setLoadRowEnabled( enabled );
            }
            
            public void widgetDefaultSelected(SelectionEvent pE) {
                widgetSelected( pE );
            }
        });
        
        // Create the controls for the manifest file selection
        Composite loadOptions = new Composite( body, SWT.NONE );
        loadOptions.setLayout( new GridLayout( LAYOUT_COLS, false ) );
        gd = new GridData( SWT.FILL, SWT.BEGINNING, true, false );
        gd.horizontalIndent = HORIZONTAL_INDENT;
        loadOptions.setLayoutData( gd );
        
        createManifestLoadRow( loadOptions );
    }

    /**
     * Create the manifest file save row.
     * 
     * @param pParent the parent composite where to create the controls
     */
    private void createManifestSaveOptions( Composite pParent ) {
        mSaveManifestBtn = new Button( pParent, SWT.CHECK );
        mSaveManifestBtn.setText( Messages.getString("ManifestExportPage.SaveManifestText") ); //$NON-NLS-1$
        
        GridData gd = new GridData( SWT.FILL, SWT.BEGINNING, true, false );
        gd.horizontalSpan = LAYOUT_COLS;
        mSaveManifestBtn.setLayoutData( gd );
        mSaveManifestBtn.addSelectionListener( new SelectionListener() {
            
            public void widgetSelected(SelectionEvent pE) {
                boolean enabled = mSaveManifestBtn.getSelection();
                setSaveRowEnabled( enabled );
            }
            
            public void widgetDefaultSelected(SelectionEvent pE) {
                widgetSelected( pE );
            }
        });
        
        mSaveRowLbl = new Label( pParent, SWT.NONE );
        mSaveRowLbl.setText( Messages.getString("ManifestExportPage.SaveRowLabel") ); //$NON-NLS-1$
        mSaveRowLbl.setLayoutData( new GridData( SWT.BEGINNING, SWT.CENTER, false, false ) );
        
        mSaveRowTxt = new Text( pParent, SWT.SINGLE | SWT.BORDER );
        mSaveRowTxt.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) );
        
        mSaveRowBtn = new Button( pParent, SWT.PUSH );
        mSaveRowBtn.setText( Messages.getString("ManifestExportPage.Browse") ); //$NON-NLS-1$
        mSaveRowBtn.setLayoutData( new GridData( SWT.END, SWT.CENTER, false, false ) );
        mSaveRowBtn.addSelectionListener( new SelectionAdapter() {
            
            @Override
            public void widgetSelected(SelectionEvent pE) {
                SaveAsDialog dlg = new SaveAsDialog( getShell() );
                dlg.setOriginalName( MANIFEST_FILENAME );
                dlg.setOriginalFile( mProject.getFile( MANIFEST_FILENAME ) );
                dlg.create();
                dlg.getShell().setText( Messages.getString("ManifestExportPage.SaveDialogTitle") ); //$NON-NLS-1$
                dlg.setMessage( Messages.getString("ManifestExportPage.SaveDialogMessage") ); //$NON-NLS-1$
                
                if ( dlg.open() == Window.OK ) {
                    mSaveRowTxt.setText( dlg.getResult().toString() );
                }
            }
        });
        
        setSaveRowEnabled( false );
    }
    
    /**
     * Create the manifest file load row.
     * 
     * @param pParent the parent composite where to create the controls
     */
    private void createManifestLoadRow( Composite pParent ) {
        mLoadRowLbl = new Label( pParent, SWT.NONE );
        mLoadRowLbl.setText( Messages.getString("ManifestExportPage.LoadRowLabel") ); //$NON-NLS-1$
        mLoadRowLbl.setLayoutData( new GridData( SWT.BEGINNING, SWT.CENTER, false, false ) );
        
        mLoadRowTxt = new Text( pParent, SWT.SINGLE | SWT.BORDER );
        mLoadRowTxt.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) );
        
        mLoadRowBtn = new Button( pParent, SWT.PUSH );
        mLoadRowBtn.setText( Messages.getString("ManifestExportPage.Browse") ); //$NON-NLS-1$
        mLoadRowBtn.setLayoutData( new GridData( SWT.END, SWT.CENTER, false, false ) );
        mLoadRowBtn.addSelectionListener( new SelectionAdapter() {
            
            @Override
            public void widgetSelected(SelectionEvent pE) {
                // Create a workspace file selection dialog
                ElementTreeSelectionDialog dlg = new ElementTreeSelectionDialog( 
                        getShell(), new WorkbenchLabelProvider(), new WorkbenchContentProvider() );
                dlg.setAllowMultiple( false );
                dlg.setValidator( new ISelectionStatusValidator() {
                    
                    public IStatus validate(Object[] pSelection) {
                        Status status = new Status( IStatus.ERROR, OOEclipsePlugin.OOECLIPSE_PLUGIN_ID, new String() );
                        // only single selection
                        if ( pSelection.length == 1 && ( pSelection[0] instanceof IFile ) ) {
                            status = new Status( IStatus.OK, OOEclipsePlugin.OOECLIPSE_PLUGIN_ID, new String() );
                        }
                        return status;
                    }
                });
                
                dlg.addFilter( new ViewerFilter() {
                    
                    @Override
                    public boolean select(Viewer pViewer, Object pParentElement, Object pElement) {
                        boolean select = true;
                        if ( pElement instanceof IResource ) {
                            IResource res = ( IResource )pElement;
                            select &= !res.getName().startsWith( "." ); //$NON-NLS-1$
                            select &= ( res instanceof IContainer ) ||
                                ( res.getName().equals( MANIFEST_FILENAME ) );
                        }
                        return select;
                    }
                });
                dlg.setTitle( Messages.getString("ManifestExportPage.LoadDialogTitle") ); //$NON-NLS-1$
                dlg.setMessage( Messages.getString("ManifestExportPage.LoadDialogMessage") ); //$NON-NLS-1$
                dlg.setStatusLineAboveButtons( true );
                dlg.setInput( ResourcesPlugin.getWorkspace().getRoot() );
                dlg.setInitialSelection( getProject().getFile( MANIFEST_FILENAME ) );
                if ( dlg.open() == Window.OK ) {
                    Object result = dlg.getFirstResult();
                    IFile file = ( IFile )result;
                    mLoadRowTxt.setText( file.getFullPath().toString() );
                }
            }
        });
        
        setLoadRowEnabled( false );
    }
    
    /**
     * Change the language specific part from the selected project.
     */
    private void reloadLanguagePart( ) {
        if ( mLangPart != null ) {
            mLangPart.dispose( );
        }
        
        // Add the language specific controls
        if ( mProject != null ) {
            mLangPart = mProject.getLanguage().getExportBuildPart();
            if ( mLangPart != null ) {
                mLangPart.setPage( this );
                Composite body = ( Composite ) getControl();
                if ( body != null ) {
                    // The body can be null before the page creation
                    mLangPart.createControls( body );
                    body.layout();
                }
            }
        }
    }

    /**
     * @return the selected file to reuse or <code>null</code>
     */
    private IFile getReadManifestFile() {
        IFile file = null;
        if ( mReuseManifestBtn.getSelection() ) {
            IPath path = new Path( mLoadRowTxt.getText().trim() );
            file = ResourcesPlugin.getWorkspace().getRoot().getFile( path );
        }
        
        return file;
    }

    /**
     * @return the selected file to generate or <code>null</code>
     */
    private IFile getSaveManifestFile() {
        IFile file = null;
        if ( mGenerateManifestBtn.getSelection() && mSaveManifestBtn.getSelection() ) {
            IPath path = new Path( mSaveRowTxt.getText().trim() );
            file = ResourcesPlugin.getWorkspace().getRoot().getFile( path );
        }
        return file;
    }
}