ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / RenameTraceDialog.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 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 * Francois Chouinard - Copied and adapted from NewFolderDialog
11 * Patrick Tasse - Close editors to release resources
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ui.project.wizards;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IWorkspace;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.jface.dialogs.IDialogConstants;
22 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
23 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.graphics.Font;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Event;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Listener;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.swt.widgets.Text;
35 import org.eclipse.ui.dialogs.SelectionStatusDialog;
36
37 /**
38 * Implementation of a dialog box to rename a trace.
39 * <p>
40 * @version 1.0
41 * @author Francois Chouinard
42 */
43 public class RenameTraceDialog extends SelectionStatusDialog {
44
45 // ------------------------------------------------------------------------
46 // Members
47 // ------------------------------------------------------------------------
48
49 private final TmfTraceElement fTrace;
50 private Text fNewTraceNameText;
51
52 // ------------------------------------------------------------------------
53 // Constructor
54 // ------------------------------------------------------------------------
55 /**
56 * Constructor
57 * @param shell The parent shell
58 * @param trace The trace element to rename
59 */
60 public RenameTraceDialog(Shell shell, TmfTraceElement trace) {
61 super(shell);
62 fTrace = trace;
63 setTitle(Messages.RenameTraceDialog_DialogTitle);
64 setStatusLineAboveButtons(true);
65 }
66
67 // ------------------------------------------------------------------------
68 // Dialog
69 // ------------------------------------------------------------------------
70
71 @Override
72 protected Control createDialogArea(Composite parent) {
73 Composite composite = (Composite) super.createDialogArea(parent);
74 composite.setLayout(new GridLayout());
75 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
76
77 createNewTraceNameGroup(composite);
78 return composite;
79 }
80
81 private void createNewTraceNameGroup(Composite parent) {
82 Font font = parent.getFont();
83 Composite folderGroup = new Composite(parent, SWT.NONE);
84 GridLayout layout = new GridLayout();
85 layout.numColumns = 2;
86 folderGroup.setLayout(layout);
87 folderGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
88
89 // Old trace name label
90 Label oldTraceLabel = new Label(folderGroup, SWT.NONE);
91 oldTraceLabel.setFont(font);
92 oldTraceLabel.setText(Messages.RenameTraceDialog_TraceName);
93
94 // Old trace name field
95 Text oldTraceName = new Text(folderGroup, SWT.BORDER);
96 GridData data = new GridData(GridData.FILL_HORIZONTAL);
97 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
98 oldTraceName.setLayoutData(data);
99 oldTraceName.setFont(font);
100 oldTraceName.setText(fTrace.getName());
101 oldTraceName.setEnabled(false);
102
103 // New trace name label
104 Label newTaceLabel = new Label(folderGroup, SWT.NONE);
105 newTaceLabel.setFont(font);
106 newTaceLabel.setText(Messages.RenameTraceDialog_TraceNewName);
107
108 // New trace name entry field
109 fNewTraceNameText = new Text(folderGroup, SWT.BORDER);
110 data = new GridData(GridData.FILL_HORIZONTAL);
111 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
112 fNewTraceNameText.setLayoutData(data);
113 fNewTraceNameText.setFont(font);
114 fNewTraceNameText.addListener(SWT.Modify, new Listener() {
115 @Override
116 public void handleEvent(Event event) {
117 validateNewTraceName();
118 }
119 });
120 }
121
122 private void validateNewTraceName() {
123
124 String newTraceName = fNewTraceNameText.getText();
125 IWorkspace workspace = fTrace.getResource().getWorkspace();
126 IStatus nameStatus = workspace.validateName(newTraceName, IResource.FOLDER);
127
128 if ("".equals(newTraceName)) { //$NON-NLS-1$
129 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
130 Messages.Dialog_EmptyNameError, null));
131 return;
132 }
133
134 if (!nameStatus.isOK()) {
135 updateStatus(nameStatus);
136 return;
137 }
138
139 IContainer parentFolder = fTrace.getResource().getParent();
140 if (parentFolder.findMember(newTraceName) != null) {
141 updateStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
142 Messages.Dialog_ExistingNameError, null));
143 return;
144 }
145
146 updateStatus(new Status(IStatus.OK, Activator.PLUGIN_ID, "")); //$NON-NLS-1$
147 }
148
149 // ------------------------------------------------------------------------
150 // SelectionStatusDialog
151 // ------------------------------------------------------------------------
152
153 @Override
154 protected void computeResult() {
155 }
156
157 @Override
158 public void create() {
159 super.create();
160 getButton(IDialogConstants.OK_ID).setEnabled(false);
161 }
162
163 @Override
164 protected void okPressed() {
165 setSelectionResult(new String[] { fNewTraceNameText.getText() });
166 super.okPressed();
167 }
168
169 }
This page took 0.03544 seconds and 5 git commands to generate.