Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » CellModifier in TreeViewer in Dialog class
CellModifier in TreeViewer in Dialog class [message #16479] Wed, 24 June 2009 15:49 Go to next message
Igor Ganapolsky is currently offline Igor Ganapolsky
Messages: 39
Registered: July 2009
Location: New York
Member

I have a TreeViewer in a class that implements a JFace Dialog. When I set
a CellModifier for this viewer, it does not work. The method canModify
doesn't ever get called. When I use same code in a class that DOES NOT
implement a Dialog, everything works fine. What gives???
Re: CellModifier in TreeViewer in Dialog class [message #16497 is a reply to message #16479] Wed, 24 June 2009 16:00 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5008
Registered: July 2009
Senior Member
Hi,

Are you really sure about that?

To make a ICellModifier work you need:

=> setColumnProperties()
=> setCellEditors()
=> setCellModifier()

Are all of them set.

Tom

Igor Ganapolsky schrieb:
> I have a TreeViewer in a class that implements a JFace Dialog. When I
> set a CellModifier for this viewer, it does not work. The method
> canModify doesn't ever get called. When I use same code in a class that
> DOES NOT implement a Dialog, everything works fine. What gives???
>
Re: CellModifier in TreeViewer in Dialog class [message #16515 is a reply to message #16497] Wed, 24 June 2009 16:02 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor Ganapolsky
Messages: 39
Registered: July 2009
Location: New York
Member

Yes, here is the brief snippet of how that code looks:
try{

viewerBills = new TableViewer(tableBills);
viewerBills.setUseHashlookup(true);
viewerBills.setColumnProperties(COLUMNNAMESBILLS);
// Create the cell editors
CellEditor[] editors = new CellEditor[COLUMNNAMESBILLS.length];
for(int i=0;i<editors.length;i++){
TextCellEditor textEditor = new TextCellEditor(tableBills);
// quantity (Text with digits only)
textEditor = new TextCellEditor(tableBills);
((Text) textEditor.getControl()).addVerifyListener(
new VerifyListener() {
public void verifyText(VerifyEvent e) {
// Here, we could use a RegExp such as the following
// if using JRE1.4 such as e.doit = e.text.matches("[\\-0-9]*");
e.doit = "0123456789".indexOf(e.text) >= 0 ;
}
});


editors[i] = textEditor;

}

viewerBills.setCellEditors(editors);
// Set the cell modifier for the viewer
viewerBills.setCellModifier(new CellBillModifier(this));

}
catch(Exception e){
System.out.println(e.getMessage());
}
Re: CellModifier in TreeViewer in Dialog class [message #16532 is a reply to message #16515] Wed, 24 June 2009 16:04 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5008
Registered: July 2009
Senior Member
Hi,

I need a full snippet to reproduce it :-)

Tom

Igor Ganapolsky schrieb:
> Yes, here is the brief snippet of how that code looks:
> try{
>
> viewerBills = new TableViewer(tableBills);
> viewerBills.setUseHashlookup(true);
> viewerBills.setColumnProperties(COLUMNNAMESBILLS);
> // Create the cell editors
> CellEditor[] editors = new CellEditor[COLUMNNAMESBILLS.length];
> for(int i=0;i<editors.length;i++){
> TextCellEditor textEditor = new TextCellEditor(tableBills);
> // quantity (Text with digits only)
> textEditor = new TextCellEditor(tableBills);
> ((Text) textEditor.getControl()).addVerifyListener(
> new VerifyListener() {
> public void verifyText(VerifyEvent e) {
> // Here, we could use a RegExp such as the
> following // if using JRE1.4 such as e.doit
> = e.text.matches("[\\-0-9]*");
> e.doit = "0123456789".indexOf(e.text) >= 0 ;
> }
> });
>
>
> editors[i] = textEditor;
>
> }
>
> viewerBills.setCellEditors(editors);
> // Set the cell modifier for the viewer
> viewerBills.setCellModifier(new CellBillModifier(this));
>
> }
> catch(Exception e){
> System.out.println(e.getMessage());
> }
>
Re: CellModifier in TreeViewer in Dialog class [message #16552 is a reply to message #16532] Wed, 24 June 2009 16:05 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor Ganapolsky
Messages: 39
Registered: July 2009
Location: New York
Member

Here is the entire class:

public class ApproveDialog extends Dialog {

private File file = new File("util/Login_Accounts.txt");
private Text passwordField;
private Text balanceText;
private Label amount;
private Label currencyBills;
private Label currencyCoins;
private String[] displayApprovalError = new String[3];
public boolean approvalSuccessful;
public String approvalAmount;
private String feature = null;
GridData gridData;
TableViewer viewerBills;
TableViewer viewerCoins;
Table tableBills;
Table tableCoins;
TableViewerColumn column;

Color white = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
final static String[] COLUMNNAMESBILLS = { "£100 (Scottish)", "£50",
"£20", "£10", "£5"};
final static String[] COLUMNNAMESCOINS = { "£2", "£1", "50p", "20p",
"10p", "5p", "2p", "1p" };
// Create a CoinList and assign it to an instance variable
private static CurrencyBillList CURRENCYBILLSLIST = new
CurrencyBillList();
// Create a CoinList and assign it to an instance variable
private static CurrencyCoinList CURRENCYCOINLIST = new
CurrencyCoinList();

// Below we create a formatter with a pattern of #0.00. The # symbol
// means any number but leading zero will not be displayed. The 0
// symbol will display the remaining digit and will display as zero if no
digit is available.
NumberFormat formatter = new DecimalFormat("0.00");
private static String BALANCETYPE;

//enum for setting approval feature
private enum Approval{
discount,openingBalance,closingBalance,NOVALUE;
public static Approval TOAPPROVAL(String str)
{
try {
return valueOf(str);
}
catch (Exception ex) {
return NOVALUE;
}
}
}

public ApproveDialog(Shell parentShell, String feature){
super(parentShell);
this.feature = feature;
}


protected void configureShell(Shell shell) {
super.configureShell(shell);
switch (Approval.TOAPPROVAL(feature)){
case discount: shell.setText("DISCOUNT APPROVAL");
break;
case openingBalance: shell.setText("SET OPENING BALANCE");
break;
case closingBalance: shell.setText("SET CLOSING BALANCE");
break;
}

}

/*
* Creates and returns the upper contents of this dialog's area.
* @see
org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclips e.swt.widgets.Composite)
*/
protected Control createDialogArea(Composite parent){
Composite comp = (Composite)super.createDialogArea(parent);

GridLayout layout = (GridLayout)comp.getLayout();
layout.numColumns=2;

GridData data = new GridData(SWT.BEGINNING,SWT.TOP,false,false);
//data.horizontalSpan=2;
//if approving an opening balance display the text field and tableviewer
for opening bal
if(feature.equals("openingBalance")){
amount = new Label(comp,SWT.NONE);
amount.setText("Amount: £");
amount.setLayoutData(data);
amount.setVisible(true);
data = new GridData(50,15);
balanceText = new Text(comp,SWT.SINGLE);
balanceText.setLayoutData(data);
balanceText.setVisible(true);
balanceText.setText("");

createTableBills(comp);
createTableViewerBills("opening",comp);

createTableCoins(comp);
createTableViewerCoins("opening",comp);
viewerBills.setContentProvider(new CurrencyBillContentProvider());
viewerBills.setLabelProvider(new CurrencyOpeningBillLabelProvider());
viewerCoins.setContentProvider(new CurrencyCoinContentProvider());
viewerCoins.setLabelProvider(new CurrencyOpeningCoinLabelProvider());
// The input for the table viewer is the instance of ExampleTaskList
CURRENCYCOINLIST = new CurrencyCoinList();
viewerCoins.setInput(CURRENCYCOINLIST);
CURRENCYBILLSLIST = new CurrencyBillList();
viewerBills.setInput(CURRENCYBILLSLIST);
}
//if approving a closing balance display the text field and tableviewer
closing bal
else if(feature.equals("closingBalance")){
amount = new Label(comp,SWT.NONE);
amount.setText("Amount: £");
amount.setLayoutData(data);
amount.setVisible(true);
data = new GridData(50,15);
balanceText = new Text(comp,SWT.SINGLE);
balanceText.setLayoutData(data);
balanceText.setVisible(true);
balanceText.setText("");

createTableBills(comp);
createTableViewerBills("closing",comp);

createTableCoins(comp);
createTableViewerCoins("closing",comp);
viewerBills.setContentProvider(new CurrencyBillContentProvider());
viewerBills.setLabelProvider(new CurrencyClosingBillLabelProvider());
viewerCoins.setContentProvider(new CurrencyCoinContentProvider());
viewerCoins.setLabelProvider(new CurrencyClosingCoinLabelProvider());
// The input for the table viewer is the instance of ExampleTaskList
CURRENCYCOINLIST = new CurrencyCoinList();
viewerCoins.setInput(CURRENCYCOINLIST);
CURRENCYBILLSLIST = new CurrencyBillList();
viewerBills.setInput(CURRENCYBILLSLIST);
}
else{ //otherwise make balanceText invisible
balanceText = new Text(comp,SWT.SINGLE);
amount = new Label(comp,SWT.NONE);
//balanceText.setLayoutData(data);
balanceText.setVisible(false);
amount.setVisible(false);
//currencyCoins.setVisible(false);
//currencyBills.setVisible(false);
}

data= new GridData(130,15);
Label passwordLabel = new Label(comp,SWT.NONE);
passwordLabel.setText("Manager Password: ");
passwordLabel.setLayoutData(data);
data= new GridData(130,15);
data.horizontalAlignment = 2;
passwordField = new Text(comp,SWT.SINGLE|SWT.PASSWORD);
passwordField.setLayoutData(data);
return comp;
}

/*
* Adds buttons to this dialog's button bar.
* @see
org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(o rg.eclipse.swt.widgets.Composite)
*/
protected void createButtonsForButtonBar(Composite parent){
super.createButtonsForButtonBar(parent);
//createButton(parent,RESET_ID,"Reset All", false);
}

/*
* Notifies that this dialog's button with the given id has been pressed.
* @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
*/
protected void buttonPressed(int buttonId)
{

if(buttonId == 1){ //cancel was pressed, close the dialog
this.cancelPressed();
}
else
{ //check for valid manager password discount
try {
Scanner scanner = new Scanner(file);
try {
//first use a Scanner to get each line
while ( scanner.hasNextLine() ){
if(processApproval( scanner.nextLine() )){
//ready to apply discount
approvalSuccessful = true;
break;
}
}
if(displayApprovalError[0].equals("show")){
MessageDialog.openError(getParentShell(), "Error",
displayApprovalError[1]+" password incorrect.");
}
if(displayApprovalError[0].equals("show")&&displayApprovalError[2].equals( "format")){
MessageDialog.openError(getParentShell(), "Error",
displayApprovalError[1]+" balance incorrect, " +
"must be in the format xx.xx");
}
}
catch(Exception e){
System.out.println("exception: " + e.getMessage());
}
finally {
//ensure the underlying stream is always closed
scanner.close();
//closes this dialog
if(approvalSuccessful){
super.okPressed();
}
}
} catch (FileNotFoundException e) {
System.err.println("file not found");
// log error message that file was not found
//logger.log("login.txt not found");
}
}
}

/**
* This method checks approvals.txt for valid manager password.
* @param next line of the file
* @return approvalResult <b>true</b> if password was correct, false
otherwise
*/
private boolean processApproval(String aLine){
boolean approvalResult = false;
//use a second Scanner to parse the content of each line
Scanner scanner = new Scanner(aLine);
if ( scanner.hasNext() ){
scanner.next();
String function = scanner.next();
String password = scanner.next();

//if we are doing approval for openingBalance of closingBalance
if(feature.equals("openingBalance")||feature.equals("closingBalance ")){
boolean invalidFormat = false;
//check that correct balance format has been entered
try{

formatter.format(Float.valueOf(balanceText.getText()));
}
catch(IllegalArgumentException e){
System.out.println("invalid format was entered");
invalidFormat = true;
}
if(balanceText.isVisible() && !balanceText.getText().equals("") &&
!invalidFormat){
System.out.println("correct fomat and not empty");
//approval was successful for manager
approvalResult = true;
approvalAmount =
formatter.format(Float.valueOf(balanceText.getText()));
displayApprovalError[0]=""; //don't display an approval error
displayApprovalError[2]="";
//check that manager password is correct
if(function.equals("manager") &&
password.equals(passwordField.getText())){
//approval was successful for manager
approvalResult = true;
displayApprovalError[0]=""; //don't display an approval error
displayApprovalError[1]="";
}
else{
approvalResult = false;
displayApprovalError[0] = "show";
displayApprovalError[1] = "Manager";
}
}
else{
approvalResult = false;
displayApprovalError[0] = "show"; //display an approval error
displayApprovalError[2] = "format";
}
}

//if we are doing approval for discount
else if(feature.equals("discount")){
//check that manager password is correct
if(function.equals("manager") &&
password.equals(passwordField.getText())){
//approval was successful for manager
approvalResult = true;
displayApprovalError[0]=""; //don't display an approval error
displayApprovalError[1]="";
}
else{
approvalResult = false;
displayApprovalError[0] = "show";
displayApprovalError[1] = "Manager";
}
}
}
else { //this line in approval.txt is not relevant
approvalResult = false;
}
scanner.close();

return approvalResult;
}

private void createTableBills(Composite parent) {
int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL |
SWT.FULL_SELECTION | SWT.HIDE_SELECTION;

gridData = new GridData(100, 15);
gridData.verticalIndent=20;
currencyBills = new Label(parent,SWT.None);
currencyBills.setText("Currency Notes/Bills: ");
currencyBills.setLayoutData(gridData);
currencyBills.setVisible(true);
gridData = new GridData(SWT.BEGINNING,SWT.TOP,false,false);
tableBills = new Table(parent,style);
//table.setLayout(gridLayout);
tableBills.setLayoutData(gridData);
tableBills.setBackground(white);
tableBills.setHeaderVisible(true);
tableBills.setLinesVisible(true);

// 1st currency
TableColumn column = new TableColumn(tableBills, SWT.CENTER, 0);
column.setText("£100 (Scottish)");
column.setWidth(120);

// 2nd currency
column = new TableColumn(tableBills, SWT.LEFT, 1);
column.setText("£50");
column.setWidth(50);

// 3rd currency
column = new TableColumn(tableBills, SWT.LEFT, 2);
column.setText("£20");
column.setWidth(50);

// 4th currency
column = new TableColumn(tableBills, SWT.LEFT, 3);
column.setText("£10");
column.setWidth(50);

// 5th currency
column = new TableColumn(tableBills, SWT.LEFT, 4);
column.setText("£5");
column.setWidth(50);

}


private void createTableViewerBills(String balType, Composite parent) {
ApproveDialog.BALANCETYPE = balType;
try{

viewerBills = new TableViewer(tableBills);
viewerBills.setUseHashlookup(true);
viewerBills.setColumnProperties(COLUMNNAMESBILLS);
// Create the cell editors
CellEditor[] editors = new CellEditor[COLUMNNAMESBILLS.length];
for(int i=0;i<editors.length;i++){
TextCellEditor textEditor = new TextCellEditor(tableBills);
// quantity (Text with digits only)
textEditor = new TextCellEditor(tableBills);
((Text) textEditor.getControl()).addVerifyListener(
new VerifyListener() {
public void verifyText(VerifyEvent e) {
// Here, we could use a RegExp such as the following
// if using JRE1.4 such as e.doit = e.text.matches("[\\-0-9]*");
e.doit = "0123456789".indexOf(e.text) >= 0 ;
}
});


editors[i] = textEditor;

}

viewerBills.setCellEditors(editors);
// Set the cell modifier for the viewer
viewerBills.setCellModifier(new CellBillModifier(this));

}
catch(Exception e){
System.out.println(e.getMessage());
}
}

private void createTableCoins(Composite parent) {
int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL |
SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
gridData = new GridData(100, 15);
gridData.verticalIndent=10;
currencyCoins = new Label(parent,SWT.None);
currencyCoins.setText("Currency Coins: ");
currencyCoins.setLayoutData(gridData);
currencyCoins.setVisible(true);
gridData = new GridData(SWT.BEGINNING,SWT.TOP,false,false);
tableCoins = new Table(parent,style);
tableCoins.setLayoutData(gridData);
tableCoins.setBackground(white);
tableCoins.setHeaderVisible(true);
tableCoins.setLinesVisible(true);

// 1st currency
TableColumn column = new TableColumn(tableCoins, SWT.CENTER, 0);
column.setText("£2");
column.setWidth(50);

// 2nd currency
column = new TableColumn(tableCoins, SWT.LEFT, 1);
column.setText("£1");
column.setWidth(50);

// 3rd currency
column = new TableColumn(tableCoins, SWT.LEFT, 2);
column.setText("50p");
column.setWidth(50);

// 4th currency
column = new TableColumn(tableCoins, SWT.LEFT, 3);
column.setText("20p");
column.setWidth(50);

// 5th currency
column = new TableColumn(tableCoins, SWT.LEFT, 4);
column.setText("10p");
column.setWidth(50);

// 6th currency
column = new TableColumn(tableCoins, SWT.LEFT, 5);
column.setText("5p");
column.setWidth(50);

// 7th currency
column = new TableColumn(tableCoins, SWT.LEFT, 6);
column.setText("2p");
column.setWidth(50);

// 8th currency
column = new TableColumn(tableCoins, SWT.LEFT, 7);
column.setText("1p");
column.setWidth(50);

}


private void createTableViewerCoins(String balType, Composite parent) {
try{
viewerCoins = new TableViewer(tableCoins);
viewerCoins.setUseHashlookup(true);
viewerCoins.setColumnProperties(COLUMNNAMESCOINS);
// Create the cell editors
CellEditor[] editors = new CellEditor[COLUMNNAMESCOINS.length];
TextCellEditor textEditor = new TextCellEditor(tableCoins);
// Column 3 : quantity (Text with digits only)
textEditor = new TextCellEditor(tableCoins);
((Text) textEditor.getControl()).addVerifyListener(
new VerifyListener() {
public void verifyText(VerifyEvent e) {
// Here, we could use a RegExp such as the following
// if using JRE1.4 such as e.doit = e.text.matches("[\\-0-9]*");
e.doit = "0123456789".indexOf(e.text) >= 0 ;
}
});

for(int i=0;i<editors.length;i++){
editors[i] = textEditor;

}

viewerCoins.setCellEditors(editors);
// Set the cell modifier for the viewer
viewerCoins.setCellModifier(new CellCoinModifier(viewerCoins));

}
catch(Exception e){
System.out.println(e.getMessage());
}
}


/**
* This inner class implements an ICellModifier
* An ICellModifier is called when the user modifes a cell in the
* tableViewer.
*/
class CellBillModifier implements ICellModifier {
private ApproveDialog dialog;
//ApproveDialog dialog;
/**
* Constructor
* @param OrderTableViewer an instance of a OrderTableViewer
*/
public CellBillModifier(ApproveDialog dialog) {
super();
// this.dialog=dialog;
System.out.println("been here 1");
}

/**
* @see
org.eclipse.jface.viewers.ICellModifier#canModify(java.lang. Object,
java.lang.String)
*/
public boolean canModify(Object element, String property) {
System.out.println("been here 2");
return true;
}


/**
* @see
org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.O bject,
java.lang.String)
*/
public Object getValue(Object element, String property) {
System.out.println("been here 3");
// Find the index of the column
int columnIndex = ApproveDialog.GETCOLUMNNAMESBILLS().indexOf(property);
Object result = null;
CurrencyBill currencyBill = (CurrencyBill) element;
if(ApproveDialog.BALANCETYPE.equals("opening")){ //we are modifying a
cell in the openingBalance tableviewer
switch (columnIndex) {
case 0 : // CURRENCY A COLUMN
System.out.println("been here 4");
result = currencyBill.getOpeningCurrencyA();
break;
case 1 : // CURRENCY B COLUMN
result = currencyBill.getOpeningCurrencyB();
break;
case 2 : // CURRENCY C COLUMN
result = currencyBill.getOpeningCurrencyC();
break;
case 3 : // CURRENCY D COLUMN
result = currencyBill.getOpeningCurrencyD();
break;
case 4 : // CURRENCY E COLUMN
result = currencyBill.getOpeningCurrencyE();
break;
default :
result = "";
}
}
if(ApproveDialog.BALANCETYPE.equals("closing")){ //we are modifying a
cell in the closingBalance tableviewer
switch (columnIndex) {
case 0 : // CURRENCY A COLUMN
System.out.println("been here 5");
result = currencyBill.getClosingCurrencyA();
break;
case 1 : // CURRENCY B COLUMN
result = currencyBill.getClosingCurrencyB();
break;
case 2 : // CURRENCY C COLUMN
result = currencyBill.getClosingCurrencyC();
break;
case 3 : // CURRENCY D COLUMN
result = currencyBill.getClosingCurrencyD();
break;
case 4 : // CURRENCY E COLUMN
result = currencyBill.getClosingCurrencyE();
break;
default :
result = "";
}
}
return result;
}

/**
* @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Obj ect,
java.lang.String, java.lang.Object)
*/
public void modify(Object element, String property, Object value) {
System.out.println("been here 6");
// Find the index of the column
int columnIndex = ApproveDialog.GETCOLUMNNAMESBILLS().indexOf(property);

TableItem tableItem = (TableItem) element;
CurrencyBill currencyBill = (CurrencyBill) tableItem.getData();
String valueString;
if(BALANCETYPE.equals("opening")){
switch (columnIndex) {
case 0 : // CURRENCY A COLUMN
System.out.println("been here 7");
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
currencyBill.setOpeningCurrA(Integer.parseInt(valueString));
break;
case 1 : // CURRENCY B COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
currencyBill.setOpeningCurrB(Integer.parseInt(valueString));
break;
case 2 : // CURRENCY C COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
currencyBill.setOpeningCurrC(Integer.parseInt(valueString));
break;
case 3 : // CURRENCY D COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
currencyBill.setOpeningCurrD(Integer.parseInt(valueString));
break;
case 4 : // CURRENCY E COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
currencyBill.setOpeningCurrE(Integer.parseInt(valueString));
break;
default :
}
}
ApproveDialog.getCurrencyBillList().quantityChanged(currency Bill);

}
}

/**
* This inner class implements an ICellModifier
* An ICellModifier is called when the user modifes a cell in the
* tableViewer.
*/
class CellCoinModifier implements ICellModifier {
private String balanceType;
private Viewer viewerCoins;
/**
* Constructor
* @param OrderTableViewer an instance of a OrderTableViewer
*/
public CellCoinModifier(TableViewer viewerCoins) {
super();
this.balanceType=ApproveDialog.BALANCETYPE;
this.viewerCoins = viewerCoins;
}

/**
* @see
org.eclipse.jface.viewers.ICellModifier#canModify(java.lang. Object,
java.lang.String)
*/
public boolean canModify(Object element, String property) {
return true;
}


/**
* @see
org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.O bject,
java.lang.String)
*/
public Object getValue(Object element, String property) {
// Find the index of the column
int columnIndex = ApproveDialog.GETCOLUMNNAMESCOINS().indexOf(property);
Object result = null;
CurrencyCoin currencyCoin = (CurrencyCoin) element;
switch (columnIndex) {
case 0 : // CURRENCY A COLUMN
if(balanceType.equals("opening"))
result = currencyCoin.getOpeningCurrencyA();
else result = currencyCoin.getClosingCurrencyA();
break;
case 1 : // CURRENCY B COLUMN
if(balanceType.equals("opening"))
result = currencyCoin.getOpeningCurrencyB();
else result = currencyCoin.getClosingCurrencyB();
break;
case 2 : // CURRENCY C COLUMN
if(balanceType.equals("opening"))
result = currencyCoin.getOpeningCurrencyC();
else result = currencyCoin.getClosingCurrencyC();
break;
case 3 : // CURRENCY D COLUMN
if(balanceType.equals("opening"))
result = currencyCoin.getOpeningCurrencyD();
else result = currencyCoin.getClosingCurrencyD();
break;
case 4 : // CURRENCY E COLUMN
if(balanceType.equals("opening"))
result = currencyCoin.getOpeningCurrencyE();
else result = currencyCoin.getClosingCurrencyE();
break;
case 5 : // CURRENCY F COLUMN
if(balanceType.equals("opening"))
result = currencyCoin.getOpeningCurrencyF();
else result = currencyCoin.getClosingCurrencyF();
break;
case 6 : // CURRENCY G COLUMN
if(balanceType.equals("opening"))
result = currencyCoin.getOpeningCurrencyG();
else result = currencyCoin.getClosingCurrencyG();
break;
case 7 : // CURRENCY H COLUMN
if(balanceType.equals("opening"))
result = currencyCoin.getOpeningCurrencyH();
else result = currencyCoin.getClosingCurrencyH();
break;
default :
result = "";
}

return result;
}

/**
* @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Obj ect,
java.lang.String, java.lang.Object)
*/
public void modify(Object element, String property, Object value) {
// Find the index of the column
int columnIndex = ApproveDialog.GETCOLUMNNAMESCOINS().indexOf(property);

TableItem tableItem = (TableItem) element;
CurrencyCoin currencyCoin = (CurrencyCoin) tableItem.getData();
String valueString;
switch (columnIndex) {
case 0 : // CURRENCY A COLUMN

valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
if(balanceType.equals("opening"))
currencyCoin.setOpeningCurrA(Integer.parseInt(valueString));
else currencyCoin.setClosingCurrA(Integer.parseInt(valueString));
break;
case 1 : // CURRENCY B COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
if(balanceType.equals("opening"))
currencyCoin.setOpeningCurrB(Integer.parseInt(valueString));
else currencyCoin.setClosingCurrB(Integer.parseInt(valueString));
break;
case 2 : // CURRENCY C COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
if(balanceType.equals("opening"))
currencyCoin.setOpeningCurrC(Integer.parseInt(valueString));
else currencyCoin.setClosingCurrC(Integer.parseInt(valueString));
break;
case 3 : // CURRENCY D COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
if(balanceType.equals("opening"))
currencyCoin.setOpeningCurrD(Integer.parseInt(valueString));
else currencyCoin.setClosingCurrD(Integer.parseInt(valueString));
break;
case 4 : // CURRENCY E COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
if(balanceType.equals("opening"))
currencyCoin.setOpeningCurrE(Integer.parseInt(valueString));
else currencyCoin.setClosingCurrE(Integer.parseInt(valueString));
break;
case 5 : // CURRENCY F COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
if(balanceType.equals("opening"))
currencyCoin.setOpeningCurrF(Integer.parseInt(valueString));
else currencyCoin.setClosingCurrF(Integer.parseInt(valueString));
break;
case 6 : // CURRENCY G COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
if(balanceType.equals("opening"))
currencyCoin.setOpeningCurrG(Integer.parseInt(valueString));
else currencyCoin.setClosingCurrG(Integer.parseInt(valueString));
break;
case 7 : // CURRENCY H COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
if(balanceType.equals("opening"))
currencyCoin.setOpeningCurrH(Integer.parseInt(valueString));
else currencyCoin.setClosingCurrH(Integer.parseInt(valueString));
break;
default :
}
ApproveDialog.getCurrencyCoinList().quantityChanged(currency Coin);
}
}

/**
* Return the column names in a collection
*
* @return List containing column names
*/
public static java.util.List<String> GETCOLUMNNAMESBILLS() {
return Arrays.asList(COLUMNNAMESBILLS);
}

/**
* Return the column names in a collection
*
* @return List containing column names
*/
public static java.util.List<String> GETCOLUMNNAMESCOINS() {
return Arrays.asList(COLUMNNAMESCOINS);
}


/**
* Return the CurrencyCoinList
*/
public static CurrencyCoinList getCurrencyCoinList() {
return CURRENCYCOINLIST;
}

/**
* Return the CurrencyCoinList
*/
public static CurrencyBillList getCurrencyBillList() {
return CURRENCYBILLSLIST;
}

class CurrencyBillContentProvider implements
IStructuredContentProvider,ICurrencyBillListViewer{

@Override
public Object[] getElements(Object inputElement) {
if(CURRENCYBILLSLIST.getCurrency()!=null) {
return CURRENCYBILLSLIST.getCurrency().toArray();
}

else
return new Object[]{};

}

@Override
public void dispose() {
// TODO Auto-generated method stub

}

@Override
public void inputChanged(Viewer viewer, Object oldInput, Object
newInput) {
if (newInput != null)
((CurrencyBillList) newInput).addChangeListener(this);
if (oldInput != null)
((CurrencyBillList) oldInput).removeChangeListener(this);
}

@Override
public void updateQuantity(CurrencyBill bill) {
viewerBills.update(bill,null);

}
}

class CurrencyCoinContentProvider implements
IStructuredContentProvider,ICurrencyCoinListViewer{

@Override
public Object[] getElements(Object inputElement) {
if(CURRENCYCOINLIST.getCurrency()!=null) {
return CURRENCYCOINLIST.getCurrency().toArray();
}

else
return new Object[]{};

}

@Override
public void dispose() {
// TODO Auto-generated method stub

}

@Override
public void inputChanged(Viewer viewer, Object oldInput, Object
newInput) {
if (newInput != null)
((CurrencyCoinList) newInput).addChangeListener(this);
if (oldInput != null)
((CurrencyCoinList) oldInput).removeChangeListener(this);
}

@Override
public void updateQuantity(CurrencyCoin coin) {
viewerCoins.update(coin,null);

}
}
}
Re: CellModifier in TreeViewer in Dialog class [message #16574 is a reply to message #16552] Wed, 24 June 2009 16:57 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5008
Registered: July 2009
Senior Member
Hi Igor,

I need something I can run locally without modification. This snippet is
missing a lot of stuff! Could you start with a snippet from our
collection [1] and modify it in a way that I can see the problem?

Please keep it as minimal as possible!

Tom

[1]http://wiki.eclipse.org/JFaceSnippets

Igor Ganapolsky schrieb:
> Here is the entire class:
>
> public class ApproveDialog extends Dialog {
>
> private File file = new File("util/Login_Accounts.txt");
> private Text passwordField;
> private Text balanceText;
> private Label amount;
> private Label currencyBills;
> private Label currencyCoins;
> private String[] displayApprovalError = new String[3];
> public boolean approvalSuccessful;
> public String approvalAmount;
> private String feature = null;
> GridData gridData;
> TableViewer viewerBills;
> TableViewer viewerCoins;
> Table tableBills;
> Table tableCoins;
> TableViewerColumn column;
>
> Color white = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
> final static String[] COLUMNNAMESBILLS = { "£100 (Scottish)", "£50",
> "£20", "£10", "£5"};
> final static String[] COLUMNNAMESCOINS = { "£2", "£1", "50p", "20p",
> "10p", "5p", "2p", "1p" };
> // Create a CoinList and assign it to an instance variable
> private static CurrencyBillList CURRENCYBILLSLIST = new
> CurrencyBillList(); // Create a CoinList and assign it to an
> instance variable
> private static CurrencyCoinList CURRENCYCOINLIST = new
> CurrencyCoinList();
> // Below we create a formatter with a pattern of #0.00. The # symbol
> // means any number but leading zero will not be displayed. The 0
> // symbol will display the remaining digit and will display as zero
> if no digit is available.
> NumberFormat formatter = new DecimalFormat("0.00");
> private static String BALANCETYPE;
>
> //enum for setting approval feature
> private enum Approval{
> discount,openingBalance,closingBalance,NOVALUE;
> public static Approval TOAPPROVAL(String str)
> {
> try {
> return valueOf(str);
> } catch (Exception ex) {
> return NOVALUE;
> }
> } }
>
> public ApproveDialog(Shell parentShell, String feature){
> super(parentShell);
> this.feature = feature;
> }
>
>
> protected void configureShell(Shell shell) {
> super.configureShell(shell);
> switch (Approval.TOAPPROVAL(feature)){
> case discount: shell.setText("DISCOUNT APPROVAL");
> break;
> case openingBalance: shell.setText("SET OPENING BALANCE");
> break;
> case closingBalance: shell.setText("SET CLOSING BALANCE");
> break;
> }
>
> }
>
> /*
> * Creates and returns the upper contents of this dialog's area.
> * @see
> org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclips e.swt.widgets.Composite)
>
> */
> protected Control createDialogArea(Composite parent){
> Composite comp = (Composite)super.createDialogArea(parent);
>
> GridLayout layout = (GridLayout)comp.getLayout();
> layout.numColumns=2;
>
> GridData data = new GridData(SWT.BEGINNING,SWT.TOP,false,false);
> //data.horizontalSpan=2;
> //if approving an opening balance display the text field and
> tableviewer for opening bal
> if(feature.equals("openingBalance")){
> amount = new Label(comp,SWT.NONE);
> amount.setText("Amount: £");
> amount.setLayoutData(data);
> amount.setVisible(true);
> data = new GridData(50,15);
> balanceText = new Text(comp,SWT.SINGLE);
> balanceText.setLayoutData(data);
> balanceText.setVisible(true);
> balanceText.setText("");
>
> createTableBills(comp);
> createTableViewerBills("opening",comp);
>
> createTableCoins(comp);
> createTableViewerCoins("opening",comp);
> viewerBills.setContentProvider(new
> CurrencyBillContentProvider());
> viewerBills.setLabelProvider(new
> CurrencyOpeningBillLabelProvider());
> viewerCoins.setContentProvider(new
> CurrencyCoinContentProvider());
> viewerCoins.setLabelProvider(new
> CurrencyOpeningCoinLabelProvider());
> // The input for the table viewer is the instance of
> ExampleTaskList
> CURRENCYCOINLIST = new CurrencyCoinList();
> viewerCoins.setInput(CURRENCYCOINLIST);
> CURRENCYBILLSLIST = new CurrencyBillList();
> viewerBills.setInput(CURRENCYBILLSLIST);
> }
> //if approving a closing balance display the text field and
> tableviewer closing bal
> else if(feature.equals("closingBalance")){
> amount = new Label(comp,SWT.NONE);
> amount.setText("Amount: £");
> amount.setLayoutData(data);
> amount.setVisible(true);
> data = new GridData(50,15);
> balanceText = new Text(comp,SWT.SINGLE);
> balanceText.setLayoutData(data);
> balanceText.setVisible(true);
> balanceText.setText("");
>
> createTableBills(comp);
> createTableViewerBills("closing",comp);
>
> createTableCoins(comp);
> createTableViewerCoins("closing",comp);
> viewerBills.setContentProvider(new
> CurrencyBillContentProvider());
> viewerBills.setLabelProvider(new
> CurrencyClosingBillLabelProvider());
> viewerCoins.setContentProvider(new
> CurrencyCoinContentProvider());
> viewerCoins.setLabelProvider(new
> CurrencyClosingCoinLabelProvider());
> // The input for the table viewer is the instance of
> ExampleTaskList
> CURRENCYCOINLIST = new CurrencyCoinList();
> viewerCoins.setInput(CURRENCYCOINLIST);
> CURRENCYBILLSLIST = new CurrencyBillList();
> viewerBills.setInput(CURRENCYBILLSLIST);
> }
> else{ //otherwise make balanceText invisible
> balanceText = new Text(comp,SWT.SINGLE);
> amount = new Label(comp,SWT.NONE);
> //balanceText.setLayoutData(data);
> balanceText.setVisible(false);
> amount.setVisible(false);
> //currencyCoins.setVisible(false);
> //currencyBills.setVisible(false);
> }
>
> data= new GridData(130,15);
> Label passwordLabel = new Label(comp,SWT.NONE);
> passwordLabel.setText("Manager Password: ");
> passwordLabel.setLayoutData(data);
> data= new GridData(130,15);
> data.horizontalAlignment = 2;
> passwordField = new Text(comp,SWT.SINGLE|SWT.PASSWORD);
> passwordField.setLayoutData(data);
> return comp;
> }
>
> /*
> * Adds buttons to this dialog's button bar.
> * @see
> org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(o rg.eclipse.swt.widgets.Composite)
>
> */
> protected void createButtonsForButtonBar(Composite parent){
> super.createButtonsForButtonBar(parent);
> //createButton(parent,RESET_ID,"Reset All", false);
> }
>
> /*
> * Notifies that this dialog's button with the given id has been
> pressed.
> * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
> */
> protected void buttonPressed(int buttonId)
> {
>
> if(buttonId == 1){ //cancel was pressed, close the dialog
> this.cancelPressed();
> }
> else { //check for valid manager password discount
> try {
> Scanner scanner = new Scanner(file);
> try {
> //first use a Scanner to get each line
> while ( scanner.hasNextLine() ){
> if(processApproval( scanner.nextLine() )){
> //ready to apply discount
> approvalSuccessful = true;
> break; }
> }
> if(displayApprovalError[0].equals("show")){
> MessageDialog.openError(getParentShell(),
> "Error", displayApprovalError[1]+" password incorrect.");
> }
>
> if(displayApprovalError[0].equals("show")&&displayApprovalError[2].equals( "format")){
>
> MessageDialog.openError(getParentShell(),
> "Error", displayApprovalError[1]+" balance incorrect, " +
> "must be in the format xx.xx");
> }
> }
> catch(Exception e){
> System.out.println("exception: " + e.getMessage());
> }
> finally {
> //ensure the underlying stream is always closed
> scanner.close();
> //closes this dialog
> if(approvalSuccessful){
> super.okPressed();
> }
> }
> } catch (FileNotFoundException e) {
> System.err.println("file not found");
> // log error message that file was not found
> //logger.log("login.txt not found");
> }
> }
> }
>
> /** * This method checks approvals.txt for valid manager password.
> * @param next line of the file
> * @return approvalResult <b>true</b> if password was correct, false
> otherwise
> */
> private boolean processApproval(String aLine){
> boolean approvalResult = false;
> //use a second Scanner to parse the content of each line
> Scanner scanner = new Scanner(aLine);
> if ( scanner.hasNext() ){
> scanner.next();
> String function = scanner.next();
> String password = scanner.next();
>
> //if we are doing approval for openingBalance of closingBalance
>
> if(feature.equals("openingBalance")||feature.equals("closingBalance ")){
> boolean invalidFormat = false;
> //check that correct balance format has been entered
> try{
>
> formatter.format(Float.valueOf(balanceText.getText()));
> }
> catch(IllegalArgumentException e){
> System.out.println("invalid format was entered");
> invalidFormat = true;
> }
> if(balanceText.isVisible() &&
> !balanceText.getText().equals("") && !invalidFormat){
> System.out.println("correct fomat and not empty");
> //approval was successful for manager
> approvalResult = true;
> approvalAmount =
> formatter.format(Float.valueOf(balanceText.getText()));
> displayApprovalError[0]=""; //don't display an
> approval error
> displayApprovalError[2]="";
> //check that manager password is correct
> if(function.equals("manager") &&
> password.equals(passwordField.getText())){
> //approval was successful for manager
> approvalResult = true;
> displayApprovalError[0]=""; //don't display an
> approval error
> displayApprovalError[1]="";
> }
> else{
> approvalResult = false;
> displayApprovalError[0] = "show";
> displayApprovalError[1] = "Manager";
> } }
> else{
> approvalResult = false;
> displayApprovalError[0] = "show"; //display an
> approval error
> displayApprovalError[2] = "format";
> } }
>
> //if we are doing approval for discount
> else if(feature.equals("discount")){
> //check that manager password is correct
> if(function.equals("manager") &&
> password.equals(passwordField.getText())){
> //approval was successful for manager
> approvalResult = true;
> displayApprovalError[0]=""; //don't display an
> approval error
> displayApprovalError[1]="";
> }
> else{
> approvalResult = false;
> displayApprovalError[0] = "show";
> displayApprovalError[1] = "Manager";
> } }
> }
> else { //this line in approval.txt is not relevant
> approvalResult = false;
> }
> scanner.close();
>
> return approvalResult;
> }
>
> private void createTableBills(Composite parent) {
> int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL |
> SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
>
> gridData = new GridData(100, 15);
> gridData.verticalIndent=20;
> currencyBills = new Label(parent,SWT.None);
> currencyBills.setText("Currency Notes/Bills: ");
> currencyBills.setLayoutData(gridData);
> currencyBills.setVisible(true);
> gridData = new GridData(SWT.BEGINNING,SWT.TOP,false,false);
> tableBills = new Table(parent,style);
> //table.setLayout(gridLayout);
> tableBills.setLayoutData(gridData);
> tableBills.setBackground(white);
> tableBills.setHeaderVisible(true);
> tableBills.setLinesVisible(true);
>
> // 1st currency
> TableColumn column = new TableColumn(tableBills, SWT.CENTER,
> 0);
> column.setText("£100 (Scottish)");
> column.setWidth(120);
>
> // 2nd currency
> column = new TableColumn(tableBills, SWT.LEFT, 1);
> column.setText("£50");
> column.setWidth(50);
>
> // 3rd currency
> column = new TableColumn(tableBills, SWT.LEFT, 2);
> column.setText("£20");
> column.setWidth(50);
>
> // 4th currency
> column = new TableColumn(tableBills, SWT.LEFT, 3);
> column.setText("£10");
> column.setWidth(50);
>
> // 5th currency
> column = new TableColumn(tableBills, SWT.LEFT, 4);
> column.setText("£5");
> column.setWidth(50);
>
> }
>
>
> private void createTableViewerBills(String balType, Composite parent) {
> ApproveDialog.BALANCETYPE = balType;
> try{
>
> viewerBills = new TableViewer(tableBills);
> viewerBills.setUseHashlookup(true);
> viewerBills.setColumnProperties(COLUMNNAMESBILLS);
> // Create the cell editors
> CellEditor[] editors = new CellEditor[COLUMNNAMESBILLS.length];
> for(int i=0;i<editors.length;i++){
> TextCellEditor textEditor = new TextCellEditor(tableBills);
> // quantity (Text with digits only)
> textEditor = new TextCellEditor(tableBills);
> ((Text) textEditor.getControl()).addVerifyListener(
> new VerifyListener() {
> public void verifyText(VerifyEvent e) {
> // Here, we could use a RegExp such as the
> following // if using JRE1.4 such as e.doit
> = e.text.matches("[\\-0-9]*");
> e.doit = "0123456789".indexOf(e.text) >= 0 ;
> }
> });
>
>
> editors[i] = textEditor;
>
> }
>
> viewerBills.setCellEditors(editors);
> // Set the cell modifier for the viewer
> viewerBills.setCellModifier(new CellBillModifier(this));
>
> }
> catch(Exception e){
> System.out.println(e.getMessage());
> }
> }
>
> private void createTableCoins(Composite parent) {
> int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL |
> SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
> gridData = new GridData(100, 15);
> gridData.verticalIndent=10;
> currencyCoins = new Label(parent,SWT.None);
> currencyCoins.setText("Currency Coins: ");
> currencyCoins.setLayoutData(gridData);
> currencyCoins.setVisible(true);
> gridData = new GridData(SWT.BEGINNING,SWT.TOP,false,false);
> tableCoins = new Table(parent,style);
> tableCoins.setLayoutData(gridData);
> tableCoins.setBackground(white);
> tableCoins.setHeaderVisible(true);
> tableCoins.setLinesVisible(true);
>
> // 1st currency
> TableColumn column = new TableColumn(tableCoins, SWT.CENTER,
> 0);
> column.setText("£2");
> column.setWidth(50);
>
> // 2nd currency
> column = new TableColumn(tableCoins, SWT.LEFT, 1);
> column.setText("£1");
> column.setWidth(50);
>
> // 3rd currency
> column = new TableColumn(tableCoins, SWT.LEFT, 2);
> column.setText("50p");
> column.setWidth(50);
>
> // 4th currency
> column = new TableColumn(tableCoins, SWT.LEFT, 3);
> column.setText("20p");
> column.setWidth(50);
>
> // 5th currency
> column = new TableColumn(tableCoins, SWT.LEFT, 4);
> column.setText("10p");
> column.setWidth(50);
>
> // 6th currency
> column = new TableColumn(tableCoins, SWT.LEFT, 5);
> column.setText("5p");
> column.setWidth(50);
>
> // 7th currency
> column = new TableColumn(tableCoins, SWT.LEFT, 6);
> column.setText("2p");
> column.setWidth(50);
>
> // 8th currency
> column = new TableColumn(tableCoins, SWT.LEFT, 7);
> column.setText("1p");
> column.setWidth(50);
>
> }
>
>
> private void createTableViewerCoins(String balType, Composite parent) {
> try{
> viewerCoins = new TableViewer(tableCoins);
> viewerCoins.setUseHashlookup(true);
> viewerCoins.setColumnProperties(COLUMNNAMESCOINS);
> // Create the cell editors
> CellEditor[] editors = new CellEditor[COLUMNNAMESCOINS.length];
> TextCellEditor textEditor = new TextCellEditor(tableCoins);
> // Column 3 : quantity (Text with digits only)
> textEditor = new TextCellEditor(tableCoins);
> ((Text) textEditor.getControl()).addVerifyListener(
> new VerifyListener() {
> public void verifyText(VerifyEvent e) {
> // Here, we could use a RegExp such as the
> following // if using JRE1.4 such as e.doit
> = e.text.matches("[\\-0-9]*");
> e.doit = "0123456789".indexOf(e.text) >= 0 ;
> }
> });
>
> for(int i=0;i<editors.length;i++){
> editors[i] = textEditor;
>
> }
>
> viewerCoins.setCellEditors(editors);
> // Set the cell modifier for the viewer
> viewerCoins.setCellModifier(new CellCoinModifier(viewerCoins));
>
> }
> catch(Exception e){
> System.out.println(e.getMessage());
> }
> }
>
>
> /**
> * This inner class implements an ICellModifier
> * An ICellModifier is called when the user modifes a cell in the
> * tableViewer.
> */
> class CellBillModifier implements ICellModifier {
> private ApproveDialog dialog;
> //ApproveDialog dialog;
> /**
> * Constructor * @param OrderTableViewer an instance of
> a OrderTableViewer */
> public CellBillModifier(ApproveDialog dialog) {
> super();
> // this.dialog=dialog;
> System.out.println("been here 1");
> }
>
> /**
> * @see
> org.eclipse.jface.viewers.ICellModifier#canModify(java.lang. Object,
> java.lang.String)
> */
> public boolean canModify(Object element, String property) {
> System.out.println("been here 2");
> return true;
> }
>
>
> /**
> * @see
> org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.O bject,
> java.lang.String)
> */
> public Object getValue(Object element, String property) {
> System.out.println("been here 3");
> // Find the index of the column
> int columnIndex =
> ApproveDialog.GETCOLUMNNAMESBILLS().indexOf(property);
> Object result = null;
> CurrencyBill currencyBill = (CurrencyBill) element;
> if(ApproveDialog.BALANCETYPE.equals("opening")){ //we are
> modifying a cell in the openingBalance tableviewer
> switch (columnIndex) {
> case 0 : // CURRENCY A COLUMN
> System.out.println("been here 4");
> result = currencyBill.getOpeningCurrencyA();
> break;
> case 1 : // CURRENCY B COLUMN
> result = currencyBill.getOpeningCurrencyB();
> break;
> case 2 : // CURRENCY C COLUMN result
> = currencyBill.getOpeningCurrencyC();
> break;
> case 3 : // CURRENCY D COLUMN
> result = currencyBill.getOpeningCurrencyD();
> break;
> case 4 : // CURRENCY E COLUMN
> result = currencyBill.getOpeningCurrencyE();
> break;
> default :
> result = "";
> }
> }
> if(ApproveDialog.BALANCETYPE.equals("closing")){ //we are
> modifying a cell in the closingBalance tableviewer
> switch (columnIndex) {
> case 0 : // CURRENCY A COLUMN
> System.out.println("been here 5");
> result = currencyBill.getClosingCurrencyA();
> break;
> case 1 : // CURRENCY B COLUMN
> result = currencyBill.getClosingCurrencyB();
> break;
> case 2 : // CURRENCY C COLUMN result
> = currencyBill.getClosingCurrencyC();
> break;
> case 3 : // CURRENCY D COLUMN
> result = currencyBill.getClosingCurrencyD();
> break;
> case 4 : // CURRENCY E COLUMN
> result = currencyBill.getClosingCurrencyE();
> break;
> default :
> result = "";
> }
> }
> return result;
> }
>
> /**
> * @see
> org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Obj ect,
> java.lang.String, java.lang.Object)
> */
> public void modify(Object element, String property, Object
> value) {
> System.out.println("been here 6");
> // Find the index of the column int
> columnIndex = ApproveDialog.GETCOLUMNNAMESBILLS().indexOf(property);
>
> TableItem tableItem = (TableItem) element;
> CurrencyBill currencyBill = (CurrencyBill) tableItem.getData();
> String valueString;
> if(BALANCETYPE.equals("opening")){
> switch (columnIndex) {
> case 0 : // CURRENCY A COLUMN
> System.out.println("been here 7");
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
>
> currencyBill.setOpeningCurrA(Integer.parseInt(valueString));
> break;
> case 1 : // CURRENCY B COLUMN
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
>
> currencyBill.setOpeningCurrB(Integer.parseInt(valueString));
> break;
> case 2 : // CURRENCY C COLUMN
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
>
> currencyBill.setOpeningCurrC(Integer.parseInt(valueString));
> break;
> case 3 : // CURRENCY D COLUMN
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
>
> currencyBill.setOpeningCurrD(Integer.parseInt(valueString));
> break;
> case 4 : // CURRENCY E COLUMN
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
>
> currencyBill.setOpeningCurrE(Integer.parseInt(valueString));
> break;
> default :
> }
> }
>
> ApproveDialog.getCurrencyBillList().quantityChanged(currency Bill);
>
> }
> }
>
> /**
> * This inner class implements an ICellModifier
> * An ICellModifier is called when the user modifes a cell in the
> * tableViewer.
> */
> class CellCoinModifier implements ICellModifier {
> private String balanceType;
> private Viewer viewerCoins;
> /**
> * Constructor * @param OrderTableViewer an instance of
> a OrderTableViewer */
> public CellCoinModifier(TableViewer viewerCoins) {
> super();
> this.balanceType=ApproveDialog.BALANCETYPE;
> this.viewerCoins = viewerCoins;
> }
>
> /**
> * @see
> org.eclipse.jface.viewers.ICellModifier#canModify(java.lang. Object,
> java.lang.String)
> */
> public boolean canModify(Object element, String property) {
> return true;
> }
>
>
> /**
> * @see
> org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.O bject,
> java.lang.String)
> */
> public Object getValue(Object element, String property) {
> // Find the index of the column
> int columnIndex =
> ApproveDialog.GETCOLUMNNAMESCOINS().indexOf(property);
> Object result = null;
> CurrencyCoin currencyCoin = (CurrencyCoin) element;
> switch (columnIndex) {
> case 0 : // CURRENCY A COLUMN
> if(balanceType.equals("opening"))
> result = currencyCoin.getOpeningCurrencyA();
> else result = currencyCoin.getClosingCurrencyA();
> break;
> case 1 : // CURRENCY B COLUMN
> if(balanceType.equals("opening"))
> result = currencyCoin.getOpeningCurrencyB();
> else result = currencyCoin.getClosingCurrencyB();
> break;
> case 2 : // CURRENCY C COLUMN
> if(balanceType.equals("opening"))
> result = currencyCoin.getOpeningCurrencyC();
> else result = currencyCoin.getClosingCurrencyC();
> break;
> case 3 : // CURRENCY D COLUMN
> if(balanceType.equals("opening"))
> result = currencyCoin.getOpeningCurrencyD();
> else result = currencyCoin.getClosingCurrencyD();
> break;
> case 4 : // CURRENCY E COLUMN
> if(balanceType.equals("opening"))
> result = currencyCoin.getOpeningCurrencyE();
> else result = currencyCoin.getClosingCurrencyE();
> break;
> case 5 : // CURRENCY F COLUMN
> if(balanceType.equals("opening"))
> result = currencyCoin.getOpeningCurrencyF();
> else result = currencyCoin.getClosingCurrencyF();
> break;
> case 6 : // CURRENCY G COLUMN
> if(balanceType.equals("opening"))
> result = currencyCoin.getOpeningCurrencyG();
> else result = currencyCoin.getClosingCurrencyG();
> break;
> case 7 : // CURRENCY H COLUMN
> if(balanceType.equals("opening"))
> result = currencyCoin.getOpeningCurrencyH();
> else result = currencyCoin.getClosingCurrencyH();
> break;
> default :
> result = "";
> }
>
> return result;
> }
>
> /**
> * @see
> org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Obj ect,
> java.lang.String, java.lang.Object)
> */
> public void modify(Object element, String property, Object
> value) {
> // Find the index of the column int
> columnIndex = ApproveDialog.GETCOLUMNNAMESCOINS().indexOf(property);
>
> TableItem tableItem = (TableItem) element;
> CurrencyCoin currencyCoin = (CurrencyCoin) tableItem.getData();
> String valueString;
> switch (columnIndex) {
> case 0 : // CURRENCY A COLUMN
>
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
> if(balanceType.equals("opening"))
>
> currencyCoin.setOpeningCurrA(Integer.parseInt(valueString));
> else
> currencyCoin.setClosingCurrA(Integer.parseInt(valueString));
> break;
> case 1 : // CURRENCY B COLUMN
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
> if(balanceType.equals("opening"))
>
> currencyCoin.setOpeningCurrB(Integer.parseInt(valueString));
> else
> currencyCoin.setClosingCurrB(Integer.parseInt(valueString));
> break;
> case 2 : // CURRENCY C COLUMN
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
> if(balanceType.equals("opening"))
>
> currencyCoin.setOpeningCurrC(Integer.parseInt(valueString));
> else
> currencyCoin.setClosingCurrC(Integer.parseInt(valueString));
> break;
> case 3 : // CURRENCY D COLUMN
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
> if(balanceType.equals("opening"))
>
> currencyCoin.setOpeningCurrD(Integer.parseInt(valueString));
> else
> currencyCoin.setClosingCurrD(Integer.parseInt(valueString));
> break;
> case 4 : // CURRENCY E COLUMN
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
> if(balanceType.equals("opening"))
>
> currencyCoin.setOpeningCurrE(Integer.parseInt(valueString));
> else
> currencyCoin.setClosingCurrE(Integer.parseInt(valueString));
> break;
> case 5 : // CURRENCY F COLUMN
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
> if(balanceType.equals("opening"))
>
> currencyCoin.setOpeningCurrF(Integer.parseInt(valueString));
> else
> currencyCoin.setClosingCurrF(Integer.parseInt(valueString));
> break;
> case 6 : // CURRENCY G COLUMN
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
> if(balanceType.equals("opening"))
>
> currencyCoin.setOpeningCurrG(Integer.parseInt(valueString));
> else
> currencyCoin.setClosingCurrG(Integer.parseInt(valueString));
> break;
> case 7 : // CURRENCY H COLUMN
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
> if(balanceType.equals("opening"))
>
> currencyCoin.setOpeningCurrH(Integer.parseInt(valueString));
> else
> currencyCoin.setClosingCurrH(Integer.parseInt(valueString));
> break;
> default :
> }
>
> ApproveDialog.getCurrencyCoinList().quantityChanged(currency Coin);
> }
> }
>
> /**
> * Return the column names in a collection
> * * @return List containing column names
> */
> public static java.util.List<String> GETCOLUMNNAMESBILLS() {
> return Arrays.asList(COLUMNNAMESBILLS);
> }
>
> /**
> * Return the column names in a collection
> * * @return List containing column names
> */
> public static java.util.List<String> GETCOLUMNNAMESCOINS() {
> return Arrays.asList(COLUMNNAMESCOINS);
> }
>
>
> /**
> * Return the CurrencyCoinList
> */
> public static CurrencyCoinList getCurrencyCoinList() {
> return CURRENCYCOINLIST;
> }
>
> /**
> * Return the CurrencyCoinList
> */
> public static CurrencyBillList getCurrencyBillList() {
> return CURRENCYBILLSLIST;
> }
>
> class CurrencyBillContentProvider implements
> IStructuredContentProvider,ICurrencyBillListViewer{
>
> @Override
> public Object[] getElements(Object inputElement) {
> if(CURRENCYBILLSLIST.getCurrency()!=null) {
> return CURRENCYBILLSLIST.getCurrency().toArray();
> }
>
> else
> return new Object[]{};
>
> }
>
> @Override
> public void dispose() {
> // TODO Auto-generated method stub
>
> }
>
> @Override
> public void inputChanged(Viewer viewer, Object oldInput, Object
> newInput) {
> if (newInput != null)
> ((CurrencyBillList) newInput).addChangeListener(this);
> if (oldInput != null)
> ((CurrencyBillList) oldInput).removeChangeListener(this);
> }
>
> @Override
> public void updateQuantity(CurrencyBill bill) {
> viewerBills.update(bill,null);
>
> }
> }
>
> class CurrencyCoinContentProvider implements
> IStructuredContentProvider,ICurrencyCoinListViewer{
>
> @Override
> public Object[] getElements(Object inputElement) {
> if(CURRENCYCOINLIST.getCurrency()!=null) {
> return CURRENCYCOINLIST.getCurrency().toArray();
> }
>
> else
> return new Object[]{};
>
> }
>
> @Override
> public void dispose() {
> // TODO Auto-generated method stub
>
> }
>
> @Override
> public void inputChanged(Viewer viewer, Object oldInput, Object
> newInput) {
> if (newInput != null)
> ((CurrencyCoinList) newInput).addChangeListener(this);
> if (oldInput != null)
> ((CurrencyCoinList) oldInput).removeChangeListener(this);
> }
>
> @Override
> public void updateQuantity(CurrencyCoin coin) {
> viewerCoins.update(coin,null);
>
> }
> }
> }
>
>
Re: CellModifier in TreeViewer in Dialog class [message #16700 is a reply to message #16574] Thu, 25 June 2009 21:26 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor Ganapolsky
Messages: 39
Registered: July 2009
Location: New York
Member

I modified a JFace example as you asked -
Snippet012DialogWithImageButtons. I don't know how to "keep it as minimal
as possible." I'm want to illustrate the way I create a table and
tableviewer and use a cellmodifier. Therefore, the code I'm going to
present here isn't short. If you have suggestions on how to make it
shorter please let me know.
Anyway, here is the entire class of the example:

/*********************************************************** ********************
* Copyright (c) 2006, 2008 Tom Schindl and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Tom Schindl - initial API and implementation
************************************************************ *******************/
package dialogs;

import java.util.Arrays;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

/**
* A snippet to demonstrate a dialog with image buttons.
*
*/
public class Snippet012DialogWithImageButtons {
private ImageRegistry registry;
Table tableBills;
final static String[] COLUMNNAMESBILLS = { "£100 (Scottish)", "£50",
"£20", "£10", "£5"};
private static String BALANCETYPE;

public Snippet012DialogWithImageButtons(final Shell shell) {

Dialog dia = new Dialog(shell) {
private ImageDescriptor getImageDescriptor(String path) {
if( registry == null ) {
registry = new ImageRegistry(shell.getDisplay());
}

ImageDescriptor desc = registry.getDescriptor(path);
if( desc == null ) {
desc =
ImageDescriptor.createFromFile(Snippet012DialogWithImageButt ons.class,
path);
registry.put(path, desc);
}

return desc;
}

protected Button createButton(Composite parent, int id, String label,
boolean defaultButton) {
createTableBills(parent);
createTableViewerBills("opening",parent);
Button b = super.createButton(parent, id, label, defaultButton);
if( id == IDialogConstants.OK_ID ) {
b.setImage(getImageDescriptor("filesave.png").createImage());
//$NON-NLS-1$
// reset the button layout
setButtonLayoutData(b);
} else {
b.setImage(getImageDescriptor("cancel.png").createImage());
//$NON-NLS-1$
// reset the button layout
setButtonLayoutData(b);
return b;
}

return b;
}
};
dia.open();
}

public static void main(String[] args) {

Display display = new Display ();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());

shell.open ();

new Snippet012DialogWithImageButtons(shell);

while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}

display.dispose ();
}

private void createTableBills(Composite parent) {
int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL |
SWT.FULL_SELECTION | SWT.HIDE_SELECTION;

GridData gridData = new GridData(100, 15);
gridData.verticalIndent=20;
Label currencyBills = new Label(parent,SWT.None);
currencyBills.setText("Currency Notes/Bills: ");
currencyBills.setLayoutData(gridData);
currencyBills.setVisible(true);
gridData = new GridData(SWT.BEGINNING,SWT.TOP,false,false);
tableBills = new Table(parent,style);
//table.setLayout(gridLayout);
tableBills.setLayoutData(gridData);
tableBills.setHeaderVisible(true);
tableBills.setLinesVisible(true);

// 1st currency
TableColumn column = new TableColumn(tableBills, SWT.CENTER, 0);
column.setText("£100 (Scottish)");
column.setWidth(120);

// 2nd currency
column = new TableColumn(tableBills, SWT.LEFT, 1);
column.setText("£50");
column.setWidth(50);

// 3rd currency
column = new TableColumn(tableBills, SWT.LEFT, 2);
column.setText("£20");
column.setWidth(50);

// 4th currency
column = new TableColumn(tableBills, SWT.LEFT, 3);
column.setText("£10");
column.setWidth(50);

// 5th currency
column = new TableColumn(tableBills, SWT.LEFT, 4);
column.setText("£5");
column.setWidth(50);

}


private void createTableViewerBills(String balType, Composite parent) {
Snippet012DialogWithImageButtons.BALANCETYPE = balType;
try{

TableViewer viewerBills = new TableViewer(tableBills);
viewerBills.setUseHashlookup(true);
viewerBills.setColumnProperties(COLUMNNAMESBILLS);
// Create the cell editors
CellEditor[] editors = new CellEditor[COLUMNNAMESBILLS.length];
for(int i=0;i<editors.length;i++){
TextCellEditor textEditor = new TextCellEditor(tableBills);
// quantity (Text with digits only)
textEditor = new TextCellEditor(tableBills);
((Text) textEditor.getControl()).addVerifyListener(
new VerifyListener() {
public void verifyText(VerifyEvent e) {
// Here, we could use a RegExp such as the following
// if using JRE1.4 such as e.doit = e.text.matches("[\\-0-9]*");
e.doit = "0123456789".indexOf(e.text) >= 0 ;
}
});


editors[i] = textEditor;

}

viewerBills.setCellEditors(editors);
// Set the cell modifier for the viewer
viewerBills.setCellModifier(new CellBillModifier(this));


}
catch(Exception e){
System.out.println(e.getMessage());
}
}

/**
* This inner class implements an ICellModifier
* An ICellModifier is called when the user modifes a cell in the
* tableViewer.
*/
class CellBillModifier implements ICellModifier {
private Snippet012DialogWithImageButtons dialog;
//ApproveDialog dialog;
/**
* Constructor
* @param OrderTableViewer an instance of a OrderTableViewer
*/
public CellBillModifier(Snippet012DialogWithImageButtons dialog) {
super();
// this.dialog=dialog;
System.out.println("been here 1");
}

/**
* @see
org.eclipse.jface.viewers.ICellModifier#canModify(java.lang. Object,
java.lang.String)
*/
public boolean canModify(Object element, String property) {
System.out.println("been here 2");
return true;
}


/**
* @see
org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.O bject,
java.lang.String)
*/
public Object getValue(Object element, String property) {
System.out.println("been here 3");
// Find the index of the column
int columnIndex =
Snippet012DialogWithImageButtons.GETCOLUMNNAMESBILLS().index Of(property);
Object result = null;
//CurrencyBill currencyBill = (CurrencyBill) element;
if(Snippet012DialogWithImageButtons .BALANCETYPE.equals("opening")){
//we are modifying a cell in the openingBalance tableviewer
switch (columnIndex) {
case 0 : // CURRENCY A COLUMN
System.out.println("been here 4");
// result = currencyBill.getOpeningCurrencyA();
break;
case 1 : // CURRENCY B COLUMN
// result = currencyBill.getOpeningCurrencyB();
break;
case 2 : // CURRENCY C COLUMN
// result = currencyBill.getOpeningCurrencyC();
break;
case 3 : // CURRENCY D COLUMN
//result = currencyBill.getOpeningCurrencyD();
break;
case 4 : // CURRENCY E COLUMN
//result = currencyBill.getOpeningCurrencyE();
break;
default :
result = "";
}
}
if(Snippet012DialogWithImageButtons .BALANCETYPE.equals("closing")){
//we are modifying a cell in the closingBalance tableviewer
switch (columnIndex) {
case 0 : // CURRENCY A COLUMN
System.out.println("been here 5");
//result = currencyBill.getClosingCurrencyA();
break;
case 1 : // CURRENCY B COLUMN
//result = currencyBill.getClosingCurrencyB();
break;
case 2 : // CURRENCY C COLUMN
//result = currencyBill.getClosingCurrencyC();
break;
case 3 : // CURRENCY D COLUMN
//result = currencyBill.getClosingCurrencyD();
break;
case 4 : // CURRENCY E COLUMN
//result = currencyBill.getClosingCurrencyE();
break;
default :
result = "";
}
}
return result;
}

/**
* @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Obj ect,
java.lang.String, java.lang.Object)
*/
public void modify(Object element, String property, Object value) {
System.out.println("been here 6");
// Find the index of the column
int columnIndex =
Snippet012DialogWithImageButtons.GETCOLUMNNAMESBILLS().index Of(property);

TableItem tableItem = (TableItem) element;
//CurrencyBill currencyBill = (CurrencyBill) tableItem.getData();
String valueString;
if(BALANCETYPE.equals("opening")){
switch (columnIndex) {
case 0 : // CURRENCY A COLUMN
System.out.println("been here 7");
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
//currencyBill.setOpeningCurrA(Integer.parseInt(valueString) );
break;
case 1 : // CURRENCY B COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
//currencyBill.setOpeningCurrB(Integer.parseInt(valueString) );
break;
case 2 : // CURRENCY C COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
//currencyBill.setOpeningCurrC(Integer.parseInt(valueString) );
break;
case 3 : // CURRENCY D COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
//currencyBill.setOpeningCurrD(Integer.parseInt(valueString) );
break;
case 4 : // CURRENCY E COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
//currencyBill.setOpeningCurrE(Integer.parseInt(valueString) );
break;
default :
}
}
//ApproveDialog.getCurrencyBillList().quantityChanged(curren cyBill);

}
}


/**
* Return the column names in a collection
*
* @return List containing column names
*/
public static java.util.List<String> GETCOLUMNNAMESBILLS() {
return Arrays.asList(COLUMNNAMESBILLS);
}

}
Re: CellModifier in TreeViewer in Dialog class [message #16715 is a reply to message #16700] Fri, 26 June 2009 03:44 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5008
Registered: July 2009
Senior Member
.... and now your viewers have no input so I can't reproduce the problem.
Could you init them statically with a list of elements?

Tom

Igor Ganapolsky schrieb:
> I modified a JFace example as you asked -
> Snippet012DialogWithImageButtons. I don't know how to "keep it as
> minimal as possible." I'm want to illustrate the way I create a table
> and tableviewer and use a cellmodifier. Therefore, the code I'm going
> to present here isn't short. If you have suggestions on how to make it
> shorter please let me know. Anyway, here is the entire class of the
> example:
>
> /*********************************************************** ********************
>
> * Copyright (c) 2006, 2008 Tom Schindl and others.
> * All rights reserved. This program and the accompanying materials
> * are made available under the terms of the Eclipse Public License v1.0
> * which accompanies this distribution, and is available at
> * http://www.eclipse.org/legal/epl-v10.html
> *
> * Contributors:
> * Tom Schindl - initial API and implementation
> ************************************************************ *******************/
>
> package dialogs;
>
> import java.util.Arrays;
>
> import org.eclipse.jface.dialogs.Dialog;
> import org.eclipse.jface.dialogs.IDialogConstants;
> import org.eclipse.jface.resource.ImageDescriptor;
> import org.eclipse.jface.resource.ImageRegistry;
> import org.eclipse.jface.viewers.CellEditor;
> import org.eclipse.jface.viewers.ICellModifier;
> import org.eclipse.jface.viewers.TableViewer;
> import org.eclipse.jface.viewers.TextCellEditor;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.VerifyEvent;
> import org.eclipse.swt.events.VerifyListener;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.widgets.Button;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Label;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.swt.widgets.Table;
> import org.eclipse.swt.widgets.TableColumn;
> import org.eclipse.swt.widgets.TableItem;
> import org.eclipse.swt.widgets.Text;
>
> /**
> * A snippet to demonstrate a dialog with image buttons.
> *
> */
> public class Snippet012DialogWithImageButtons {
> private ImageRegistry registry;
> Table tableBills;
> final static String[] COLUMNNAMESBILLS = { "£100 (Scottish)", "£50",
> "£20", "£10", "£5"};
> private static String BALANCETYPE;
>
> public Snippet012DialogWithImageButtons(final Shell shell) {
>
> Dialog dia = new Dialog(shell) {
> private ImageDescriptor getImageDescriptor(String path) {
> if( registry == null ) {
> registry = new ImageRegistry(shell.getDisplay());
> }
>
> ImageDescriptor desc = registry.getDescriptor(path);
> if( desc == null ) {
> desc =
> ImageDescriptor.createFromFile(Snippet012DialogWithImageButt ons.class,
> path);
> registry.put(path, desc);
> }
>
> return desc;
> }
>
> protected Button createButton(Composite parent, int id,
> String label, boolean defaultButton) {
> createTableBills(parent);
> createTableViewerBills("opening",parent);
> Button b = super.createButton(parent, id, label,
> defaultButton);
> if( id == IDialogConstants.OK_ID ) {
>
> b.setImage(getImageDescriptor("filesave.png").createImage()); //$NON-NLS-1$
> // reset the button layout
> setButtonLayoutData(b);
> } else {
>
> b.setImage(getImageDescriptor("cancel.png").createImage()); //$NON-NLS-1$
> // reset the button layout
> setButtonLayoutData(b);
> return b;
> }
>
> return b;
> }
> };
> dia.open();
> }
>
> public static void main(String[] args) {
>
> Display display = new Display ();
> Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
>
> shell.open ();
>
> new Snippet012DialogWithImageButtons(shell);
>
> while (!shell.isDisposed ()) {
> if (!display.readAndDispatch ()) display.sleep ();
> }
>
> display.dispose ();
> }
>
> private void createTableBills(Composite parent) {
> int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL |
> SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
>
> GridData gridData = new GridData(100, 15);
> gridData.verticalIndent=20;
> Label currencyBills = new Label(parent,SWT.None);
> currencyBills.setText("Currency Notes/Bills: ");
> currencyBills.setLayoutData(gridData);
> currencyBills.setVisible(true);
> gridData = new GridData(SWT.BEGINNING,SWT.TOP,false,false);
> tableBills = new Table(parent,style);
> //table.setLayout(gridLayout);
> tableBills.setLayoutData(gridData);
> tableBills.setHeaderVisible(true);
> tableBills.setLinesVisible(true);
>
> // 1st currency
> TableColumn column = new TableColumn(tableBills, SWT.CENTER,
> 0);
> column.setText("£100 (Scottish)");
> column.setWidth(120);
>
> // 2nd currency
> column = new TableColumn(tableBills, SWT.LEFT, 1);
> column.setText("£50");
> column.setWidth(50);
>
> // 3rd currency
> column = new TableColumn(tableBills, SWT.LEFT, 2);
> column.setText("£20");
> column.setWidth(50);
>
> // 4th currency
> column = new TableColumn(tableBills, SWT.LEFT, 3);
> column.setText("£10");
> column.setWidth(50);
>
> // 5th currency
> column = new TableColumn(tableBills, SWT.LEFT, 4);
> column.setText("£5");
> column.setWidth(50);
>
> }
>
>
> private void createTableViewerBills(String balType, Composite parent) {
> Snippet012DialogWithImageButtons.BALANCETYPE = balType;
> try{
>
> TableViewer viewerBills = new TableViewer(tableBills);
> viewerBills.setUseHashlookup(true);
> viewerBills.setColumnProperties(COLUMNNAMESBILLS);
> // Create the cell editors
> CellEditor[] editors = new CellEditor[COLUMNNAMESBILLS.length];
> for(int i=0;i<editors.length;i++){
> TextCellEditor textEditor = new TextCellEditor(tableBills);
> // quantity (Text with digits only)
> textEditor = new TextCellEditor(tableBills);
> ((Text) textEditor.getControl()).addVerifyListener(
> new VerifyListener() {
> public void verifyText(VerifyEvent e) {
> // Here, we could use a RegExp such as
> the following // if using JRE1.4 such
> as e.doit = e.text.matches("[\\-0-9]*");
> e.doit = "0123456789".indexOf(e.text) >=
> 0 ;
> }
> });
>
>
> editors[i] = textEditor;
>
> }
>
> viewerBills.setCellEditors(editors);
> // Set the cell modifier for the viewer
> viewerBills.setCellModifier(new CellBillModifier(this));
>
>
> }
> catch(Exception e){
> System.out.println(e.getMessage());
> }
> }
>
> /**
> * This inner class implements an ICellModifier
> * An ICellModifier is called when the user modifes a cell in the
> * tableViewer.
> */
> class CellBillModifier implements ICellModifier {
> private Snippet012DialogWithImageButtons dialog;
> //ApproveDialog dialog;
> /**
> * Constructor * @param OrderTableViewer an instance of
> a OrderTableViewer */
> public CellBillModifier(Snippet012DialogWithImageButtons dialog) {
> super();
> // this.dialog=dialog;
> System.out.println("been here 1");
> }
>
> /**
> * @see
> org.eclipse.jface.viewers.ICellModifier#canModify(java.lang. Object,
> java.lang.String)
> */
> public boolean canModify(Object element, String property) {
> System.out.println("been here 2");
> return true;
> }
>
>
> /**
> * @see
> org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.O bject,
> java.lang.String)
> */
> public Object getValue(Object element, String property) {
> System.out.println("been here 3");
> // Find the index of the column
> int columnIndex =
> Snippet012DialogWithImageButtons.GETCOLUMNNAMESBILLS().index Of(property);
> Object result = null;
> //CurrencyBill currencyBill = (CurrencyBill) element;
> if(Snippet012DialogWithImageButtons
> .BALANCETYPE.equals("opening")){ //we are modifying a cell in the
> openingBalance tableviewer
> switch (columnIndex) {
> case 0 : // CURRENCY A COLUMN
> System.out.println("been here 4");
> // result = currencyBill.getOpeningCurrencyA();
> break;
> case 1 : // CURRENCY B COLUMN
> // result = currencyBill.getOpeningCurrencyB();
> break;
> case 2 : // CURRENCY C COLUMN //
> result = currencyBill.getOpeningCurrencyC();
> break;
> case 3 : // CURRENCY D COLUMN
> //result = currencyBill.getOpeningCurrencyD();
> break;
> case 4 : // CURRENCY E COLUMN
> //result = currencyBill.getOpeningCurrencyE();
> break;
> default :
> result = "";
> }
> }
> if(Snippet012DialogWithImageButtons
> .BALANCETYPE.equals("closing")){ //we are modifying a cell in the
> closingBalance tableviewer
> switch (columnIndex) {
> case 0 : // CURRENCY A COLUMN
> System.out.println("been here 5");
> //result = currencyBill.getClosingCurrencyA();
> break;
> case 1 : // CURRENCY B COLUMN
> //result = currencyBill.getClosingCurrencyB();
> break;
> case 2 : // CURRENCY C COLUMN
> //result = currencyBill.getClosingCurrencyC();
> break;
> case 3 : // CURRENCY D COLUMN
> //result = currencyBill.getClosingCurrencyD();
> break;
> case 4 : // CURRENCY E COLUMN
> //result = currencyBill.getClosingCurrencyE();
> break;
> default :
> result = "";
> }
> }
> return result;
> }
>
> /**
> * @see
> org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Obj ect,
> java.lang.String, java.lang.Object)
> */
> public void modify(Object element, String property, Object
> value) {
> System.out.println("been here 6");
> // Find the index of the column int
> columnIndex =
> Snippet012DialogWithImageButtons.GETCOLUMNNAMESBILLS().index Of(property);
>
> TableItem tableItem = (TableItem) element;
> //CurrencyBill currencyBill = (CurrencyBill)
> tableItem.getData();
> String valueString;
> if(BALANCETYPE.equals("opening")){
> switch (columnIndex) {
> case 0 : // CURRENCY A COLUMN
> System.out.println("been here 7");
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
>
> //currencyBill.setOpeningCurrA(Integer.parseInt(valueString) );
> break;
> case 1 : // CURRENCY B COLUMN
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
>
> //currencyBill.setOpeningCurrB(Integer.parseInt(valueString) );
> break;
> case 2 : // CURRENCY C COLUMN
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
>
> //currencyBill.setOpeningCurrC(Integer.parseInt(valueString) );
> break;
> case 3 : // CURRENCY D COLUMN
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
>
> //currencyBill.setOpeningCurrD(Integer.parseInt(valueString) );
> break;
> case 4 : // CURRENCY E COLUMN
> valueString = ((String) value).trim();
> if (valueString.length() == 0)
> valueString = "0";
>
> //currencyBill.setOpeningCurrE(Integer.parseInt(valueString) );
> break;
> default :
> }
> }
>
> //ApproveDialog.getCurrencyBillList().quantityChanged(curren cyBill);
>
> }
> }
>
>
> /**
> * Return the column names in a collection
> * * @return List containing column names
> */
> public static java.util.List<String> GETCOLUMNNAMESBILLS() {
> return Arrays.asList(COLUMNNAMESBILLS);
> }
>
> }
>
>
>
Re: CellModifier in TreeViewer in Dialog class [message #17508 is a reply to message #16715] Fri, 26 June 2009 10:56 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor Ganapolsky
Messages: 39
Registered: July 2009
Location: New York
Member

Do I have to have a ContentProvider and LabelProvider? I think I was
getting confused. What I want to do is have an empty table that the user
can fill in. Upon pressing a submit button in the dialog, this table data
should be saved in a model.
So what I was trying to do is have an empty table that has an input of an
empty model. I just want to allow editing and saving the table's data.
Is that possible?
Re: CellModifier in TreeViewer in Dialog class [message #17514 is a reply to message #17508] Fri, 26 June 2009 11:02 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5008
Registered: July 2009
Senior Member
Hi,

You can only edit if there's a model object and hence there has to be
content in the table. The Viewer can't store it in the nirvana but has
to have a model instance it can write the data to.

Tom

Igor Ganapolsky schrieb:
> Do I have to have a ContentProvider and LabelProvider? I think I was
> getting confused. What I want to do is have an empty table that the
> user can fill in. Upon pressing a submit button in the dialog, this
> table data should be saved in a model. So what I was trying to do is
> have an empty table that has an input of an empty model. I just want to
> allow editing and saving the table's data. Is that possible?
Re: CellModifier in TreeViewer in Dialog class [message #17525 is a reply to message #17514] Fri, 26 June 2009 11:38 Go to previous messageGo to next message
Igor Ganapolsky is currently offline Igor Ganapolsky
Messages: 39
Registered: July 2009
Location: New York
Member

Does that imply that I have to have a ContentProvider and LabelProvider?
Or no?
Re: CellModifier in TreeViewer in Dialog class [message #17537 is a reply to message #17525] Fri, 26 June 2009 12:13 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5008
Registered: July 2009
Senior Member
Hi,

Yes but that's quite easy. You set a new ArrayContentProvider() and as
the input you set an array list.

Tom

Igor Ganapolsky schrieb:
> Does that imply that I have to have a ContentProvider and
> LabelProvider? Or no?
>
Re: CellModifier in TreeViewer in Dialog class [message #17550 is a reply to message #17537] Fri, 26 June 2009 12:30 Go to previous message
Igor Ganapolsky is currently offline Igor Ganapolsky
Messages: 39
Registered: July 2009
Location: New York
Member

Ok, that makes sense. Now I don't understand why the tableviewer has 2
columns instead of 1. Take a look at the modified code:
/*********************************************************** ********************
* Copyright (c) 2006, 2008 Tom Schindl and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Tom Schindl - initial API and implementation
************************************************************ *******************/
package dialogs;

import java.util.ArrayList;
import java.util.Arrays;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

/**
* A snippet to demonstrate a dialog with image buttons.
*
*/
public class Snippet012DialogWithImageButtons {
private ImageRegistry registry;
Table tableBills;
final static String[] COLUMNNAMESBILLS = { "£100 (Scottish)", "£50",
"£20", "£10", "£5"};
private static String BALANCETYPE;

public Snippet012DialogWithImageButtons(final Shell shell) {

Dialog dia = new Dialog(shell) {
private ImageDescriptor getImageDescriptor(String path) {
if( registry == null ) {
registry = new ImageRegistry(shell.getDisplay());
}

ImageDescriptor desc = registry.getDescriptor(path);
if( desc == null ) {
desc =
ImageDescriptor.createFromFile(Snippet012DialogWithImageButt ons.class,
path);
registry.put(path, desc);
}

return desc;
}

protected Button createButton(Composite parent, int id, String label,
boolean defaultButton) {
createTableBills(parent);
createTableViewerBills("opening",parent);
Button b = super.createButton(parent, id, label, defaultButton);
if( id == IDialogConstants.OK_ID ) {
b.setImage(getImageDescriptor("filesave.png").createImage());
//$NON-NLS-1$
// reset the button layout
setButtonLayoutData(b);
} else {
b.setImage(getImageDescriptor("cancel.png").createImage());
//$NON-NLS-1$
// reset the button layout
setButtonLayoutData(b);
return b;
}

return b;
}
};
dia.open();
}

public static void main(String[] args) {

Display display = new Display ();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());

shell.open ();

new Snippet012DialogWithImageButtons(shell);

while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}

display.dispose ();
}

private void createTableBills(Composite parent) {
int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL |
SWT.FULL_SELECTION | SWT.HIDE_SELECTION;

GridData gridData = new GridData(100, 15);
gridData.verticalIndent=20;
Label currencyBills = new Label(parent,SWT.None);
currencyBills.setText("Currency Notes/Bills: ");
currencyBills.setLayoutData(gridData);
currencyBills.setVisible(true);
gridData = new GridData(SWT.BEGINNING,SWT.TOP,false,false);
tableBills = new Table(parent,style);
//table.setLayout(gridLayout);
tableBills.setLayoutData(gridData);
tableBills.setHeaderVisible(true);
tableBills.setLinesVisible(true);

// 1st currency
TableColumn column = new TableColumn(tableBills, SWT.CENTER, 0);
column.setText("£100 (Scottish)");
column.setWidth(120);

// 2nd currency
column = new TableColumn(tableBills, SWT.LEFT, 1);
column.setText("£50");
column.setWidth(50);

// 3rd currency
column = new TableColumn(tableBills, SWT.LEFT, 2);
column.setText("£20");
column.setWidth(50);

// 4th currency
column = new TableColumn(tableBills, SWT.LEFT, 3);
column.setText("£10");
column.setWidth(50);

// 5th currency
column = new TableColumn(tableBills, SWT.LEFT, 4);
column.setText("£5");
column.setWidth(50);

}


private void createTableViewerBills(String balType, Composite parent) {
Snippet012DialogWithImageButtons.BALANCETYPE = balType;
try{

TableViewer viewerBills = new TableViewer(tableBills);
viewerBills.setUseHashlookup(true);
viewerBills.setColumnProperties(COLUMNNAMESBILLS);
// Create the cell editors
CellEditor[] editors = new CellEditor[COLUMNNAMESBILLS.length];
for(int i=0;i<editors.length;i++){
TextCellEditor textEditor = new TextCellEditor(tableBills);
// quantity (Text with digits only)
textEditor = new TextCellEditor(tableBills);
((Text) textEditor.getControl()).addVerifyListener(
new VerifyListener() {
public void verifyText(VerifyEvent e) {
// Here, we could use a RegExp such as the following
// if using JRE1.4 such as e.doit = e.text.matches("[\\-0-9]*");
e.doit = "0123456789".indexOf(e.text) >= 0 ;
}
});


editors[i] = textEditor;

}

viewerBills.setCellEditors(editors);
// Set the cell modifier for the viewer
viewerBills.setCellModifier(new CellBillModifier(this));
ArrayList arrlist = new ArrayList();
arrlist.add("test1");
viewerBills.setContentProvider(new ArrayContentProvider());//{

//});
viewerBills.setInput(arrlist);


}
catch(Exception e){
System.out.println(e.getMessage());
}
}

/**
* This inner class implements an ICellModifier
* An ICellModifier is called when the user modifes a cell in the
* tableViewer.
*/
class CellBillModifier implements ICellModifier {
private Snippet012DialogWithImageButtons dialog;
//ApproveDialog dialog;
/**
* Constructor
* @param OrderTableViewer an instance of a OrderTableViewer
*/
public CellBillModifier(Snippet012DialogWithImageButtons dialog) {
super();
// this.dialog=dialog;
System.out.println("been here 1");
}

/**
* @see
org.eclipse.jface.viewers.ICellModifier#canModify(java.lang. Object,
java.lang.String)
*/
public boolean canModify(Object element, String property) {
System.out.println("been here 2");
return true;
}


/**
* @see
org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.O bject,
java.lang.String)
*/
public Object getValue(Object element, String property) {
System.out.println("been here 3");
// Find the index of the column
int columnIndex =
Snippet012DialogWithImageButtons.GETCOLUMNNAMESBILLS().index Of(property);
Object result = null;
//CurrencyBill currencyBill = (CurrencyBill) element;
if(Snippet012DialogWithImageButtons .BALANCETYPE.equals("opening")){
//we are modifying a cell in the openingBalance tableviewer
switch (columnIndex) {
case 0 : // CURRENCY A COLUMN
System.out.println("been here 4");
// result = currencyBill.getOpeningCurrencyA();
break;
case 1 : // CURRENCY B COLUMN
// result = currencyBill.getOpeningCurrencyB();
break;
case 2 : // CURRENCY C COLUMN
// result = currencyBill.getOpeningCurrencyC();
break;
case 3 : // CURRENCY D COLUMN
//result = currencyBill.getOpeningCurrencyD();
break;
case 4 : // CURRENCY E COLUMN
//result = currencyBill.getOpeningCurrencyE();
break;
default :
result = "";
}
}
if(Snippet012DialogWithImageButtons .BALANCETYPE.equals("closing")){
//we are modifying a cell in the closingBalance tableviewer
switch (columnIndex) {
case 0 : // CURRENCY A COLUMN
System.out.println("been here 5");
//result = currencyBill.getClosingCurrencyA();
break;
case 1 : // CURRENCY B COLUMN
//result = currencyBill.getClosingCurrencyB();
break;
case 2 : // CURRENCY C COLUMN
//result = currencyBill.getClosingCurrencyC();
break;
case 3 : // CURRENCY D COLUMN
//result = currencyBill.getClosingCurrencyD();
break;
case 4 : // CURRENCY E COLUMN
//result = currencyBill.getClosingCurrencyE();
break;
default :
result = "";
}
}
return result;
}

/**
* @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Obj ect,
java.lang.String, java.lang.Object)
*/
public void modify(Object element, String property, Object value) {
System.out.println("been here 6");
// Find the index of the column
int columnIndex =
Snippet012DialogWithImageButtons.GETCOLUMNNAMESBILLS().index Of(property);

TableItem tableItem = (TableItem) element;
//CurrencyBill currencyBill = (CurrencyBill) tableItem.getData();
String valueString;
if(BALANCETYPE.equals("opening")){
switch (columnIndex) {
case 0 : // CURRENCY A COLUMN
System.out.println("been here 7");
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
//currencyBill.setOpeningCurrA(Integer.parseInt(valueString) );
break;
case 1 : // CURRENCY B COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
//currencyBill.setOpeningCurrB(Integer.parseInt(valueString) );
break;
case 2 : // CURRENCY C COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
//currencyBill.setOpeningCurrC(Integer.parseInt(valueString) );
break;
case 3 : // CURRENCY D COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
//currencyBill.setOpeningCurrD(Integer.parseInt(valueString) );
break;
case 4 : // CURRENCY E COLUMN
valueString = ((String) value).trim();
if (valueString.length() == 0)
valueString = "0";
//currencyBill.setOpeningCurrE(Integer.parseInt(valueString) );
break;
default :
}
}
//ApproveDialog.getCurrencyBillList().quantityChanged(curren cyBill);

}
}


/**
* Return the column names in a collection
*
* @return List containing column names
*/
public static java.util.List<String> GETCOLUMNNAMESBILLS() {
return Arrays.asList(COLUMNNAMESBILLS);
}

}
Previous Topic:A composite on top of another one.
Next Topic:SWTException: Invalid thread access when binding in non-UI thread
Goto Forum:
  


Current Time: Thu Apr 17 05:56:41 EDT 2014

Powered by FUDForum. Page generated in 0.27154 seconds