summaryrefslogtreecommitdiff
path: root/core/source/org/openoffice/ide/eclipse/core/launch/office/PackageConfigTab.java
blob: b463f44ceff61d3278cc9e561d959eb06f5f6b2f (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
/*************************************************************************
 *
 * 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.launch.office;

import java.util.ArrayList;
import java.util.List;

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.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.openoffice.ide.eclipse.core.OOEclipsePlugin;
import org.openoffice.ide.eclipse.core.PluginLogger;
import org.openoffice.ide.eclipse.core.gui.PackageContentSelector;
import org.openoffice.ide.eclipse.core.i18n.ImagesConstants;
import org.openoffice.ide.eclipse.core.model.ProjectsManager;

/**
 * Tab for selecting the content of the package to test.
 * 
 * @author Cédric Bosdonnat
 *
 */
public class PackageConfigTab extends AbstractLaunchConfigurationTab {

    PackageContentSelector mContentSelector;
    
    /**
     * {@inheritDoc}
     */
    public void createControl(Composite pParent) {
        
        Composite body = new Composite( pParent, SWT.NONE );
        body.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
        body.setLayout( new GridLayout() );
        
        mContentSelector = new PackageContentSelector( body, SWT.NONE );
        
        setControl( body );
    }

    /**
     * {@inheritDoc}
     */
    public String getName() {
        return "Package content";
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public Image getImage() {
        return OOEclipsePlugin.getImage(ImagesConstants.PACKAGE_CONTENT);
    }

    /**
     * {@inheritDoc}
     */
    public void initializeFrom(ILaunchConfiguration pConfiguration) {
        try {
            String prjName = pConfiguration.getAttribute( IOfficeLaunchConstants.PROJECT_NAME, new String() );
            IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject( prjName );
            mContentSelector.setProject( prj );
            
            String paths = pConfiguration.getAttribute( IOfficeLaunchConstants.CONTENT_PATHS, new String() );
            if ( paths.isEmpty() ) {
                mContentSelector.loadData( ProjectsManager.getProject( prjName ) );
            } else {
                String[] pathsItems = paths.split( IOfficeLaunchConstants.PATHS_SEPARATOR );
                ArrayList<IResource> selected = new ArrayList<IResource>();
                for (String path : pathsItems) {
                    IResource res = prj.findMember( path );
                    if ( res != null ) {
                        selected.add( res );
                    }
                }
                mContentSelector.setSelected( selected );
            }
        } catch (CoreException e) {
            PluginLogger.error(Messages.OfficeTab_Configurationerror, e);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void performApply(ILaunchConfigurationWorkingCopy pConfiguration) {
        List<?> selected = mContentSelector.getSelected();
        String paths = new String();
        for (Object obj : selected) {
            if ( obj instanceof IResource ) {
                IResource res = (IResource)obj;
                
                if ( !paths.isEmpty() ) {
                    paths += IOfficeLaunchConstants.PATHS_SEPARATOR;
                }
                paths += res.getProjectRelativePath().toString();
            }
        }
        pConfiguration.setAttribute( IOfficeLaunchConstants.CONTENT_PATHS, paths );
    }

    /**
     * {@inheritDoc}
     */
    public void setDefaults(ILaunchConfigurationWorkingCopy pConfiguration) {
        try {
            String prjName = pConfiguration.getAttribute( IOfficeLaunchConstants.PROJECT_NAME, new String() );
            if ( !prjName.isEmpty() ) {
                IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject( prjName );
                mContentSelector.setProject( prj );
                
                mContentSelector.loadData( ProjectsManager.getProject( prjName ) );
            }
        } catch (CoreException e) {
            // Don't do anything in that case
        }
    }
}