debuggercdi/com.nokia.cdt.debug.launch/src/com/nokia/cdt/internal/debug/launch/newwizard/AbstractUnifiedLaunchOptionsPage.java
changeset 2160 3a82092877ea
child 2163 f0a9f2d04d4a
equal deleted inserted replaced
2159:db61d072b92b 2160:3a82092877ea
       
     1 /*
       
     2 * Copyright (c) 2010 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 
       
    19 package com.nokia.cdt.internal.debug.launch.newwizard;
       
    20 
       
    21 import java.text.MessageFormat;
       
    22 import java.util.ArrayList;
       
    23 
       
    24 import org.eclipse.core.runtime.IStatus;
       
    25 import org.eclipse.core.runtime.Status;
       
    26 import org.eclipse.jface.layout.GridDataFactory;
       
    27 import org.eclipse.jface.layout.GridLayoutFactory;
       
    28 import org.eclipse.jface.wizard.WizardPage;
       
    29 import org.eclipse.swt.SWT;
       
    30 import org.eclipse.swt.widgets.Composite;
       
    31 import org.eclipse.swt.widgets.Control;
       
    32 
       
    33 import com.nokia.cdt.internal.debug.launch.LaunchPlugin;
       
    34 import com.nokia.cdt.internal.debug.launch.newwizard.IWizardSection.ISectionChangeListener;
       
    35 import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils;
       
    36 
       
    37 public abstract class AbstractUnifiedLaunchOptionsPage extends WizardPage implements ISectionChangeListener {
       
    38 
       
    39 	protected LaunchWizardData data;
       
    40 	protected ArrayList<IWizardSection> sections;
       
    41 
       
    42 	protected AbstractUnifiedLaunchOptionsPage(String pageName, LaunchWizardData data) {
       
    43 		super(pageName);
       
    44 		this.data = data;
       
    45 		this.sections = new ArrayList<IWizardSection>();
       
    46 	}
       
    47 
       
    48 	protected void addSection(IWizardSection section) {
       
    49 		sections.add(section);
       
    50 	}
       
    51 	
       
    52 	public void createControl(Composite parent) {
       
    53 		Composite composite = new Composite(parent, SWT.NONE);
       
    54 		
       
    55 		GridLayoutFactory.fillDefaults().applyTo(composite);
       
    56 		
       
    57 		setPageComplete(false);
       
    58 		
       
    59 		for (IWizardSection section : sections) {
       
    60 			section.createControl(composite);
       
    61 			section.setChangeListener(this);
       
    62 			GridDataFactory.fillDefaults().grab(true, true).applyTo(section.getControl());
       
    63 		}
       
    64 		
       
    65 		WorkbenchUtils.setHelpContextId(composite, LaunchWizardHelpIds.WIZARD_DIALOG_LAUNCH_OPTIONS_PAGE);
       
    66 		
       
    67 		setControl(composite);
       
    68 	}
       
    69 
       
    70 	public void initializeSettings() {
       
    71 		for (IWizardSection section : sections) {
       
    72 			section.initializeSettings();
       
    73 		}
       
    74 		validatePage();
       
    75 	}
       
    76 
       
    77 	public void validatePage() {
       
    78 		setMessage(null, INFORMATION);
       
    79 		setErrorMessage(null);
       
    80 		setPageComplete(true);
       
    81 		
       
    82 		IStatus pageStatus = null;
       
    83 		
       
    84 		// validate the subsections
       
    85 		StringBuilder builder = new StringBuilder();
       
    86 		int severity = IStatus.OK;
       
    87 		for (IWizardSection section : sections) {
       
    88 			IStatus status = section.getStatus();
       
    89 			if (status.isOK())
       
    90 				continue;
       
    91 			if (builder.length() > 0)
       
    92 				builder.append("\n"); //$NON-NLS-1$
       
    93 			
       
    94 			builder.append(MessageFormat.format("{0}: {1}", //$NON-NLS-1$
       
    95 					section.getSectionName(), 
       
    96 					status.getMessage()));
       
    97 			severity = Math.max(severity, status.getSeverity());
       
    98 		}
       
    99 		if (severity == IStatus.OK) {
       
   100 			IStatus status = getStatus();
       
   101 			if (!status.isOK()) {
       
   102 				builder.append(status.getMessage());
       
   103 				severity = status.getSeverity();
       
   104 			}
       
   105 		}
       
   106 		if (severity != 0 || builder.length() > 0) {
       
   107 			// error from one or more sections
       
   108 			pageStatus = new Status(severity, LaunchPlugin.PLUGIN_ID, builder.toString());
       
   109 		} else {
       
   110 			// sections are good; validate the page as a whole
       
   111 			pageStatus = data.validate();
       
   112 		}
       
   113 		
       
   114 		setTitle(Messages.getString("UnifiedLaunchOptionsPage.TitleText")); //$NON-NLS-1$
       
   115 		
       
   116 		if (pageStatus != null && !pageStatus.isOK()) {
       
   117 			setMessage(pageStatus.getMessage(), severityToMsgType(pageStatus.getSeverity()));
       
   118 			setPageComplete(pageStatus.getSeverity() < IStatus.ERROR);
       
   119 		}
       
   120 	}
       
   121 
       
   122 	private int severityToMsgType(int severity) {
       
   123 		switch (severity) {
       
   124 		case IStatus.OK:
       
   125 		case IStatus.INFO:
       
   126 			return INFORMATION;
       
   127 		case IStatus.WARNING:
       
   128 			return WARNING;
       
   129 		case IStatus.ERROR:
       
   130 		default:
       
   131 			return ERROR;
       
   132 		}
       
   133 	}
       
   134 
       
   135 	protected abstract IStatus getStatus();
       
   136 
       
   137 	public void changed() {
       
   138 		validatePage();
       
   139 		Control control = getControl();
       
   140 		if (control != null && !control.isDisposed())
       
   141 			getWizard().getContainer().getShell().pack();
       
   142 	}
       
   143 }