Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 72ef8e3388e6f3a0739cb4a7474a8927869e0d30 (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
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, Inc. 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.tm.internal.tcf.cdt.ui.launch;

import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.tm.internal.tcf.cdt.ui.launch.FileSystemBrowserControl.FileInfo;
import org.eclipse.tm.tcf.protocol.IPeer;

/**
 * Dialog to select a remote file.
 */
public class RemoteFileSelectionDialog extends Dialog {

    private String fSelection;
    private FileSystemBrowserControl fFileList;
    private IPeer fPeer;
    private boolean fForSave;
    private Text fFileNameText;

    protected RemoteFileSelectionDialog(IShellProvider parentShell, int style) {
        super(parentShell);
        setShellStyle(getShellStyle() | SWT.RESIZE);
        fForSave = (style & SWT.SAVE) != 0;
    }

    public void setPeer(IPeer peer) {
        fPeer = peer;
        if (fFileList != null) {
            fFileList.setInput(peer);
        }
    }

    public void setSelection(String fileSelection) {
        fSelection = fileSelection;
    }

    public String getSelection() {
        return fSelection;
    }

    @Override
    protected void configureShell(Shell newShell) {
        newShell.setText("Select File");
        super.configureShell(newShell);
    }

    @Override
    protected Control createContents(Composite parent) {
        Control control = super.createContents(parent);
        updateButtonState();
        return control;
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        Composite composite = (Composite) super.createDialogArea(parent);
        fFileList = new FileSystemBrowserControl(composite, false);
        fFileList.getTree().addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                FileInfo contextInfo = fFileList.findFileInfo((TreeItem) e.item);
                if (contextInfo != null) {
                    handleFileSelected(contextInfo);
                }
            }
            @Override
            public void widgetDefaultSelected(SelectionEvent e) {
                widgetSelected(e);
                if (getButton(IDialogConstants.OK_ID).isEnabled()) {
                    buttonPressed(IDialogConstants.OK_ID);
                }
            }
        });

        if (fForSave) {
            Composite fileNameComp = new Composite(composite, SWT.NONE);
            fileNameComp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
            GridLayout layout = new GridLayout(2, false);
            layout.marginWidth = 0;
            fileNameComp.setLayout(layout);
            new Label(fileNameComp, SWT.NONE).setText("File Name:");
            fFileNameText = new Text(fileNameComp, SWT.BORDER);
            fFileNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
            fFileNameText.addFocusListener(new FocusAdapter() {
                @Override
                public void focusLost(FocusEvent e) {
                    handleFileNameChanged();
                }
            });
        }

        if (fSelection != null) {
            fFileList.setInitialSelection(fSelection);
            if (fFileNameText != null) {
                String basename = new Path(fSelection).lastSegment();
                if (basename != null) {
                    fFileNameText.setText(basename);
                }
            }
        }
        if (fPeer != null) {
            fFileList.setInput(fPeer);
        }
        return composite;
    }

    private void updateButtonState() {
        boolean enabled = fSelection != null;
        if (enabled && fForSave) {
            enabled = fFileNameText.getText().trim().length() > 0;
        }
        getButton(IDialogConstants.OK_ID).setEnabled(enabled);
    }

    protected void handleFileNameChanged() {
        if (fSelection != null) {
            fSelection = new Path(fSelection).removeLastSegments(1).append(fFileNameText.getText().trim()).toString();
            updateButtonState();
        }
    }

    protected void handleFileSelected(FileInfo fileInfo) {
        if (fileInfo.isDir) {
            if (fForSave) {
                String basename = fFileNameText.getText().trim();
                if (basename.length() > 0) {
                    fSelection = new Path(fileInfo.fullname).append(basename).toString();
                } else {
                    fSelection = fileInfo.fullname;
                }
            } else {
                fSelection = null;
            }
        } else {
            fSelection = fileInfo.fullname;
            if (fFileNameText != null) {
                String basename = new Path(fSelection).lastSegment();
                if (basename != null) {
                    fFileNameText.setText(basename);
                }
            }
        }
        updateButtonState();
    }

}

Back to the top