summaryrefslogtreecommitdiff
path: root/core/source/org/openoffice/ide/eclipse/core/editors/pack/PackageDescriptionSection.java
blob: 4b21c5eadd605ff8e27a7e69dd3a4f81ddf94aa6 (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
/*************************************************************************
 *
 * $RCSfile: PackageDescriptionSection.java,v $
 *
 * $Revision: 1.3 $
 *
 * last change: $Author: cedricbosdo $ $Date: 2007/11/25 20:32:28 $
 *
 * The Contents of this file are made available subject to the terms of
 * the GNU Lesser General Public License Version 2.1
 *
 * Sun Microsystems Inc., October, 2000
 *
 *
 * GNU Lesser General Public License Version 2.1
 * =============================================
 * Copyright 2000 by Sun Microsystems, Inc.
 * 901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 * 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: Sun Microsystems, Inc..
 *
 * Copyright: 2002 by Sun Microsystems, Inc.
 *
 * All Rights Reserved.
 *
 * Contributor(s): Cedric Bosdonnat
 *
 *
 ************************************************************************/
package org.openoffice.ide.eclipse.core.editors.pack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
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.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.openoffice.ide.eclipse.core.editors.Messages;
import org.openoffice.ide.eclipse.core.gui.LocaleCellProvider;
import org.openoffice.ide.eclipse.core.gui.ProjectSelectionDialog;
import org.openoffice.ide.eclipse.core.model.pack.UnoPackage;

/**
 * @author cedricbosdo
 *
 */
public class PackageDescriptionSection extends SectionPart {
    
    private static final String P_NAME = "__p_name"; //$NON-NLS-1$
    private static final String P_LOCALE = "__p_locale"; //$NON-NLS-1$
    private static final int NAME_WIDTH = 200;
    private static final int LOCALE_WIDTH = 200;
    
    private PackageFormPage mPage;
    private TableViewer mTableViewer;
    
    private HashMap<IFile, Locale> mDescriptions = new HashMap<IFile, Locale>();
    
    /**
     * Constructor.
     * 
     * @param pPage the package page where to create the section
     */
    public PackageDescriptionSection(PackageFormPage pPage) {
        super(pPage.getManagedForm().getForm().getBody(), 
                pPage.getManagedForm().getToolkit(), Section.TITLE_BAR);
        
        mPage = pPage;
        
        Section section = getSection();
        
        section.setText(Messages.getString("PackageDescriptionSection.Title")); //$NON-NLS-1$
        GridData gd = new GridData(GridData.FILL_BOTH);
        gd.horizontalSpan = 2;
        section.setLayoutData(gd);
        
        Composite clientArea = mPage.getManagedForm().getToolkit().createComposite(section);
        clientArea.setLayout(new GridLayout(2, false));
        
        // Add the list here
        Table table = new Table(clientArea, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
        table.setLayoutData(new GridData(GridData.FILL_BOTH));
        
        TableColumn nameCol = new TableColumn(table, SWT.LEFT);
        nameCol.setMoveable(false);
        nameCol.setResizable(false);
        nameCol.setWidth(NAME_WIDTH);
        
        TableColumn localeCol = new TableColumn(table, SWT.LEFT);
        localeCol.setMoveable(false);
        localeCol.setResizable(false);
        localeCol.setWidth(LOCALE_WIDTH);
        
        mTableViewer = new TableViewer(table);
        mTableViewer.setColumnProperties(new String[]{P_NAME, P_LOCALE});
        mTableViewer.setCellEditors(new CellEditor[]{
            null,
            new LocaleCellProvider(table)
        });
        mTableViewer.setContentProvider(new DescrContentProvider());
        mTableViewer.setLabelProvider(new DescrLabelProvider());
        mTableViewer.setCellModifier(new DescrCellModifier());
        
        
        // Add the buttons here
        createButtons(clientArea);
        
        mTableViewer.setInput(this);
        section.setClient(clientArea);
    }

    /**
     * @return the package descriptions shown in the section
     */
    public Map<Locale, IFile> getDescriptions() {
        HashMap<Locale, IFile> descriptions = new HashMap<Locale, IFile>();
        
        Iterator<Entry<IFile, Locale>> iter = mDescriptions.entrySet().iterator();
        while (iter.hasNext()) {
            Entry<IFile, Locale> entry = iter.next();
            descriptions.put(entry.getValue(), entry.getKey());
        }
        
        return descriptions;
    }
    
    /**
     * Set the package descriptions to show in the section.
     * 
     * @param pDescriptions the descriptions to show.
     */
    public void setDescriptions(Map<Locale, IFile> pDescriptions) {
        mDescriptions.clear();
        Iterator<Entry<Locale, IFile>> iter = pDescriptions.entrySet().iterator();
        
        while (iter.hasNext()) {
            Entry<Locale, IFile> entry = iter.next();
            mDescriptions.put(entry.getValue(), entry.getKey());
        }
        mTableViewer.refresh();
    }
    
    /**
     * Tells the listeners when the descriptions have changed.
     */
    private void fireSectionModified() {
        PackagePropertiesEditor editor = (PackagePropertiesEditor)mPage.getEditor();
        
        Map<Locale, IFile> descriptions = getDescriptions();
        editor.getModel().clearDescriptions();
        Iterator<Entry<Locale, IFile>> iter = descriptions.entrySet().iterator();
        while (iter.hasNext()) {
            Entry<Locale, IFile> entry = iter.next();
            editor.getModel().addDescriptionFile(entry.getValue(), entry.getKey());
        }
    }
    
    /**
     * Create the buttons of the section.
     * 
     * @param pParent the composite where to create the buttons
     */
    private void createButtons(Composite pParent) {
        Composite buttons = mPage.getManagedForm().getToolkit().createComposite(pParent);
        buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
        buttons.setLayout(new GridLayout());
        
        Button add = mPage.getManagedForm().getToolkit().createButton(
                buttons, Messages.getString("PackageDescriptionSection.AddButton"), SWT.PUSH); //$NON-NLS-1$
        add.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
        add.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent pEvent) {
                // Open the folder chooser dialog and refresh the table
                IProject prj = mPage.getProject();
                PackagePropertiesEditor editor = (PackagePropertiesEditor)mPage.getEditor();
                
                ProjectSelectionDialog dlg = new ProjectSelectionDialog(prj, 
                        Messages.getString("PackageDescriptionSection.AddDescription")); //$NON-NLS-1$
                
                ArrayList<IResource> hiddenResources = new ArrayList<IResource>();
                hiddenResources.add(prj.getFolder("build")); //$NON-NLS-1$
                hiddenResources.add(prj.getFolder("bin")); //$NON-NLS-1$
                hiddenResources.add(prj.getFile("package.properties")); //$NON-NLS-1$
                hiddenResources.addAll(editor.getModel().getBasicLibraries());
                hiddenResources.addAll(editor.getModel().getDialogLibraries());
                hiddenResources.addAll(editor.getModel().getContents());
                hiddenResources.addAll(editor.getModel().getDescriptionFiles().values());
                hiddenResources.addAll(UnoPackage.getContainedFile(prj));
                dlg.setFilteredElements(hiddenResources);
                
                if (ProjectSelectionDialog.OK == dlg.open()) {
                    IResource res = dlg.getSelected();
                    if (res instanceof IFile) {
                        mDescriptions.put((IFile)res, Locale.getDefault());
                        mTableViewer.add(res);
                        mTableViewer.refresh();
                        fireSectionModified();
                    }
                }
            }
        });
        
        Button del = mPage.getManagedForm().getToolkit().createButton(
                buttons, Messages.getString("PackageDescriptionSection.DelButton"), SWT.PUSH); //$NON-NLS-1$
        del.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
        del.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent pEvent) {
                // Delete the selected line
                ISelection sel = mTableViewer.getSelection();
                if (sel instanceof IStructuredSelection) {
                    IStructuredSelection structuredSel = (IStructuredSelection)sel;
                    Iterator<?> iter = structuredSel.iterator();
                    while (iter.hasNext()) {
                        Object o = iter.next();
                        mDescriptions.remove(o);
                        mTableViewer.remove(o);
                    }
                    fireSectionModified();
                    mTableViewer.refresh();
                }
            }
        });
    }
    
    /**
     * Provides the data for the descriptions table.
     * 
     * @author cedricbosdo
     */
    private class DescrContentProvider implements IStructuredContentProvider {

        /**
         * {@inheritDoc}
         */
        public Object[] getElements(Object pInputElement) {
            return mDescriptions.keySet().toArray();
        }

        /**
         * {@inheritDoc}
         */
        public void dispose() {
        }

        /**
         * {@inheritDoc}
         */
        public void inputChanged(Viewer pViewer, Object pOldInput, Object pNewInput) {
        }
    }
    
    /**
     * Modification handler of the description table.
     * 
     * @author cedricbosdo
     */
    private class DescrCellModifier implements ICellModifier {

        /**
         * {@inheritDoc}
         */
        public boolean canModify(Object pElement, String pProperty) {
            return pProperty.equals(P_LOCALE);
        }

        /**
         * {@inheritDoc}
         */
        public Object getValue(Object pElement, String pProperty) {
            Object value = null;
            if (pProperty.equals(P_LOCALE)) {
                value = mDescriptions.get(pElement);
            }
            return value;
        }

        /**
         * {@inheritDoc}
         */
        public void modify(Object pElement, String pProperty, Object pValue) {
            if (pProperty.equals(P_LOCALE) && pValue instanceof Locale) {
                if (pElement instanceof TableItem) {
                    Object o = ((TableItem)pElement).getData();
                    if (o instanceof IFile) {
                        mDescriptions.put((IFile)o, (Locale)pValue);
                        mTableViewer.refresh(o);
                        fireSectionModified();
                    }
                }
            }
        }
    }
    
    /**
     * Provides the labels and images to show in the descriptions table.
     * 
     * @author cedricbosdo
     */
    private class DescrLabelProvider extends LabelProvider implements ITableLabelProvider {

        /**
         * {@inheritDoc}
         */
        public Image getColumnImage(Object pElement, int pColumnIndex) {
            Image image = null;
            if (pColumnIndex == 0 && pElement instanceof IAdaptable) {
                IAdaptable adaptable = (IAdaptable)pElement;
                IWorkbenchAdapter adapter = (IWorkbenchAdapter)adaptable.getAdapter(IWorkbenchAdapter.class);
                image = adapter.getImageDescriptor(pElement).createImage();
            }
            return image;
        }

        /**
         * {@inheritDoc}
         */
        public String getColumnText(Object pElement, int pColumnIndex) {
            String label = null;
            if (pColumnIndex == 0 && pElement instanceof IFile) {
                label = ((IResource)pElement).getProjectRelativePath().toOSString();
            } else if (pColumnIndex == 1) {
                label = mDescriptions.get(pElement).getDisplayName();
            }
            return label;
        }
    }
}