1 12 13 package org.eclipse.team.internal.ccvs.ui; 14 15 import org.eclipse.jface.dialogs.Dialog; 16 import org.eclipse.jface.dialogs.IDialogConstants; 17 import org.eclipse.jface.preference.PreferencePage; 18 import org.eclipse.jface.viewers.*; 19 import org.eclipse.jface.window.Window; 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.layout.GridData; 22 import org.eclipse.swt.layout.GridLayout; 23 import org.eclipse.swt.widgets.Button; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.swt.widgets.Control; 26 import org.eclipse.swt.widgets.Event; 27 import org.eclipse.swt.widgets.Label; 28 import org.eclipse.swt.widgets.List; 29 import org.eclipse.swt.widgets.Listener; 30 import org.eclipse.swt.widgets.Text; 31 import org.eclipse.team.core.TeamException; 32 import org.eclipse.team.internal.ccvs.core.util.Util; 33 import org.eclipse.ui.*; 34 35 public class CommentTemplatesPreferencePage extends PreferencePage implements 36 IWorkbenchPreferencePage, ISelectionChangedListener { 37 38 private ListViewer viewer; 39 private Button editButton; 40 private Button removeButton; 41 private Text preview; 42 43 protected Control createContents(Composite ancestor) { 44 Composite parent = new Composite(ancestor, SWT.NONE); 45 GridLayout layout = new GridLayout(); 46 layout.marginWidth = 0; 47 layout.marginHeight = 0; 48 layout.numColumns = 1; 49 parent.setLayout(layout); 50 parent.setLayoutData(new GridData(GridData.FILL_BOTH)); 51 52 createListAndButtons(parent); 53 54 Label previewLabel = new Label(parent, SWT.NONE); 55 previewLabel.setText(CVSUIMessages.CommentTemplatesPreferencePage_Preview); 56 57 preview = new Text(parent, SWT.MULTI | SWT.READ_ONLY | SWT.BORDER); 58 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 59 gd.heightHint = convertHeightInCharsToPixels(5); 60 preview.setLayoutData(gd); 61 62 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.COMMENT_TEMPLATE_PREFERENCE_PAGE); 63 Dialog.applyDialogFont(ancestor); 64 65 return parent; 66 } 67 68 private Composite createListAndButtons(Composite parent) { 69 Composite listAndButtons = new Composite(parent, SWT.NONE); 70 GridLayout layout = new GridLayout(); 71 layout.marginWidth = 0; 72 layout.marginHeight = 0; 73 layout.numColumns = 2; 74 listAndButtons.setLayout(layout); 75 listAndButtons.setLayoutData(new GridData(GridData.FILL_BOTH)); 76 77 Label viewerLabel = new Label(listAndButtons, SWT.NONE); 78 viewerLabel.setText(CVSUIMessages.CommentTemplatesPreferencePage_Description); 79 GridData data = new GridData(GridData.FILL_HORIZONTAL); 80 data.horizontalSpan = 2; 81 viewerLabel.setLayoutData(data); 82 83 viewer = new ListViewer(listAndButtons); 84 viewer.setLabelProvider(new LabelProvider() { 85 public String getText(Object element) { 86 String template = (String ) element; 87 return Util.flattenText(template); 88 } 89 }); 90 viewer.addSelectionChangedListener(this); 91 viewer.setComparator(new ViewerComparator() { 92 public int compare(Viewer viewer, Object e1, Object e2) { 93 String template1 = Util.flattenText((String ) e1); 94 String template2 = Util.flattenText((String ) e2); 95 return template1.compareToIgnoreCase(template2); 96 } 97 }); 98 viewer.addDoubleClickListener(new IDoubleClickListener() { 99 public void doubleClick(DoubleClickEvent event) { 100 editTemplate(); 101 } 102 }); 103 List list = viewer.getList(); 104 list.setLayoutData(new GridData(GridData.FILL_BOTH)); 105 106 String [] templates = 108 CVSUIPlugin.getPlugin().getRepositoryManager().getCommentTemplates(); 109 for (int i = 0; i < templates.length; i++) { 110 viewer.add(templates[i]); 111 } 112 113 createButtons(listAndButtons); 114 return listAndButtons; 115 } 116 117 private void createButtons(Composite parent) { 118 Composite buttons = new Composite(parent, SWT.NONE); 119 buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING)); 120 GridLayout layout = new GridLayout(); 121 layout.marginHeight = 0; 122 layout.marginWidth = 0; 123 buttons.setLayout(layout); 124 125 Button newButton = new Button(buttons, SWT.PUSH); 126 newButton.setText(CVSUIMessages.CommentTemplatesPreferencePage_New); 127 GridData data = new GridData(); 128 data.horizontalAlignment = GridData.FILL; 129 int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 130 data.widthHint = Math.max(widthHint, 131 newButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); 132 newButton.setLayoutData(data); 133 newButton.setEnabled(true); 134 newButton.addListener(SWT.Selection, new Listener() { 135 public void handleEvent(Event event) { 136 newTemplate(); 137 } 138 }); 139 140 editButton = new Button(buttons, SWT.PUSH); 141 editButton.setText(CVSUIMessages.CommentTemplatesPreferencePage_Edit); 142 data = new GridData(); 143 data.horizontalAlignment = GridData.FILL; 144 widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 145 data.widthHint = Math.max(widthHint, 146 editButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); 147 editButton.setLayoutData(data); 148 editButton.setEnabled(false); 149 editButton.addListener(SWT.Selection, new Listener() { 150 public void handleEvent(Event e) { 151 editTemplate(); 152 } 153 }); 154 155 removeButton = new Button(buttons, SWT.PUSH); 156 removeButton.setText(CVSUIMessages.CommentTemplatesPreferencePage_Remove); 157 data = new GridData(); 158 data.horizontalAlignment = GridData.FILL; 159 widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 160 data.widthHint = Math.max(widthHint, 161 removeButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); 162 removeButton.setLayoutData(data); 163 removeButton.setEnabled(false); 164 removeButton.addListener(SWT.Selection, new Listener() { 165 public void handleEvent(Event e) { 166 remove(); 167 } 168 }); 169 } 170 171 public void selectionChanged(SelectionChangedEvent event) { 172 IStructuredSelection selection = (IStructuredSelection) event.getSelection(); 173 switch (selection.size()) { 174 case 0: 175 editButton.setEnabled(false); 176 removeButton.setEnabled(false); 177 preview.setText(""); break; 179 180 case 1: 181 editButton.setEnabled(true); 182 removeButton.setEnabled(true); 183 preview.setText((String ) selection.getFirstElement()); 184 break; 185 186 default: 187 editButton.setEnabled(false); 188 removeButton.setEnabled(true); 189 preview.setText(""); break; 191 } 192 } 193 194 void newTemplate() { 195 CommentTemplateEditDialog dialog = new CommentTemplateEditDialog( 196 getShell(), 197 CVSUIMessages.CommentTemplatesPreferencePage_EditCommentTemplateTitle, 198 CVSUIMessages.CommentTemplatesPreferencePage_EditCommentTemplateMessage, 199 "", null); if (dialog.open() == Window.OK) { 201 viewer.add(dialog.getValue()); 202 } 203 } 204 205 void editTemplate() { 206 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection(); 207 if (selection.size() == 1) { 208 String oldTemplate = (String ) selection.getFirstElement(); 209 CommentTemplateEditDialog dialog = new CommentTemplateEditDialog( 210 getShell(), 211 CVSUIMessages.CommentTemplatesPreferencePage_EditCommentTemplateTitle, 212 CVSUIMessages.CommentTemplatesPreferencePage_EditCommentTemplateMessage, 213 oldTemplate, null); 214 if (dialog.open() == Window.OK) { 215 viewer.remove(oldTemplate); 216 viewer.add(dialog.getValue()); 217 } 218 } 219 } 220 221 void remove() { 222 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection(); 223 viewer.remove(selection.toArray()); 224 } 225 226 public boolean performOk() { 227 int numTemplates = viewer.getList().getItemCount(); 228 String [] templates = new String [numTemplates]; 229 for (int i = 0; i < numTemplates; i++) { 230 templates[i] = (String ) viewer.getElementAt(i); 231 } 232 try { 233 CVSUIPlugin.getPlugin().getRepositoryManager() 234 .replaceAndSaveCommentTemplates(templates); 235 } catch (TeamException e) { 236 CVSUIPlugin.openError(getShell(), null, null, e, CVSUIPlugin.LOG_OTHER_EXCEPTIONS); 237 } 238 239 return super.performOk(); 240 } 241 242 public void init(IWorkbench workbench) { 243 } 245 } 246 | Popular Tags |