Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ab419ed6c2c4133468c9d3ab8aa9108567688acd (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Igor Fedorenko <igorfie@yahoo.com> -
 *             Fix for Bug 136921 [IDE] New File dialog locks for 20 seconds
 *       Red Hat Inc. - modified this file to work with ChangeLog Plugin
 *******************************************************************************/
package org.eclipse.linuxtools.internal.changelog.core.actions;

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

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.linuxtools.internal.changelog.core.Messages;
import org.eclipse.osgi.util.TextProcessor;
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.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.part.DrillDownComposite;

/**
 * Workbench-level composite for choosing a container.
 */
public class ChangeLogContainerSelectionGroup extends Composite {

    // Enable user to type in new container name
    private boolean allowNewContainerName = true;

    // show all projects by default
    private boolean showClosedProjects = true;

    // Last selection made by user
    private IContainer selectedContainer;

    // handle on parts
    private Text containerNameField;

    TreeViewer treeViewer;

    private IContainer initialSelection;

    // the message to display at the top of this dialog
    private static final String DEFAULT_MSG_NEW_ALLOWED = Messages.getString("ChangeLogContainerSelectionGroup.DefaultMessageNewAllowed"); //$NON-NLS-1$

    private static final String DEFAULT_MSG_SELECT_ONLY = Messages.getString("ChangeLogContainerSelectionGroup.DefaultMessageSelectOnly"); //$NON-NLS-1$

    // sizing constants
    private static final int SIZING_SELECTION_PANE_WIDTH = 320;

    private static final int SIZING_SELECTION_PANE_HEIGHT = 300;

    /**
     * Creates a new instance of the widget.
     *
     * @param parent
     *            The parent widget of the group.
     * @param allowNewContainerName
     *            Enable the user to type in a new container name instead of
     *            just selecting from the existing ones.
     * @param message
     *            The text to present to the user.
     * @param showClosedProjects
     *            Whether or not to show closed projects.
     */
    public ChangeLogContainerSelectionGroup(Composite parent, boolean allowNewContainerName, String message,
            boolean showClosedProjects, IContainer initialSelection) {
        this(parent, allowNewContainerName, message,
                showClosedProjects, SIZING_SELECTION_PANE_HEIGHT,
                SIZING_SELECTION_PANE_WIDTH, initialSelection);
    }

    /**
     * Creates a new instance of the widget.
     *
     * @param parent
     *            The parent widget of the group.
     * @param allowNewContainerName
     *            Enable the user to type in a new container name instead of
     *            just selecting from the existing ones.
     * @param message
     *            The text to present to the user.
     * @param showClosedProjects
     *            Whether or not to show closed projects.
     * @param heightHint
     *            height hint for the drill down composite
     * @param widthHint
     *            width hint for the drill down composite
     */
    public ChangeLogContainerSelectionGroup(Composite parent,
            boolean allowNewContainerName, String message,
            boolean showClosedProjects, int heightHint, int widthHint, IContainer initialSelection) {
        super(parent, SWT.NONE);
        this.allowNewContainerName = allowNewContainerName;
        this.showClosedProjects = showClosedProjects;
        this.initialSelection = initialSelection;
        if (message != null) {
            createContents(message, heightHint, widthHint);
        } else if (allowNewContainerName) {
            createContents(DEFAULT_MSG_NEW_ALLOWED, heightHint, widthHint);
        } else {
            createContents(DEFAULT_MSG_SELECT_ONLY, heightHint, widthHint);
        }
    }

    /**
     * The container selection has changed in the tree view. Update the
     * container name field value and notify all listeners.
     *
     * @param container
     *            The container that changed
     */
    public void containerSelectionChanged(IContainer container) {
        selectedContainer = container;

        if (allowNewContainerName) {
            if (container == null) {
                containerNameField.setText("");//$NON-NLS-1$
            } else {
                String text = TextProcessor.process(container.getFullPath()
                        .makeRelative().toString());
                containerNameField.setText(text);
                containerNameField.setToolTipText(text);
            }
        }
    }

    /**
     * Creates the contents of the composite.
     *
     * @param message message
     */
    public void createContents(String message) {
        createContents(message, SIZING_SELECTION_PANE_HEIGHT,
                SIZING_SELECTION_PANE_WIDTH);
    }

    /**
     * Creates the contents of the composite.
     *
     * @param message message
     * @param heightHint height hint for the drill down composite
     * @param widthHint specifies the perfered width in pixels
     */
    private void createContents(String message, int heightHint, int widthHint) {
        GridLayout layout = new GridLayout();
        layout.marginWidth = 0;
        setLayout(layout);
        setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        Label label = new Label(this, SWT.WRAP);
        label.setText(message);
        label.setFont(this.getFont());

        if (allowNewContainerName) {
            containerNameField = new Text(this, SWT.SINGLE | SWT.BORDER);
            GridData gd = new GridData(GridData.FILL_HORIZONTAL);
            gd.widthHint = widthHint;
            containerNameField.setLayoutData(gd);
            containerNameField.setFont(this.getFont());
        } else {
            // filler...
            new Label(this, SWT.NONE);
        }

        createTreeViewer(heightHint);
        Dialog.applyDialogFont(this);
    }

    /**
     * Returns a new drill down viewer for this dialog.
     *
     * @param heightHint
     *            height hint for the drill down composite
     */
    private void createTreeViewer(int heightHint) {
        // Create drill down.
        DrillDownComposite drillDown = new DrillDownComposite(this, SWT.BORDER);
        GridData spec = new GridData(SWT.FILL, SWT.FILL, true, true);
        spec.widthHint = SIZING_SELECTION_PANE_WIDTH;
        spec.heightHint = heightHint;
        drillDown.setLayoutData(spec);

        // Create tree viewer inside drill down.
        treeViewer = new TreeViewer(drillDown, SWT.NONE);
        drillDown.setChildTree(treeViewer);
        ChangeLogContainerContentProvider cp = new ChangeLogContainerContentProvider();
        cp.showClosedProjects(showClosedProjects);
        treeViewer.setContentProvider(cp);
        treeViewer.setLabelProvider(WorkbenchLabelProvider
                .getDecoratingWorkbenchLabelProvider());
        treeViewer.setComparator(new ViewerComparator());
        treeViewer.setUseHashlookup(true);
        treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
            @Override
            public void selectionChanged(SelectionChangedEvent event) {
                IStructuredSelection selection = (IStructuredSelection) event
                        .getSelection();
                containerSelectionChanged((IContainer) selection
                        .getFirstElement()); // allow null
            }
        });
        treeViewer.addDoubleClickListener(new IDoubleClickListener() {
            @Override
            public void doubleClick(DoubleClickEvent event) {
                ISelection selection = event.getSelection();
                if (selection instanceof IStructuredSelection) {
                    Object item = ((IStructuredSelection) selection)
                            .getFirstElement();
                    if (item == null) {
                        return;
                    }
                    if (treeViewer.getExpandedState(item)) {
                        treeViewer.collapseToLevel(item, 1);
                    } else {
                        treeViewer.expandToLevel(item, 1);
                    }
                }
            }
        });

        /*
         * This helps in displaying all folders under the document
         * root as well as the actual root itself.
         */
        ChangeLogRootContainer root = new ChangeLogRootContainer(this.initialSelection.getProject());
        // This has to be done after the viewer has been laid out
        treeViewer.setInput(root);
    }

    /**
     * Returns the currently entered container name. Null if the field is empty.
     * Note that the container may not exist yet if the user entered a new
     * container name in the field.
     *
     * @return IPath
     */
    public IPath getContainerFullPath() {
        if (allowNewContainerName) {
            String pathName = containerNameField.getText();
            if (pathName == null || pathName.isEmpty()) {
                return null;
            }
            // The user may not have made this absolute so do it for them
            return (new Path(TextProcessor.deprocess(pathName))).makeAbsolute();

        }
        if (selectedContainer == null) {
            return null;
        }
        return selectedContainer.getFullPath();

    }

    /**
     * Sets the selected existing container.
     *
     * @param container container to set
     */
    public void setSelectedContainer(IContainer container) {
        selectedContainer = container;

        // expand to and select the specified container
        List<IContainer> itemsToExpand = new ArrayList<>();
        IContainer parent = container.getParent();
        while (parent != null) {
            itemsToExpand.add(0, parent);
            parent = parent.getParent();
        }
        treeViewer.setExpandedElements(itemsToExpand.toArray());
        treeViewer.setSelection(new StructuredSelection(container), true);
    }
}

Back to the top