Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0fe90ee76cb723a328e62a8e8d70d25fe32aec16 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*******************************************************************************
 * Copyright (c) 2004, 2016 QNX Software Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.ui.actions; 

import java.util.Arrays;
import java.util.List;

import org.eclipse.cdt.debug.core.model.IRegisterDescriptor;
import org.eclipse.cdt.debug.internal.ui.CDebugImages;
import org.eclipse.cdt.debug.internal.ui.ICDebugHelpContextIds;
import org.eclipse.cdt.debug.internal.ui.dialogfields.CheckedListDialogField;
import org.eclipse.cdt.debug.internal.ui.dialogfields.DialogField;
import org.eclipse.cdt.debug.internal.ui.dialogfields.IDialogFieldListener;
import org.eclipse.cdt.debug.internal.ui.dialogfields.IListAdapter;
import org.eclipse.cdt.debug.internal.ui.dialogfields.LayoutUtil;
import org.eclipse.cdt.debug.internal.ui.dialogfields.Separator;
import org.eclipse.cdt.debug.internal.ui.dialogfields.StringDialogField;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.layout.PixelConverter;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

import com.ibm.icu.text.MessageFormat;
 
/**
 * This dialog is used to add/edit user-defined register groups.
 */
public class RegisterGroupDialog extends TitleAreaDialog {

	public class RegisterLabelProvider extends LabelProvider {

		@Override
		public Image getImage( Object element ) {
			if ( element instanceof IRegisterDescriptor ) {
				return CDebugImages.get( CDebugImages.IMG_OBJS_REGISTER );
			}
			return super.getImage( element );
		}

		@Override
		public String getText( Object element ) {
			if ( element instanceof IRegisterDescriptor ) {
				IRegisterDescriptor rd = (IRegisterDescriptor)element;
				return MessageFormat.format( "{0} - {1}", new Object[] { rd.getName(), rd.getGroupName() } ); //$NON-NLS-1$
			}
			return super.getText( element );
		}
	}

	private StringDialogField fNameField;
	private CheckedListDialogField fListField;
	private String fName;
	private IRegisterDescriptor[] fDescriptors;

	public RegisterGroupDialog( Shell parentShell, IRegisterDescriptor[] allRegisters ) {
		this( parentShell, ActionMessages.getString( "RegisterGroupDialog.0" ), allRegisters, new IRegisterDescriptor[0] ); //$NON-NLS-1$
	}

	public RegisterGroupDialog( Shell parentShell, String groupName, IRegisterDescriptor[] allRegisters, IRegisterDescriptor[] groupRegisters ) {
		super( parentShell );
		fName = groupName;
		fDescriptors = groupRegisters;
		String[] buttonLabels = new String[] { ActionMessages.getString( "RegisterGroupDialog.1" ), ActionMessages.getString( "RegisterGroupDialog.2" ) }; //$NON-NLS-1$ //$NON-NLS-2$
 		fNameField = new StringDialogField();
		fNameField.setLabelText( ActionMessages.getString( "RegisterGroupDialog.3" ) ); //$NON-NLS-1$
		fNameField.setTextWithoutUpdate( groupName );
		fNameField.setDialogFieldListener( new IDialogFieldListener() {
			@Override
			public void dialogFieldChanged( DialogField field ) {
				update();
			}
		} );
		fListField = new CheckedListDialogField( new IListAdapter() {
			
			@Override
			public void customButtonPressed( DialogField field, int index ) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void selectionChanged( DialogField field ) {
				// TODO Auto-generated method stub
				
			}
		}, buttonLabels, new RegisterLabelProvider() );
		fListField.setLabelText( ActionMessages.getString( "RegisterGroupDialog.4" ) ); //$NON-NLS-1$
		fListField.setCheckAllButtonIndex( 0 );
		fListField.setUncheckAllButtonIndex( 1 );
		fListField.setElements( Arrays.asList( allRegisters ) );
		fListField.setCheckedElements( Arrays.asList( groupRegisters ) );
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createDialogArea( Composite parent ) {
		getShell().setText( ActionMessages.getString( "RegisterGroupDialog.5" ) ); //$NON-NLS-1$
		setTitle( ActionMessages.getString( "RegisterGroupDialog.6" ) ); //$NON-NLS-1$
	//	setTitleImage( CDebugImages.get( CDebugImages.IMG_WIZBAN_REGISTER_GROUP ) );
		PlatformUI.getWorkbench().getHelpSystem().setHelp( getShell(), ICDebugHelpContextIds.REGISTER_GROUP );
        Composite composite = new Composite( parent, SWT.NONE );
		GridLayout layout = new GridLayout();
		layout.numColumns = Math.max( fNameField.getNumberOfControls(), fListField.getNumberOfControls() );
		layout.marginHeight = convertVerticalDLUsToPixels( IDialogConstants.VERTICAL_MARGIN );
		layout.marginWidth = convertHorizontalDLUsToPixels( IDialogConstants.HORIZONTAL_MARGIN );
		composite.setLayout( layout );
		composite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
		Dialog.applyDialogFont( composite );
		PixelConverter converter = new PixelConverter( composite );
		new Separator().doFillIntoGrid( composite, layout.numColumns, converter.convertHeightInCharsToPixels( 1 ) );
		fNameField.doFillIntoGrid( composite, layout.numColumns );
		fNameField.getTextControl( null ).selectAll();
		new Separator().doFillIntoGrid( composite, layout.numColumns, converter.convertHeightInCharsToPixels( 1 ) );
		fListField.doFillIntoGrid( composite, layout.numColumns + 1 );
		LayoutUtil.setHorizontalSpan( fListField.getLabelControl( null ), layout.numColumns );
		LayoutUtil.setHeigthHint( fListField.getListControl( null ), convertWidthInCharsToPixels( 30 ) );
		LayoutUtil.setHorizontalGrabbing( fListField.getListControl( null ) );
		setMessage( null );
		return composite;
	}

	protected void update() {
		setErrorMessage( null );
		String name = fNameField.getText().trim();
		if ( name.length() == 0 ) {
			setErrorMessage( ActionMessages.getString( "RegisterGroupDialog.7" ) ); //$NON-NLS-1$
		}
		getButton( IDialogConstants.OK_ID ).setEnabled( name.length() > 0 );
	}

	@Override
	protected void okPressed() {
		super.okPressed();
		fName = fNameField.getText().trim();
		List<IRegisterDescriptor> elements = fListField.getCheckedElements();
		fDescriptors = elements.toArray( new IRegisterDescriptor[elements.size()] );
	}

	public String getName() {
		return fName;
	}

	public IRegisterDescriptor[] getDescriptors() {
		return fDescriptors;
	}
}

Back to the top