Search in sources :

Example 1 with ResourceSelectionDialog

use of org.csstudio.ui.util.dialogs.ResourceSelectionDialog in project yamcs-studio by yamcs.

the class SingleSourceHelper method handleTextInputFigureFileSelector.

public static void handleTextInputFigureFileSelector(TextInputFigure textInput) {
    String startPath = textInput.getStartPath();
    String currentPath = textInput.getCurrentPath();
    switch(textInput.getFileReturnPart()) {
        case DIRECTORY:
        case FULL_PATH:
            currentPath = textInput.getText();
            break;
        default:
            if (currentPath == null) {
                if (startPath == null)
                    currentPath = textInput.getText();
                else
                    currentPath = startPath;
            }
            break;
    }
    switch(textInput.getFileSource()) {
        case WORKSPACE:
            ResourceSelectionDialog dialog = new ResourceSelectionDialog(Display.getCurrent().getActiveShell(), "Select workspace file", // $NON-NLS-2$
            textInput.getFileReturnPart() == FileReturnPart.DIRECTORY ? null : new String[] { "*.*" });
            if (currentPath != null)
                dialog.setSelectedResource(new Path(currentPath));
            if (dialog.open() == Window.OK) {
                IPath path = dialog.getSelectedResource();
                currentPath = path.toPortableString();
                String fileString = currentPath;
                switch(textInput.getFileReturnPart()) {
                    case NAME_ONLY:
                        fileString = path.removeFileExtension().lastSegment();
                        break;
                    case NAME_EXT:
                        fileString = path.lastSegment();
                        break;
                    case FULL_PATH:
                    case DIRECTORY:
                    default:
                        break;
                }
                textInput.setText(fileString);
                textInput.setCurrentPath(currentPath);
                textInput.fireManualValueChange(textInput.getText());
            }
            break;
        case LOCAL:
            IPath[] paths = null;
            if (textInput.getFileReturnPart() == FileReturnPart.DIRECTORY) {
                DirectoryDialog directoryDialog = new DirectoryDialog(Display.getCurrent().getActiveShell());
                directoryDialog.setFilterPath(currentPath);
                String directory = directoryDialog.open();
                if (directory != null)
                    paths = new Path[] { new Path(directory) };
            } else {
                FileDialog fileDialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.MULTI);
                if (currentPath != null)
                    ((FileDialog) fileDialog).setFileName(currentPath);
                String firstPath = fileDialog.open();
                if (firstPath != null) {
                    paths = new Path[fileDialog.getFileNames().length];
                    paths[0] = new Path(firstPath);
                    for (int i = 1; i < paths.length; i++) {
                        paths[i] = paths[0].removeLastSegments(1).append(fileDialog.getFileNames()[i]);
                    }
                }
            }
            if (paths != null) {
                currentPath = paths[0].toOSString();
                StringBuilder result = new StringBuilder();
                switch(textInput.getFileReturnPart()) {
                    case NAME_ONLY:
                        for (int i = 0; i < paths.length; i++) {
                            if (i > 0)
                                result.append(SEPARATOR);
                            result.append(paths[i].removeFileExtension().lastSegment());
                        }
                        break;
                    case NAME_EXT:
                        for (int i = 0; i < paths.length; i++) {
                            if (i > 0)
                                result.append(SEPARATOR);
                            result.append(paths[i].lastSegment());
                        }
                        break;
                    case FULL_PATH:
                    case DIRECTORY:
                    default:
                        for (int i = 0; i < paths.length; i++) {
                            if (i > 0)
                                result.append(SEPARATOR);
                            result.append(paths[i].toOSString());
                        }
                        break;
                }
                textInput.setText(result.toString());
                textInput.setCurrentPath(currentPath);
                textInput.fireManualValueChange(textInput.getText());
            }
            break;
        default:
            break;
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) ResourceSelectionDialog(org.csstudio.ui.util.dialogs.ResourceSelectionDialog) IPath(org.eclipse.core.runtime.IPath) FileDialog(org.eclipse.swt.widgets.FileDialog) DirectoryDialog(org.eclipse.swt.widgets.DirectoryDialog)

Aggregations

ResourceSelectionDialog (org.csstudio.ui.util.dialogs.ResourceSelectionDialog)1 IPath (org.eclipse.core.runtime.IPath)1 Path (org.eclipse.core.runtime.Path)1 DirectoryDialog (org.eclipse.swt.widgets.DirectoryDialog)1 FileDialog (org.eclipse.swt.widgets.FileDialog)1