ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / NewFolderDialog.java
CommitLineData
339d539c
PT
1/*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.ui.project.wizards;
14
15import java.lang.reflect.InvocationTargetException;
16
17import org.eclipse.core.resources.IFolder;
18import org.eclipse.core.resources.IResource;
19import org.eclipse.core.resources.IWorkspace;
20import org.eclipse.core.runtime.CoreException;
21import org.eclipse.core.runtime.IProgressMonitor;
22import org.eclipse.core.runtime.IStatus;
23import org.eclipse.core.runtime.Path;
24import org.eclipse.core.runtime.Status;
25import org.eclipse.jface.dialogs.IDialogConstants;
26import org.eclipse.jface.dialogs.MessageDialog;
27import org.eclipse.linuxtools.internal.tmf.ui.Activator;
28import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
29import org.eclipse.osgi.util.NLS;
30import org.eclipse.swt.SWT;
31import org.eclipse.swt.graphics.Font;
32import org.eclipse.swt.layout.GridData;
33import org.eclipse.swt.layout.GridLayout;
34import org.eclipse.swt.widgets.Composite;
35import org.eclipse.swt.widgets.Control;
36import org.eclipse.swt.widgets.Event;
37import org.eclipse.swt.widgets.Label;
38import org.eclipse.swt.widgets.Listener;
39import org.eclipse.swt.widgets.Shell;
40import org.eclipse.swt.widgets.Text;
41import org.eclipse.ui.PlatformUI;
42import org.eclipse.ui.actions.WorkspaceModifyOperation;
43import org.eclipse.ui.dialogs.SelectionStatusDialog;
44
45/**
46 * Implementation of new folder dialog that creates the folder element.
47 * @since 3.0
48 */
49public class NewFolderDialog extends SelectionStatusDialog {
50
51 // ------------------------------------------------------------------------
52 // Members
53 // ------------------------------------------------------------------------
54
55 private Text fFolderName;
56 private final IFolder fParentFolder;
57
58 // ------------------------------------------------------------------------
59 // Constructor
60 // ------------------------------------------------------------------------
61 /**
62 * Constructor
63 *
64 * @param shell
65 * The parent shell
66 * @param parent
67 * The parent trace folder
68 */
69 public NewFolderDialog(Shell shell, TmfTraceFolder parent) {
70 super(shell);
71 fParentFolder = parent.getResource();
72 setTitle(Messages.NewFolderDialog_DialogTitle);
73 setStatusLineAboveButtons(true);
74 }
75
76 // ------------------------------------------------------------------------
77 // Dialog
78 // ------------------------------------------------------------------------
79
80 @Override
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));
85
86 createFolderNameGroup(composite);
87 return composite;
88 }
89
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));
97
98 // New folder label
99 Label folderLabel = new Label(folderGroup, SWT.NONE);
100 folderLabel.setFont(font);
101 folderLabel.setText(Messages.NewFolderDialog_FolderName);
102
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() {
110 @Override
111 public void handleEvent(Event event) {
112 validateNewFolderName();
113 }
114 });
115 }
116
117 private void validateNewFolderName() {
118
119 String name = fFolderName.getText();
120 IWorkspace workspace = fParentFolder.getWorkspace();
121 IStatus nameStatus = workspace.validateName(name, IResource.FOLDER);
122
123 if ("".equals(name)) { //$NON-NLS-1$
124 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_EmptyNameError, null));
125 return;
126 }
127
128 if (!nameStatus.isOK()) {
129 updateStatus(nameStatus);
130 return;
131 }
132
133 if (fParentFolder.findMember(name) != null) {
134 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, Messages.Dialog_ExistingNameError, null));
135 return;
136 }
137
138 updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$
139 }
140
141 // ------------------------------------------------------------------------
142 // SelectionStatusDialog
143 // ------------------------------------------------------------------------
144
145 @Override
146 protected void computeResult() {
147 }
148
149 @Override
150 public void create() {
151 super.create();
152 getButton(IDialogConstants.OK_ID).setEnabled(false);
153 }
154
155 @Override
156 protected void okPressed() {
157 IFolder folder = createNewFolder(fFolderName.getText());
158 if (folder == null) {
159 return;
160 }
161 setSelectionResult(new IFolder[] { folder });
162 super.okPressed();
163 }
164
165 private IFolder createNewFolder(String folderName) {
166
167 final IFolder folder = fParentFolder.getFolder(new Path(folderName));
168
169 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
170 @Override
171 public void execute(IProgressMonitor monitor) throws CoreException {
172 try {
173 monitor.beginTask("", 1000); //$NON-NLS-1$
174 folder.create(false, true, monitor);
175 } finally {
176 monitor.done();
177 }
178 }
179 };
180 try {
181 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
182 } catch (InterruptedException | RuntimeException exception) {
183 return null;
184 } catch (InvocationTargetException exception) {
185 MessageDialog.openError(getShell(), "", NLS.bind("", exception.getTargetException().getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
186 return null;
187 }
188
189 return folder;
190 }
191
192}
This page took 0.049549 seconds and 5 git commands to generate.