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