1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.project
.wizards
;
15 import java
.lang
.reflect
.InvocationTargetException
;
17 import org
.eclipse
.core
.resources
.IFolder
;
18 import org
.eclipse
.core
.resources
.IResource
;
19 import org
.eclipse
.core
.resources
.IWorkspace
;
20 import org
.eclipse
.core
.runtime
.CoreException
;
21 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
22 import org
.eclipse
.core
.runtime
.IStatus
;
23 import org
.eclipse
.core
.runtime
.Path
;
24 import org
.eclipse
.core
.runtime
.Status
;
25 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
26 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
27 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
29 import org
.eclipse
.osgi
.util
.NLS
;
30 import org
.eclipse
.swt
.SWT
;
31 import org
.eclipse
.swt
.graphics
.Font
;
32 import org
.eclipse
.swt
.layout
.GridData
;
33 import org
.eclipse
.swt
.layout
.GridLayout
;
34 import org
.eclipse
.swt
.widgets
.Composite
;
35 import org
.eclipse
.swt
.widgets
.Control
;
36 import org
.eclipse
.swt
.widgets
.Event
;
37 import org
.eclipse
.swt
.widgets
.Label
;
38 import org
.eclipse
.swt
.widgets
.Listener
;
39 import org
.eclipse
.swt
.widgets
.Shell
;
40 import org
.eclipse
.swt
.widgets
.Text
;
41 import org
.eclipse
.ui
.PlatformUI
;
42 import org
.eclipse
.ui
.actions
.WorkspaceModifyOperation
;
43 import org
.eclipse
.ui
.dialogs
.SelectionStatusDialog
;
46 * Implementation of new folder dialog that creates the folder element.
49 public class NewFolderDialog
extends SelectionStatusDialog
{
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
55 private Text fFolderName
;
56 private final IFolder fParentFolder
;
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
67 * The parent trace folder
69 public NewFolderDialog(Shell shell
, TmfTraceFolder parent
) {
71 fParentFolder
= parent
.getResource();
72 setTitle(Messages
.NewFolderDialog_DialogTitle
);
73 setStatusLineAboveButtons(true);
76 // ------------------------------------------------------------------------
78 // ------------------------------------------------------------------------
81 protected Control
createDialogArea(Composite parent
) {
82 Composite composite
= (Composite
) super.createDialogArea(parent
);
83 composite
.setLayout(new GridLayout());
84 composite
.setLayoutData(new GridData(GridData
.FILL_BOTH
));
86 createFolderNameGroup(composite
);
90 private void createFolderNameGroup(Composite parent
) {
91 Font font
= parent
.getFont();
92 Composite folderGroup
= new Composite(parent
, SWT
.NONE
);
93 GridLayout layout
= new GridLayout();
94 layout
.numColumns
= 2;
95 folderGroup
.setLayout(layout
);
96 folderGroup
.setLayoutData(new GridData(GridData
.FILL_HORIZONTAL
));
99 Label folderLabel
= new Label(folderGroup
, SWT
.NONE
);
100 folderLabel
.setFont(font
);
101 folderLabel
.setText(Messages
.NewFolderDialog_FolderName
);
103 // New folder name entry field
104 fFolderName
= new Text(folderGroup
, SWT
.BORDER
);
105 GridData data
= new GridData(GridData
.FILL_HORIZONTAL
);
106 data
.widthHint
= IDialogConstants
.ENTRY_FIELD_WIDTH
;
107 fFolderName
.setLayoutData(data
);
108 fFolderName
.setFont(font
);
109 fFolderName
.addListener(SWT
.Modify
, new Listener() {
111 public void handleEvent(Event event
) {
112 validateNewFolderName();
117 private void validateNewFolderName() {
119 String name
= fFolderName
.getText();
120 IWorkspace workspace
= fParentFolder
.getWorkspace();
121 IStatus nameStatus
= workspace
.validateName(name
, IResource
.FOLDER
);
123 if ("".equals(name
)) { //$NON-NLS-1$
124 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_EmptyNameError
, null));
128 if (!nameStatus
.isOK()) {
129 updateStatus(nameStatus
);
133 if (fParentFolder
.findMember(name
) != null) {
134 updateStatus(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
, Messages
.Dialog_ExistingNameError
, null));
138 updateStatus(new Status(IStatus
.OK
, Activator
.PLUGIN_ID
, "")); //$NON-NLS-1$
141 // ------------------------------------------------------------------------
142 // SelectionStatusDialog
143 // ------------------------------------------------------------------------
146 protected void computeResult() {
150 public void create() {
152 getButton(IDialogConstants
.OK_ID
).setEnabled(false);
156 protected void okPressed() {
157 IFolder folder
= createNewFolder(fFolderName
.getText());
158 if (folder
== null) {
161 setSelectionResult(new IFolder
[] { folder
});
165 private IFolder
createNewFolder(String folderName
) {
167 final IFolder folder
= fParentFolder
.getFolder(new Path(folderName
));
169 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
171 public void execute(IProgressMonitor monitor
) throws CoreException
{
173 monitor
.beginTask("", 1000); //$NON-NLS-1$
174 folder
.create(false, true, monitor
);
181 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
182 } catch (InterruptedException
| RuntimeException exception
) {
184 } catch (InvocationTargetException exception
) {
185 MessageDialog
.openError(getShell(), "", NLS
.bind("", exception
.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$