IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

SWT/JFace Java Discussion :

ComboBoxCellEditor dans un tableViewer


Sujet :

SWT/JFace Java

  1. #1
    Inactif  
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    2 189
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2006
    Messages : 2 189
    Points : 2 336
    Points
    2 336
    Par défaut ComboBoxCellEditor dans un tableViewer
    Hello,

    Je cherche a placé un ComboBoxCellEditor dans un table viewer mais je n'y arrive pas, un simple CellEditor est afficher au resultat.

    Voici mon code de création du combo :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
     
            MessageCategory [] messageCategories = MessageCategory.values();
            String[] convertedCategories = new String[messageCategories.length];
            for (int i = 0; i < messageCategories.length;i++) {
                MessageCategory messageCategory = messageCategories[i];
                convertedCategories[i] = messageCategory.getName();
            }
            ComboBoxCellEditor categoryEditor = new ComboBoxCellEditor(table, convertedCategories, SWT.READ_ONLY);
            tableViewer.setCellEditors(new CellEditor[]{keyEditor, categoryEditor, null, valueEditor});
    et voici le resultat obtenu (voir capture d écran)

  2. #2
    Inactif  
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    2 189
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2006
    Messages : 2 189
    Points : 2 336
    Points
    2 336
    Par défaut
    Finalement j ai obté pour une autre solution voici mon code

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    /**
             * Creates the TableViewer which wraps the Table.
             * 
             * @param table The Table to wrap
             * @return TableViewer The newly created TableViewer
             */
    	private TableViewer createTableViewer(Table table) {
    	    TableViewer tableViewer = new TableViewer(table);
    	    tableViewer.setUseHashlookup(true);
    	    tableViewer.setColumnProperties(MessageTableConstants.COLUMN_NAMES);
     
    		tableViewer.setLabelProvider(new MessageTableLabelProvider());
    		tableViewer.setContentProvider(new MessageRepositoryContentProvider());	    
     
    	    CellEditor keyEditor = new TextCellEditor(table);
    	    CellEditor valueEditor = new TextCellEditor(table);
    	    //CellEditor categoryEditor = new TextCellEditor(table);
    	    ComboBoxViewerCellEditor categoryEditor = new ComboBoxViewerCellEditor(table);
    		categoryEditor.setContenProvider(new IStructuredContentProvider() {
     
    			/**
                             * Listen the input change
                             * @param viewer
                             *                      The viewer
                             * @param oldInput
                             *                      The old input
                             * @param newInput
                             *                      The new input
                             */
    			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
     
    			}
     
    			/**
                             * On dispose
                             */
    			public void dispose() {
     
    			}
    			/**
                             * Get the array of elements.
                             * 
                             * @param inputElement
                             *                      The input element
                             */
    			public Object[] getElements(Object inputElement) {
    				Object [] array = (Object []) inputElement;
    				return array;
    			}
     
    		});
    		categoryEditor.setLabelProvider(new LabelProvider() {
    			/**
                             * Gets the text of the selected value.
                             * 
                             * @param element
                             *                      The selected element
                             */	
    			public String getText(Object element) {
    				MessageCategory messageCategory;
    				if (element instanceof MessageCategory) {
    					messageCategory = (MessageCategory) element;
    					return messageCategory.getName();
    				}
    				return null;
    			}
     
    		});
    		MessageCategory [] messageCategories = MessageCategory.values();
    		categoryEditor.setInput(messageCategories);
    	    tableViewer.setCellEditors(new CellEditor[]{keyEditor,categoryEditor, null, valueEditor});
     
    	    tableViewer.setCellModifier(new MessageTableCellModifier(commandStack));
     
    	    return tableViewer;
    	}
    une classe a ajouté

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
     
    package com.odcgroup.message.ui.table;
     
    import java.text.MessageFormat;
     
    import org.eclipse.core.runtime.Assert;
    import org.eclipse.jface.viewers.CellEditor;
    import org.eclipse.jface.viewers.ComboBoxCellEditor;
    import org.eclipse.jface.viewers.ComboViewer;
    import org.eclipse.jface.viewers.IBaseLabelProvider;
    import org.eclipse.jface.viewers.IContentProvider;
    import org.eclipse.jface.viewers.ISelection;
    import org.eclipse.jface.viewers.IStructuredContentProvider;
    import org.eclipse.jface.viewers.IStructuredSelection;
    import org.eclipse.jface.viewers.StructuredSelection;
    import org.eclipse.jface.viewers.StructuredViewer;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.custom.CCombo;
    import org.eclipse.swt.events.FocusAdapter;
    import org.eclipse.swt.events.FocusEvent;
    import org.eclipse.swt.events.KeyAdapter;
    import org.eclipse.swt.events.KeyEvent;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.events.TraverseEvent;
    import org.eclipse.swt.events.TraverseListener;
    import org.eclipse.swt.graphics.GC;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Control;
     
    /**
     * A cell editor that presents a list of items in a combo box. In contrast to
     * {@link ComboBoxCellEditor} it wraps the underlying {@link CCombo} using a
     * {@link ComboViewer}
     */
    public class ComboBoxViewerCellEditor extends CellEditor {
     
    	/**
             * The custom combo box control.
             */
    	ComboViewer viewer;
     
    	Object selectedValue;
     
    	/**
             * Default ComboBoxCellEditor style
             */
    	private static final int defaultStyle = SWT.NONE;
     
    	/**
             * Creates a new cell editor with a combo viewer and a default style
             *
             * @param parent
             *            the parent control
             */
    	public ComboBoxViewerCellEditor(Composite parent) {
    		this(parent, defaultStyle);
    	}
     
    	/**
             * Creates a new cell editor with a combo viewer and the given style
             *
             * @param parent
             *            the parent control
             * @param style
             *            the style bits
             */
    	public ComboBoxViewerCellEditor(Composite parent, int style) {
    		super(parent, style);
    		setValueValid(true);
    	}
     
    	/*
    	 * (non-Javadoc) Method declared on CellEditor.
    	 */
    	protected Control createControl(Composite parent) {
     
    		CCombo comboBox = new CCombo(parent, getStyle());
    		comboBox.setFont(parent.getFont());
    		viewer = new ComboViewer(comboBox);
     
    		comboBox.addKeyListener(new KeyAdapter() {
    			// hook key pressed - see PR 14201
    			public void keyPressed(KeyEvent e) {
    				keyReleaseOccured(e);
    			}
    		});
     
    		comboBox.addSelectionListener(new SelectionAdapter() {
    			public void widgetDefaultSelected(SelectionEvent event) {
    				applyEditorValueAndDeactivate();
    			}
     
    			public void widgetSelected(SelectionEvent event) {
    				ISelection selection = viewer.getSelection();
    				if (selection.isEmpty()) {
    					selectedValue = null;
    				} else {
    					selectedValue = ((IStructuredSelection) selection)
    							.getFirstElement();
    				}
    			}
    		});
     
    		comboBox.addTraverseListener(new TraverseListener() {
    			public void keyTraversed(TraverseEvent e) {
    				if (e.detail == SWT.TRAVERSE_ESCAPE
    						|| e.detail == SWT.TRAVERSE_RETURN) {
    					e.doit = false;
    				}
    			}
    		});
     
    		comboBox.addFocusListener(new FocusAdapter() {
    			public void focusLost(FocusEvent e) {
    				ComboBoxViewerCellEditor.this.focusLost();
    			}
    		});
    		return comboBox;
    	}
     
    	/**
             * The <code>ComboBoxCellEditor</code> implementation of this
             * <code>CellEditor</code> framework method returns the zero-based index
             * of the current selection.
             *
             * @return the zero-based index of the current selection wrapped as an
             *         <code>Integer</code>
             */
    	protected Object doGetValue() {
    		return selectedValue;
    	}
     
    	/*
    	 * (non-Javadoc) Method declared on CellEditor.
    	 */
    	protected void doSetFocus() {
    		viewer.getControl().setFocus();
    	}
     
    	/**
             * The <code>ComboBoxCellEditor</code> implementation of this
             * <code>CellEditor</code> framework method sets the minimum width of the
             * cell. The minimum width is 10 characters if <code>comboBox</code> is
             * not <code>null</code> or <code>disposed</code> eles it is 60 pixels
             * to make sure the arrow button and some text is visible. The list of
             * CCombo will be wide enough to show its longest item.
             */
    	public LayoutData getLayoutData() {
    		LayoutData layoutData = super.getLayoutData();
    		if ((viewer.getControl() == null) || viewer.getControl().isDisposed()) {
    			layoutData.minimumWidth = 60;
    		} else {
    			// make the comboBox 10 characters wide
    			GC gc = new GC(viewer.getControl());
    			layoutData.minimumWidth = (gc.getFontMetrics()
    					.getAverageCharWidth() * 10) + 10;
    			gc.dispose();
    		}
    		return layoutData;
    	}
     
    	/**
             * Set a new value
             *
             * @param value
             *            the new value
             */
    	protected void doSetValue(Object value) {
    		Assert.isTrue(viewer != null);
    		viewer.setSelection(new StructuredSelection(value));
    	}
     
    	/**
             * @param labelProvider
             *            the label provider used
             * @see StructuredViewer#setLabelProvider(IBaseLabelProvider)
             */
    	public void setLabelProvider(IBaseLabelProvider labelProvider) {
    		viewer.setLabelProvider(labelProvider);
    	}
     
    	/**
             * @param provider
             *            the content provider used
             * @see StructuredViewer#setContentProvider(IContentProvider)
             */
    	public void setContenProvider(IStructuredContentProvider provider) {
    		viewer.setContentProvider(provider);
    	}
     
    	/**
             * @param input
             *            the input used
             * @see StructuredViewer#setInput(Object)
             */
    	public void setInput(Object input) {
    		viewer.setInput(input);
    	}
     
    	/**
             * @return get the viewer
             */
    	public ComboViewer getViewer() {
    		return viewer;
    	}
     
    	/**
             * Applies the currently selected value and deactiavates the cell editor
             */
    	void applyEditorValueAndDeactivate() {
    		// must set the selection before getting value
    		ISelection selection = viewer.getSelection();
    		if (selection.isEmpty()) {
    			selectedValue = null;
    		} else {
    			selectedValue = ((IStructuredSelection) selection)
    					.getFirstElement();
    		}
     
    		Object newValue = doGetValue();
    		markDirty();
    		boolean isValid = isCorrect(newValue);
    		setValueValid(isValid);
     
    		if (!isValid) {
    			MessageFormat.format(getErrorMessage(),
    					new Object[] { selectedValue });
    		}
     
    		fireApplyEditorValue();
    		deactivate();
    	}
     
    	/*
    	 * (non-Javadoc)
    	 *
    	 * @see org.eclipse.jface.viewers.CellEditor#focusLost()
    	 */
    	protected void focusLost() {
    		if (isActivated()) {
    			applyEditorValueAndDeactivate();
    		}
    	}
     
    	/*
    	 * (non-Javadoc)
    	 *
    	 * @see org.eclipse.jface.viewers.CellEditor#keyReleaseOccured(org.eclipse.swt.events.KeyEvent)
    	 */
    	protected void keyReleaseOccured(KeyEvent keyEvent) {
    		if (keyEvent.character == '\u001b') { // Escape character
    			fireCancelEditor();
    		} else if (keyEvent.character == '\t') { // tab key
    			applyEditorValueAndDeactivate();
    		}
    	}
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 7
    Dernier message: 31/03/2011, 11h38
  2. Réponses: 6
    Dernier message: 23/04/2009, 10h07
  3. [iPhone SDK] Image cellule dans un tableview
    Par dim971 dans le forum Apple
    Réponses: 3
    Dernier message: 31/08/2008, 11h33
  4. [JFace] Ajout d un element dans un tableviewer
    Par *alexandre* dans le forum SWT/JFace
    Réponses: 1
    Dernier message: 29/05/2008, 16h14
  5. Remplir les colonnes dans un TableViewer dynamiquement
    Par gargantua dans le forum Eclipse Platform
    Réponses: 13
    Dernier message: 07/11/2007, 21h13

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo