|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Common Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/cpl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.releng.tools.preferences; |
| 12 |
|
| 13 |
import org.eclipse.jface.preference.IPreferenceStore; |
| 14 |
import org.eclipse.jface.preference.PreferencePage; |
| 15 |
import org.eclipse.jface.text.BadLocationException; |
| 16 |
import org.eclipse.jface.text.Document; |
| 17 |
import org.eclipse.jface.text.IDocument; |
| 18 |
import org.eclipse.jface.text.IRegion; |
| 19 |
import org.eclipse.jface.text.source.SourceViewer; |
| 20 |
import org.eclipse.jface.text.source.SourceViewerConfiguration; |
| 21 |
import org.eclipse.releng.tools.Messages; |
| 22 |
import org.eclipse.releng.tools.RelEngPlugin; |
| 23 |
import org.eclipse.swt.SWT; |
| 24 |
import org.eclipse.swt.events.KeyAdapter; |
| 25 |
import org.eclipse.swt.events.KeyEvent; |
| 26 |
import org.eclipse.swt.events.KeyListener; |
| 27 |
import org.eclipse.swt.graphics.Font; |
| 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.Label; |
| 34 |
import org.eclipse.swt.widgets.Text; |
| 35 |
import org.eclipse.ui.IWorkbench; |
| 36 |
import org.eclipse.ui.IWorkbenchPreferencePage; |
| 37 |
|
| 38 |
/** |
| 39 |
* Copyright tools preference page |
| 40 |
*/ |
| 41 |
public class CopyrightPreferencePage extends PreferencePage implements IWorkbenchPreferencePage { |
| 42 |
private final String NEW_LINE = "\n"; //$NON-NLS-1$ |
| 43 |
private Composite fComposite; |
| 44 |
private Label fCopyrightLabel; |
| 45 |
private SourceViewer fEditor; |
| 46 |
private Text fInstructions; |
| 47 |
private Label fCreationYearLabel; |
| 48 |
private Text fCreationYear; |
| 49 |
private Label fRevisionYearLabel; |
| 50 |
private Text fRevisionYear; |
| 51 |
private Button fUseDefaultRevisionYear; |
| 52 |
private Button fReplaceAllExisting; |
| 53 |
// disable fix up existing copyright till it works better |
| 54 |
// private Button fFixExisting; |
| 55 |
private Button fIgnoreProperties; |
| 56 |
|
| 57 |
/* (non-Javadoc) |
| 58 |
* @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench) |
| 59 |
*/ |
| 60 |
public void init(IWorkbench workbench) { |
| 61 |
// TODO Auto-generated method stub |
| 62 |
|
| 63 |
} |
| 64 |
/* (non-Javadoc) |
| 65 |
* @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite) |
| 66 |
*/ |
| 67 |
protected Control createContents(Composite parent) { |
| 68 |
Font font = parent.getFont(); |
| 69 |
|
| 70 |
//The main composite |
| 71 |
fComposite = new Composite(parent, SWT.NONE); |
| 72 |
GridLayout layout = new GridLayout(); |
| 73 |
layout.numColumns = 2; |
| 74 |
layout.marginWidth = 0; |
| 75 |
layout.marginHeight = 0; |
| 76 |
fComposite.setLayout(layout); |
| 77 |
fComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 78 |
fComposite.setFont(font); |
| 79 |
|
| 80 |
// copyright template editor |
| 81 |
fEditor = createEditor(fComposite); |
| 82 |
|
| 83 |
GridData data = new GridData(GridData.FILL_HORIZONTAL); |
| 84 |
data.horizontalSpan = 2; |
| 85 |
data.horizontalIndent = 0; |
| 86 |
fInstructions = new Text(fComposite, SWT.READ_ONLY); |
| 87 |
fInstructions.setText(Messages.getString("CopyrightPreferencePage.0")); //$NON-NLS-1$ |
| 88 |
fInstructions.setLayoutData(data); |
| 89 |
|
| 90 |
// default creation year |
| 91 |
fCreationYearLabel = new Label(fComposite, SWT.NONE); |
| 92 |
fCreationYearLabel.setText(Messages.getString("CopyrightPreferencePage.1")); //$NON-NLS-1$ |
| 93 |
fCreationYear = new Text(fComposite, SWT.BORDER); |
| 94 |
fCreationYear.setTextLimit(4); |
| 95 |
|
| 96 |
// default revision year |
| 97 |
fRevisionYearLabel = new Label(fComposite, SWT.NONE); |
| 98 |
fRevisionYearLabel.setText(Messages.getString("CopyrightPreferencePage.7")); //$NON-NLS-1$ |
| 99 |
fRevisionYear = new Text(fComposite, SWT.BORDER); |
| 100 |
fRevisionYear.setTextLimit(4); |
| 101 |
|
| 102 |
// always use default revision year instead of cvs lookup |
| 103 |
fUseDefaultRevisionYear = new Button(fComposite, SWT.CHECK); |
| 104 |
fUseDefaultRevisionYear.setText(Messages.getString("CopyrightPreferencePage.8")); //$NON-NLS-1$ |
| 105 |
data = new GridData(); |
| 106 |
data.horizontalSpan = 2; |
| 107 |
fUseDefaultRevisionYear.setLayoutData(data); |
| 108 |
|
| 109 |
// replace all existing copyright statement |
| 110 |
fReplaceAllExisting = new Button(fComposite, SWT.CHECK); |
| 111 |
fReplaceAllExisting.setText(Messages.getString("CopyrightPreferencePage.2")); //$NON-NLS-1$ |
| 112 |
data = new GridData(); |
| 113 |
data.horizontalSpan = 2; |
| 114 |
fReplaceAllExisting.setLayoutData(data); |
| 115 |
|
| 116 |
// disable fix up existing copyright till it works better |
| 117 |
// // fix up existing copyright statement |
| 118 |
// fFixExisting = new Button(fComposite, SWT.CHECK); |
| 119 |
// fFixExisting.setText(Messages.getString("CopyrightPreferencePage.3")); //$NON-NLS-1$ |
| 120 |
// data = new GridData(); |
| 121 |
// data.horizontalSpan = 2; |
| 122 |
// fFixExisting.setLayoutData(data); |
| 123 |
|
| 124 |
// ignore properties files |
| 125 |
fIgnoreProperties = new Button(fComposite, SWT.CHECK); |
| 126 |
fIgnoreProperties.setText(Messages.getString("CopyrightPreferencePage.4")); //$NON-NLS-1$ |
| 127 |
data = new GridData(); |
| 128 |
data.horizontalSpan = 2; |
| 129 |
fIgnoreProperties.setLayoutData(data); |
| 130 |
|
| 131 |
KeyListener listener1 = new KeyAdapter() { |
| 132 |
/* (non-Javadoc) |
| 133 |
* @see org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.swt.events.KeyEvent) |
| 134 |
*/ |
| 135 |
public void keyReleased(KeyEvent e) { |
| 136 |
validateValues(); |
| 137 |
} |
| 138 |
}; |
| 139 |
fCreationYear.addKeyListener(listener1); |
| 140 |
fRevisionYear.addKeyListener(listener1); |
| 141 |
|
| 142 |
// disable fix up existing copyright till it works better |
| 143 |
// SelectionListener listener2 = new SelectionAdapter() { |
| 144 |
// public void widgetSelected(SelectionEvent e) { |
| 145 |
// handleReplaceAllEnabled(fReplaceAllExisting.getSelection(), fFixExisting.getSelection()); |
| 146 |
// } |
| 147 |
// }; |
| 148 |
// fReplaceAllExisting.addSelectionListener(listener2); |
| 149 |
|
| 150 |
initializeValues(); |
| 151 |
return fComposite; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Create the sourceviewer editor to be used to edit the copyright template |
| 156 |
*/ |
| 157 |
private SourceViewer createEditor(Composite parent) { |
| 158 |
fCopyrightLabel = new Label(parent, SWT.NONE); |
| 159 |
fCopyrightLabel.setText(Messages.getString("CopyrightPreferencePage.5")); //$NON-NLS-1$ |
| 160 |
GridData data= new GridData(); |
| 161 |
data.horizontalSpan= 2; |
| 162 |
fCopyrightLabel.setLayoutData(data); |
| 163 |
|
| 164 |
SourceViewer viewer= createViewer(parent); |
| 165 |
|
| 166 |
IDocument document= new Document(); |
| 167 |
viewer.setEditable(true); |
| 168 |
viewer.setDocument(document); |
| 169 |
|
| 170 |
// just use a default 10 lines |
| 171 |
int nLines = 10; |
| 172 |
// int nLines= document.getNumberOfLines(); |
| 173 |
// if (nLines < 5) { |
| 174 |
// nLines= 5; |
| 175 |
// } else if (nLines > 12) { |
| 176 |
// nLines= 12; |
| 177 |
// } |
| 178 |
|
| 179 |
Control control= viewer.getControl(); |
| 180 |
data= new GridData(GridData.FILL_HORIZONTAL); |
| 181 |
data.widthHint= convertWidthInCharsToPixels(80); |
| 182 |
data.heightHint= convertHeightInCharsToPixels(nLines); |
| 183 |
data.horizontalSpan = 2; |
| 184 |
control.setLayoutData(data); |
| 185 |
|
| 186 |
// TODO add content assist support |
| 187 |
// viewer.addTextListener(new ITextListener() { |
| 188 |
// public void textChanged(TextEvent event) { |
| 189 |
// if (event.getDocumentEvent() != null) |
| 190 |
// doSourceChanged(event.getDocumentEvent().getDocument()); |
| 191 |
// } |
| 192 |
// }); |
| 193 |
// |
| 194 |
// viewer.addSelectionChangedListener(new ISelectionChangedListener() { |
| 195 |
// public void selectionChanged(SelectionChangedEvent event) { |
| 196 |
// updateSelectionDependentActions(); |
| 197 |
// } |
| 198 |
// }); |
| 199 |
// |
| 200 |
// viewer.prependVerifyKeyListener(new VerifyKeyListener() { |
| 201 |
// public void verifyKey(VerifyEvent event) { |
| 202 |
// handleVerifyKeyPressed(event); |
| 203 |
// } |
| 204 |
// }); |
| 205 |
|
| 206 |
return viewer; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Creates the viewer to be used to display the copyright. |
| 211 |
* |
| 212 |
* @param parent the parent composite of the viewer |
| 213 |
* @return a configured <code>SourceViewer</code> |
| 214 |
*/ |
| 215 |
private SourceViewer createViewer(Composite parent) { |
| 216 |
SourceViewer viewer= new SourceViewer(parent, null, null, false, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); |
| 217 |
SourceViewerConfiguration configuration= new SourceViewerConfiguration() { |
| 218 |
// TODO add content assist support |
| 219 |
// public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { |
| 220 |
// |
| 221 |
// ContentAssistant assistant= new ContentAssistant(); |
| 222 |
// assistant.enableAutoActivation(true); |
| 223 |
// assistant.enableAutoInsert(true); |
| 224 |
// assistant.setContentAssistProcessor(fTemplateProcessor, IDocument.DEFAULT_CONTENT_TYPE); |
| 225 |
// return assistant; |
| 226 |
// } |
| 227 |
}; |
| 228 |
viewer.configure(configuration); |
| 229 |
return viewer; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Initialize the control values in this preference page |
| 234 |
*/ |
| 235 |
private void initializeValues() { |
| 236 |
IPreferenceStore store = getPreferenceStore(); |
| 237 |
|
| 238 |
fEditor.getDocument().set(store.getString(RelEngCopyrightConstants.COPYRIGHT_TEMPLATE_KEY)); |
| 239 |
fCreationYear.setText(store.getString(RelEngCopyrightConstants.CREATION_YEAR_KEY)); |
| 240 |
fRevisionYear.setText(store.getString(RelEngCopyrightConstants.REVISION_YEAR_KEY)); |
| 241 |
fUseDefaultRevisionYear.setSelection(store.getBoolean(RelEngCopyrightConstants.USE_DEFAULT_REVISION_YEAR_KEY)); |
| 242 |
fReplaceAllExisting.setSelection(store.getBoolean(RelEngCopyrightConstants.REPLACE_ALL_EXISTING_KEY)); |
| 243 |
// disable fix up existing copyright till it works better |
| 244 |
// handleReplaceAllEnabled(fReplaceAllExisting.getSelection(), store.getBoolean(RelEngCopyrightConstants.FIX_UP_EXISTING_KEY)); |
| 245 |
fIgnoreProperties.setSelection(store.getBoolean(RelEngCopyrightConstants.IGNORE_PROPERTIES_KEY)); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Validate the control values in this preference page |
| 250 |
*/ |
| 251 |
private void validateValues() { |
| 252 |
String ERROR_MESSAGE = Messages.getString("CopyrightPreferencePage.6"); //$NON-NLS-1$ |
| 253 |
|
| 254 |
String errorMsg = null; |
| 255 |
|
| 256 |
// creation & revision year must be an integer |
| 257 |
String creationYear = fCreationYear.getText(); |
| 258 |
String revisionYear = fRevisionYear.getText(); |
| 259 |
try { |
| 260 |
int year = Integer.parseInt(creationYear); |
| 261 |
if (year < 0) { |
| 262 |
errorMsg = ERROR_MESSAGE; |
| 263 |
} |
| 264 |
year = Integer.parseInt(revisionYear); |
| 265 |
if (year < 0) { |
| 266 |
errorMsg = ERROR_MESSAGE; |
| 267 |
} |
| 268 |
} catch (NumberFormatException e) { |
| 269 |
errorMsg = ERROR_MESSAGE; |
| 270 |
} |
| 271 |
setErrorMessage(errorMsg); |
| 272 |
setValid(errorMsg == null); |
| 273 |
} |
| 274 |
|
| 275 |
// disable fix up existing copyright till it works better |
| 276 |
// /** |
| 277 |
// * Handles when the Replace all copyrights checkbox is checked/unchecked. |
| 278 |
// * When checked, fix up copyright checkbox is disabled and checked |
| 279 |
// * When unchecked, fix up copyright checkbox is enabled and set to default value |
| 280 |
// * @param replaceAll |
| 281 |
// * @param defaultValue |
| 282 |
// */ |
| 283 |
// private void handleReplaceAllEnabled(boolean replaceAll, boolean defaultValue) { |
| 284 |
// if (fReplaceAllExisting.isEnabled() && !replaceAll) |
| 285 |
// fFixExisting.setEnabled(true); |
| 286 |
// else |
| 287 |
// fFixExisting.setEnabled(false); |
| 288 |
// |
| 289 |
// if (replaceAll) { |
| 290 |
// fFixExisting.setSelection(replaceAll); |
| 291 |
// } else { |
| 292 |
// fFixExisting.setSelection(defaultValue); |
| 293 |
// } |
| 294 |
// } |
| 295 |
|
| 296 |
/* (non-Javadoc) |
| 297 |
* @see org.eclipse.jface.preference.PreferencePage#doGetPreferenceStore() |
| 298 |
*/ |
| 299 |
protected IPreferenceStore doGetPreferenceStore() { |
| 300 |
return RelEngPlugin.getDefault().getPreferenceStore(); |
| 301 |
} |
| 302 |
|
| 303 |
/* (non-Javadoc) |
| 304 |
* @see org.eclipse.jface.preference.PreferencePage#performDefaults() |
| 305 |
*/ |
| 306 |
protected void performDefaults() { |
| 307 |
IPreferenceStore store = getPreferenceStore(); |
| 308 |
|
| 309 |
fEditor.getDocument().set(store.getDefaultString(RelEngCopyrightConstants.COPYRIGHT_TEMPLATE_KEY)); |
| 310 |
fCreationYear.setText(store.getDefaultString(RelEngCopyrightConstants.CREATION_YEAR_KEY)); |
| 311 |
fRevisionYear.setText(store.getDefaultString(RelEngCopyrightConstants.REVISION_YEAR_KEY)); |
| 312 |
fUseDefaultRevisionYear.setSelection(getPreferenceStore().getDefaultBoolean(RelEngCopyrightConstants.USE_DEFAULT_REVISION_YEAR_KEY)); |
| 313 |
fReplaceAllExisting.setSelection(getPreferenceStore().getDefaultBoolean(RelEngCopyrightConstants.REPLACE_ALL_EXISTING_KEY)); |
| 314 |
// disable fix up existing copyright till it works better |
| 315 |
// handleReplaceAllEnabled(fReplaceAllExisting.getSelection(), getPreferenceStore().getDefaultBoolean(RelEngCopyrightConstants.FIX_UP_EXISTING_KEY)); |
| 316 |
fIgnoreProperties.setSelection(getPreferenceStore().getDefaultBoolean(RelEngCopyrightConstants.IGNORE_PROPERTIES_KEY)); |
| 317 |
|
| 318 |
super.performDefaults(); |
| 319 |
} |
| 320 |
|
| 321 |
/* (non-Javadoc) |
| 322 |
* @see org.eclipse.jface.preference.IPreferencePage#performOk() |
| 323 |
*/ |
| 324 |
public boolean performOk() { |
| 325 |
IPreferenceStore store = getPreferenceStore(); |
| 326 |
|
| 327 |
store.setValue(RelEngCopyrightConstants.COPYRIGHT_TEMPLATE_KEY, fixupLineDelimiters(fEditor.getDocument())); |
| 328 |
store.setValue(RelEngCopyrightConstants.CREATION_YEAR_KEY, fCreationYear.getText()); |
| 329 |
store.setValue(RelEngCopyrightConstants.REVISION_YEAR_KEY, fRevisionYear.getText()); |
| 330 |
store.setValue(RelEngCopyrightConstants.USE_DEFAULT_REVISION_YEAR_KEY, fUseDefaultRevisionYear.getSelection()); |
| 331 |
store.setValue(RelEngCopyrightConstants.REPLACE_ALL_EXISTING_KEY, fReplaceAllExisting.getSelection()); |
| 332 |
// disable fix up existing copyright till it works better |
| 333 |
// store.setValue(RelEngCopyrightConstants.FIX_UP_EXISTING_KEY, fFixExisting.getSelection()); |
| 334 |
store.setValue(RelEngCopyrightConstants.IGNORE_PROPERTIES_KEY, fIgnoreProperties.getSelection()); |
| 335 |
|
| 336 |
RelEngPlugin.getDefault().savePluginPreferences(); |
| 337 |
|
| 338 |
return super.performOk(); |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Fix up line delimiters in doc to use only \n |
| 343 |
* @param doc |
| 344 |
* @return |
| 345 |
*/ |
| 346 |
private String fixupLineDelimiters(IDocument doc) { |
| 347 |
String docContents = doc.get(); |
| 348 |
String newText = ""; //$NON-NLS-1$ |
| 349 |
int lineCount = doc.getNumberOfLines(); |
| 350 |
for (int i = 0; i < lineCount; i++) { |
| 351 |
try { |
| 352 |
IRegion lineInfo = doc.getLineInformation(i); |
| 353 |
int lineStartOffset = lineInfo.getOffset(); |
| 354 |
int lineLength = lineInfo.getLength(); |
| 355 |
int lineEndOffset = lineStartOffset + lineLength; |
| 356 |
newText += docContents.substring(lineStartOffset, lineEndOffset); |
| 357 |
|
| 358 |
if ((i < lineCount - 1) && (fEditor.getDocument().getLineDelimiter(i) != null)) |
| 359 |
newText += NEW_LINE; |
| 360 |
} |
| 361 |
catch (BadLocationException exception) { |
| 362 |
// exception |
| 363 |
} |
| 364 |
} |
| 365 |
return newText; |
| 366 |
} |
| 367 |
} |