How to add new entities scanned by LocalContainerEntityManagerFactoryBean? - spring-data-jpa

Existing:
public class DefaultPersistenceUnitManager
implements PersistenceUnitManager, ResourceLoaderAware, LoadTimeWeaverAware, InitializingBean {
private static final Set entityTypeFilters;
static {
entityTypeFilters = new LinkedHashSet<>(8);
entityTypeFilters.add(new AnnotationTypeFilter(Entity.class, false));
entityTypeFilters.add(new AnnotationTypeFilter(Embeddable.class, false));
entityTypeFilters.add(new AnnotationTypeFilter(MappedSuperclass.class, false));
entityTypeFilters.add(new AnnotationTypeFilter(Converter.class, false));
}
To be:
static {
entityTypeFilters = new LinkedHashSet<>(8);
entityTypeFilters.add(new AnnotationTypeFilter(Entity.class, false));
entityTypeFilters.add(new AnnotationTypeFilter(Embeddable.class, false));
entityTypeFilters.add(new AnnotationTypeFilter(MappedSuperclass.class, false));
entityTypeFilters.add(new AnnotationTypeFilter(Converter.class, false));
entityTypeFilters.add(new AnnotationTypeFilter(MyEntity.class, false));
}
I have no clue on how to achieve this on the Spring boot application 2.7.6 version.

Related

AssertionFailedException while creating Commbobox in tableviewer

I am trying to make a combobox in table viewer in Eclipse SWT.pointing me in the right direction.I think I've done everything ok until now, problem is the combo box not display in the table,I got error this:
Error:
Block of Code is:
public void createPartControl(Composite parent) {
System.out.println("createPartControl call");
// For Testing
Composite tableComposite = new Composite(parent, SWT.NONE);
tableColumnLayout = new TableColumnLayout();
tableComposite.setLayout(tableColumnLayout);
tableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
true));
tableViewer = new TableViewer(tableComposite, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL);
tableViewer.setContentProvider(ArrayContentProvider.getInstance());
// TODO viewer.setLabelProvider(new ViewLabelProvider());
table = tableViewer.getTable();
// Table table = tableViewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
String[] titles = { "Threat Name", "Category Name", "Status",
"Priority", "Description", "Justification" };
for (int loopIndex = 0; loopIndex < titles.length; loopIndex++) {
tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tblclmn = tableViewerColumn.getColumn();
tableColumnLayout.setColumnData(tblclmn, new ColumnPixelData(200,
true, true));
tblclmn.setText(titles[loopIndex]);
}
}
private void fillRows(String shortdesc, String categ, String descp) {
System.out.println("fillRows call from above method.");
TableColumn status_Name_Col = tableViewer.getTable().getColumn(2);
System.out.println("**************** status_Name_Col ************ "+ status_Name_Col);
tableViewerColumn.setLabelProvider(new ColumnLabelProvider()
{
#Override
public String getText(Object element)
{
Dummy p = (Dummy) element;
return p.getValue();
}
});
tableViewer.addSelectionChangedListener(new ISelectionChangedListener()
{
#Override
public void selectionChanged(SelectionChangedEvent selectionChangedEvent)
{
StructuredSelection selection = (StructuredSelection) selectionChangedEvent.getSelection();
System.out.println(((Dummy) selection.getFirstElement()).getValue());
}
});
List<Dummy> elements = new ArrayList<>();
for (int i = 0; i < Connection.Number_Of_Connection; i++) {
elements.add(new Dummy("First option"));
}
tableViewer.setInput(elements);
tableColumnLayout.setColumnData(status_Name_Col, new ColumnWeightData(1, true));
tableViewerColumn.setEditingSupport(new FirstValueEditingSupport(tableViewer));
}
The assertion message is pretty clear - you must set a label provider for each column in the table.
You don't show us where you are calling fillRows but setting the column label provider in that method looks wrong - set the label providers in your loop creating the columns.

Sorting TableView with ListProperty bind

From this code.makery blog, i was able to understand how tableview sorting and filtering works and implement. The problem is the blog showed an example that does not use ListProperty. When i tried to change the code example with ListProperty no sorting and filtering happens.
This is what the blog provided
FilteredList<Person> filteredData = new FilteredList<>(masterData, p -> true);
filterField.textProperty().addListener((observable, oldValue, newValue) -> {
filteredData.setPredicate(person -> {
if (newValue == null || newValue.isEmpty()) {
return true;
}
String lowerCaseFilter = newValue.toLowerCase();
if (person.getFirstName().toLowerCase().contains(lowerCaseFilter)) {
return true;
} else if (person.getLastName().toLowerCase().contains(lowerCaseFilter)) {
return true;
}
return false;
});
});
SortedList<Person> sortedData = new SortedList<>(filteredData);sortedData.comparatorProperty().bind(personTable.comparatorProperty());
personTable.setItems(sortedData);
The following is SSCCE for my implementation with ListProperty
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.FloatProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.ReadOnlyFloatProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleFloatProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class SortTable extends Application{
private ObservableList<Collection> collectionList = FXCollections.observableArrayList();
private ListProperty<Collection> collectionListProperty = new SimpleListProperty<>();
public static void main(String[] args) { launch(args); }
#Override
public void start(Stage primaryStage) throws Exception {
BorderPane borderPane = new BorderPane();
collectionList.setAll(getCollection());
TableView<Collection> tv = new TableView();
TextField sortTF = new TextField();
TableColumn<Collection, Number> colAmount = createAmountColumn();
TableColumn<Collection, String> colMno = createMNOColumn();
TableColumn<Collection, Number> colPrice = createPriceColumn();
TableColumn<Collection, Number> colQty = createQuantityColumn();
tv.getColumns().addAll(colMno, colQty, colPrice, colAmount);
collectionListProperty.set(collectionList);
tv.itemsProperty().bind(collectionListProperty);
FilteredList<Collection> filteredData = new FilteredList<>(collectionList, p -> true);
sortTF.textProperty().addListener((observable, oldValue, newValue) -> {
filteredData.setPredicate(SaleTransaction -> {
if (newValue == null || newValue.isEmpty()) {
return true;
}
String lowerCaseFilter = newValue.toLowerCase();
if (SaleTransaction.getMno().toLowerCase().contains(lowerCaseFilter)) {
return true;
}
return false;
});
});
SortedList<Collection> sortedData = new SortedList<>(filteredData);
sortedData.comparatorProperty().bind(tv.comparatorProperty());
collectionList.setAll(sortedData);
borderPane.setTop(sortTF);
borderPane.setCenter(tv);
Scene scene = new Scene(borderPane, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
private TableColumn createQuantityColumn() {
TableColumn<Collection, Float> colQty = new TableColumn("Quantity");
colQty.setMinWidth(25);
colQty.setId("colQty");
colQty.setCellValueFactory(cellData -> cellData.getValue().quantityProperty().asObject());
return colQty;
}
private TableColumn createPriceColumn() {
TableColumn<Collection, Float> colPrice = new TableColumn("Price");
colPrice.setMinWidth(25);
colPrice.setId("colPrice");
colPrice.setCellValueFactory(cellData -> cellData.getValue().priceProperty().asObject());
return colPrice;
}
private TableColumn createAmountColumn() {
TableColumn<Collection, Float> colAmount = new TableColumn("Amount");
colAmount.setMinWidth(25);
colAmount.setId("colAmount");
colAmount.setCellValueFactory(cellData -> cellData.getValue().amountProperty().asObject());
return colAmount;
}
private TableColumn createMNOColumn() {
TableColumn colMNO = new TableColumn("M/NO");
colMNO.setMinWidth(25);
colMNO.setId("colMNO");
colMNO.setCellValueFactory(new PropertyValueFactory("mno"));
return colMNO;
}
private List<Collection> getCollection(){
List<Collection> collections = new ArrayList<>();
collections.add(new Collection(1, 10, "1", false));
collections.add(new Collection(2, 10, "12", true));
collections.add(new Collection(3, 10, "123", true));
collections.add(new Collection(4, 10, "312", true));
collections.add(new Collection(5, 10, "311", false));
collections.add(new Collection(6, 10, "322", true));
collections.add(new Collection(7, 10, "333", true));
collections.add(new Collection(8, 10, "321", false));
collections.add(new Collection(9, 10, "456", true));
collections.add(new Collection(10, 10, "551", true));
collections.add(new Collection(11, 10, "515", false));
collections.add(new Collection(12, 10, "134", true));
collections.add(new Collection(13, 10, "789", true));
collections.add(new Collection(14, 10, "879", false));
collections.add(new Collection(15, 10, "987", true));
collections.add(new Collection(16, 10, "856", true));
collections.add(new Collection(17, 10, "956", true));
collections.add(new Collection(18, 10, "589", true));
collections.add(new Collection(19, 10, "852", false));
collections.add(new Collection(20, 10, "456", false));
collections.add(new Collection(21, 10, "623", true));
collections.add(new Collection(22, 10, "147", false));
collections.add(new Collection(23, 10, "125", true));
collections.add(new Collection(24, 10, "258", false));
collections.add(new Collection(25, 10, "325", true));
collections.add(new Collection(26, 10, "753", true));
collections.add(new Collection(27, 10, "357", false));
collections.add(new Collection(28, 10, "159", false));
return collections;
}
public class Collection{
private final FloatProperty quantity = new SimpleFloatProperty();
private final FloatProperty price = new SimpleFloatProperty();
private final FloatProperty amount = new SimpleFloatProperty();
private final BooleanProperty paid = new SimpleBooleanProperty(false);
private String mno;
public Collection(){
this(0f, 0f, null, false);
}
public Collection(float quantity, float price, String mno, boolean paid) {
setQuantity(quantity);
setPrice(price);
setMno(mno);
setPaid(paid);
this.amount.bind(this.quantity.multiply(this.price));
}
public String getMno() {
return mno;
}
public void setMno(String mno) {
this.mno = mno;
}
public float getQuantity() {
return quantityProperty().get();
}
public void setQuantity(float quantity) {
quantityProperty().set(quantity);
}
public FloatProperty quantityProperty() {
return quantity ;
}
public float getPrice() {
return priceProperty().get();
}
public void setPrice(float price) {
priceProperty().set(price);
}
public FloatProperty priceProperty() {
return price ;
}
public float getAmount() {
return amountProperty().get();
}
public ReadOnlyFloatProperty amountProperty() {
return amount ;
}
public BooleanProperty paidProperty() {
return paid;
}
public void setPaid(boolean approved) {
this.paid.set(approved);
}
public boolean isPaid() {
return paid.get();
}
}
}

Seting TableCell Disable programmatically wierd behaivior

I have a table with quantity and price columns that are editable and not disabled. The table is populated with ObservableList<Collection> . Collection object has a boolean attribute paid. What i am tring to achieve is whenever paid is true, make both price and quantity tablecells disabled and not editable.
This is what i have done so far:
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import javafx.application.Application;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.FloatProperty;
import javafx.beans.property.ReadOnlyFloatProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleFloatProperty;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.converter.FloatStringConverter;
public class CollectionTable extends Application{
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
TableView<Collection> tv = new TableView();
tv.setEditable(true);
TableColumn<Collection, Number> colQty = createQuantityColumn();
colQty.setCellFactory(
new Callback<TableColumn<Collection, Number>, TableCell<Collection, Number>>() {
#Override
public TableCell<Collection, Number> call(TableColumn<Collection, Number> paramTableColumn) {
return new TextFieldTableCell<Collection, Number>() {
#Override
public void updateItem(Number s, boolean b) {
super.updateItem(s, b);
TableRow row = getTableRow();
if (row != null) {
Collection item = (Collection) row.getItem();
//Test for disable condition
if (item != null && item.isPaid()) {
setDisable(true);
setEditable(false);
this.setStyle("-fx-text-fill: grey;-fx-border-color: red");
}
}
}
};
}
});
TableColumn<Collection, Number> colPrice = createPriceColumn();
colPrice.setCellFactory(
new Callback<TableColumn<Collection, Number>, TableCell<Collection, Number>>() {
#Override
public TableCell<Collection, Number> call(TableColumn<Collection, Number> paramTableColumn) {
return new TextFieldTableCell<Collection, Number>() {
#Override
public void updateItem(Number s, boolean b) {
super.updateItem(s, b);
TableRow row = getTableRow();
if (row != null) {
Collection item = (Collection) row.getItem();
//Test for disable condition
if (item != null && !item.isPaid()) {
setDisable(true);
setEditable(false);
this.setStyle("-fx-text-fill: grey;-fx-border-color: red");
}
}
}
};
}
});
TableColumn<Collection, Number> colAmount = createAmountColumn();
TableColumn<Collection, String> colMno = createMNOColumn();
tv.getColumns().addAll(colMno, colQty, colPrice, colAmount);
tv.getItems().addAll(getCollection());
Scene scene = new Scene(new BorderPane(tv), 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
private TableColumn createQuantityColumn() {
TableColumn<Collection, Float> colQty = new TableColumn("Quantity");
colQty.setMinWidth(25);
colQty.setId("colQty");
colQty.setCellFactory(TextFieldTableCell.<Collection, Float>forTableColumn(new FloatStringConverter()));
colQty.setCellValueFactory(cellData -> cellData.getValue().quantityProperty().asObject());
colQty.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Collection, Float>>() {
#Override
public void handle(TableColumn.CellEditEvent<Collection, Float> t) {
}
});
return colQty;
}
private TableColumn createPriceColumn() {
TableColumn<Collection, Float> colPrice = new TableColumn("Price");
colPrice.setMinWidth(25);
colPrice.setId("colPrice");
colPrice.setCellFactory(TextFieldTableCell.<Collection, Float>forTableColumn(new FloatStringConverter()));
colPrice.setCellValueFactory(cellData -> cellData.getValue().priceProperty().asObject());
colPrice.setOnEditStart(new EventHandler<TableColumn.CellEditEvent<Collection, Float>>() {
#Override
public void handle(TableColumn.CellEditEvent<Collection, Float> t) {
Collection c = ((Collection) t.getTableView().getItems().get(t.getTablePosition().getRow()));
c.setPrice(Math.abs(c.getPrice()));
}
});
colPrice.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Collection, Float>>() {
#Override
public void handle(TableColumn.CellEditEvent<Collection, Float> t) {
Collection c = ((Collection) t.getTableView().getItems().get(t.getTablePosition().getRow()));
c.setPrice(Math.abs((float)t.getNewValue()));
//int i = collectionHandler.updateCollection(c);
}
});
return colPrice;
}
private TableColumn createAmountColumn() {
TableColumn<Collection, Float> colAmount = new TableColumn("Amount");
colAmount.setMinWidth(25);
colAmount.setId("colAmount");
colAmount.setCellValueFactory(cellData -> cellData.getValue().amountProperty().asObject());
return colAmount;
}
private TableColumn createMNOColumn() {
TableColumn colMNO = new TableColumn("M/NO");
colMNO.setMinWidth(25);
colMNO.setId("colMNO");
colMNO.setCellValueFactory(new PropertyValueFactory("mno"));
return colMNO;
}
private List<Collection> getCollection(){
List<Collection> collections = new ArrayList<>();
collections.add(new Collection(1, 10, "1", false));
collections.add(new Collection(2, 10, "12", true));
collections.add(new Collection(3, 10, "123", true));
collections.add(new Collection(4, 10, "312", true));
collections.add(new Collection(5, 10, "311", false));
collections.add(new Collection(6, 10, "322", true));
collections.add(new Collection(7, 10, "333", true));
collections.add(new Collection(8, 10, "321", false));
collections.add(new Collection(9, 10, "456", true));
collections.add(new Collection(10, 10, "551", true));
collections.add(new Collection(11, 10, "515", false));
collections.add(new Collection(12, 10, "134", true));
collections.add(new Collection(13, 10, "789", true));
collections.add(new Collection(14, 10, "879", false));
collections.add(new Collection(15, 10, "987", true));
collections.add(new Collection(16, 10, "856", true));
collections.add(new Collection(17, 10, "956", true));
collections.add(new Collection(18, 10, "589", true));
collections.add(new Collection(19, 10, "852", false));
collections.add(new Collection(20, 10, "456", false));
collections.add(new Collection(21, 10, "623", true));
collections.add(new Collection(22, 10, "147", false));
collections.add(new Collection(23, 10, "125", true));
collections.add(new Collection(24, 10, "258", false));
collections.add(new Collection(25, 10, "325", true));
collections.add(new Collection(26, 10, "753", true));
collections.add(new Collection(27, 10, "357", false));
collections.add(new Collection(28, 10, "159", false));
return collections;
}
public class Collection{
private final FloatProperty quantity = new SimpleFloatProperty();
private final FloatProperty price = new SimpleFloatProperty();
private final FloatProperty amount = new SimpleFloatProperty();
private final BooleanProperty paid = new SimpleBooleanProperty(false);
private String mno;
public Collection(){
this(0f, 0f, null, false);
}
public Collection(float quantity, float price, String mno, boolean paid) {
setQuantity(quantity);
setPrice(price);
setMno(mno);
setPaid(paid);
this.amount.bind(this.quantity.multiply(this.price));
}
public String getMno() {
return mno;
}
public void setMno(String mno) {
this.mno = mno;
}
public float getQuantity() {
return quantityProperty().get();
}
public void setQuantity(float quantity) {
quantityProperty().set(quantity);
}
public FloatProperty quantityProperty() {
return quantity ;
}
public float getPrice() {
return priceProperty().get();
}
public void setPrice(float price) {
priceProperty().set(price);
}
public FloatProperty priceProperty() {
return price ;
}
public float getAmount() {
return amountProperty().get();
}
public ReadOnlyFloatProperty amountProperty() {
return amount ;
}
public BooleanProperty paidProperty() {
return paid;
}
public void setPaid(boolean approved) {
this.paid.set(approved);
}
public boolean isPaid() {
return paid.get();
}
}
}
The problem with my code is that as i scroll down the table and up again, cells which were previously enabled and editable change to disabled and not editable.
Before scroll After scroll:
The first problem is that you don't reset the state when a cell is reused from one which is paid to one which is not paid. This will happen, among other times, when you scroll. If a cell was previously used in a row that represented a "paid" item (so it is disabled, not editable, and has a red border), and is reused for an "unpaid" item, your updateItem() method will not change the editable or disabled state (or the style). So you should have something like:
if (item != null && item.isPaid()) {
setDisable(true);
setEditable(false);
this.setStyle("-fx-text-fill: grey;-fx-border-color: red");
} else {
setDisable(false);
setEditable(true);
setStyle("");
}
The second problem is that you have no control over the order in which the cell's state is updated. It seems that sometimes the row property is updated after the updateItem() method is called, so you end up with inconsistent state. You can safely use the cell's index to get the correct item from the table's data.
Also note that since both cell factories are identical, there is no need to replicate the code. This works for me:
#Override
public void start(Stage primaryStage) throws Exception {
TableView<Collection> tv = new TableView();
tv.setEditable(true);
TableColumn<Collection, Number> colQty = createQuantityColumn();
Callback<TableColumn<Collection, Number>, TableCell<Collection, Number>> cellFactory = new Callback<TableColumn<Collection, Number>, TableCell<Collection, Number>>() {
#Override
public TableCell<Collection, Number> call(TableColumn<Collection, Number> paramTableColumn) {
return new TextFieldTableCell<Collection, Number>() {
#Override
public void updateItem(Number s, boolean b) {
super.updateItem(s, b);
if (! isEmpty()) {
Collection item = getTableView().getItems().get(getIndex());
// Test for disable condition
if (item != null && item.isPaid()) {
setDisable(true);
setEditable(false);
this.setStyle("-fx-text-fill: grey;-fx-border-color: red");
} else {
setDisable(false);
setEditable(true);
setStyle("");
}
}
}
};
}
};
colQty.setCellFactory(cellFactory);
TableColumn<Collection, Number> colPrice = createPriceColumn();
colPrice.setCellFactory(cellFactory);
TableColumn<Collection, Number> colAmount = createAmountColumn();
TableColumn<Collection, String> colMno = createMNOColumn();
tv.getColumns().addAll(colMno, colQty, colPrice, colAmount);
tv.getItems().addAll(getCollection());
Scene scene = new Scene(new BorderPane(tv), 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}

SWT CTabFolder doesn't display when running from eclipse

I am creating an app using WindowBuilder in eclipse 4.4.2. I've added CTabFolders and corresponding CTabItems. The tabs are displayed properly when Click the preview button(Quickly test/preview the window without compiling or running it) in windowbuilder.
Screen Shot===> http://i.imgur.com/Sx9TQrL.jpg
But the real problem is when I run the app,the tabs are not displayed.
Screen Shot===> http://i.imgur.com/OP7WY8W.jpg
Code
public class Dashboard {
public static void main(String[] args) {
Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setImage(SWTResourceManager.getImage(Dashboard.class,
"/com/sun/java/swing/plaf/windows/icons/Computer.gif"));
shell.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WIDGET_BORDER));
shell.setSize(1080, 700);
shell.setLayout(new GridLayout(2, false));
/**
* Base Layout
* **/
Composite content = new Composite(shell, SWT.BORDER);
content.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WIDGET_BACKGROUND));
content.setLayout(new StackLayout());
// content.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
// false));
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final CTabFolder tabFolder = new CTabFolder(content, SWT.CLOSE);
tabFolder.setTabHeight(28);
tabFolder.setSimple(false);
tabFolder.setSelectionForeground(SWTResourceManager
.getColor(SWT.COLOR_BLACK));
tabFolder.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WIDGET_BACKGROUND));
tabFolder.setSelectionBackground(Display.getCurrent().getSystemColor(
SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
CTabItem homeTab = new CTabItem(tabFolder, SWT.NONE);
homeTab.setText("Home");
Composite composite_4 = new Composite(tabFolder, SWT.NONE);
homeTab.setControl(composite_4);
Label lblHome = new Label(composite_4, SWT.NONE);
lblHome.setBounds(185, 105, 55, 15);
lblHome.setText("Home");
final CTabItem empTab = new CTabItem(tabFolder, SWT.CLOSE);
empTab.setImage(SWTResourceManager.getImage(
Dashboard.class, "/resource/icons/employee.png"));
empTab.setText("Employees");
Composite composite_5 = new Composite(tabFolder, SWT.NONE);
empTab.setControl(composite_5);
Label lblEmployees = new Label(composite_5, SWT.NONE);
lblEmployees.setBounds(155, 102, 55, 15);
lblEmployees.setText("Employees");
CTabItem workTab = new CTabItem(tabFolder, SWT.CLOSE);
workTab.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/worksite.png"));
workTab.setText("Work Site");
Composite composite_6 = new Composite(tabFolder, SWT.NONE);
workTab.setControl(composite_6);
Label lblWorkSite = new Label(composite_6, SWT.NONE);
lblWorkSite.setBounds(222, 123, 55, 15);
lblWorkSite.setText("Work site");
CTabItem paymentTab = new CTabItem(tabFolder, SWT.NONE);
paymentTab.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/payments.png"));
paymentTab.setText("Payments");
Composite composite_7 = new Composite(tabFolder, SWT.NONE);
paymentTab.setControl(composite_7);
Label lblPayments = new Label(composite_7, SWT.NONE);
lblPayments.setBounds(185, 176, 55, 15);
lblPayments.setText("Payments");
CTabItem toolsTab = new CTabItem(tabFolder, SWT.NONE);
toolsTab.setImage(SWTResourceManager.getImage(Dashboard.class, "/resource/icons/tools.png"));
toolsTab.setShowClose(true);
toolsTab.setText("Tools");
Composite composite_8 = new Composite(tabFolder, SWT.NONE);
toolsTab.setControl(composite_8);
Label lblTools = new Label(composite_8, SWT.NONE);
lblTools.setBounds(264, 125, 55, 15);
lblTools.setText("Tools");
/**
* MenuBar
* **/
Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);
MenuItem mntmFile = new MenuItem(menu, SWT.CASCADE);
mntmFile.setImage(null);
mntmFile.setText("File");
Menu menu_1 = new Menu(mntmFile);
mntmFile.setMenu(menu_1);
MenuItem mntmExit = new MenuItem(menu_1, SWT.NONE);
mntmExit.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
shell.dispose();
}
});
mntmExit.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/close.png"));
mntmExit.setText("Exit");
MenuItem mntmHelp = new MenuItem(menu, SWT.CASCADE);
mntmHelp.setText("Help");
Menu menu_2 = new Menu(mntmHelp);
mntmHelp.setMenu(menu_2);
MenuItem mntmAbout = new MenuItem(menu_2, SWT.NONE);
mntmAbout.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
About abdialog = new About(shell, 0);
abdialog.open();
}
});
mntmAbout.setText("About");
Composite sidebar = new Composite(shell, SWT.NONE);
sidebar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1,
1));
sidebar.setBackground(SWTResourceManager.getColor(112, 128, 144));
sidebar.setLayout(new GridLayout(1, false));
ExpandBar expandBar = new ExpandBar(sidebar, SWT.NONE);
expandBar.setBackground(SWTResourceManager.getColor(112, 128, 144));
expandBar.setTouchEnabled(true);
GridData gd_expandBar = new GridData(SWT.CENTER, SWT.CENTER, false,
false, 1, 1);
gd_expandBar.heightHint = 619;
expandBar.setLayoutData(gd_expandBar);
ExpandItem xpndtmEmployeeDetails = new ExpandItem(expandBar, SWT.NONE,
0);
xpndtmEmployeeDetails.setExpanded(true);
xpndtmEmployeeDetails.setImage(SWTResourceManager.getImage(
Dashboard.class, "/resource/icons/employee.png"));
xpndtmEmployeeDetails.setText("Employee Details");
Composite expandbarComposite = new Composite(expandBar, SWT.NONE);
xpndtmEmployeeDetails.setControl(expandbarComposite);
xpndtmEmployeeDetails.setHeight(100);
expandbarComposite.setLayout(new FormLayout());
expandbarComposite.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WIDGET_BACKGROUND));
Button btnAddNew = new Button(expandbarComposite, SWT.NONE);
btnAddNew.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/ic_add.png"));
FormData fd_btnAddNew = new FormData();
fd_btnAddNew.right = new FormAttachment(100, -10);
fd_btnAddNew.left = new FormAttachment(0, 38);
btnAddNew.setLayoutData(fd_btnAddNew);
btnAddNew.setText("Add New");
Button btnEdit = new Button(expandbarComposite, SWT.FLAT | SWT.CENTER);
btnEdit.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
}
});
btnEdit.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/edit.png"));
fd_btnAddNew.bottom = new FormAttachment(btnEdit, -6);
FormData fd_btnEdit = new FormData();
fd_btnEdit.left = new FormAttachment(btnAddNew, 0, SWT.LEFT);
fd_btnEdit.right = new FormAttachment(btnAddNew, 0, SWT.RIGHT);
fd_btnEdit.bottom = new FormAttachment(100);
btnEdit.setLayoutData(fd_btnEdit);
btnEdit.setText("Edit");
/**
* View Employees Button
* **/
Button btnViewEmployees = new Button(expandbarComposite, SWT.NONE);
btnViewEmployees.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
tabFolder.setSelection(empTab);
}
});
FormData fd_btnViewEmployees = new FormData();
fd_btnViewEmployees.right = new FormAttachment(btnAddNew, 0, SWT.RIGHT);
fd_btnViewEmployees.top = new FormAttachment(0, 6);
fd_btnViewEmployees.left = new FormAttachment(btnAddNew, 0, SWT.LEFT);
btnViewEmployees.setLayoutData(fd_btnViewEmployees);
btnViewEmployees.setText("View Employees");
ExpandItem xpndtmWorkSiteDetails = new ExpandItem(expandBar, SWT.NONE);
xpndtmWorkSiteDetails.setImage(SWTResourceManager.getImage(
Dashboard.class, "/resource/icons/worksite.png"));
xpndtmWorkSiteDetails.setText("Work Site Details");
Composite composite = new Composite(expandBar, SWT.NONE);
xpndtmWorkSiteDetails.setControl(composite);
xpndtmWorkSiteDetails.setHeight(100);
composite.setLayout(new FormLayout());
Button btnAddNew_1 = new Button(composite, SWT.NONE);
btnAddNew_1.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/ic_add.png"));
FormData fd_btnAddNew_1 = new FormData();
fd_btnAddNew_1.right = new FormAttachment(100, -10);
fd_btnAddNew_1.top = new FormAttachment(0, 37);
btnAddNew_1.setLayoutData(fd_btnAddNew_1);
btnAddNew_1.setText("Add New");
Button btnEdit_1 = new Button(composite, SWT.NONE);
fd_btnAddNew_1.bottom = new FormAttachment(btnEdit_1, -6);
fd_btnAddNew_1.left = new FormAttachment(btnEdit_1, 0, SWT.LEFT);
btnEdit_1.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/edit.png"));
FormData fd_btnEdit_1 = new FormData();
fd_btnEdit_1.left = new FormAttachment(0, 40);
fd_btnEdit_1.right = new FormAttachment(100, -10);
fd_btnEdit_1.bottom = new FormAttachment(100);
btnEdit_1.setLayoutData(fd_btnEdit_1);
btnEdit_1.setText("Edit");
Button btnViewWorks = new Button(composite, SWT.NONE);
FormData fd_btnViewWorks = new FormData();
fd_btnViewWorks.top = new FormAttachment(0, 6);
fd_btnViewWorks.left = new FormAttachment(btnAddNew_1, 0, SWT.LEFT);
fd_btnViewWorks.right = new FormAttachment(100, -10);
btnViewWorks.setLayoutData(fd_btnViewWorks);
btnViewWorks.setText("View Works");
ExpandItem xpndtmPaymentDetails = new ExpandItem(expandBar, SWT.NONE);
xpndtmPaymentDetails.setImage(SWTResourceManager.getImage(
Dashboard.class, "/resource/icons/payments.png"));
xpndtmPaymentDetails.setText("Payment Details");
Composite composite_1 = new Composite(expandBar, SWT.NONE);
xpndtmPaymentDetails.setControl(composite_1);
xpndtmPaymentDetails.setHeight(100);
composite_1.setLayout(new FormLayout());
Button btnAddNew_2 = new Button(composite_1, SWT.NONE);
btnAddNew_2.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/ic_add.png"));
FormData fd_btnAddNew_2 = new FormData();
fd_btnAddNew_2.left = new FormAttachment(0, 44);
btnAddNew_2.setLayoutData(fd_btnAddNew_2);
btnAddNew_2.setText("Add New");
Button btnEditPayments = new Button(composite_1, SWT.NONE);
fd_btnAddNew_2.bottom = new FormAttachment(btnEditPayments, -6);
fd_btnAddNew_2.right = new FormAttachment(btnEditPayments, 0, SWT.RIGHT);
btnEditPayments.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/edit.png"));
FormData fd_btnEditPayments = new FormData();
fd_btnEditPayments.left = new FormAttachment(0, 44);
fd_btnEditPayments.right = new FormAttachment(100, -10);
fd_btnEditPayments.bottom = new FormAttachment(100);
btnEditPayments.setLayoutData(fd_btnEditPayments);
btnEditPayments.setText("Edit Payments");
Button btnViewPayments = new Button(composite_1, SWT.NONE);
FormData fd_btnViewPayments = new FormData();
fd_btnViewPayments.right = new FormAttachment(btnAddNew_2, 0, SWT.RIGHT);
fd_btnViewPayments.top = new FormAttachment(0, 6);
fd_btnViewPayments.left = new FormAttachment(btnAddNew_2, 0, SWT.LEFT);
btnViewPayments.setLayoutData(fd_btnViewPayments);
btnViewPayments.setText("View Payments");
ExpandItem xpndtmOurTools = new ExpandItem(expandBar, SWT.NONE);
xpndtmOurTools.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/tools.png"));
xpndtmOurTools.setText("Tools Available");
Composite composite_3 = new Composite(expandBar, SWT.NONE);
xpndtmOurTools.setControl(composite_3);
xpndtmOurTools.setHeight(80);
composite_3.setLayout(new FormLayout());
Button btnViewTools = new Button(composite_3, SWT.NONE);
FormData fd_btnViewTools = new FormData();
fd_btnViewTools.top = new FormAttachment(0, 10);
fd_btnViewTools.right = new FormAttachment(100, -10);
fd_btnViewTools.left = new FormAttachment(100, -131);
btnViewTools.setLayoutData(fd_btnViewTools);
btnViewTools.setText("View Tools");
Button btnAddNew_3 = new Button(composite_3, SWT.NONE);
btnAddNew_3.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
System.out.println("Add tools");
}
});
FormData fd_btnAddNew_3 = new FormData();
fd_btnAddNew_3.right = new FormAttachment(btnViewTools, 0, SWT.RIGHT);
fd_btnAddNew_3.top = new FormAttachment(btnViewTools, 6);
fd_btnAddNew_3.left = new FormAttachment(btnViewTools, 0, SWT.LEFT);
btnAddNew_3.setLayoutData(fd_btnAddNew_3);
btnAddNew_3.setText("Add New");
ExpandItem xpndtmReports = new ExpandItem(expandBar, SWT.NONE);
xpndtmReports.setExpanded(true);
xpndtmReports.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/reports.png"));
xpndtmReports.setText("Reports");
xpndtmReports.setHeight(80);
Composite composite_2 = new Composite(expandBar, SWT.NONE);
xpndtmReports.setControl(composite_2);
composite_2.setLayout(new FormLayout());
Button btnGenerateReport = new Button(composite_2, SWT.NONE);
btnGenerateReport.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/gen_report.png"));
FormData fd_btnGenerateReport = new FormData();
fd_btnGenerateReport.top = new FormAttachment(0, 10);
fd_btnGenerateReport.left = new FormAttachment(0, 10);
fd_btnGenerateReport.right = new FormAttachment(100, -28);
btnGenerateReport.setLayoutData(fd_btnGenerateReport);
btnGenerateReport.setText("Generate Report");
xpndtmReports.setHeight(100);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
Can anyone suggest a solution. Thanks in advance.
And Finally I've figured the problem in the code. The composite in the base layout was assigned with the StackLayout as follows.
Composite content = new Composite(shell, SWT.BORDER);
content.setLayout(new StackLayout());
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
When I changed the StackLayout to GridLayout, I got a working solution.
Composite content = new Composite(shell, SWT.BORDER);
content.setLayout(new GridLayout(1, false));
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

The barchart(jfreechart) is displayed as small icon on a composite in a view of Eclipse RCP plugin

The barchart is displayed as small icon on a composite of a view in Eclipse RCP plugin. The chart does not cover the entire composite which should be the actual case. what additional setting needs to be made in code to display the graph on entire composite
Following is the code for displaying the bargraph
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
if(flag == false){
frame.dispose();
}
frame = new ChartComposite(barchartComposite,SWT.NONE,chart,true);
frame.setLayoutData(new GridData(GridData.FILL_BOTH));
frame.setChart(chart);
frame.forceRedraw();
frame.pack();
frame.setVisible(true);
flag= false;
The method createDataset() generates the data for the barchart and method createChart(dataset) generates the barchart.
THE COMPLETE SOURCE CODE FOR DISPLAY OF VIEW
public class BarChartDisplay extends ViewPart {
Text searchfield = null;
String path = SelectDataBase.path;
public static int error=0;
public static int info=0;
public static int critical=0;
public static int warning=0;
ChartComposite frame;
boolean flag=true;
public BarChartDisplay() {
}
#Override
public void createPartControl(Composite parent) {
//Composite A:
final Composite mainComposite = new Composite(parent, SWT.NONE);
GridData mainLayoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
mainLayoutData.horizontalSpan = 1;
mainComposite.setLayoutData(mainLayoutData);
GridLayout outerLayout = new GridLayout();
outerLayout.marginTop = 30;
outerLayout.marginLeft = 20;
outerLayout.marginRight = 20;
mainComposite.setLayout(new GridLayout(1, false));
//Composite B:
final Composite selectComposite = new Composite(mainComposite, SWT.NONE);
selectComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
selectComposite.setLayout(new GridLayout(4, false));
//Composite C:
final Composite barchartComposite = new Composite(mainComposite, SWT.NONE);
barchartComposite.setLayout(new GridLayout(1, false));
barchartComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final CalendarCombo ccombo = new CalendarCombo(selectComposite, SWT.READ_ONLY | SWT.FLAT);
GridData layoutDataCal = new GridData(150, 40);
ccombo.computeSize(SWT.DEFAULT, SWT.DEFAULT);
ccombo.showCalendar();
ccombo.setLayoutData(layoutDataCal);
org.eclipse.swt.widgets.Button button = new org.eclipse.swt.widgets.Button(selectComposite, SWT.PUSH);
button.setText("Go");
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
switch (e.type) {
case SWT.Selection:
error = 0;
info = 0;
warning = 0;
critical = 0;
DB db = new DB();
Connection conn = null;
conn = db.ConnTable(path);
Statement statement;
try {
statement = conn.createStatement();
String query = null;
String textfielddata = ccombo.getDateAsString();
System.out.println(textfielddata);
query = "select priority from log where creation_date = '"+ textfielddata +"'";
System.out.println(query);
ResultSet rs = statement.executeQuery(query);
while (rs.next()) {
int prioritydata = rs.getInt("priority");
if (prioritydata == 1)
error++;
else if (prioritydata == 2)
info++;
else if (prioritydata == 3)
warning++;
else if (prioritydata == 4)
critical++;
}
} catch (SQLException er) {
er.printStackTrace();
}
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
if(flag == false){
frame.dispose();
}
frame = new ChartComposite(barchartComposite,SWT.BORDER,chart,true);
frame.setLayoutData(new GridData(GridData.FILL_BOTH));
frame.setChart(chart);
frame.forceRedraw();
frame.pack();
frame.setVisible(true);
flag= false;
break;
}
}
});
}
/**
* Returns a sample dataset.
*
* #return The dataset.
*/
private CategoryDataset createDataset() {
// row keys...
final String series1 = "First";
// column keys...
final String category1 = "error";
final String category2 = "info";
final String category3 = "warning";
final String category4 = "critical";
// create the dataset...
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(error, series1, category1);
dataset.addValue(info, series1, category2);
dataset.addValue(warning, series1, category3);
dataset.addValue(critical, series1, category4);
return dataset;
}
/**
* Creates a sample chart.
*
* #param dataset the dataset.
*
* #return The chart.
*/
private JFreeChart createChart(final CategoryDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createBarChart(
"Priority BarChart", // chart title
"priority", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips?
false // URLs?
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
// set the background color for the chart...
chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
final CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
// set the range axis to display integers only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// disable bar outlines...
final BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
// set up gradient paints for series...
final GradientPaint gp0 = new GradientPaint(
0.0f, 0.0f, Color.blue,
0.0f, 0.0f, Color.lightGray
);
renderer.setSeriesPaint(0, gp0);
final CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
);
// OPTIONAL CUSTOMISATION COMPLETED.
return chart;
}
#Override
public void setFocus() {
}
}
You have to modify the parent composite named barchartComposite.
parent.setLayout(new GridLayout(1, false));
parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final CategoryDataset dataset = createDataset();
final JFreeChart chart = createChart(dataset);
Composite barchartComposite = new Composite(parent, SWT.NONE);
barchartComposite.setLayout(new GridLayout(1, false));
barchartComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
ChartComposite frame = new ChartComposite(barchartComposite, SWT.BORDER,chart,true);
frame.setLayoutData(new GridData(GridData.FILL_BOTH));
You have to make sure that barchartcomposite grabs the wohle space of the parent composite. This can be achieved with GridLayout and GridData.
You can find a very useful tutorial about all SWT layouts here:
Understanding Layouts in SWT