summaryrefslogtreecommitdiff
path: root/core/source/org/openoffice/ide/eclipse/core/gui/PackageContentSelector.java
blob: b1194b64fc6d555ef0376650b11b4f0d1d85e565 (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
/*************************************************************************
 *
 * 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 2010 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: 2010 by Cédric Bosdonnat
 *
 * All Rights Reserved.
 * 
 ************************************************************************/
package org.openoffice.ide.eclipse.core.gui;

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.viewers.ITreeContentProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
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.PluginLogger;
import org.openoffice.ide.eclipse.core.model.IUnoidlProject;
import org.openoffice.ide.eclipse.core.model.ProjectsManager;
import org.openoffice.ide.eclipse.core.utils.FilesFinder;

/**
 * Common helper GUI part to select elements to add in the UNO package to be exported.
 * 
 * @author Cedric Bosdonnat
 *
 */
@SuppressWarnings("restriction")
public class PackageContentSelector extends Composite {
    
    private ResourceTreeAndListGroup mResourceGroup;
    
    /**
     * Constructor based on SWT composite's one.
     * 
     * @param pParent the parent composite.
     * @param pStyle the SWT style to give to the composite
     */
    public PackageContentSelector( Composite pParent, int pStyle ) {
        super( pParent, pStyle );
        
        setLayout( new GridLayout( 2, false ) );
        setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
        
        mResourceGroup = new ResourceTreeAndListGroup(this, new ArrayList<Object>(),
                getResourceProvider(IResource.FOLDER | IResource.FILE),
                WorkbenchLabelProvider.getDecoratingWorkbenchLabelProvider(),
                getResourceProvider(IResource.FILE), WorkbenchLabelProvider
                        .getDecoratingWorkbenchLabelProvider(), SWT.NONE,
                DialogUtil.inRegularFontMode(this));
    }
    
    /**
     * Set the project to work on.
     * 
     * @param pPrj the project to show.
     */
    public void setProject(IProject pPrj) {
        mResourceGroup.setRoot( pPrj );
    }
    
    /**
     * Populate the resource view with some fresh data.
     * 
     * @param pSelectedProject the UNO project for which to show the resources
     */
    public void loadData( IUnoidlProject pSelectedProject ) {
        // Select the XCU / XCS files by default
        IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject( pSelectedProject.getName() );
        FilesFinder finder = new FilesFinder( 
            new String[] { IUnoidlProject.XCU_EXTENSION, IUnoidlProject.XCS_EXTENSION } );
        try {
            finder.addExclude( pSelectedProject.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 );
        }
    }
    
    /**
     * @return all the selected items 
     */
    public List<?> getSelected() {
        return mResourceGroup.getAllWhiteCheckedItems();
    }
    
    /**
     * Set the given resources to selected.
     * 
     * @param pSelected the items to select.
     */
    public void setSelected( List<IResource> pSelected ) {
        for (IResource res : pSelected) {
            mResourceGroup.initialCheckTreeItem( res );   
        }
    }
    
    /**
     * @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( );
            }
        };
    }
    
    /**
     * @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;
    }
}