sftemplateswizard/com.nokia.s60tools.symbianfoundationtemplates/src/com/nokia/s60tools/symbianfoundationtemplates/ui/wizards/s60/sourcewizards/SourcePage.java
changeset 0 61163b28edca
equal deleted inserted replaced
-1:000000000000 0:61163b28edca
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 package com.nokia.s60tools.symbianfoundationtemplates.ui.wizards.s60.sourcewizards;
       
    18 
       
    19 import java.io.File;
       
    20 import java.util.AbstractList;
       
    21 import java.util.ArrayList;
       
    22 import java.util.HashMap;
       
    23 import java.util.Map;
       
    24 
       
    25 import org.eclipse.core.resources.IFile;
       
    26 import org.eclipse.core.resources.ResourcesPlugin;
       
    27 import org.eclipse.core.runtime.IStatus;
       
    28 import org.eclipse.core.runtime.Path;
       
    29 import org.eclipse.swt.SWT;
       
    30 import org.eclipse.swt.graphics.FontMetrics;
       
    31 import org.eclipse.swt.graphics.GC;
       
    32 import org.eclipse.swt.layout.GridData;
       
    33 import org.eclipse.swt.layout.GridLayout;
       
    34 import org.eclipse.swt.widgets.Button;
       
    35 import org.eclipse.swt.widgets.Combo;
       
    36 import org.eclipse.swt.widgets.Composite;
       
    37 import org.eclipse.swt.widgets.Label;
       
    38 import org.eclipse.swt.widgets.Text;
       
    39 import org.eclipse.ui.PlatformUI;
       
    40 
       
    41 import com.nokia.s60tools.symbianfoundationtemplates.SymbianFoundationTemplates;
       
    42 import com.nokia.s60tools.symbianfoundationtemplates.engine.OriginalTemplate;
       
    43 import com.nokia.s60tools.symbianfoundationtemplates.engine.s60.S60TransformKeys;
       
    44 import com.nokia.s60tools.symbianfoundationtemplates.resources.HelpContextIDs;
       
    45 import com.nokia.s60tools.symbianfoundationtemplates.resources.LastUsedData;
       
    46 import com.nokia.s60tools.symbianfoundationtemplates.resources.Messages;
       
    47 import com.nokia.s60tools.symbianfoundationtemplates.resources.PreferenceConstants;
       
    48 import com.nokia.s60tools.symbianfoundationtemplates.ui.wizards.s60.FileSelectionControl;
       
    49 import com.nokia.s60tools.symbianfoundationtemplates.ui.wizards.s60.FolderSelectionControl;
       
    50 import com.nokia.s60tools.symbianfoundationtemplates.ui.wizards.s60.S60TemplatePageType;
       
    51 import com.nokia.s60tools.symbianfoundationtemplates.ui.wizards.s60.S60TemplateWizardPage;
       
    52 import com.nokia.s60tools.symbianfoundationtemplates.util.Util;
       
    53 
       
    54 /**
       
    55  * Source page.
       
    56  *
       
    57  */
       
    58 public class SourcePage extends S60TemplateWizardPage {
       
    59 	private FolderSelectionControl folderSelectionControl;
       
    60 	
       
    61 	private FileSelectionControl fileSelectionControl;
       
    62 	
       
    63 	private Text subSystemText;
       
    64 	private Text moduleText;
       
    65 	private Text descriptionText;
       
    66 	private Text copyrightText;
       
    67 	private Combo licenseText;
       
    68 	private Combo companyNameText;
       
    69 	private Composite topLevel;
       
    70 	private Button cleanBtn;
       
    71 	private Combo copyrightString;
       
    72 	
       
    73 	public SourcePage(S60TemplatePageType pageType) {
       
    74 		super(pageType);
       
    75 		setTitle(pageType.getTitle());
       
    76 	}
       
    77 	
       
    78 	public void createControl(Composite parent) {
       
    79 		topLevel = new Composite(parent, SWT.NONE);
       
    80 		// Layout manager
       
    81 		topLevel.setLayout(new GridLayout(1, false));
       
    82 		topLevel.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
       
    83 		
       
    84 		Composite selection = new Composite(topLevel, SWT.NONE);
       
    85 		selection.setLayout(new GridLayout(3, false));
       
    86 		selection.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
       
    87 		
       
    88 		// Folder selection control
       
    89 		folderSelectionControl = new FolderSelectionControl(selection, this, Messages.getString("WizardPageSourceFolderLabel")); //$NON-NLS-1$
       
    90 		
       
    91 		// File selection control
       
    92 		fileSelectionControl = new FileSelectionControl(selection, this, Messages.getString("WizardPageSourceFileLabel")); //$NON-NLS-1$
       
    93 		
       
    94 		// Separator
       
    95 		Label separator = new Label(topLevel, SWT.SEPARATOR | SWT.HORIZONTAL);
       
    96 		separator.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
       
    97 		
       
    98 		// Info control
       
    99 		Composite info = new Composite(topLevel, SWT.NONE);
       
   100 		info.setLayout(new GridLayout(2, false));
       
   101 		info.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
       
   102 	
       
   103 		new Label(info, SWT.NONE).setText(Messages.getString("WizardPageSubSystemLabel")); //$NON-NLS-1$
       
   104 		subSystemText = new Text(info, SWT.BORDER);
       
   105 		subSystemText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
       
   106 		
       
   107 		new Label(info, SWT.NONE).setText(Messages.getString("WizardPageModuleLabel")); //$NON-NLS-1$
       
   108 		moduleText = new Text(info, SWT.BORDER);
       
   109 		moduleText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
       
   110 		
       
   111 		new Label(info, SWT.NONE).setText(Messages.getString("WizardPageDescriptionLabel")); //$NON-NLS-1$
       
   112 		descriptionText = new Text(info, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI | SWT.WRAP);
       
   113 			
       
   114 		GC gc = new GC(descriptionText);
       
   115 		FontMetrics fm = gc.getFontMetrics();
       
   116 		int cols = 20;
       
   117 		int rows = 6;
       
   118 		int width = cols * fm.getAverageCharWidth();
       
   119 		int height = rows * fm.getHeight();
       
   120 			
       
   121 		GridData gridData = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL);
       
   122 		gridData.widthHint = width;
       
   123 		gridData.heightHint = height;
       
   124 			
       
   125 		descriptionText.setLayoutData(gridData);
       
   126 		
       
   127 		new Label(info, SWT.NONE).setText(Messages.getString("WizardPageCopyrightLabel")); //$NON-NLS-1$
       
   128 		copyrightText = new Text(info, SWT.BORDER);
       
   129 		copyrightText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
       
   130 		
       
   131 		new Label(info, SWT.NONE).setText(Messages.getString("WizardPageLicenseLabel")); //$NON-NLS-1$
       
   132 		licenseText = new Combo(info, SWT.BORDER|SWT.READ_ONLY);
       
   133 		licenseText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
       
   134 		
       
   135 		new Label(info, SWT.NONE).setText(Messages.getString("WizardPageCompanyLabel")); //$NON-NLS-1$
       
   136 		companyNameText = new Combo(info, SWT.BORDER);
       
   137 		companyNameText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
       
   138 		
       
   139 		new Label(info, SWT.NONE).setText(Messages.getString("WizardPageCopyrightStringLabel"));
       
   140 		copyrightString = new Combo(info, SWT.BORDER);// | SWT.V_SCROLL | SWT.MULTI | SWT.WRAP);
       
   141 		copyrightString.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
       
   142 		
       
   143 		GridData data=new GridData(GridData.FILL_HORIZONTAL);
       
   144 		data.heightHint=30;
       
   145 		cleanBtn=new Button(topLevel,SWT.CHECK);
       
   146 		cleanBtn.setText(Messages.getString("WizardPageIncludeInstructionsLabel"));
       
   147 		cleanBtn.setSelection(true);
       
   148 		cleanBtn.setLayoutData(data);
       
   149 		
       
   150 		setControl(topLevel);
       
   151 		setPageComplete(true);
       
   152 		
       
   153 		copyrightText.setText(new Integer(Util.getCopyrightYear()).toString());
       
   154 		licenseText.setItems(Util.getDefaultLicence());
       
   155 		licenseText.select(0);
       
   156 
       
   157 		LastUsedData previousData = new LastUsedData();
       
   158 		String[] values = previousData.getPreviousValues(LastUsedData.ValueTypes.NAME);
       
   159 		if(values!=null)
       
   160 		{
       
   161 			companyNameText.setItems(values);
       
   162 			companyNameText.select(0);
       
   163 		}
       
   164 		else
       
   165 		{
       
   166 			companyNameText.setText(Util.getDefaultCompanyName());
       
   167 			previousData.saveValues(LastUsedData.ValueTypes.NAME, companyNameText.getText());
       
   168 		}
       
   169 		
       
   170 		values = previousData.getPreviousValues(LastUsedData.ValueTypes.COPYRIGHT);
       
   171 		if(values !=null)
       
   172 		{
       
   173 			copyrightString.setItems(values);
       
   174 			copyrightString.select(0);
       
   175 		}
       
   176 		else
       
   177 		{
       
   178 			copyrightString.setText(Util.getDefaultCompanyCopyright());
       
   179 			previousData.saveValues(LastUsedData.ValueTypes.COPYRIGHT, copyrightString.getText());
       
   180 		}
       
   181 		
       
   182 		if(!preferenceStore.getBoolean(PreferenceConstants.OPENED_FROM_VIEW))
       
   183 			setPreferencesFromStore();
       
   184 		performHelp();
       
   185 		//updateStatus();
       
   186 		topLevel.setFocus();
       
   187 	}
       
   188 	
       
   189 	public void performHelp() {
       
   190 		PlatformUI.getWorkbench().getHelpSystem().displayHelp(
       
   191 			HelpContextIDs.SF_TEMPLATES_SOURCE_WIZARD_PAGE);
       
   192 		PlatformUI.getWorkbench().getHelpSystem().setHelp(topLevel,
       
   193 				HelpContextIDs.SF_TEMPLATES_OVERVIEW);
       
   194 	}
       
   195 	
       
   196 	@Override
       
   197 	public AbstractList<OriginalTemplate> getTemplates() {
       
   198 		AbstractList<OriginalTemplate> templates = new ArrayList<OriginalTemplate>();
       
   199 		
       
   200 		String[] templateFiles = pageType.getTemplateFiles().split(";");
       
   201 		String fileName = templateFiles[0];
       
   202 		//To return the empty template file name
       
   203 		if(!cleanBtn.getSelection())
       
   204 			fileName = templateFiles[1];
       
   205 		templates.add(new OriginalTemplate(SymbianFoundationTemplates.getDefault().getTemplatesPath() + File.separator + fileName, getSelectedFile(), getTransformRules()));
       
   206 		
       
   207 		return templates;
       
   208 	}
       
   209 	
       
   210 	public AbstractList<IStatus> getStatuses() {
       
   211 		AbstractList<IStatus> statuses = new ArrayList<IStatus>();
       
   212 		
       
   213 		statuses.add(Util.getFolderNameStatus(folderSelectionControl.getSelectedFolder(), Messages.getString("WizardPageSourceFolderMsg"))); //$NON-NLS-1$
       
   214 		statuses.add(Util.getFileNameStatus(fileSelectionControl.getSelectedFile(),
       
   215 				pageType.getTemplateFiles(), folderSelectionControl.getSelectedFolder()));
       
   216 		
       
   217 		return statuses;
       
   218 	}
       
   219 	
       
   220 	protected void updateFields() {
       
   221 		moduleText.setText(Util.getModuleName(folderSelectionControl.getSelectedFolder()));
       
   222 		subSystemText.setText(Util.getSubSystemName(folderSelectionControl.getSelectedFolder()));
       
   223 	}
       
   224 	
       
   225 	protected void savePreferencesToStore() {
       
   226 		if(preferenceStore.getBoolean(PreferenceConstants.OPENED_FROM_VIEW)==false)
       
   227 		{
       
   228 		preferenceStore.setValue(PreferenceConstants.SF_SOURCE_PAGE_DIRECTORY, folderSelectionControl.getSelectedFolder());
       
   229 		preferenceStore.setValue(PreferenceConstants.SF_SOURCE_PAGE_MODULE, moduleText.getText());
       
   230 		preferenceStore.setValue(PreferenceConstants.SF_SOURCE_PAGE_SUBSYSTEM, subSystemText.getText());
       
   231 		}
       
   232 		preferenceStore.setValue(PreferenceConstants.SF_LICENSE_INDEX, licenseText.getSelectionIndex());
       
   233 		LastUsedData saveData = new LastUsedData();
       
   234 		saveData.saveValues(LastUsedData.ValueTypes.NAME, companyNameText.getText());
       
   235 		saveData.saveValues(LastUsedData.ValueTypes.COPYRIGHT, copyrightString.getText());
       
   236 	}
       
   237 
       
   238 	private Map<String, String> getTransformRules() {
       
   239 		Map<String, String> transformRules = new HashMap<String, String>();
       
   240 		
       
   241 		transformRules.put(S60TransformKeys.getString("key_filename"), fileSelectionControl.getSelectedFileWithoutExtension()); //$NON-NLS-1$
       
   242 		transformRules.put(S60TransformKeys.getString("key_module"), moduleText.getText()); //$NON-NLS-1$
       
   243 		transformRules.put(S60TransformKeys.getString("key_subsystem"), subSystemText.getText()); //$NON-NLS-1$
       
   244 		transformRules.put(S60TransformKeys.getString("key_description"), descriptionText.getText()); //$NON-NLS-1$
       
   245 		transformRules.put(S60TransformKeys.getString("key_copyright"), copyrightText.getText()); //$NON-NLS-1$
       
   246 		transformRules.put(S60TransformKeys.getString("key_classname"), fileSelectionControl.getSelectedFileWithoutExtension()); //$NON-NLS-1$
       
   247 		transformRules.put(S60TransformKeys.getString("key_license"), licenseText.getText()); //$NON-NLS-1$		
       
   248 		transformRules.put(S60TransformKeys.getString("key_companyname"), companyNameText.getText()); //$NON-NLS-1$
       
   249 		transformRules.put(S60TransformKeys.getString("key_companycopyright"), copyrightString.getText());
       
   250 		
       
   251 		if(licenseText.getText().equalsIgnoreCase("Symbian Foundation License v1.0"))
       
   252 			transformRules.put(S60TransformKeys.getString("key_licenseurl"), "http://www.symbianfoundation.org/legal/sfl-v10.html");
       
   253 		else if(licenseText.getText().equalsIgnoreCase("Eclipse Public License v1.0"))
       
   254 			transformRules.put(S60TransformKeys.getString("key_licenseurl"), "http://www.eclipse.org/legal/epl-v10.html");
       
   255 		
       
   256 		return transformRules;
       
   257 	}	
       
   258 	
       
   259 	private IFile getSelectedFile() {
       
   260 		return ResourcesPlugin.getWorkspace().getRoot().getFile(
       
   261 				new Path(folderSelectionControl.getSelectedFolder() 
       
   262 						+ File.separator 
       
   263 						+ fileSelectionControl.getSelectedFile()));
       
   264 	}
       
   265 	
       
   266 	private void setPreferencesFromStore() {
       
   267 		if(preferenceStore.getBoolean(PreferenceConstants.OPENED_FROM_VIEW)==false && preferenceStore.getString(PreferenceConstants.SF_SOURCE_PAGE_DIRECTORY)!="")
       
   268 		{
       
   269 		folderSelectionControl.setSelectedFolder(preferenceStore.getString(PreferenceConstants.SF_SOURCE_PAGE_DIRECTORY));
       
   270 		moduleText.setText(preferenceStore.getString(PreferenceConstants.SF_SOURCE_PAGE_MODULE));
       
   271 		subSystemText.setText(preferenceStore.getString(PreferenceConstants.SF_SOURCE_PAGE_SUBSYSTEM));
       
   272 		}
       
   273 		licenseText.select(preferenceStore.getInt(PreferenceConstants.SF_LICENSE_INDEX));
       
   274 	}
       
   275 }