]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 IBM Corporation and others. | |
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 | * IBM Corporation - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.ui.preferences.formatter; | |
12 | ||
13 | import java.io.File; | |
14 | import java.util.ArrayList; | |
15 | import java.util.Collection; | |
16 | import java.util.HashMap; | |
17 | import java.util.Iterator; | |
18 | import java.util.List; | |
19 | import java.util.Map; | |
20 | import java.util.Map.Entry; | |
21 | ||
22 | import org.eclipse.swt.SWT; | |
23 | import org.eclipse.swt.events.SelectionEvent; | |
24 | import org.eclipse.swt.events.SelectionListener; | |
25 | import org.eclipse.swt.graphics.Point; | |
26 | import org.eclipse.swt.graphics.Rectangle; | |
27 | import org.eclipse.swt.layout.GridData; | |
28 | import org.eclipse.swt.layout.GridLayout; | |
29 | import org.eclipse.swt.widgets.Button; | |
30 | import org.eclipse.swt.widgets.Composite; | |
31 | import org.eclipse.swt.widgets.Control; | |
32 | import org.eclipse.swt.widgets.FileDialog; | |
33 | import org.eclipse.swt.widgets.Label; | |
34 | import org.eclipse.swt.widgets.Shell; | |
35 | import org.eclipse.swt.widgets.TabFolder; | |
36 | import org.eclipse.swt.widgets.TabItem; | |
37 | ||
38 | import org.eclipse.core.runtime.CoreException; | |
39 | import org.eclipse.core.runtime.IStatus; | |
40 | import org.eclipse.core.runtime.Platform; | |
41 | import org.eclipse.core.runtime.Status; | |
42 | import org.eclipse.core.runtime.content.IContentType; | |
43 | ||
44 | import org.eclipse.jface.dialogs.IDialogConstants; | |
45 | import org.eclipse.jface.dialogs.IDialogSettings; | |
46 | import org.eclipse.jface.dialogs.MessageDialog; | |
47 | import org.eclipse.jface.dialogs.StatusDialog; | |
48 | ||
49 | import org.eclipse.ui.PlatformUI; | |
50 | ||
51 | import org.eclipse.jdt.internal.corext.util.Messages; | |
52 | ||
53 | import org.eclipse.jdt.ui.JavaUI; | |
54 | ||
55 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
56 | import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; | |
57 | import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile; | |
58 | import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile; | |
59 | import org.eclipse.jdt.internal.ui.util.ExceptionHandler; | |
60 | import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels; | |
61 | import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; | |
62 | import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; | |
63 | import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField; | |
64 | ||
65 | public abstract class ModifyDialog extends StatusDialog implements IModifyDialogTabPage.IModificationListener { | |
66 | ||
67 | /** | |
68 | * The keys to retrieve the preferred area from the dialog settings. | |
69 | */ | |
70 | private static final String DS_KEY_PREFERRED_WIDTH= "modify_dialog.preferred_width"; //$NON-NLS-1$ | |
71 | private static final String DS_KEY_PREFERRED_HEIGHT= "modify_dialog.preferred_height"; //$NON-NLS-1$ | |
72 | private static final String DS_KEY_PREFERRED_X= "modify_dialog.preferred_x"; //$NON-NLS-1$ | |
73 | private static final String DS_KEY_PREFERRED_Y= "modify_dialog.preferred_y"; //$NON-NLS-1$ | |
74 | ||
75 | ||
76 | /** | |
77 | * The key to store the number (beginning at 0) of the tab page which had the | |
78 | * focus last time. | |
79 | */ | |
80 | private static final String DS_KEY_LAST_FOCUS= "modify_dialog.last_focus"; //$NON-NLS-1$ | |
81 | ||
82 | private static final int APPLAY_BUTTON_ID= IDialogConstants.CLIENT_ID; | |
83 | private static final int SAVE_BUTTON_ID= IDialogConstants.CLIENT_ID + 1; | |
84 | ||
85 | private final String fKeyPreferredWidth; | |
86 | private final String fKeyPreferredHight; | |
87 | private final String fKeyPreferredX; | |
88 | private final String fKeyPreferredY; | |
89 | private final String fKeyLastFocus; | |
90 | private final String fLastSaveLoadPathKey; | |
91 | private final ProfileStore fProfileStore; | |
92 | private final boolean fNewProfile; | |
93 | private Profile fProfile; | |
94 | private final Map<String, String> fWorkingValues; | |
95 | private final List<IModifyDialogTabPage> fTabPages; | |
96 | private final IDialogSettings fDialogSettings; | |
97 | private TabFolder fTabFolder; | |
98 | private final ProfileManager fProfileManager; | |
99 | private Button fApplyButton; | |
100 | private Button fSaveButton; | |
101 | private StringDialogField fProfileNameField; | |
102 | ||
103 | public ModifyDialog(Shell parentShell, Profile profile, ProfileManager profileManager, ProfileStore profileStore, boolean newProfile, String dialogPreferencesKey, String lastSavePathKey) { | |
104 | super(parentShell); | |
105 | ||
106 | fProfileStore= profileStore; | |
107 | fLastSaveLoadPathKey= lastSavePathKey; | |
108 | ||
109 | fKeyPreferredWidth= JavaUI.ID_PLUGIN + dialogPreferencesKey + DS_KEY_PREFERRED_WIDTH; | |
110 | fKeyPreferredHight= JavaUI.ID_PLUGIN + dialogPreferencesKey + DS_KEY_PREFERRED_HEIGHT; | |
111 | fKeyPreferredX= JavaUI.ID_PLUGIN + dialogPreferencesKey + DS_KEY_PREFERRED_X; | |
112 | fKeyPreferredY= JavaUI.ID_PLUGIN + dialogPreferencesKey + DS_KEY_PREFERRED_Y; | |
113 | fKeyLastFocus= JavaUI.ID_PLUGIN + dialogPreferencesKey + DS_KEY_LAST_FOCUS; | |
114 | ||
115 | fProfileManager= profileManager; | |
116 | fNewProfile= newProfile; | |
117 | ||
118 | fProfile= profile; | |
119 | setTitle(Messages.format(FormatterMessages.ModifyDialog_dialog_title, profile.getName())); | |
120 | fWorkingValues= new HashMap<String, String>(fProfile.getSettings()); | |
121 | setStatusLineAboveButtons(false); | |
122 | fTabPages= new ArrayList<IModifyDialogTabPage>(); | |
123 | fDialogSettings= JavaPlugin.getDefault().getDialogSettings(); | |
124 | } | |
125 | ||
126 | /* | |
127 | * @see org.eclipse.jface.dialogs.Dialog#isResizable() | |
128 | * @since 3.4 | |
129 | */ | |
130 | @Override | |
131 | protected boolean isResizable() { | |
132 | return true; | |
133 | } | |
134 | ||
135 | protected abstract void addPages(Map<String, String> values); | |
136 | ||
137 | @Override | |
138 | public void create() { | |
139 | super.create(); | |
140 | int lastFocusNr= 0; | |
141 | try { | |
142 | lastFocusNr= fDialogSettings.getInt(fKeyLastFocus); | |
143 | if (lastFocusNr < 0) lastFocusNr= 0; | |
144 | if (lastFocusNr > fTabPages.size() - 1) lastFocusNr= fTabPages.size() - 1; | |
145 | } catch (NumberFormatException x) { | |
146 | lastFocusNr= 0; | |
147 | } | |
148 | ||
149 | if (!fNewProfile) { | |
150 | fTabFolder.setSelection(lastFocusNr); | |
151 | ((IModifyDialogTabPage)fTabFolder.getSelection()[0].getData()).setInitialFocus(); | |
152 | } | |
153 | } | |
154 | ||
155 | ||
156 | ||
157 | @Override | |
158 | protected Control createDialogArea(Composite parent) { | |
159 | ||
160 | final Composite composite= (Composite)super.createDialogArea(parent); | |
161 | ||
162 | Composite nameComposite= new Composite(composite, SWT.NONE); | |
163 | nameComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); | |
164 | nameComposite.setLayout(new GridLayout(3, false)); | |
165 | ||
166 | fProfileNameField= new StringDialogField(); | |
167 | fProfileNameField.setLabelText(FormatterMessages.ModifyDialog_ProfileName_Label); | |
168 | fProfileNameField.setText(fProfile.getName()); | |
169 | fProfileNameField.getLabelControl(nameComposite).setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false)); | |
170 | fProfileNameField.getTextControl(nameComposite).setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); | |
171 | fProfileNameField.setDialogFieldListener(new IDialogFieldListener() { | |
172 | public void dialogFieldChanged(DialogField field) { | |
173 | doValidate(); | |
174 | } | |
175 | }); | |
176 | ||
177 | fSaveButton= createButton(nameComposite, SAVE_BUTTON_ID, FormatterMessages.ModifyDialog_Export_Button, false); | |
178 | ||
179 | fTabFolder = new TabFolder(composite, SWT.NONE); | |
180 | fTabFolder.setFont(composite.getFont()); | |
181 | fTabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); | |
182 | ||
183 | addPages(fWorkingValues); | |
184 | ||
185 | applyDialogFont(composite); | |
186 | ||
187 | fTabFolder.addSelectionListener(new SelectionListener() { | |
188 | public void widgetDefaultSelected(SelectionEvent e) {} | |
189 | public void widgetSelected(SelectionEvent e) { | |
190 | final TabItem tabItem= (TabItem)e.item; | |
191 | final IModifyDialogTabPage page= (IModifyDialogTabPage)tabItem.getData(); | |
192 | // page.fSashForm.setWeights(); | |
193 | fDialogSettings.put(fKeyLastFocus, fTabPages.indexOf(page)); | |
194 | page.makeVisible(); | |
195 | } | |
196 | }); | |
197 | ||
198 | doValidate(); | |
199 | ||
200 | PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, getHelpContextId()); | |
201 | ||
202 | return composite; | |
203 | } | |
204 | ||
205 | /** | |
206 | * Returns the context ID for the Help system | |
207 | * | |
208 | * @return the string used as ID for the Help context | |
209 | * @since 3.5 | |
210 | */ | |
211 | protected abstract String getHelpContextId(); | |
212 | ||
213 | @Override | |
214 | public void updateStatus(IStatus status) { | |
215 | if (status == null) { | |
216 | doValidate(); | |
217 | } else { | |
218 | super.updateStatus(status); | |
219 | } | |
220 | } | |
221 | ||
222 | /* (non-Javadoc) | |
223 | * @see org.eclipse.jface.window.Window#getInitialSize() | |
224 | */ | |
225 | @Override | |
226 | protected Point getInitialSize() { | |
227 | Point initialSize= super.getInitialSize(); | |
228 | try { | |
229 | int lastWidth= fDialogSettings.getInt(fKeyPreferredWidth); | |
230 | if (initialSize.x > lastWidth) | |
231 | lastWidth= initialSize.x; | |
232 | int lastHeight= fDialogSettings.getInt(fKeyPreferredHight); | |
233 | if (initialSize.y > lastHeight) | |
234 | lastHeight= initialSize.y; | |
235 | return new Point(lastWidth, lastHeight); | |
236 | } catch (NumberFormatException ex) { | |
237 | } | |
238 | return initialSize; | |
239 | } | |
240 | ||
241 | /* (non-Javadoc) | |
242 | * @see org.eclipse.jface.window.Window#getInitialLocation(org.eclipse.swt.graphics.Point) | |
243 | */ | |
244 | @Override | |
245 | protected Point getInitialLocation(Point initialSize) { | |
246 | try { | |
247 | return new Point(fDialogSettings.getInt(fKeyPreferredX), fDialogSettings.getInt(fKeyPreferredY)); | |
248 | } catch (NumberFormatException ex) { | |
249 | return super.getInitialLocation(initialSize); | |
250 | } | |
251 | } | |
252 | ||
253 | @Override | |
254 | public boolean close() { | |
255 | final Rectangle shell= getShell().getBounds(); | |
256 | ||
257 | fDialogSettings.put(fKeyPreferredWidth, shell.width); | |
258 | fDialogSettings.put(fKeyPreferredHight, shell.height); | |
259 | fDialogSettings.put(fKeyPreferredX, shell.x); | |
260 | fDialogSettings.put(fKeyPreferredY, shell.y); | |
261 | ||
262 | return super.close(); | |
263 | } | |
264 | ||
265 | /* (non-Javadoc) | |
266 | * @see org.eclipse.jface.dialogs.Dialog#okPressed() | |
267 | */ | |
268 | @Override | |
269 | protected void okPressed() { | |
270 | applyPressed(); | |
271 | super.okPressed(); | |
272 | } | |
273 | ||
274 | @Override | |
275 | protected void buttonPressed(int buttonId) { | |
276 | if (buttonId == APPLAY_BUTTON_ID) { | |
277 | applyPressed(); | |
278 | setTitle(Messages.format(FormatterMessages.ModifyDialog_dialog_title, fProfile.getName())); | |
279 | } else if (buttonId == SAVE_BUTTON_ID) { | |
280 | saveButtonPressed(); | |
281 | } else { | |
282 | super.buttonPressed(buttonId); | |
283 | } | |
284 | } | |
285 | ||
286 | private void applyPressed() { | |
287 | if (!fProfile.getName().equals(fProfileNameField.getText())) { | |
288 | fProfile= fProfile.rename(fProfileNameField.getText(), fProfileManager); | |
289 | } | |
290 | fProfile.setSettings(new HashMap<String, String>(fWorkingValues)); | |
291 | fProfileManager.setSelected(fProfile); | |
292 | doValidate(); | |
293 | } | |
294 | ||
295 | private void saveButtonPressed() { | |
296 | Profile selected= new CustomProfile(fProfileNameField.getText(), new HashMap<String, String>(fWorkingValues), fProfile.getVersion(), fProfileManager.getProfileVersioner().getProfileKind()); | |
297 | ||
298 | final FileDialog dialog= new FileDialog(getShell(), SWT.SAVE); | |
299 | dialog.setText(FormatterMessages.CodingStyleConfigurationBlock_save_profile_dialog_title); | |
300 | dialog.setFilterExtensions(new String [] {"*.xml"}); //$NON-NLS-1$ | |
301 | ||
302 | final String lastPath= JavaPlugin.getDefault().getDialogSettings().get(fLastSaveLoadPathKey + ".savepath"); //$NON-NLS-1$ | |
303 | if (lastPath != null) { | |
304 | dialog.setFilterPath(lastPath); | |
305 | } | |
306 | final String path= dialog.open(); | |
307 | if (path == null) | |
308 | return; | |
309 | ||
310 | JavaPlugin.getDefault().getDialogSettings().put(fLastSaveLoadPathKey + ".savepath", dialog.getFilterPath()); //$NON-NLS-1$ | |
311 | ||
312 | final File file= new File(path); | |
313 | if (file.exists() && !MessageDialog.openQuestion(getShell(), FormatterMessages.CodingStyleConfigurationBlock_save_profile_overwrite_title, Messages.format(FormatterMessages.CodingStyleConfigurationBlock_save_profile_overwrite_message, BasicElementLabels.getPathLabel(file)))) { | |
314 | return; | |
315 | } | |
316 | String encoding= ProfileStore.ENCODING; | |
317 | final IContentType type= Platform.getContentTypeManager().getContentType("org.eclipse.core.runtime.xml"); //$NON-NLS-1$ | |
318 | if (type != null) | |
319 | encoding= type.getDefaultCharset(); | |
320 | final Collection<Profile> profiles= new ArrayList<Profile>(); | |
321 | profiles.add(selected); | |
322 | try { | |
323 | fProfileStore.writeProfilesToFile(profiles, file, encoding); | |
324 | } catch (CoreException e) { | |
325 | final String title= FormatterMessages.CodingStyleConfigurationBlock_save_profile_error_title; | |
326 | final String message= FormatterMessages.CodingStyleConfigurationBlock_save_profile_error_message; | |
327 | ExceptionHandler.handle(e, getShell(), title, message); | |
328 | } | |
329 | } | |
330 | ||
331 | @Override | |
332 | protected void createButtonsForButtonBar(Composite parent) { | |
333 | fApplyButton= createButton(parent, APPLAY_BUTTON_ID, FormatterMessages.ModifyDialog_apply_button, false); | |
334 | fApplyButton.setEnabled(false); | |
335 | ||
336 | GridLayout layout= (GridLayout) parent.getLayout(); | |
337 | layout.numColumns++; | |
338 | layout.makeColumnsEqualWidth= false; | |
339 | Label label= new Label(parent, SWT.NONE); | |
340 | GridData data= new GridData(); | |
341 | data.widthHint= layout.horizontalSpacing; | |
342 | label.setLayoutData(data); | |
343 | super.createButtonsForButtonBar(parent); | |
344 | } | |
345 | ||
346 | protected final void addTabPage(String title, IModifyDialogTabPage tabPage) { | |
347 | final TabItem tabItem= new TabItem(fTabFolder, SWT.NONE); | |
348 | applyDialogFont(tabItem.getControl()); | |
349 | tabItem.setText(title); | |
350 | tabItem.setData(tabPage); | |
351 | tabItem.setControl(tabPage.createContents(fTabFolder)); | |
352 | fTabPages.add(tabPage); | |
353 | } | |
354 | ||
355 | public void valuesModified() { | |
356 | doValidate(); | |
357 | } | |
358 | ||
359 | @Override | |
360 | protected void updateButtonsEnableState(IStatus status) { | |
361 | super.updateButtonsEnableState(status); | |
362 | if (fApplyButton != null && !fApplyButton.isDisposed()) { | |
363 | fApplyButton.setEnabled(hasChanges() && !status.matches(IStatus.ERROR)); | |
364 | } | |
365 | if (fSaveButton != null && !fSaveButton.isDisposed()) { | |
366 | fSaveButton.setEnabled(!validateProfileName().matches(IStatus.ERROR)); | |
367 | } | |
368 | } | |
369 | ||
370 | private void doValidate() { | |
371 | String name= fProfileNameField.getText().trim(); | |
372 | if (name.equals(fProfile.getName()) && fProfile.hasEqualSettings(fWorkingValues, fWorkingValues.keySet())) { | |
373 | updateStatus(StatusInfo.OK_STATUS); | |
374 | return; | |
375 | } | |
376 | ||
377 | IStatus status= validateProfileName(); | |
378 | if (status.matches(IStatus.ERROR)) { | |
379 | updateStatus(status); | |
380 | return; | |
381 | } | |
382 | ||
383 | if (!name.equals(fProfile.getName()) && fProfileManager.containsName(name)) { | |
384 | updateStatus(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, FormatterMessages.ModifyDialog_Duplicate_Status)); | |
385 | return; | |
386 | } | |
387 | ||
388 | if (fProfile.isBuiltInProfile() || fProfile.isSharedProfile()) { | |
389 | updateStatus(new Status(IStatus.INFO, JavaUI.ID_PLUGIN, FormatterMessages.ModifyDialog_NewCreated_Status)); | |
390 | return; | |
391 | } | |
392 | ||
393 | updateStatus(StatusInfo.OK_STATUS); | |
394 | } | |
395 | ||
396 | private IStatus validateProfileName() { | |
397 | final String name= fProfileNameField.getText().trim(); | |
398 | ||
399 | if (fProfile.isBuiltInProfile()) { | |
400 | if (fProfile.getName().equals(name)) { | |
401 | return new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, FormatterMessages.ModifyDialog_BuiltIn_Status); | |
402 | } | |
403 | } | |
404 | ||
405 | if (fProfile.isSharedProfile()) { | |
406 | if (fProfile.getName().equals(name)) { | |
407 | return new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, FormatterMessages.ModifyDialog_Shared_Status); | |
408 | } | |
409 | } | |
410 | ||
411 | if (name.length() == 0) { | |
412 | return new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, FormatterMessages.ModifyDialog_EmptyName_Status); | |
413 | } | |
414 | ||
415 | return StatusInfo.OK_STATUS; | |
416 | } | |
417 | ||
418 | private boolean hasChanges() { | |
419 | if (!fProfileNameField.getText().trim().equals(fProfile.getName())) | |
420 | return true; | |
421 | ||
422 | Iterator<Entry<String, String>> iter= fProfile.getSettings().entrySet().iterator(); | |
423 | for (;iter.hasNext();) { | |
424 | Entry<String, String> curr= iter.next(); | |
425 | if (!fWorkingValues.get(curr.getKey()).equals(curr.getValue())) { | |
426 | return true; | |
427 | } | |
428 | } | |
429 | return false; | |
430 | } | |
431 | ||
432 | } |