Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e5489973ce9f47641c1fe0d48819106de93a07a8 (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
/*******************************************************************************
 * Copyright (c) 2006, 2009 Wind River Systems 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:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.examples.dsf.filebrowser;

import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
import org.eclipse.debug.internal.ui.viewers.model.provisional.PresentationContext;
import org.eclipse.debug.internal.ui.viewers.model.provisional.TreeModelViewer;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * File Browser example dialog.  It hold a tree viewer that displays 
 * file system contents and a text box for entering a file path to be
 * shown in the tree. 
 */
@SuppressWarnings("restriction")
public class FileBrowserDialog extends Dialog {

    /**
     * Tree viewer for showing the filesystem contents.
     */
    private TreeModelViewer fViewer;

    /**
     * The model adapter for the tree viewer. 
     */
    private FileBrowserModelAdapter fModelAdapter;
    
    /**
     * Flag used to disable text-box changed events, when the text
     * box is updated due to selection change in tree.
     */
    private boolean fDisableTextChangeNotifications = false;
    
    public FileBrowserDialog(Shell parent) {
        super(parent);
        setShellStyle(getShellStyle() | SWT.RESIZE);        
    }    

    @Override
    protected Control createDialogArea(Composite parent) {
        Composite area = (Composite) super.createDialogArea(parent);
        IPresentationContext presentationContext = new PresentationContext("org.eclipse.cdt.examples.dsf.filebrowser");  //$NON-NLS-1$
        
        fViewer = new TreeModelViewer(area, SWT.VIRTUAL, presentationContext);
        fViewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
        
        fModelAdapter = new FileBrowserModelAdapter(presentationContext);
        fViewer.setInput(fModelAdapter.getVMProvider().getViewerInputObject());

        final Text text = new Text(area, SWT.SINGLE | SWT.BORDER);
        text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        
        fViewer.addSelectionChangedListener(new ISelectionChangedListener() {
            public void selectionChanged(SelectionChangedEvent event) {
                /*
                 * Update the file name in the text control, to match the 
                 * selection in the tree.  Do this only if the user is not
                 * actively typing in the text field (test if text has focus).
                 */
                if (!text.isFocusControl() &&
                    event.getSelection() instanceof IStructuredSelection &&
                    ((IStructuredSelection)event.getSelection()).getFirstElement() instanceof FileVMContext)
                {
                    FileVMContext fileVmc = (FileVMContext)((IStructuredSelection)event.getSelection()).getFirstElement();
                    
                    fDisableTextChangeNotifications = true;
                    text.setText(fileVmc.getFile().getAbsolutePath());
                    fDisableTextChangeNotifications = false;
                }
            }
        });
        
        text.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                if (!fDisableTextChangeNotifications) {
                    fModelAdapter.getVMProvider().selectionTextChanged(text.getText());
                }
            }
        });
        
        return area;
    }
    
    @Override
    public boolean close() {
        if (super.close()) {
            fModelAdapter.dispose();
            fModelAdapter = null;
            return true;
        }
        return false;
    }

}

Back to the top