david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
nitind | d72d517 | 2010-12-31 08:27:32 +0000 | [diff] [blame] | 2 | * Copyright (c) 2001, 2010 IBM Corporation and others. |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 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 |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 7 | * |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | * Jens Lukowski/Innoopract - initial renaming/restructuring |
| 11 | * |
| 12 | *******************************************************************************/ |
nitind | d6e591d | 2005-03-14 22:21:57 +0000 | [diff] [blame] | 13 | package org.eclipse.wst.dtd.ui.internal.preferences; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 14 | |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 15 | import java.util.ArrayList; |
| 16 | import java.util.Arrays; |
| 17 | import java.util.Iterator; |
| 18 | import java.util.List; |
| 19 | |
| 20 | import org.eclipse.core.runtime.Platform; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 21 | import org.eclipse.core.runtime.Preferences; |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 22 | import org.eclipse.core.runtime.content.IContentType; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 23 | import org.eclipse.jface.preference.IPreferenceStore; |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 24 | import org.eclipse.osgi.util.NLS; |
nitind | 65cbd8e | 2006-12-12 06:09:32 +0000 | [diff] [blame] | 25 | import org.eclipse.swt.widgets.Combo; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 26 | import org.eclipse.swt.widgets.Composite; |
| 27 | import org.eclipse.swt.widgets.Control; |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 28 | import org.eclipse.swt.widgets.Group; |
nitind | ec93f94 | 2005-11-16 00:06:14 +0000 | [diff] [blame] | 29 | import org.eclipse.ui.PlatformUI; |
nitind | d6e591d | 2005-03-14 22:21:57 +0000 | [diff] [blame] | 30 | import org.eclipse.wst.dtd.core.internal.DTDCorePlugin; |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 31 | import org.eclipse.wst.dtd.core.internal.preferences.DTDCorePreferenceNames; |
| 32 | import org.eclipse.wst.dtd.core.internal.provisional.contenttype.ContentTypeIdForDTD; |
| 33 | import org.eclipse.wst.dtd.ui.internal.DTDUIMessages; |
david_williams | 425ffe7 | 2004-12-07 21:46:39 +0000 | [diff] [blame] | 34 | import org.eclipse.wst.dtd.ui.internal.DTDUIPlugin; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 35 | import org.eclipse.wst.dtd.ui.internal.editor.IHelpContextIds; |
david_williams | f3680f0 | 2005-04-13 22:43:54 +0000 | [diff] [blame] | 36 | import org.eclipse.wst.sse.ui.internal.preferences.ui.AbstractPreferencePage; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | public class DTDFilesPreferencePage extends AbstractPreferencePage { |
nitind | 65cbd8e | 2006-12-12 06:09:32 +0000 | [diff] [blame] | 40 | private Combo fDefaultSuffix = null; |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 41 | private List fValidExtensions = null; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 42 | |
nitind | de12ce6 | 2008-01-31 02:24:31 +0000 | [diff] [blame] | 43 | protected Control createContents(final Composite parent) { |
| 44 | Composite composite = super.createComposite(parent, 1); |
nitind | ec93f94 | 2005-11-16 00:06:14 +0000 | [diff] [blame] | 45 | PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IHelpContextIds.DTD_PREFWEBX_FILES_HELPID); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 46 | |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 47 | Group creatingGroup = createGroup(composite, 2); |
| 48 | creatingGroup.setText(DTDUIMessages.Creating_files); |
| 49 | |
| 50 | // Default extension for New file Wizard |
| 51 | createLabel(creatingGroup, DTDUIMessages.DTDFilesPreferencePage_ExtensionLabel); |
nitind | 65cbd8e | 2006-12-12 06:09:32 +0000 | [diff] [blame] | 52 | fDefaultSuffix = createDropDownBox(creatingGroup); |
| 53 | String[] validExtensions = (String[]) getValidExtensions().toArray(new String[0]); |
| 54 | Arrays.sort(validExtensions); |
| 55 | fDefaultSuffix.setItems(validExtensions); |
| 56 | fDefaultSuffix.addSelectionListener(this); |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 57 | |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 58 | setSize(composite); |
| 59 | loadPreferences(); |
| 60 | |
| 61 | return composite; |
| 62 | } |
| 63 | |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 64 | public void dispose() { |
| 65 | fDefaultSuffix.removeModifyListener(this); |
| 66 | super.dispose(); |
| 67 | } |
| 68 | |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 69 | protected IPreferenceStore doGetPreferenceStore() { |
david_williams | 425ffe7 | 2004-12-07 21:46:39 +0000 | [diff] [blame] | 70 | return DTDUIPlugin.getDefault().getPreferenceStore(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | protected void doSavePreferenceStore() { |
nitind | d6e591d | 2005-03-14 22:21:57 +0000 | [diff] [blame] | 74 | DTDCorePlugin.getInstance().savePluginPreferences(); // model |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 75 | } |
| 76 | |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 77 | /** |
| 78 | * Get content type associated with this new file wizard |
| 79 | * |
| 80 | * @return IContentType |
| 81 | */ |
| 82 | private IContentType getContentType() { |
| 83 | return Platform.getContentTypeManager().getContentType(ContentTypeIdForDTD.ContentTypeID_DTD); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get list of valid extensions |
| 88 | * |
| 89 | * @return List |
| 90 | */ |
| 91 | private List getValidExtensions() { |
| 92 | if (fValidExtensions == null) { |
| 93 | IContentType type = getContentType(); |
| 94 | fValidExtensions = new ArrayList(Arrays.asList(type.getFileSpecs(IContentType.FILE_EXTENSION_SPEC))); |
| 95 | } |
| 96 | return fValidExtensions; |
| 97 | } |
| 98 | |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 99 | /* |
| 100 | * (non-Javadoc) |
| 101 | * |
| 102 | * @see org.eclipse.wst.sse.ui.preferences.ui.AbstractPreferencePage#getModelPreferences() |
| 103 | */ |
| 104 | protected Preferences getModelPreferences() { |
nitind | d6e591d | 2005-03-14 22:21:57 +0000 | [diff] [blame] | 105 | return DTDCorePlugin.getInstance().getPluginPreferences(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 106 | } |
| 107 | |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 108 | protected void initializeValues() { |
| 109 | String suffix = getModelPreferences().getString(DTDCorePreferenceNames.DEFAULT_EXTENSION); |
| 110 | fDefaultSuffix.setText(suffix); |
| 111 | } |
| 112 | |
| 113 | protected void performDefaults() { |
| 114 | String suffix = getModelPreferences().getDefaultString(DTDCorePreferenceNames.DEFAULT_EXTENSION); |
| 115 | fDefaultSuffix.setText(suffix); |
| 116 | |
| 117 | super.performDefaults(); |
| 118 | } |
| 119 | |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 120 | public boolean performOk() { |
| 121 | boolean result = super.performOk(); |
| 122 | |
| 123 | doSavePreferenceStore(); |
| 124 | |
| 125 | return result; |
| 126 | } |
amywu | fe0f361 | 2006-01-25 03:48:57 +0000 | [diff] [blame] | 127 | |
| 128 | protected void storeValues() { |
| 129 | String suffix = fDefaultSuffix.getText(); |
| 130 | getModelPreferences().setValue(DTDCorePreferenceNames.DEFAULT_EXTENSION, suffix); |
| 131 | } |
| 132 | |
| 133 | protected void validateValues() { |
| 134 | boolean isValid = false; |
| 135 | Iterator i = getValidExtensions().iterator(); |
| 136 | while (i.hasNext() && !isValid) { |
| 137 | String extension = (String) i.next(); |
| 138 | isValid = extension.equalsIgnoreCase(fDefaultSuffix.getText()); |
| 139 | } |
| 140 | |
| 141 | if (!isValid) { |
| 142 | setErrorMessage(NLS.bind(DTDUIMessages.DTDFilesPreferencePage_ExtensionError, getValidExtensions().toString())); |
| 143 | setValid(false); |
| 144 | } |
| 145 | else { |
| 146 | setErrorMessage(null); |
| 147 | setValid(true); |
| 148 | } |
| 149 | } |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 150 | } |