001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.preferences.formatter;
011:
012: import java.io.File;
013: import java.util.ArrayList;
014: import java.util.Collection;
015: import java.util.HashMap;
016: import java.util.Iterator;
017: import java.util.List;
018: import java.util.Map;
019:
020: import org.eclipse.core.runtime.CoreException;
021: import org.eclipse.core.runtime.IStatus;
022: import org.eclipse.core.runtime.Platform;
023: import org.eclipse.core.runtime.Status;
024: import org.eclipse.core.runtime.content.IContentType;
025:
026: import org.eclipse.swt.SWT;
027: import org.eclipse.swt.events.SelectionEvent;
028: import org.eclipse.swt.events.SelectionListener;
029: import org.eclipse.swt.graphics.Point;
030: import org.eclipse.swt.graphics.Rectangle;
031: import org.eclipse.swt.layout.GridData;
032: import org.eclipse.swt.layout.GridLayout;
033: import org.eclipse.swt.widgets.Button;
034: import org.eclipse.swt.widgets.Composite;
035: import org.eclipse.swt.widgets.Control;
036: import org.eclipse.swt.widgets.FileDialog;
037: import org.eclipse.swt.widgets.Label;
038: import org.eclipse.swt.widgets.Shell;
039: import org.eclipse.swt.widgets.TabFolder;
040: import org.eclipse.swt.widgets.TabItem;
041:
042: import org.eclipse.jface.dialogs.IDialogConstants;
043: import org.eclipse.jface.dialogs.IDialogSettings;
044: import org.eclipse.jface.dialogs.MessageDialog;
045: import org.eclipse.jface.dialogs.StatusDialog;
046:
047: import org.eclipse.jdt.internal.corext.util.Messages;
048:
049: import org.eclipse.jdt.ui.JavaUI;
050:
051: import org.eclipse.jdt.internal.ui.JavaPlugin;
052: import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
053: import org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage.IModificationListener;
054: import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile;
055: import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile;
056: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
057: import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
058: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
059: import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
060:
061: public abstract class ModifyDialog extends StatusDialog implements
062: IModificationListener {
063:
064: /**
065: * The keys to retrieve the preferred area from the dialog settings.
066: */
067: private static final String DS_KEY_PREFERRED_WIDTH = "modify_dialog.preferred_width"; //$NON-NLS-1$
068: private static final String DS_KEY_PREFERRED_HEIGHT = "modify_dialog.preferred_height"; //$NON-NLS-1$
069: private static final String DS_KEY_PREFERRED_X = "modify_dialog.preferred_x"; //$NON-NLS-1$
070: private static final String DS_KEY_PREFERRED_Y = "modify_dialog.preferred_y"; //$NON-NLS-1$
071:
072: /**
073: * The key to store the number (beginning at 0) of the tab page which had the
074: * focus last time.
075: */
076: private static final String DS_KEY_LAST_FOCUS = "modify_dialog.last_focus"; //$NON-NLS-1$
077:
078: private static final int APPLAY_BUTTON_ID = IDialogConstants.CLIENT_ID;
079: private static final int SAVE_BUTTON_ID = IDialogConstants.CLIENT_ID + 1;
080:
081: private final String fKeyPreferredWidth;
082: private final String fKeyPreferredHight;
083: private final String fKeyPreferredX;
084: private final String fKeyPreferredY;
085: private final String fKeyLastFocus;
086: private final String fLastSaveLoadPathKey;
087: private final ProfileStore fProfileStore;
088: private final boolean fNewProfile;
089: private Profile fProfile;
090: private final Map fWorkingValues;
091: private final List fTabPages;
092: private final IDialogSettings fDialogSettings;
093: private TabFolder fTabFolder;
094: private final ProfileManager fProfileManager;
095: private Button fApplyButton;
096: private Button fSaveButton;
097: private StringDialogField fProfileNameField;
098:
099: public ModifyDialog(Shell parentShell, Profile profile,
100: ProfileManager profileManager, ProfileStore profileStore,
101: boolean newProfile, String dialogPreferencesKey,
102: String lastSavePathKey) {
103: super (parentShell);
104:
105: fProfileStore = profileStore;
106: fLastSaveLoadPathKey = lastSavePathKey;
107:
108: fKeyPreferredWidth = JavaUI.ID_PLUGIN + dialogPreferencesKey
109: + DS_KEY_PREFERRED_WIDTH;
110: fKeyPreferredHight = JavaUI.ID_PLUGIN + dialogPreferencesKey
111: + DS_KEY_PREFERRED_HEIGHT;
112: fKeyPreferredX = JavaUI.ID_PLUGIN + dialogPreferencesKey
113: + DS_KEY_PREFERRED_X;
114: fKeyPreferredY = JavaUI.ID_PLUGIN + dialogPreferencesKey
115: + DS_KEY_PREFERRED_Y;
116: fKeyLastFocus = JavaUI.ID_PLUGIN + dialogPreferencesKey
117: + DS_KEY_LAST_FOCUS;
118:
119: fProfileManager = profileManager;
120: fNewProfile = newProfile;
121: setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX);
122:
123: fProfile = profile;
124: setTitle(Messages.format(
125: FormatterMessages.ModifyDialog_dialog_title, profile
126: .getName()));
127: fWorkingValues = new HashMap(fProfile.getSettings());
128: setStatusLineAboveButtons(false);
129: fTabPages = new ArrayList();
130: fDialogSettings = JavaPlugin.getDefault().getDialogSettings();
131: }
132:
133: protected abstract void addPages(Map values);
134:
135: public void create() {
136: super .create();
137: int lastFocusNr = 0;
138: try {
139: lastFocusNr = fDialogSettings.getInt(fKeyLastFocus);
140: if (lastFocusNr < 0)
141: lastFocusNr = 0;
142: if (lastFocusNr > fTabPages.size() - 1)
143: lastFocusNr = fTabPages.size() - 1;
144: } catch (NumberFormatException x) {
145: lastFocusNr = 0;
146: }
147:
148: if (!fNewProfile) {
149: fTabFolder.setSelection(lastFocusNr);
150: ((ModifyDialogTabPage) fTabFolder.getSelection()[0]
151: .getData()).setInitialFocus();
152: }
153: }
154:
155: protected Control createDialogArea(Composite parent) {
156:
157: final Composite composite = (Composite) super
158: .createDialogArea(parent);
159:
160: Composite nameComposite = new Composite(composite, SWT.NONE);
161: nameComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
162: true, false));
163: nameComposite.setLayout(new GridLayout(3, false));
164:
165: fProfileNameField = new StringDialogField();
166: fProfileNameField
167: .setLabelText(FormatterMessages.ModifyDialog_ProfileName_Label);
168: fProfileNameField.setText(fProfile.getName());
169: fProfileNameField.getLabelControl(nameComposite).setLayoutData(
170: new GridData(SWT.LEFT, SWT.CENTER, false, false));
171: fProfileNameField.getTextControl(nameComposite).setLayoutData(
172: new GridData(SWT.FILL, SWT.CENTER, true, false));
173: fProfileNameField
174: .setDialogFieldListener(new IDialogFieldListener() {
175: public void dialogFieldChanged(DialogField field) {
176: doValidate();
177: }
178: });
179:
180: fSaveButton = createButton(nameComposite, SAVE_BUTTON_ID,
181: FormatterMessages.ModifyDialog_Export_Button, false);
182:
183: fTabFolder = new TabFolder(composite, SWT.NONE);
184: fTabFolder.setFont(composite.getFont());
185: fTabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
186: true));
187:
188: addPages(fWorkingValues);
189:
190: applyDialogFont(composite);
191:
192: fTabFolder.addSelectionListener(new SelectionListener() {
193: public void widgetDefaultSelected(SelectionEvent e) {
194: }
195:
196: public void widgetSelected(SelectionEvent e) {
197: final TabItem tabItem = (TabItem) e.item;
198: final ModifyDialogTabPage page = (ModifyDialogTabPage) tabItem
199: .getData();
200: // page.fSashForm.setWeights();
201: fDialogSettings.put(fKeyLastFocus, fTabPages
202: .indexOf(page));
203: page.makeVisible();
204: }
205: });
206:
207: doValidate();
208:
209: return composite;
210: }
211:
212: public void updateStatus(IStatus status) {
213: if (status == null) {
214: doValidate();
215: } else {
216: super .updateStatus(status);
217: }
218: }
219:
220: /* (non-Javadoc)
221: * @see org.eclipse.jface.window.Window#getInitialSize()
222: */
223: protected Point getInitialSize() {
224: Point initialSize = super .getInitialSize();
225: try {
226: int lastWidth = fDialogSettings.getInt(fKeyPreferredWidth);
227: if (initialSize.x > lastWidth)
228: lastWidth = initialSize.x;
229: int lastHeight = fDialogSettings.getInt(fKeyPreferredHight);
230: if (initialSize.y > lastHeight)
231: lastHeight = initialSize.y;
232: return new Point(lastWidth, lastHeight);
233: } catch (NumberFormatException ex) {
234: }
235: return initialSize;
236: }
237:
238: /* (non-Javadoc)
239: * @see org.eclipse.jface.window.Window#getInitialLocation(org.eclipse.swt.graphics.Point)
240: */
241: protected Point getInitialLocation(Point initialSize) {
242: try {
243: return new Point(fDialogSettings.getInt(fKeyPreferredX),
244: fDialogSettings.getInt(fKeyPreferredY));
245: } catch (NumberFormatException ex) {
246: return super .getInitialLocation(initialSize);
247: }
248: }
249:
250: public boolean close() {
251: final Rectangle shell = getShell().getBounds();
252:
253: fDialogSettings.put(fKeyPreferredWidth, shell.width);
254: fDialogSettings.put(fKeyPreferredHight, shell.height);
255: fDialogSettings.put(fKeyPreferredX, shell.x);
256: fDialogSettings.put(fKeyPreferredY, shell.y);
257:
258: return super .close();
259: }
260:
261: /* (non-Javadoc)
262: * @see org.eclipse.jface.dialogs.Dialog#okPressed()
263: */
264: protected void okPressed() {
265: applyPressed();
266: super .okPressed();
267: }
268:
269: protected void buttonPressed(int buttonId) {
270: if (buttonId == APPLAY_BUTTON_ID) {
271: applyPressed();
272: setTitle(Messages.format(
273: FormatterMessages.ModifyDialog_dialog_title,
274: fProfile.getName()));
275: } else if (buttonId == SAVE_BUTTON_ID) {
276: saveButtonPressed();
277: } else {
278: super .buttonPressed(buttonId);
279: }
280: }
281:
282: private void applyPressed() {
283: if (!fProfile.getName().equals(fProfileNameField.getText())) {
284: fProfile = fProfile.rename(fProfileNameField.getText(),
285: fProfileManager);
286: }
287: fProfile.setSettings(new HashMap(fWorkingValues));
288: fProfileManager.setSelected(fProfile);
289: doValidate();
290: }
291:
292: private void saveButtonPressed() {
293: Profile selected = new CustomProfile(fProfileNameField
294: .getText(), new HashMap(fWorkingValues), fProfile
295: .getVersion(), fProfileManager.getProfileVersioner()
296: .getProfileKind());
297:
298: final FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
299: dialog
300: .setText(FormatterMessages.CodingStyleConfigurationBlock_save_profile_dialog_title);
301: dialog.setFilterExtensions(new String[] { "*.xml" }); //$NON-NLS-1$
302:
303: final String lastPath = JavaPlugin.getDefault()
304: .getDialogSettings().get(
305: fLastSaveLoadPathKey + ".savepath"); //$NON-NLS-1$
306: if (lastPath != null) {
307: dialog.setFilterPath(lastPath);
308: }
309: final String path = dialog.open();
310: if (path == null)
311: return;
312:
313: JavaPlugin
314: .getDefault()
315: .getDialogSettings()
316: .put(
317: fLastSaveLoadPathKey + ".savepath", dialog.getFilterPath()); //$NON-NLS-1$
318:
319: final File file = new File(path);
320: if (file.exists()
321: && !MessageDialog
322: .openQuestion(
323: getShell(),
324: FormatterMessages.CodingStyleConfigurationBlock_save_profile_overwrite_title,
325: Messages
326: .format(
327: FormatterMessages.CodingStyleConfigurationBlock_save_profile_overwrite_message,
328: path))) {
329: return;
330: }
331: String encoding = ProfileStore.ENCODING;
332: final IContentType type = Platform.getContentTypeManager()
333: .getContentType("org.eclipse.core.runtime.xml"); //$NON-NLS-1$
334: if (type != null)
335: encoding = type.getDefaultCharset();
336: final Collection profiles = new ArrayList();
337: profiles.add(selected);
338: try {
339: fProfileStore.writeProfilesToFile(profiles, file, encoding);
340: } catch (CoreException e) {
341: final String title = FormatterMessages.CodingStyleConfigurationBlock_save_profile_error_title;
342: final String message = FormatterMessages.CodingStyleConfigurationBlock_save_profile_error_message;
343: ExceptionHandler.handle(e, getShell(), title, message);
344: }
345: }
346:
347: protected void createButtonsForButtonBar(Composite parent) {
348: fApplyButton = createButton(parent, APPLAY_BUTTON_ID,
349: FormatterMessages.ModifyDialog_apply_button, false);
350: fApplyButton.setEnabled(false);
351:
352: GridLayout layout = (GridLayout) parent.getLayout();
353: layout.numColumns++;
354: layout.makeColumnsEqualWidth = false;
355: Label label = new Label(parent, SWT.NONE);
356: GridData data = new GridData();
357: data.widthHint = layout.horizontalSpacing;
358: label.setLayoutData(data);
359: super .createButtonsForButtonBar(parent);
360: }
361:
362: protected final void addTabPage(String title,
363: ModifyDialogTabPage tabPage) {
364: final TabItem tabItem = new TabItem(fTabFolder, SWT.NONE);
365: applyDialogFont(tabItem.getControl());
366: tabItem.setText(title);
367: tabItem.setData(tabPage);
368: tabItem.setControl(tabPage.createContents(fTabFolder));
369: fTabPages.add(tabPage);
370: }
371:
372: public void valuesModified() {
373: doValidate();
374: }
375:
376: protected void updateButtonsEnableState(IStatus status) {
377: super .updateButtonsEnableState(status);
378: if (fApplyButton != null && !fApplyButton.isDisposed()) {
379: fApplyButton.setEnabled(hasChanges()
380: && !status.matches(IStatus.ERROR));
381: }
382: if (fSaveButton != null && !fSaveButton.isDisposed()) {
383: fSaveButton.setEnabled(!validateProfileName().matches(
384: IStatus.ERROR));
385: }
386: }
387:
388: private void doValidate() {
389: IStatus status = validateProfileName();
390: if (status.matches(IStatus.ERROR)) {
391: updateStatus(status);
392: return;
393: }
394:
395: String name = fProfileNameField.getText().trim();
396: if (!name.equals(fProfile.getName())
397: && fProfileManager.containsName(name)) {
398: updateStatus(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN,
399: FormatterMessages.ModifyDialog_Duplicate_Status));
400: return;
401: }
402:
403: if (fProfile.isBuiltInProfile() || fProfile.isSharedProfile()) {
404: updateStatus(new Status(IStatus.INFO, JavaUI.ID_PLUGIN,
405: FormatterMessages.ModifyDialog_NewCreated_Status));
406: return;
407: }
408:
409: updateStatus(StatusInfo.OK_STATUS);
410: }
411:
412: private IStatus validateProfileName() {
413: final String name = fProfileNameField.getText().trim();
414:
415: if (fProfile.isBuiltInProfile()) {
416: if (fProfile.getName().equals(name)) {
417: return new Status(IStatus.ERROR, JavaUI.ID_PLUGIN,
418: FormatterMessages.ModifyDialog_BuiltIn_Status);
419: }
420: }
421:
422: if (fProfile.isSharedProfile()) {
423: if (fProfile.getName().equals(name)) {
424: return new Status(IStatus.ERROR, JavaUI.ID_PLUGIN,
425: FormatterMessages.ModifyDialog_Shared_Status);
426: }
427: }
428:
429: if (name.length() == 0) {
430: return new Status(IStatus.ERROR, JavaUI.ID_PLUGIN,
431: FormatterMessages.ModifyDialog_EmptyName_Status);
432: }
433:
434: return StatusInfo.OK_STATUS;
435: }
436:
437: private boolean hasChanges() {
438: if (!fProfileNameField.getText().trim().equals(
439: fProfile.getName()))
440: return true;
441:
442: Iterator iter = fProfile.getSettings().entrySet().iterator();
443: for (; iter.hasNext();) {
444: Map.Entry curr = (Map.Entry) iter.next();
445: if (!fWorkingValues.get(curr.getKey()).equals(
446: curr.getValue())) {
447: return true;
448: }
449: }
450: return false;
451: }
452:
453: }
|