1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.team.internal.ccvs.ui.tags;
15 
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.widgets.*;
24 import org.eclipse.team.internal.ccvs.core.CVSTag;
25 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
26 import org.eclipse.team.internal.ccvs.ui.IHelpContextIds;
27 import org.eclipse.team.internal.ccvs.ui.wizards.CVSWizardPage;
28 import org.eclipse.team.internal.ui.PixelConverter;
29 import org.eclipse.team.internal.ui.SWTUtils;
30 import org.eclipse.team.internal.ui.dialogs.DetailsDialog;
31 
32 public class BranchPromptDialog extends DetailsDialog {
33 
34 	private String branchTag = ""; //$NON-NLS-1$
35 	private String versionTag= ""; //$NON-NLS-1$
36 	private String versionName= "";		 //$NON-NLS-1$
37 
38 	private boolean allStickyResources;
39 	private boolean update;
40 
41 	private Text versionText;
42 	private Text branchText;
43 
44 	private static final int TAG_AREA_HEIGHT_HINT = 200;
45 
46 	// widgets;
47 	private TagSource tagSource;
48 	private TagSelectionArea tagArea;
49 	private final IResource[] resources;
50 
BranchPromptDialog(Shell parentShell, String title, IResource[] resources, boolean allResourcesSticky, String versionName)51 	public BranchPromptDialog(Shell parentShell, String title, IResource[] resources, boolean allResourcesSticky, String versionName) {
52 		super(parentShell, title);
53 		this.resources = resources;
54 		this.tagSource = TagSource.create(resources);
55 		this.allStickyResources = allResourcesSticky;
56 		this.versionName = versionName;
57 	}
58 
59 	@Override
createMainDialogArea(Composite composite)60 	protected void createMainDialogArea(Composite composite) {
61 
62 		applyDialogFont(composite);
63 		initializeDialogUnits(composite);
64 
65 		final int areaWidth= convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
66 
67 		final Label description= SWTUtils.createLabel(composite, allStickyResources ? CVSUIMessages.BranchWizardPage_pageDescriptionVersion : CVSUIMessages.BranchWizardPage_pageDescription);
68 		description.setLayoutData(SWTUtils.createGridData(areaWidth, SWT.DEFAULT, true, false));
69 
70 		final Label name= SWTUtils.createLabel(composite, CVSUIMessages.BranchWizardPage_branchName);
71 		name.setLayoutData(SWTUtils.createGridData(areaWidth, SWT.DEFAULT, true, false));
72 
73 		branchText = CVSWizardPage.createTextField(composite);
74 		branchText.addListener(SWT.Modify, event -> {
75 			branchTag = branchText.getText();
76 			updateEnablements();
77 			updateVersionName(branchTag);
78 		});
79 		addBranchContentAssist();
80 
81 		final Button check = SWTUtils.createCheckBox(composite, CVSUIMessages.BranchWizardPage_startWorking);
82 		check.addListener(SWT.Selection, event -> update = check.getSelection());
83 		check.setSelection(true);
84 		update = true;
85 
86 		final Label versionLabel1= SWTUtils.createLabel(composite, CVSUIMessages.BranchWizardPage_specifyVersion);
87 		versionLabel1.setLayoutData(SWTUtils.createGridData(areaWidth, SWT.DEFAULT, true, false));
88 
89 		final Label versionLabel2= SWTUtils.createLabel(composite, CVSUIMessages.BranchWizardPage_versionName);
90 		versionLabel2.setLayoutData(SWTUtils.createGridData(areaWidth, SWT.DEFAULT, true, false));
91 
92 		versionText = CVSWizardPage.createTextField(composite);
93 		versionText.addListener(SWT.Modify, event -> {
94 			versionTag = versionText.getText();
95 			updateEnablements();
96 		});
97 
98 		if(allStickyResources) {
99 			versionText.setEditable(false);
100 			versionText.setText(versionName);
101 		}
102 
103 		applyDialogFont(composite);
104 		branchText.setFocus();
105 	}
106 
107 	@Override
getHelpContextId()108 	protected String getHelpContextId() {
109 		return IHelpContextIds.BRANCH_DIALOG;
110 	}
addBranchContentAssist()111 	private void addBranchContentAssist() {
112 		TagSource projectTagSource = LocalProjectTagSource.create(getSeedProject());
113 		if (projectTagSource != null)
114 			TagContentAssistProcessor.createContentAssistant(branchText, projectTagSource, TagSelectionArea.INCLUDE_BRANCHES);
115 	}
116 
getSeedProject()117 	private IProject getSeedProject() {
118 		return resources[0].getProject();
119 	}
120 
121 	/**
122 	 * Updates version name
123 	 */
updateVersionName(String branchName)124 	protected void updateVersionName(String branchName) {
125 		if(versionText!=null && !allStickyResources) {
126 			versionText.setText(CVSUIMessages.BranchWizardPage_versionPrefix + branchName);
127 		}
128 	}
129 
130 	@Override
createDropDownDialogArea(Composite parent)131 	protected Composite createDropDownDialogArea(Composite parent) {
132 
133 		applyDialogFont(parent);
134 		final PixelConverter converter= new PixelConverter(parent);
135 
136 		final Composite composite = new Composite(parent, SWT.NONE);
137 		composite.setLayout(SWTUtils.createGridLayout(1, converter, SWTUtils.MARGINS_DIALOG));
138 		final GridData gridData = new GridData(GridData.FILL_BOTH);
139 		gridData.heightHint = TAG_AREA_HEIGHT_HINT;
140 		composite.setLayoutData(gridData);
141 
142 		tagArea = new TagSelectionArea(getShell(), tagSource, TagSelectionArea.INCLUDE_VERSIONS | TagSelectionArea.INCLUDE_BRANCHES, null);
143 		tagArea.setTagAreaLabel(CVSUIMessages.BranchWizardPage_existingVersionsAndBranches);
144 		tagArea.setIncludeFilterInputArea(false);
145 		tagArea.createArea(composite);
146 
147 		return composite;
148 	}
149 
150 	/**
151 	 * Validates branch and version names
152 	 */
153 	@Override
updateEnablements()154 	protected void updateEnablements() {
155 		String message = null;
156 
157 		if (branchTag.length() == 0) {
158 			message = ""; //$NON-NLS-1$
159 		} else {
160 			IStatus status = CVSTag.validateTagName(branchTag);
161 			if (!status.isOK()) {
162 				message = NLS.bind(CVSUIMessages.BranchWizard_branchNameWarning, new String[] { status.getMessage() });
163 			} else {
164 				if(versionText!=null) {
165 					status = CVSTag.validateTagName(versionText.getText());
166 					if (!status.isOK()) {
167 						message = NLS.bind(CVSUIMessages.BranchWizard_versionNameWarning, new String[] { status.getMessage() });
168 					} else {
169 						if(versionTag.length() != 0 && versionTag.equals(branchTag)) {
170 							message = CVSUIMessages.BranchWizard_branchAndVersionMustBeDifferent;
171 						}
172 					}
173 				}
174 			}
175 		}
176 		setPageComplete(message == null);
177 		setErrorMessage(message);
178 	}
179 
180 	/**
181 	 * Returns the branch tag name
182 	 */
getBranchTagName()183 	public String getBranchTagName() {
184 		return branchTag;
185 	}
186 
187 	/**
188 	 * Returns the version tag name
189 	 */
getVersionTagName()190 	public String getVersionTagName() {
191 		return versionTag;
192 	}
193 
194 	/**
195 	 * Returns the state of the update checkbox
196 	 */
getUpdate()197 	public boolean getUpdate() {
198 		return update;
199 	}
200 
201 	@Override
isMainGrabVertical()202 	protected boolean isMainGrabVertical() {
203 		return false;
204 	}
205 
206 }
207