core/com.nokia.carbide.bugreport/src/com/nokia/carbide/internal/bugreport/ui/wizards/BugDescriptionPage.java
changeset 0 fb279309251b
child 674 20c7966a3405
equal deleted inserted replaced
-1:000000000000 0:fb279309251b
       
     1 /*
       
     2 * Copyright (c) 2007 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 the License "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  
       
    18 package com.nokia.carbide.internal.bugreport.ui.wizards;
       
    19 
       
    20 import java.io.File;
       
    21 import java.util.Hashtable;
       
    22 import org.eclipse.swt.SWT;
       
    23 import org.eclipse.swt.events.ModifyEvent;
       
    24 import org.eclipse.swt.events.ModifyListener;
       
    25 import org.eclipse.swt.events.SelectionEvent;
       
    26 import org.eclipse.swt.events.SelectionListener;
       
    27 import org.eclipse.swt.layout.GridData;
       
    28 import org.eclipse.swt.layout.GridLayout;
       
    29 import org.eclipse.swt.widgets.Composite;
       
    30 import org.eclipse.swt.widgets.FileDialog;
       
    31 import org.eclipse.swt.widgets.Label;
       
    32 import org.eclipse.swt.widgets.Combo;
       
    33 import org.eclipse.swt.widgets.Link;
       
    34 import org.eclipse.swt.widgets.Text;
       
    35 import org.eclipse.swt.widgets.Button;
       
    36 import org.eclipse.ui.PlatformUI;
       
    37 import org.eclipse.jface.wizard.WizardPage;
       
    38 import org.eclipse.jface.dialogs.MessageDialog;
       
    39 import com.nokia.carbide.internal.bugreport.export.IProduct;
       
    40 import com.nokia.carbide.internal.bugreport.model.*;
       
    41 import com.nokia.carbide.internal.bugreport.resources.HelpContextIDs;
       
    42 import com.nokia.carbide.internal.bugreport.resources.Messages;
       
    43 
       
    44 
       
    45 /**
       
    46  * Details Description wizard page. This page is used entering issue type, summary, 
       
    47  * description and attachment file.
       
    48  *
       
    49  */
       
    50 public class BugDescriptionPage extends WizardPage implements SelectionListener,
       
    51 																ModifyListener {
       
    52 	Label typeLabel;
       
    53 	Combo typeCombo;
       
    54 	Label summaryLabel;
       
    55 	Text summaryText = null;
       
    56 	Label descriptionLabel;
       
    57 	Text descriptionText;
       
    58 	Label attachmentLabel;
       
    59 	Text attachmentText;
       
    60 	Button browseButton;
       
    61 	Link uiServiceLink;
       
    62 	IProduct product = null;
       
    63 	long maxAttachmentSize = 1024*1024; // megabyte
       
    64 	
       
    65 	public BugDescriptionPage(){
       
    66 		super(Messages.getString("BugDescriptionPage.BugReporting")); //$NON-NLS-1$
       
    67 			
       
    68 		// User cannot finish the page before some valid 
       
    69 		// selection is made.
       
    70 		setPageComplete(false);
       
    71 	 }
       
    72 	
       
    73 	/**
       
    74 	 * Sets the product for this page.
       
    75 	 * @param prdct product plug-in
       
    76 	 */
       
    77 	public void setProduct(IProduct prdct) {
       
    78 		if (prdct == null)
       
    79 			return;
       
    80 		
       
    81 		product = prdct;
       
    82 		
       
    83 		setTitle(product.getPageTitleText());
       
    84 		setDescription(product.getPageDescriptionText());
       
    85 		maxAttachmentSize = product.getMaxAttachmentSize();
       
    86 		
       
    87 		// if page is constructed.
       
    88 		if (summaryText != null) {
       
    89 			summaryText.setTextLimit(product.getMaxSummaryLength());
       
    90 			descriptionLabel.setText(product.getDescriptionLabelText());
       
    91 			descriptionText.setTextLimit(product.getMaxDescriptionLength());
       
    92 			String linkText = product.getUiServiceLinkText();
       
    93 			if (linkText == null || linkText == "") { //$NON-NLS-1$
       
    94 				uiServiceLink.setVisible(false);
       
    95 			} else {
       
    96 				uiServiceLink.setVisible(true);
       
    97 				uiServiceLink.setText("<a>"+linkText+"</a>"); //$NON-NLS-1$ //$NON-NLS-2$
       
    98 			}
       
    99 			
       
   100 		}
       
   101 	}
       
   102 
       
   103 	public void createControl(Composite parent) {
       
   104 		Composite composite =  new Composite(parent, SWT.NULL);
       
   105 		
       
   106 	    // create the desired layout for this wizard page
       
   107 		GridLayout gl = new GridLayout();
       
   108 		gl.numColumns = 2;
       
   109 		composite.setLayout(gl);
       
   110 		
       
   111 		GridData twoColumnGridData = new GridData(GridData.FILL_HORIZONTAL);
       
   112 		twoColumnGridData.horizontalSpan = 2;
       
   113 
       
   114 		typeLabel = new Label(composite, SWT.LEFT);
       
   115 		typeLabel.setText(Messages.getString("BugDescriptionPage.Type")); //$NON-NLS-1$
       
   116 		typeLabel.setLayoutData(twoColumnGridData);
       
   117 		
       
   118 		typeCombo = new Combo(composite, SWT.READ_ONLY);
       
   119 		typeCombo.setItems(new String[] {Messages.getString("BugDescriptionPage.Defect"),  //$NON-NLS-1$
       
   120 										 Messages.getString("BugDescriptionPage.Enhancement")}); //$NON-NLS-1$
       
   121 		typeCombo.select(0);
       
   122 		GridData comboGD = new GridData();
       
   123 		comboGD.horizontalSpan = 2;
       
   124 		typeCombo.setLayoutData(comboGD);
       
   125 
       
   126 		summaryLabel = new Label(composite, SWT.LEFT);
       
   127 		summaryLabel.setText(Messages.getString("BugDescriptionPage.ShortSummary")); //$NON-NLS-1$
       
   128 		summaryLabel.setLayoutData(twoColumnGridData);
       
   129 		
       
   130 		summaryText = new Text(composite, SWT.SINGLE | SWT.BORDER);
       
   131 		GridData summaryTextGD = new GridData(GridData.FILL_HORIZONTAL);
       
   132 		summaryTextGD.horizontalSpan = 2;
       
   133 		summaryText.setLayoutData(summaryTextGD);
       
   134 		summaryText.addModifyListener(this);
       
   135 		if (product == null) {
       
   136 			summaryText.setTextLimit(255);
       
   137 		} else {
       
   138 			summaryText.setTextLimit(product.getMaxSummaryLength());
       
   139 		}
       
   140 		
       
   141 		descriptionLabel = new Label(composite, SWT.LEFT | SWT.WRAP);
       
   142 		if (product == null) {
       
   143 			descriptionLabel.setText(Messages.getString("BugDescriptionPage.Description")); //$NON-NLS-1$
       
   144 		} else {
       
   145 			descriptionLabel.setText(product.getDescriptionLabelText());
       
   146 		}
       
   147 		descriptionLabel.setLayoutData(twoColumnGridData);
       
   148 		
       
   149 		descriptionText = new Text(composite, SWT.MULTI | SWT.BORDER);
       
   150 		if (product == null) {
       
   151 			descriptionText.setTextLimit(1000);
       
   152 		} else {
       
   153 			descriptionText.setTextLimit(product.getMaxDescriptionLength());
       
   154 		}
       
   155 		GridData descriptionGD = new GridData(GridData.FILL_BOTH);
       
   156 		descriptionGD.horizontalSpan = 2;
       
   157 		descriptionText.setLayoutData(descriptionGD);
       
   158 		descriptionText.addModifyListener(this);
       
   159 
       
   160 		attachmentLabel = new Label(composite, SWT.LEFT);
       
   161 		attachmentLabel.setText(Messages.getString("BugDescriptionPage.AttachmentFile")); //$NON-NLS-1$
       
   162 		attachmentLabel.setLayoutData(twoColumnGridData);
       
   163 		
       
   164 		attachmentText = new Text(composite, SWT.SINGLE | SWT.BORDER);
       
   165 		attachmentText.setTextLimit(1000);
       
   166 		GridData single = new GridData(GridData.FILL_HORIZONTAL);
       
   167 		attachmentText.setLayoutData(single);
       
   168 		attachmentText.addModifyListener(this);
       
   169 		
       
   170 		browseButton = new Button(composite, SWT.PUSH);
       
   171 		browseButton.setText(Messages.getString("BugDescriptionPage.Browse")); //$NON-NLS-1$
       
   172 		browseButton.addSelectionListener(this);
       
   173 		
       
   174 		uiServiceLink = new Link(composite, SWT.NONE);
       
   175 		uiServiceLink.addSelectionListener(this);
       
   176 		if (product == null) {
       
   177 			uiServiceLink.setVisible(false);
       
   178 		} else {
       
   179 			String linkText = product.getUiServiceLinkText();
       
   180 			if (linkText == null || linkText == "") { //$NON-NLS-1$
       
   181 				uiServiceLink.setVisible(false);
       
   182 			} else {
       
   183 				uiServiceLink.setText("<a>"+linkText+"</a>"); //$NON-NLS-1$ //$NON-NLS-2$
       
   184 			}
       
   185 		}
       
   186 		
       
   187 		setControl(composite);
       
   188 		
       
   189 		PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, HelpContextIDs.BUG_DESCRIPTION_PAGE);
       
   190 	}
       
   191 
       
   192 	public void widgetDefaultSelected(SelectionEvent arg0) {
       
   193 	}
       
   194 
       
   195 	public void widgetSelected(SelectionEvent event) {
       
   196 		if (event.widget == browseButton) {
       
   197 			FileDialog dialog = new FileDialog(this.getShell(), SWT.OPEN);
       
   198 			dialog.setText(Messages.getString("BugDescriptionPage.SelectAttachmentFile")); //$NON-NLS-1$
       
   199 			String result = dialog.open();
       
   200 			attachmentText.setText(result);
       
   201 		} else if (event.widget == uiServiceLink) {
       
   202 			product.showUiService(this.getShell());
       
   203 		}
       
   204 	}
       
   205 
       
   206 	public void modifyText(ModifyEvent arg0) {
       
   207 		try {
       
   208 			getWizard().getContainer().updateButtons();
       
   209 		} catch (Exception e) {
       
   210 			e.printStackTrace();
       
   211 		}
       
   212 	}
       
   213 	
       
   214 	@Override
       
   215 	public boolean canFlipToNextPage() {
       
   216 		
       
   217 		if (attachmentFileIsValidOrEmpty() &&
       
   218 			summaryText.getText().length() > 0 && 
       
   219 			descriptionText.getText().length() > 0)
       
   220 			return true;
       
   221 		
       
   222 		return false;
       
   223 	}
       
   224 	
       
   225 	/**
       
   226 	 * Checks that the selected attachment file is a valid file (or 
       
   227 	 * no file is even selected) and that it's too big in size. 
       
   228 	 * @return true if attachment file is valid and not too big, otherwise false.
       
   229 	 */
       
   230 	boolean attachmentFileIsValidOrEmpty() {
       
   231 		String attachmentFilePath = attachmentText.getText();
       
   232 		
       
   233 		// no attachment -> ok
       
   234 		if (attachmentFilePath.length() == 0) {
       
   235 			this.setErrorMessage(null);
       
   236 			return true;
       
   237 		// check that attachment file is ok
       
   238 		} else {
       
   239 			File file = new File(attachmentFilePath);
       
   240 			if (file.isFile() && file.exists() && file.length() <= maxAttachmentSize) { // file is ok
       
   241 				this.setErrorMessage(null);
       
   242 				return true;
       
   243 			} else if (!file.isFile() || !file.exists()) { // file does not exist or is a directory
       
   244 				this.setErrorMessage(Messages.getString("BugDescriptionPage.AttachmentFileIsInvalid"));	 //$NON-NLS-1$
       
   245 				return false;
       
   246 			} else { // file is too big
       
   247 				if (maxAttachmentSize > 1024*1024) { // format error for megabyte size
       
   248 					this.setErrorMessage(Messages.getString("BugDescriptionPage.AttachmentTooBig1")+maxAttachmentSize/(1024*1024)+Messages.getString("BugDescriptionPage.MegaByte")); //$NON-NLS-1$ //$NON-NLS-2$
       
   249 				} else if (maxAttachmentSize > 1024) { // format error for kilobyte size
       
   250 					this.setErrorMessage(Messages.getString("BugDescriptionPage.AttachmentTooBig2")+maxAttachmentSize/1024+Messages.getString("BugDescriptionPage.KiloByte")); //$NON-NLS-1$ //$NON-NLS-2$
       
   251 				} else { // format error for byte size
       
   252 					this.setErrorMessage(Messages.getString("BugDescriptionPage.AttachmentTooBig3")+maxAttachmentSize+Messages.getString("BugDescriptionPage.Byte")); //$NON-NLS-1$ //$NON-NLS-2$
       
   253 				}
       
   254 				return false;
       
   255 			}
       
   256 		}
       
   257 	}
       
   258 	
       
   259 	/**
       
   260 	 * Returns the fields and their values (entered by user) which are to 
       
   261 	 * be sent to the server.
       
   262 	 * @return fields and values which need to be sent to server
       
   263 	 */
       
   264 	public Hashtable<String, String> getFields() {
       
   265 		Hashtable<String, String> fields = new Hashtable<String, String>();
       
   266 		fields.put(FieldsHandler.FIELD_SUMMARY, summaryText.getText());
       
   267 		fields.put(FieldsHandler.FIELD_DESCRIPTION, descriptionText.getText());
       
   268 		fields.put(FieldsHandler.FIELD_TYPE, typeCombo.getText());
       
   269 		if (attachmentText.getText().trim() != "") //$NON-NLS-1$
       
   270 			fields.put(FieldsHandler.FIELD_ATTACHMENT, attachmentText.getText().trim());
       
   271 		return fields;
       
   272 	}
       
   273 	
       
   274 	/**
       
   275 	 * The product plug-in might need to ask user some settings before we can move to 
       
   276 	 * next page. This method asks the product if UI service is needed; if UI is needed,
       
   277 	 * method queries the user if (s)he wants to provide the necessary information, and 
       
   278 	 * opens the product's UI if user wants to do so.
       
   279 	 * @return true if we can move to next page, false if not.
       
   280 	 */
       
   281 	public boolean canChangePage() {
       
   282 		if (!product.uiServiceNeeded())
       
   283 			return true;
       
   284 		
       
   285 		if (MessageDialog.openQuestion(this.getShell(), Messages.getString("BugDescriptionPage.BugReporting"), product.getUiServiceText())) //$NON-NLS-1$
       
   286 			product.showUiService(this.getShell());
       
   287 		
       
   288 		return false;
       
   289 	}	
       
   290 }