I create a combo-box control in org.eclipse.swt.widgets.Table
The code snippet is below
...
TableEditor editor = new TableEditor (table_LLSimDataFileInfo);
CCombo combo = new CCombo (table_LLSimDataFileInfo, SWT.NONE);
combo.setText("CCombo");
combo.add("item 1");
combo.add("item 2");
editor.grabHorizontal = true;
editor.setEditor(combo, items[i], 0);
...
How can I dynamically change the combo-box list for a selected row in the table (For e.g. item1, item2 etc changed to item4, item5, item7 etc for row 5 only) by triggering of some event. The event in my case is selection in another combo-box whose list does not change
You should set a SelectionListener on your other CCombo, in order to call an update on your second CCombo.
This WavAudioSettingComposite class is a good example.
Something like:
public class ValueChanged extends SelectionAdapter {
public void widgetSelected(SelectionEvent e) {
if(e.getSource()==myFirstCCombo){
// call update on your second CCombo
}
}
}
public void updateSecondCCombo(int[] newValues){
int oldbitrate=getFramerate();
mySecondCCombo.removeAll();
for (int i = 0; i < newValues.length; i++) {
mySecondCCombo.add(""+newValues[i]);
}
}
The TableEditor docs show a simple example with a selection listener that identifies the current selected row.
You just need to customize this example and dynamically fill the Combo acording to the selected row.
Related
I have two comboviewer and the idea is that the second combo will display a subset of options depending of the first combo value selected. But after to set the new input in the second combo and refresh the combo width is too small. How can I set it for auto adjusting to options width?
public class ExpresionDialog extends Dialog {
private ComboViewer combo1;
private ComboViewer combo2;
#Override
protected Control createDialogArea(Composite composite) {
Composite parent = (Composite) super.createDialogArea(composite);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
combo1 = new ComboViewer(parent);
combo1.setLabelProvider(new LabelProvider());
combo1.setContentProvider(ArrayContentProvider.getInstance());
combo1.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent e) {
IStructuredSelection sel = (IStructuredSelection) e.getSelection();
AttributeOption option = (AttributeOption) sel.getFirstElement();
combo2.setInput(getValuesCombo2(option));
combo2.refresh(true);
}
});
combo1.setInput(getValuesCombo1());
combo2 = new ComboViewer(parent);
combo2.setLabelProvider(new LabelProvider());
combo2.setContentProvider(ArrayContentProvider.getInstance());
return parent;
}
// Omitted getValuesCombo1 and getValuesCombo2 methods ...
}
You need to call the layout method of the parent Composite to get it to redo the child layouts each time you change the contents.
In my TableViewer I have an OwnerDrawLabelProvider where for a specific column in my table I add a ProgressBar to a TableEditor instance.
My problem is the following:
I'm able to set the selection of the ProgressBar but when trying to update it remains at the same value.
CODE:
#Override
public void update(ViewerCell cell) {
if(columnIndex == 4){
Table table = tableViewer.getTable();
TableItem item;
TableItem[] items;
TableEditor editor;
items = table.getItems();
ProgressBar bar = new ProgressBar(table, SWT.NONE);
bar.setMinimum(0);
bar.setMaximum(100);
bar.setState(SWT.NORMAL);
bar.setSelection(0);
bar.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,true));
if(mediator.isSent()){
List<Item> itemsSendToSubmit = mediator.getItemsSendToSubmit();
if(!itemsSendToSubmit.isEmpty()){
for(Iterator<Item> itemIterator = itemsSendToSubmit.iterator(); itemIterator.hasNext(); )
{
Item itemSubmited = itemIterator.next();
for(TableItem tableItem: items)
{ if(tableItem.getText(0).contains(itemSubmited.getId()))
{
bar.setSelection(PluginUtility.getBuildProgress(itemSubmited.getPlan()));
editor = new TableEditor(table);
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(bar, tableItem, 4);
}
}
}
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
I read about an issue that setSelection method for ProgressBar has some problems. I created my own ProgressBar by extending the base class and I overrided setSelection method with the fix code but still doesn't work.
In a normal main function, this works.
Can I get some suggestions of what can be the problem or how adding this ProgressBar in a TableViewer influences its behavior ?
EDIT: If I create a single instance of progressbar when the label provider is created and then pass it to the tableeditor it will update the progressbar for the last element on which I say editor.setEditor(bar, tableItem, 4);
but I need to display a progressbar for each item and update it for each item !
Perhaps the update happens, but the visuals are not updated, depending on how you call this code.
You have to make sure that you're not mobilizing the ui thread when you're doing this update.
In eclipse 3.x this means using
Display.getDefault().asyncExec(new Runnable() {
public void run() {
// ... do any work that updates the screen ...
}
});
Or using injection to get the UISynchronize in 4.x
Basically put the setSelection code in there.
See this article on background processing in eclipse plugins :
http://www.vogella.com/tutorials/EclipseJobs/article.html
When you update the value of ProgressBar, after that you need to redraw/repaint the TableViewer with all of its properties, then it will show the update progressbar. Hope this will solve your problem.
How to make one item in a combobox unselectable because I need to separate items in a combobox with a sub-topic.
And is it possible to modify the font of that particular item individually?
jComboBox_btech_course.setFont(new java.awt.Font("Tahoma", 0, 14));
jComboBox_btech_course.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select Course" }));
jComboBox_btech_course.setName("");
private class theHandler implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
//BTech courses
if(jComboBox_mtech_dept.getSelectedItem().equals("Civil Engineering"))
{
jComboBox_btech_course.removeAllItems();
jComboBox_btech_course.addItem("Building Construction");
jComboBox_btech_course.addItem("Principle And Practice");
jComboBox_btech_course.addItem("Surveying");
jComboBox_btech_course.addItem("Engineering Geology");
jComboBox_btech_course.addItem("Structural Analysis");
jComboBox_btech_course.addItem("Hydraulic Engineering");
jComboBox_btech_course.addItem("Environmental Engineering");
jComboBox_btech_course.addItem("Structural Design");
jComboBox_btech_course.addItem("Geotechnical Engineering");
/*This item has to be unselectable*/
jComboBox_btech_course.addItem("***Sub-topic***");
jComboBox_btech_course.addItem("Transportation Engineering");
jComboBox_btech_course.addItem("Foundation Engineering");
jComboBox_btech_course.addItem("Estimation & Valuation");
jComboBox_btech_course.addItem("Hydrology & Flood Control");
jComboBox_btech_course.addItem("System Analysis, Project Planning And Construction Management");
jComboBox_btech_course.addItem("Irrigation Engineering");
jComboBox_btech_course.addItem("Computer Application in Civil Engineering");
jComboBox_btech_course.addItem("Planning, Design & Detailing");
}
}
}
Foreword: In the proposed solution I assume that you want to disable items that start with "**". You can change this logic to whatever you want to. In an improved version the MyComboModel class (see below) may even store which items are disabled allowing arbitrary items to be marked disabled.
Solution to your question involves 2 things:
1. Disallow selecting items which you want to be disabled
For this you can use a custom ComboBoxModel, and override its setSelectedItem() method to do nothing if the item to be selected is a disabled one:
class MyComboModel extends DefaultComboBoxModel<String> {
public MyComboModel() {}
public MyComboModel(Vector<String> items) {
super(items);
}
#Override
public void setSelectedItem(Object item) {
if (item.toString().startsWith("**"))
return;
super.setSelectedItem(item);
};
}
And you can set this new model by passing an instance of it to the JComboBox constructor:
JComboBox<String> cb = new JComboBox<>(new MyComboModel());
2. Display disabled items with different font
For this you have to use a custom ListCellRenderer and in getListCellRendererComponent() method you can configure different visual appearance for disabled and enabled items:
Font f1 = cb.getFont();
Font f2 = new Font("Tahoma", 0, 14);
cb.setRenderer(new DefaultListCellRenderer() {
#Override
public Component getListCellRendererComponent(JList<?> list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if (value instanceof JComponent)
return (JComponent) value;
boolean itemEnabled = !value.toString().startsWith("**");
super.getListCellRendererComponent(list, value, index,
isSelected && itemEnabled, cellHasFocus);
// Render item as disabled and with different font:
setEnabled(itemEnabled);
setFont(itemEnabled ? f1 : f2);
return this;
}
});
In order to get what you need, you need to implement ComboBoxEditor.
In this way, you can decide what you want to do in your case, or in any other cases
You could add a custom ItemListener via
addItemListener(ItemListener aListener)
and in that method disable selection or switch selection to item above or so.
I am using nebula gridTableViewer for my application, in its cell some time content is so lengthy, so I want to show as WRAP (in multiline). I did changes for it but it is not reflecting:
I added SWT.WRAP but not works, I tried to wrap the text in LabelProvider too but it also not works so how could i do it?
Should i need to add listener for this.
Adding SWT.WRAP to the gridTableViewer would not cause the Text in the grid cells to wrap.
gridColumn.setWordWrap(true) causes the text entered in the cell to be wrapped after the entered value is applied to the editor(setvalue called). To wrap the text interactively, SWT.WRAP should be added as a style to the TextCellEditor or to Text widget placed in the cells.
gridViewerColumn.getColumn.setWordWrap(true);
gridTableViewer.setEditingSupport(new EditingSupport(gridTableViewer) {
....
#Override
protected CellEditor getCellEditor(Object element) {
TextCellEditor cellEditor = new TextCellEditor(gridTableViewer.getGrid(),
SWT.WRAP)
return cellEditor;
}
Or
....
final GridItem item = grid.getItem(grid.getTopIndex());
for (int i = 0; i < grid.getColumnCount(); i++) {
final Text text = new Text(grid, SWT.WRAP);
Listener textListener = new Listener() {
public void handleEvent(final Event e) {
switch (e.type) {
case SWT.FocusOut:
item.setText(column, text.getText());
....
}
}
}
}
I have used the SWT.WRAP in the TextCellEditor before, and it works.
I use the TableViewer to show informations in a table. The user can select one of the shown options by selecting one line of the table.
I want to create a table in matrix form, in which the user can not only select the line. It should be possible to select every item of the table, like row 2 column 3. For every item selection an action is called to handle this item as it is in the TableViewer.
As far as i now, i can add CellModifier and CellEditors to the line of Columns of the table, but the reference for the action is always the line object and not the selected TableItem.
Does somebody have an example how to create such a matrix inside a Composite?
I can create it by setting a GridLayout and adding the components in a for-loop, but than i get issues, when i want to redraw the Composite with new childrens. The TableViewer does already have this handling, so i dont want to implement it again.
I had the same problem a while ago and the only way I found to solve it was to register a mouse listener on the SWT table widget associated to the table viewer.
MouseListener columnSelectionMouseListener = new ColumnSelectionMouseListener();
getViewer().getTable().addMouseListener(columnSelectionMouseListener);
public class ColumnSelectionMouseListener implements MouseListener {
private TableColumn selectedColumn;
#Override
public void mouseDoubleClick(MouseEvent e) {
// Nothing to do here
}
#Override
public void mouseDown(MouseEvent e) {
table = (Table) e.widget;
TableItem item = table.getItem(new Point(e.x, e.y));
for (int i = 0; i < table.getColumnCount(); i++) {
TableColumn column = table.getColumn(i);
Rectangle bounds = item.getBounds(i);
if (bounds.contains(e.x, e.y)) {
selectedColumn = column;
}
}
}
#Override
public void mouseUp(MouseEvent e) {
// Nothing to do here
}
public TableColumn getSelectedField() {
return selectedColumn;
}
}
Then, for example in the viewer's selection listener, you can ask to the mouse listener which column was selected when the mouse has been pressed and combine that with the selected line coming from the viewer's selection to perform the appropriate action.
Hope this can help.
Manu
Maybe the following JFace snippet will help:
Snippet058CellNavigationIn34
Ingo