1 11 package org.eclipse.team.internal.ccvs.ui; 12 13 import org.eclipse.core.resources.IResource; 14 import org.eclipse.jface.dialogs.*; 15 import org.eclipse.jface.dialogs.Dialog; 16 import org.eclipse.jface.resource.JFaceColors; 17 import org.eclipse.osgi.util.NLS; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.layout.GridData; 20 import org.eclipse.swt.layout.GridLayout; 21 import org.eclipse.swt.widgets.*; 22 import org.eclipse.team.internal.ccvs.core.util.FileNameMatcher; 23 import org.eclipse.ui.PlatformUI; 24 25 public class IgnoreResourcesDialog extends TrayDialog { 26 private IResource[] resources; 28 29 private final String ACTION_KEY = "Action"; private static final int ADD_NAME_ENTRY = 0; 32 private static final int ADD_EXTENSION_ENTRY = 1; 33 private static final int ADD_CUSTOM_ENTRY = 2; 34 35 private IDialogSettings settings; 37 38 private Button addNameEntryButton; 40 private Button addExtensionEntryButton; 41 private Button addCustomEntryButton; 42 private Text customEntryText; 43 private Label statusMessageLabel; 44 45 private int selectedAction; 46 private String customPattern; 47 48 private static final int LABEL_INDENT_WIDTH = 32; 50 51 56 public IgnoreResourcesDialog(Shell shell, IResource[] resources) { 57 super(shell); 58 this.resources = resources; 59 60 IDialogSettings workbenchSettings = CVSUIPlugin.getPlugin().getDialogSettings(); 61 this.settings = workbenchSettings.getSection("IgnoreResourcesDialog"); if (settings == null) { 63 this.settings = workbenchSettings.addNewSection("IgnoreResourcesDialog"); } 65 66 try { 67 selectedAction = settings.getInt(ACTION_KEY); 68 } catch (NumberFormatException e) { 69 selectedAction = ADD_NAME_ENTRY; 70 } 71 } 72 73 79 public String getIgnorePatternFor(IResource resource) { 80 switch (selectedAction) { 81 case ADD_NAME_ENTRY: 82 return resource.getName(); 83 case ADD_EXTENSION_ENTRY: { 84 String extension = resource.getFileExtension(); 85 return (extension == null) ? resource.getName() : "*." + extension; } 87 case ADD_CUSTOM_ENTRY: 88 return customPattern; 89 } 90 throw new IllegalStateException (); 91 } 92 93 96 protected void createButtonsForButtonBar(Composite parent) { 97 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); 98 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); 99 } 100 101 104 protected void configureShell(Shell newShell) { 105 super.configureShell(newShell); 106 if (resources.length == 1) { 107 newShell.setText(NLS.bind(CVSUIMessages.IgnoreResourcesDialog_titleSingle, new String [] { resources[0].getName() })); 108 } else { 109 newShell.setText(NLS.bind(CVSUIMessages.IgnoreResourcesDialog_titleMany, new String [] { Integer.toString(resources.length) })); 110 } 111 } 112 113 116 protected Control createContents(Composite parent) { 117 Control control = super.createContents(parent); 118 updateEnablements(); 119 return control; 120 } 121 122 125 protected Control createDialogArea(Composite parent) { 126 Composite top = new Composite(parent, SWT.NONE); 127 top.setLayout(new GridLayout()); 128 129 PlatformUI.getWorkbench().getHelpSystem().setHelp(top, IHelpContextIds.ADD_TO_CVSIGNORE); 130 131 createIndentedLabel(top, CVSUIMessages.IgnoreResourcesDialog_prompt, 0); 132 133 Listener selectionListener = new Listener() { 134 public void handleEvent(Event event) { 135 updateEnablements(); 136 } 137 }; 138 Listener modifyListener = new Listener() { 139 public void handleEvent(Event event) { 140 validate(); 141 } 142 }; 143 144 addNameEntryButton = createRadioButton(top, CVSUIMessages.IgnoreResourcesDialog_addNameEntryButton); 145 addNameEntryButton.addListener(SWT.Selection, selectionListener); 146 addNameEntryButton.setSelection(selectedAction == ADD_NAME_ENTRY); 147 createIndentedLabel(top, CVSUIMessages.IgnoreResourcesDialog_addNameEntryExample, LABEL_INDENT_WIDTH); 148 149 addExtensionEntryButton = createRadioButton(top, CVSUIMessages.IgnoreResourcesDialog_addExtensionEntryButton); 150 addExtensionEntryButton.addListener(SWT.Selection, selectionListener); 151 addExtensionEntryButton.setSelection(selectedAction == ADD_EXTENSION_ENTRY); 152 createIndentedLabel(top, CVSUIMessages.IgnoreResourcesDialog_addExtensionEntryExample, LABEL_INDENT_WIDTH); 153 154 addCustomEntryButton = createRadioButton(top, CVSUIMessages.IgnoreResourcesDialog_addCustomEntryButton); 155 addCustomEntryButton.addListener(SWT.Selection, selectionListener); 156 addCustomEntryButton.setSelection(selectedAction == ADD_CUSTOM_ENTRY); 157 createIndentedLabel(top, CVSUIMessages.IgnoreResourcesDialog_addCustomEntryExample, LABEL_INDENT_WIDTH); 158 159 customEntryText = createIndentedText(top, resources[0].getName(), LABEL_INDENT_WIDTH); 160 customEntryText.addListener(SWT.Modify, modifyListener); 161 162 statusMessageLabel = createIndentedLabel(top, "", 0); Dialog.applyDialogFont(parent); 164 return top; 165 } 166 167 170 protected void okPressed() { 171 settings.put(ACTION_KEY, selectedAction); 172 super.okPressed(); 173 } 174 175 private Label createIndentedLabel(Composite parent, String text, int indent) { 176 Label label = new Label(parent, SWT.LEFT); 177 label.setText(text); 178 GridData data = new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.FILL_HORIZONTAL); 179 data.horizontalIndent = indent; 180 label.setLayoutData(data); 181 return label; 182 } 183 184 private Text createIndentedText(Composite parent, String text, int indent) { 185 Text textbox = new Text(parent, SWT.BORDER); 186 textbox.setText(text); 187 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 188 data.horizontalIndent = indent; 189 textbox.setLayoutData(data); 190 return textbox; 191 } 192 193 private Button createRadioButton(Composite parent, String text) { 194 Button button = new Button(parent, SWT.RADIO); 195 button.setText(text); 196 button.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.FILL_HORIZONTAL)); 197 return button; 198 } 199 200 private void updateEnablements() { 201 if (addNameEntryButton.getSelection()) { 202 selectedAction = ADD_NAME_ENTRY; 203 } else if (addExtensionEntryButton.getSelection()) { 204 selectedAction = ADD_EXTENSION_ENTRY; 205 } else if (addCustomEntryButton.getSelection()) { 206 selectedAction = ADD_CUSTOM_ENTRY; 207 } 208 customEntryText.setEnabled(selectedAction == ADD_CUSTOM_ENTRY); 209 validate(); 210 } 211 212 private void validate() { 213 if (selectedAction == ADD_CUSTOM_ENTRY) { 214 customPattern = customEntryText.getText(); 215 if (customPattern.length() == 0) { 216 setError(CVSUIMessages.IgnoreResourcesDialog_patternMustNotBeEmpty); 217 return; 218 } 219 FileNameMatcher matcher = new FileNameMatcher(new String [] { customPattern }); 220 for (int i = 0; i < resources.length; i++) { 221 String name = resources[i].getName(); 222 if (! matcher.match(name)) { 223 setError(NLS.bind(CVSUIMessages.IgnoreResourcesDialog_patternDoesNotMatchFile, new String [] { name })); 224 return; 225 } 226 } 227 } 228 setError(null); 229 } 230 231 private void setError(String text) { 232 if (text == null) { 233 statusMessageLabel.setText(""); getButton(IDialogConstants.OK_ID).setEnabled(true); 235 } else { 236 statusMessageLabel.setText(text); 237 statusMessageLabel.setForeground(JFaceColors.getErrorText(getShell().getDisplay())); 238 getButton(IDialogConstants.OK_ID).setEnabled(false); 239 } 240 } 241 } 242 | Popular Tags |