2017-12-17

35: How to Listen to Spread Sheet Cells Selection Events and (Possibly) Trace the Current Cell Movement, Part One

<The previous article in this series | The table of contents of this series | The next article in this series>

Main Body START

How to Listen to Spread Sheet Cells Selection Events and (Possibly) Trace the Current Cell Movement in LibreOffice or Apache OpenOffice Is Explained

About: UNO (Universal Network Objects)

About: LibreOffice

About: Apache OpenOffice

About: Java Programming Language

Necessity for Listening to Cells Selection Events and Clarification Between Selected Cells and the Current Cell

Here are -Hypothesizer and -Rebutter sitting in front of a computer screen in a room on a brook among some mountains on the Bias planet.

-Hypothesizer

When we have created a spread sheet cell editor, we encountered an issue of how to detect changes of cell selection on spread sheets.

-Rebutter

Ah, we wanted our spread sheet cell editor to be able to automatically attach to the current cell as we select a cell on a spread sheet.

-Hypothesizer

From the viewpoint of functionality, we can do without reacting to such events, just requiring users to push a button in order to reattach the cell editor to the currently selected cell, but from the viewpoint of user experience, such a requirement can be frustrating.

-Rebutter

There are some use cases in which we don't want the cell editor to be linked to the current cell, but when the user is in the mode, "the cell editor should be linked to the current cell", . . .

-Hypothesizer

. . . that requirement is frustrating. I mean, in the user's head, they are already linked, but the program refuses to understand that expectation. The user thinks, "Why isn't my intention understood?" He or she feels alienated.

-Rebutter

I don't know whether someone has a sense of alienation just using a piece of software . . .

-Hypothesizer

At least, I feel alienated.

-Rebutter

OK. Talk only of your feelings. Don't assume you know what others feel.

-Hypothesizer

I feel being abused.

-Rebutter

'Abused' will be an overstatement . . .

-Hypothesizer

That's my feeling.

-Rebutter

Well, I can't deny what you feel, but I can say, "You are cranky".

-Hypothesizer

. . . I know.

-Rebutter

By the way, we have been using two terms, 'the current cell' and 'the selected cell', interchangeably, but in fact, multiple cells can be selected at the same time, right?

-Hypothesizer

Ah, . . . that hits us close to home . . . us who are proud of accurate expressions . . .

-Rebutter

Don't be proud of that, or of anything. Just be a student of accurate expressions, or of something.

-Hypothesizer

. . . Yes. All we have to do and can do is to do our best, admit mistakes, and improve . . . So, let's clarify things here.

First, I admit here that I was thinking only of single cell selections. Certainly, I knew that we could select multiple cells, but that wasn't in the scope of our discussions. When we use only simple single cell selections, the selected cell is the current cell. However, in general, there isn't such thing as 'the selected cell', but are multiple selected cells.

-Rebutter

So, what is 'the current cell'?

-Hypothesizer

I don't know whether 'the current cell' is a term universally agreed upon, but I am using the term as the cell that has the focus of key inputs.

-Rebutter

So, even if there are multiple selected cells, there is only one current cell?

-Hypothesizer

That's my understanding.

-Rebutter

Is it also your understanding that the current cell may not be one of the selected cells?

-Hypothesizer

Well, . . . as I far as I understand, it doesn't seem to be my understanding.

-Rebutter

Open a spread sheet document.

-Hypothesizer opens a spread sheet document.

-Rebutter

Select 3x3 rectangle of cells.

-Hypothesizer

Why 3x3?

-Rebutter

. . . It doesn't particularly have to be 3x3; I just thought, without the size being specified, someone who got the direction would be at a loss . . .

-Hypothesizer

OK. 3x3.

-Hypothesizer selects a 3x3 rectangle of cells.

-Rebutter

The top leftmost cell is the current cell, right?

-Hypothesizer

Right. That cell is enclosed in the black borders.

-Rebutter

Now, click the center cell with the control key pushed.

-Hypothesizer

Ah, . . .

How to Listen to Cells Selection Events

The same with the previous scene.

-Hypothesizer

In fact, registering a listener of spread sheet cells selection events, itself, is easy.

-Rebutter

'Itself'?

-Hypothesizer

Well, there is a problem, which I will talk about eventually.

The controller has the UNO interface, 'XSelectionSupplier', to which we can register an events listener of 'XSelectionChangeListener'.

-Rebutter

Does 'the controller' mean that controller we used before?

-Hypothesizer

Yes, I mean that controller.

'XSelectionChangeListener' has a method, 'selectionChanged', which is called when the cells selection has changed.

-Rebutter

The selection information is passed as the argument of the method, I presume?

-Hypothesizer

Actually, it isn't directly passed, but the event source is passed.

-Rebutter

What is the event source, exactly?

-Hypothesizer

The controller itself.

-Rebutter

Well, so we are supposed to get the selection information from the controller?

-Hypothesizer

That's my guess.

-Rebutter

Well, it's fine because we can get the selection information from the controller.

-Hypothesizer

A problem is, the listener method is called four times when we click a cell on a spread sheet.

-Rebutter

. . . Why?

-Hypothesizer

I don't know. I also noted that when we move the selection by pushing an arrow key, the listener method is called only once; when the selection change is caused by an activation of a spread sheet, the listener method is called twice.

-Rebutter

. . . Why?

-Hypothesizer

I don't know.

Anyway, as we don't want to unnecessarily react multiple times, we will have to have a way to ignore unnecessary events.

-Rebutter

I guess so.

-Hypothesizer

My plan is to save the last selection information, and react only when the selection information is changed compared with the last selection information.

-Rebutter

Hmm, in what form will you save the last selection information?

-Hypothesizer

The selection can be a single cell, a single cells range, or a collection of cells ranges, and we will identify a cell by its row and column indexes, a cells range by its top row, leftmost column, bottom row, and rightmost column indexes, and a collection of cells ranges by the identities of all of its element cells ranges.

-Rebutter

I guess, a selection can be split into multiple cells ranges because the selection may not consist of a consecutive area?

-Hypothesizer

It's not just a matter of consecutiveness. As a cells range is really a cells rectangle, if a selection isn't a rectangle, it will be split into rectangles.

-Rebutter

Does the splitting of a selection into cells ranges depend only on the shape of the selection?

-Hypothesizer

Ah, you seem to be asking whether it depends on the formation process of the selection . . .

-Rebutter

I seem to be asking that. For example, if a selection was first consisted of two rectangles, and is made a big rectangle by adding some cells, will the selection become a single cells range?

-Hypothesizer

Probably . . .. I don't know for sure, but that won't matter in this context.

How to Trace the Current Cell Movement (Not Found, Actually)

The same with the previous scene.

-Hypothesizer

Tracing the current cell movement is a problem . . .

-Rebutter

How is it a problem?

-Hypothesizer

I can't find any listener of such events.

-Rebutter

So, . . . did you give up?

-Hypothesizer

I didn't particularly give up; I was persuing a possibility . . .

-Rebutter

Which is?

-Hypothesizer

I thought that the current cell might have a property value or something that distinguishes itself as the current cell . . .

-Rebutter

The existence of such a property value or something aside, isn't it too resource intensive to find a single cell among so many cells?

-Hypothesizer

Ah, in that respect, it seems unpromising, or I would say, unrealistic.

-Rebutter

You will have to search the spread sheet or the controller for such a property value.

-Hypothesizer

Actually, I did, and couldn't find one. I also thought that there might be another UNO object accessible to us that provides a way to such a value, but I couldn't find one. You know, the reference documents aren't so intensive . . .

-Rebutter

Well, when a cell is clicked, does the cell always become the current cell?

-Hypothesizer

As far as I know, yes.

-Rebutter

Can we detect such clicks?

-Hypothesizer

Yes. So, limited to that particular case, we can detect the movement of the current cell. However, as you of course know, the current cell can also be moved by key strokes.

-Rebutter

Can we also detect key strokes?

-Hypothesizer

Yes.

-Rebutter

Are there other sources that can move the current cell? Psychokinesis? A strong belief or a will to power?

-Hypothesizer

As far as I am concerned, clicks and key strokes are the only sources.

-Rebutter

Hmm, . . .

-Hypothesizer

Are you suggesting that we could infer the current cell movement from clicks and key strokes, not detecting the current cell movement?

-Rebutter

I'm not suggesting anything.

By the way, what if we keep pushing the return key for a while? Can we trust that the number of key strokes we receive is the number of key strokes the LibreOffice program processes?

-Hypothesizer

Well, . . . I don't know. I don't see a better way than we just try . . .. What are you suggesting?

-Rebutter

None at all.

-Hypothesizer

. . .

Main Body END

<The previous article in this series | The table of contents of this series | The next article in this series>