debuggercdi/com.nokia.cdt.debug.launch/src/com/nokia/cdt/internal/debug/launch/newwizard/AbstractLaunchWizardSection.java
branchRCL_2_4
changeset 911 81a2e70a37d7
child 953 68b6a294ab01
equal deleted inserted replaced
909:24ba32fc0320 911:81a2e70a37d7
       
     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 package com.nokia.cdt.internal.debug.launch.newwizard;
       
    19 
       
    20 import java.text.MessageFormat;
       
    21 
       
    22 import org.eclipse.core.runtime.IStatus;
       
    23 import org.eclipse.core.runtime.Status;
       
    24 import org.eclipse.jface.layout.GridDataFactory;
       
    25 import org.eclipse.jface.layout.GridLayoutFactory;
       
    26 import org.eclipse.jface.resource.JFaceResources;
       
    27 import org.eclipse.jface.window.Window;
       
    28 import org.eclipse.swt.SWT;
       
    29 import org.eclipse.swt.events.ControlAdapter;
       
    30 import org.eclipse.swt.events.ControlEvent;
       
    31 import org.eclipse.swt.events.DisposeEvent;
       
    32 import org.eclipse.swt.events.DisposeListener;
       
    33 import org.eclipse.swt.events.SelectionAdapter;
       
    34 import org.eclipse.swt.events.SelectionEvent;
       
    35 import org.eclipse.swt.graphics.GC;
       
    36 import org.eclipse.swt.graphics.Point;
       
    37 import org.eclipse.swt.widgets.Button;
       
    38 import org.eclipse.swt.widgets.Composite;
       
    39 import org.eclipse.swt.widgets.Control;
       
    40 import org.eclipse.swt.widgets.Label;
       
    41 import org.eclipse.swt.widgets.Shell;
       
    42 
       
    43 import com.nokia.cdt.internal.debug.launch.LaunchPlugin;
       
    44 
       
    45 /**
       
    46  * 
       
    47  */
       
    48 public abstract class AbstractLaunchWizardSection implements IWizardSection {
       
    49 
       
    50 	private static final String CHANGE_LABEL = "Change...";
       
    51 	protected final LaunchOptionsData data;
       
    52 	private String sectionName;
       
    53 
       
    54 	protected IStatus status;
       
    55 	protected Label descriptionLabel;
       
    56 	protected Button changeButton;
       
    57 	protected Composite control;
       
    58 	private ISectionChangeListener changeListener;
       
    59 
       
    60 
       
    61 	public AbstractLaunchWizardSection(LaunchOptionsData data, String sectionName) {
       
    62 		this.data = data;
       
    63 		this.sectionName = sectionName;
       
    64 		status = Status.OK_STATUS;
       
    65 	}
       
    66 	
       
    67 	abstract protected void dispose();
       
    68 
       
    69 	public IStatus getStatus() {
       
    70 		return status;
       
    71 	}
       
    72 
       
    73 	/** Initialize the data for this section (before UI shown). */
       
    74 	public abstract void initializeSettings();
       
    75 	
       
    76 	/** Validate the settings and update status. */
       
    77 	abstract protected void validate();
       
    78 	
       
    79 	/** Update the UI when data changes. Called after validate(). */
       
    80 	protected abstract void updateUI();
       
    81 
       
    82 	/** Create the UI for this section. */
       
    83 	public abstract void createControl(Composite parent);
       
    84 
       
    85 	/** Create the dialog for the Change... button. */
       
    86 	protected abstract AbstractLaunchSettingsDialog createChangeSettingsDialog(Shell shell, LaunchOptionsData dialogData);
       
    87 	/** Refresh the section after the Change... dialog has been closed. */
       
    88 	protected abstract void refresh();
       
    89 
       
    90 	public Control getControl() {
       
    91 		return control; 
       
    92 	}
       
    93 
       
    94 	public String getSectionName() {
       
    95 		return sectionName;
       
    96 	}
       
    97 
       
    98 	/* (non-Javadoc)
       
    99 	 * @see com.nokia.cdt.internal.debug.launch.wizard2.IWizardSection#setChangeListener(com.nokia.cdt.internal.debug.launch.wizard2.IWizardSection.ISectionChangeListener)
       
   100 	 */
       
   101 	public void setChangeListener(ISectionChangeListener listener) {
       
   102 		this.changeListener = listener;
       
   103 	}
       
   104 	
       
   105 	protected void createSection(Composite parent, int acceleratorIndex) {
       
   106 		Composite composite = new Composite(parent, SWT.NONE);
       
   107 		
       
   108 		GC gc = new GC(parent);
       
   109 		int INDENT = gc.getAdvanceWidth('m') * 4;
       
   110 		gc.dispose();
       
   111 		
       
   112 		GridLayoutFactory.fillDefaults().numColumns(2).margins(6, 0).applyTo(composite);
       
   113 		
       
   114 		Label titleLabel = new Label(composite, SWT.NONE);
       
   115 		titleLabel.setText(sectionName);
       
   116 		titleLabel.setFont(JFaceResources.getBannerFont());
       
   117 		GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(titleLabel);
       
   118 		
       
   119 		// spacing
       
   120 		Label spacer = new Label(composite, SWT.NONE);
       
   121 		GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(spacer);
       
   122 		
       
   123 		descriptionLabel = new Label(composite, SWT.WRAP);
       
   124 		GridDataFactory.fillDefaults().grab(true, true).indent(INDENT, 0).applyTo(descriptionLabel);
       
   125 		
       
   126 		String label = MessageFormat.format("{0}&{1}", //$NON-NLS-1$
       
   127 				CHANGE_LABEL.substring(0, acceleratorIndex), CHANGE_LABEL.substring(acceleratorIndex));
       
   128 		changeButton = new Button(composite, SWT.PUSH);
       
   129 		changeButton.setText(label);
       
   130 		Point minSize = changeButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
       
   131 		GridDataFactory.defaultsFor(changeButton).grab(false, false).hint(minSize.x + INDENT, SWT.DEFAULT).applyTo(changeButton);
       
   132 		changeButton.addSelectionListener(new SelectionAdapter() {
       
   133 			@Override
       
   134 			public void widgetSelected(SelectionEvent e) {
       
   135 				doChange();
       
   136 				if (changeListener != null)
       
   137 					changeListener.changed();
       
   138 			}
       
   139 		});
       
   140 		
       
   141 		composite.addControlListener(new ControlAdapter() {
       
   142 			@Override
       
   143 			public void controlResized(ControlEvent e) {
       
   144 				descriptionLabel.pack();
       
   145 			}
       
   146 		});
       
   147 		
       
   148 		this.control = composite;
       
   149 
       
   150 		control.addDisposeListener(new DisposeListener() {
       
   151 			
       
   152 			public void widgetDisposed(DisposeEvent e) {
       
   153 				dispose();
       
   154 			}
       
   155 		});
       
   156 		
       
   157 		validate();
       
   158 		updateUI();
       
   159 	}
       
   160 	
       
   161 	/* (non-Javadoc)
       
   162 	 * @see com.nokia.cdt.internal.debug.launch.wizard2.AbstractLaunchWizardSection#doChange()
       
   163 	 */
       
   164 	protected void doChange() {
       
   165 		LaunchOptionsData dialogData = data.copy();
       
   166 		AbstractLaunchSettingsDialog dialog = createChangeSettingsDialog(getControl().getShell(), dialogData);
       
   167 		if (dialog.open() == Window.OK) {
       
   168 			data.apply(dialogData);
       
   169 			refresh();
       
   170 		}
       
   171 	}
       
   172 
       
   173 	protected static IStatus error(String msg, Object... args) {
       
   174 		return new Status(IStatus.ERROR, LaunchPlugin.PLUGIN_ID,
       
   175 				MessageFormat.format(msg, args));
       
   176 	}
       
   177 
       
   178 	protected IStatus warning(String msg, Object... args) {
       
   179 		return new Status(IStatus.WARNING, LaunchPlugin.PLUGIN_ID,
       
   180 				MessageFormat.format(msg, args));
       
   181 	}
       
   182 }