Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/galite-02GR' :
Browse files Browse the repository at this point in the history
	- Fix : Implement ImageHandler in Vaadin Flow to fix fatal error when loading an image list and show images on the list [APPS-02GR][PR#642]
  • Loading branch information
mgrati committed Nov 5, 2024
2 parents 3609a71 + 3d02092 commit af24e60
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ public JImage(URL location) {
/**
*
*/
public int getWidth() {
public int getImageWidth() {
return getIconWidth();
}

/**
*
*/
public int getHeight() {
public int getImageHeight() {
return getIconHeight();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ interface Image : Serializable {
* Returns the `Image` width
* @return The image width
*/
fun getWidth(): Int
fun getImageWidth(): Int

/**
* Returns the `Image` height
* @return The image height
*/
fun getHeight(): Int
fun getImageHeight(): Int

/**
* Returns the `Image` description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class VColorColumn(title: String,
column,
table,
VConstants.ALG_LEFT,
7,
12,
sortAscending) {

// --------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@
package org.kopi.galite.visual.ui.vaadin.base

import org.kopi.galite.visual.base.Image
import org.kopi.galite.visual.ui.vaadin.common.VImage

/**
* The vaadin implementation of an image model.
*
* @param resource The resource file attached to this image.
*/
class Image(val resource: String) : Image {
class Image(val resource: String = "image", val source: ByteArray? = null) : VImage(), Image {
//---------------------------------------------------
// IMAGE IMPLEMENTATION
//---------------------------------------------------

override fun getWidth(): Int {
override fun getImageWidth(): Int {
return -1
}

override fun getHeight(): Int {
override fun getImageHeight(): Int {
return -1
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
*/
package org.kopi.galite.visual.ui.vaadin.common

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

import com.vaadin.flow.component.Focusable
import com.vaadin.flow.component.html.Image

/**
* A widget that wraps image element.
*/
class VImage : Image(), Focusable<VImage> {
open class VImage : Image(), Focusable<VImage> {
//---------------------------------------------------
// IMPLEMENTATIONS
//---------------------------------------------------
Expand All @@ -41,4 +44,13 @@ class VImage : Image(), Focusable<VImage> {
*/
val isEmpty: Boolean
get() = src == null || "" == src

/**
* Creates the dynamic image name.
* @param baseName The base name.
* @return The dynamic image name.
*/
fun createFileName(baseName: String): String =
baseName + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")) + ".png"
}

Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,20 @@ class ListFilter(private val filterFields: List<TextField>,

override fun test(t: ListTable.ListDialogItem): Boolean {
for (i in model.columns.indices) {
val filterString = if(ignoreCase) filterFields[i].value.lowercase(Locale.getDefault()) else filterFields[i].value
val value = if (ignoreCase) t.getValueAt(i).lowercase(Locale.getDefault()) else t.getValueAt(i)
val item = t.getValueAt(i)

if (onlyMatchPrefix) {
if (!value.startsWith(filterString)) {
return false
}
} else {
if (!value.contains(filterString)) {
return false
if (item is String) {
val filterString = if(ignoreCase) filterFields[i].value.lowercase(Locale.getDefault()) else filterFields[i].value
val value = if (ignoreCase) item.lowercase(Locale.getDefault()) else item

if (onlyMatchPrefix) {
if (!value.startsWith(filterString)) {
return false
}
} else {
if (!value.contains(filterString)) {
return false
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
*/
package org.kopi.galite.visual.ui.vaadin.list

import java.io.ByteArrayInputStream

import org.kopi.galite.visual.form.VListDialog
import org.kopi.galite.visual.list.VImageColumn
import org.kopi.galite.visual.ui.vaadin.base.Image
import org.kopi.galite.visual.ui.vaadin.form.DImageField

import com.vaadin.flow.component.ComponentEventListener
import com.vaadin.flow.component.KeyDownEvent
Expand All @@ -30,6 +35,7 @@ import com.vaadin.flow.component.icon.Icon
import com.vaadin.flow.component.icon.VaadinIcon
import com.vaadin.flow.component.textfield.TextField
import com.vaadin.flow.data.provider.ListDataProvider
import com.vaadin.flow.data.renderer.ComponentRenderer
import com.vaadin.flow.data.value.ValueChangeMode

@CssImport("./styles/galite/list.css")
Expand All @@ -52,10 +58,31 @@ class ListTable(val model: VListDialog) : Grid<ListTable.ListDialogItem>() {

private fun buildColumns() {
for(col in 0 until model.getColumnCount()) {
addColumn {
it.getValueAt(col)
}.setHeader(Span(model.getColumnName(col)))
.setKey(col.toString())
if (model.columns[col] is VImageColumn) {
addColumn(
ComponentRenderer { item: ListDialogItem ->
val image = (item.getValueAt(col) as? Image)

if (image != null) {
image.apply {
element.style["outline"] = "1px solid lightgreen"
width = "100px"
height = "100px"
setBorder(0)
element.setProperty("borderStyle", "none")
setSrc(DImageField.DynamicImageResource(createFileName("image")) { ByteArrayInputStream(image.source) })
}
image
} else {
Image()
}
}
).setHeader(Span(model.getColumnName(col))).setKey(col.toString())
} else {
addColumn {
it.getValueAt(col)
}.setHeader(Span(model.getColumnName(col))).setKey(col.toString())
}
}
}

Expand All @@ -66,19 +93,23 @@ class ListTable(val model: VListDialog) : Grid<ListTable.ListDialogItem>() {
val filterRow = appendHeaderRow()

filterRow.also { element.classList.add("list-filter") }
val filterFields = this.columns.mapIndexed { _, column ->
val filterFields = this.columns.mapIndexed { i, column ->
val cell = filterRow.getCell(column)
val filterField = TextField()
val search = Icon(VaadinIcon.SEARCH)

filterField.setWidthFull()
filterField.suffixComponent = search
filterField.className = "filter-text"
filterField.addValueChangeListener {
(dataProvider as ListDataProvider).refreshAll()
if (this.model.columns[i] !is VImageColumn) {
filterField.addValueChangeListener {
(dataProvider as ListDataProvider).refreshAll()
}

filterField.valueChangeMode = ValueChangeMode.EAGER
} else {
filterField.isReadOnly = true
}

filterField.valueChangeMode = ValueChangeMode.EAGER
cell.setComponent(filterField)
filterField
}
Expand Down Expand Up @@ -122,7 +153,11 @@ class ListTable(val model: VListDialog) : Grid<ListTable.ListDialogItem>() {
width = 0
for (row in 0 until model.count) {
val value = model.columns[col]!!.formatObject(model.getValueAt(row, col)).toString()
width = width.coerceAtLeast(value.length.coerceAtLeast(model.titles[col]!!.length))
width = if (model.columns[col]!! is VImageColumn) {
model.columns[col]!!.width
} else {
width.coerceAtLeast(value.length.coerceAtLeast(model.titles[col]!!.length))
}
}
return 8 * width
}
Expand Down Expand Up @@ -150,8 +185,12 @@ class ListTable(val model: VListDialog) : Grid<ListTable.ListDialogItem>() {
* @param o The object to be formatted.
* @return The formatted property object.
*/
private fun formatObject(o: Any?, col: Int): String {
return model.columns[col]!!.formatObject(o).toString()
private fun formatObject(o: Any?, col: Int): Any?{
return if (model.columns[col]!!.formatObject(o) is Image) {
model.columns[col]!!.formatObject(o)
} else {
model.columns[col]!!.formatObject(o).toString()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

package org.kopi.galite.visual.ui.vaadin.visual

import org.kopi.galite.visual.base.Image
import org.kopi.galite.visual.ui.vaadin.base.Image
import org.kopi.galite.visual.ImageHandler

class VImageHandler : ImageHandler() {
override fun getImage(image: String): Image? = null

override fun getImage(image: ByteArray): Image? = null
override fun getImage(image: ByteArray): Image = Image(source = image)

override fun getURL(image: String): String = "ui/vaadin/$image" // FIXME
}

0 comments on commit af24e60

Please sign in to comment.