|
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2005 Maik Schreiber. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Maik Schreiber - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.team.internal.ccvs.ui; |
| 13 |
|
| 14 |
import org.eclipse.jface.dialogs.Dialog; |
| 15 |
import org.eclipse.jface.dialogs.IDialogConstants; |
| 16 |
import org.eclipse.jface.preference.PreferencePage; |
| 17 |
import org.eclipse.jface.viewers.DoubleClickEvent; |
| 18 |
import org.eclipse.jface.viewers.IDoubleClickListener; |
| 19 |
import org.eclipse.jface.viewers.ISelectionChangedListener; |
| 20 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 21 |
import org.eclipse.jface.viewers.LabelProvider; |
| 22 |
import org.eclipse.jface.viewers.ListViewer; |
| 23 |
import org.eclipse.jface.viewers.SelectionChangedEvent; |
| 24 |
import org.eclipse.jface.viewers.Viewer; |
| 25 |
import org.eclipse.jface.viewers.ViewerSorter; |
| 26 |
import org.eclipse.jface.window.Window; |
| 27 |
import org.eclipse.swt.SWT; |
| 28 |
import org.eclipse.swt.layout.GridData; |
| 29 |
import org.eclipse.swt.layout.GridLayout; |
| 30 |
import org.eclipse.swt.widgets.Button; |
| 31 |
import org.eclipse.swt.widgets.Composite; |
| 32 |
import org.eclipse.swt.widgets.Control; |
| 33 |
import org.eclipse.swt.widgets.Event; |
| 34 |
import org.eclipse.swt.widgets.Label; |
| 35 |
import org.eclipse.swt.widgets.List; |
| 36 |
import org.eclipse.swt.widgets.Listener; |
| 37 |
import org.eclipse.swt.widgets.Text; |
| 38 |
import org.eclipse.team.core.TeamException; |
| 39 |
import org.eclipse.ui.IWorkbench; |
| 40 |
import org.eclipse.ui.IWorkbenchPreferencePage; |
| 41 |
|
| 42 |
public class CommentTemplatesPreferencePage extends PreferencePage implements |
| 43 |
IWorkbenchPreferencePage, ISelectionChangedListener { |
| 44 |
|
| 45 |
private ListViewer viewer; |
| 46 |
private Button editButton; |
| 47 |
private Button removeButton; |
| 48 |
private Text preview; |
| 49 |
|
| 50 |
protected Control createContents(Composite ancestor) { |
| 51 |
Composite parent = new Composite(ancestor, SWT.NONE); |
| 52 |
GridLayout layout = new GridLayout(); |
| 53 |
layout.marginWidth = 0; |
| 54 |
layout.marginHeight = 0; |
| 55 |
layout.numColumns = 1; |
| 56 |
parent.setLayout(layout); |
| 57 |
parent.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 58 |
|
| 59 |
createListAndButtons(parent); |
| 60 |
|
| 61 |
Label previewLabel = new Label(parent, SWT.NONE); |
| 62 |
previewLabel.setText(CVSUIMessages.CommentTemplatesPreferencePage_Preview); |
| 63 |
|
| 64 |
preview = new Text(parent, SWT.MULTI | SWT.READ_ONLY | SWT.BORDER); |
| 65 |
GridData gd = new GridData(GridData.FILL_HORIZONTAL); |
| 66 |
gd.heightHint = convertHeightInCharsToPixels(5); |
| 67 |
preview.setLayoutData(gd); |
| 68 |
|
| 69 |
Dialog.applyDialogFont(ancestor); |
| 70 |
|
| 71 |
return parent; |
| 72 |
} |
| 73 |
|
| 74 |
private Composite createListAndButtons(Composite parent) { |
| 75 |
Composite listAndButtons = new Composite(parent, SWT.NONE); |
| 76 |
GridLayout layout = new GridLayout(); |
| 77 |
layout.marginWidth = 0; |
| 78 |
layout.marginHeight = 0; |
| 79 |
layout.numColumns = 2; |
| 80 |
listAndButtons.setLayout(layout); |
| 81 |
listAndButtons.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 82 |
|
| 83 |
viewer = new ListViewer(listAndButtons); |
| 84 |
viewer.setLabelProvider(new LabelProvider() { |
| 85 |
public String getText(Object element) { |
| 86 |
String template = (String) element; |
| 87 |
return HistoryView.flattenText(template); |
| 88 |
} |
| 89 |
}); |
| 90 |
viewer.addSelectionChangedListener(this); |
| 91 |
viewer.setSorter(new ViewerSorter() { |
| 92 |
public int compare(Viewer viewer, Object e1, Object e2) { |
| 93 |
String template1 = HistoryView.flattenText((String) e1); |
| 94 |
String template2 = HistoryView.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 |
// populate list |
| 107 |
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 init(IWorkbench workbench) { |
| 172 |
setDescription(CVSUIMessages.CommentTemplatesPreferencePage_Description); |
| 173 |
} |
| 174 |
|
| 175 |
public void selectionChanged(SelectionChangedEvent event) { |
| 176 |
IStructuredSelection selection = (IStructuredSelection) event.getSelection(); |
| 177 |
switch (selection.size()) { |
| 178 |
case 0: |
| 179 |
editButton.setEnabled(false); |
| 180 |
removeButton.setEnabled(false); |
| 181 |
preview.setText(""); //$NON-NLS-1$ |
| 182 |
break; |
| 183 |
|
| 184 |
case 1: |
| 185 |
editButton.setEnabled(true); |
| 186 |
removeButton.setEnabled(true); |
| 187 |
preview.setText((String) selection.getFirstElement()); |
| 188 |
break; |
| 189 |
|
| 190 |
default: |
| 191 |
editButton.setEnabled(false); |
| 192 |
removeButton.setEnabled(true); |
| 193 |
preview.setText(""); //$NON-NLS-1$ |
| 194 |
break; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
void newTemplate() { |
| 199 |
CommentTemplateEditDialog dialog = new CommentTemplateEditDialog( |
| 200 |
getShell(), |
| 201 |
CVSUIMessages.CommentTemplatesPreferencePage_EditCommentTemplateTitle, |
| 202 |
CVSUIMessages.CommentTemplatesPreferencePage_EditCommentTemplateMessage, |
| 203 |
"", null); //$NON-NLS-1$ |
| 204 |
if (dialog.open() == Window.OK) { |
| 205 |
viewer.add(dialog.getValue()); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
void editTemplate() { |
| 210 |
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection(); |
| 211 |
if (selection.size() == 1) { |
| 212 |
String oldTemplate = (String) selection.getFirstElement(); |
| 213 |
CommentTemplateEditDialog dialog = new CommentTemplateEditDialog( |
| 214 |
getShell(), |
| 215 |
CVSUIMessages.CommentTemplatesPreferencePage_EditCommentTemplateTitle, |
| 216 |
CVSUIMessages.CommentTemplatesPreferencePage_EditCommentTemplateMessage, |
| 217 |
oldTemplate, null); |
| 218 |
if (dialog.open() == Window.OK) { |
| 219 |
viewer.remove(oldTemplate); |
| 220 |
viewer.add(dialog.getValue()); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
void remove() { |
| 226 |
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection(); |
| 227 |
viewer.remove(selection.toArray()); |
| 228 |
} |
| 229 |
|
| 230 |
public boolean performOk() { |
| 231 |
int numTemplates = viewer.getList().getItemCount(); |
| 232 |
String[] templates = new String[numTemplates]; |
| 233 |
for (int i = 0; i < numTemplates; i++) { |
| 234 |
templates[i] = (String) viewer.getElementAt(i); |
| 235 |
} |
| 236 |
try { |
| 237 |
CVSUIPlugin.getPlugin().getRepositoryManager() |
| 238 |
.replaceAndSaveCommentTemplates(templates); |
| 239 |
} catch (TeamException e) { |
| 240 |
// TODO: handle save error |
| 241 |
} |
| 242 |
|
| 243 |
return super.performOk(); |
| 244 |
} |
| 245 |
} |