Hi Chris,
I'm doing the casts inside my global "TextOperations" class that
handles basically all the text fields in the application (they register
themselves with the global class when instantiated). Then the global class
adds itself to the ComponentClassListener list to listen for focus changes and
adds itself to the (three different kinds of) selection listeners of each
component type. Then the class has methods for "cut", "copy", "paste", etc.
that are invoked from the Actions objects registered to the menu/toolbar items.
So, the ugliness that this could fix would be each of the methods having to
test the focused component's class and casting as appropriate before calling
the correct method. Also, it would be nice to change the
TextInputSelectionListener (and TextAreaSelectionListener, etc.) to a single
TextSelectionListener(TextOperator textField, ...).
Anyway, the patch below implements the basic "TextOperator" interface
(not the change to SelectionListener). Nothing but the interface class itself,
an "implements" clause and some "@Override" in TextInput. TextArea and
TextPane would be equally simple. This patch builds BTW.
Index: wtk/src/org/apache/pivot/wtk/TextOperator.java
===================================================================
--- wtk/src/org/apache/pivot/wtk/TextOperator.java (revision 0)
+++ wtk/src/org/apache/pivot/wtk/TextOperator.java (revision 0)
@@ -0,0 +1,15 @@
+package org.apache.pivot.wtk;
+
+public interface TextOperator
+{
+ public void cut();
+ public void copy();
+ public void paste();
+ public void selectAll();
+ public void clearSelection();
+ public int getSelectionLength();
+ public String getSelectedText();
+ public void undo();
+ public void redo();
+}
+
Index: wtk/src/org/apache/pivot/wtk/TextInput.java
===================================================================
--- wtk/src/org/apache/pivot/wtk/TextInput.java (revision 1156290)
+++ wtk/src/org/apache/pivot/wtk/TextInput.java (working copy)
@@ -28,7 +28,7 @@
/**
* A component that allows a user to enter a single line of unformatted text.
*/
-public class TextInput extends Component {
+public class TextInput extends Component implements TextOperator {
/**
* Text input skin interface. Text input skins are required to implement
* this.
@@ -486,6 +486,7 @@
* Places any selected text on the clipboard and deletes it from
* the text input.
*/
+ @Override
public void cut() {
copy();
removeText(selectionStart, selectionLength);
@@ -494,6 +495,7 @@
/**
* Places any selected text on the clipboard.
*/
+ @Override
public void copy() {
// Copy selection to clipboard
String selectedText = getSelectedText();
@@ -508,6 +510,7 @@
/**
* Inserts text from the clipboard into the text input.
*/
+ @Override
public void paste() {
Manifest clipboardContent = Clipboard.getContent();
@@ -532,6 +535,7 @@
}
}
+ @Override
public void undo() {
int n = editHistory.getLength();
if (n > 0) {
@@ -540,6 +544,11 @@
}
}
+ @Override
+ public void redo() {
+ throw new UnsupportedOperationException();
+ }
+
private void addHistoryItem(Edit edit) {
editHistory.add(edit);
@@ -564,6 +573,7 @@
* @return
* The length of the selection; may be <tt>0</tt>.
*/
+ @Override
public int getSelectionLength() {
return selectionLength;
}
@@ -631,6 +641,7 @@
/**
* Selects all text.
*/
+ @Override
public void selectAll() {
setSelection(0, characters.length());
}
@@ -638,6 +649,7 @@
/**
* Clears the selection.
*/
+ @Override
public void clearSelection() {
setSelection(0, 0);
}
@@ -648,6 +660,7 @@
* @return
* A new string containing a copy of the text in the selected range.
*/
+ @Override
public String getSelectedText() {
return getText(selectionStart, selectionStart + selectionLength);
}
Roger Whitcomb | Architect, Engineering | [email protected] | Ingres |
500 Arguello Street | Suite 200 | Redwood City | CA | 94063 | USA +1
650-587-5596 | fax: +1 650-587-5550
-----Original Message-----
From: Chris Bartlett [mailto:[email protected]]
Sent: Friday, August 12, 2011 3:18 PM
To: [email protected]
Subject: Re: What about a TextOperator interface?
(Where are you doing the casts? Are you using Actions to provide the
functionality?)
I like the idea in principle. While the features of similar Pivot
Components have generally been kept in sync as they have evolved, it
might be time to start considering things like this to make
common/similar functionality easier to use.
Selection handling in some Components is also similar. I have my own
classes to manage item selections/deletion/reselection transparently
across ListViews & TableViews (and soon GridViews too).
I create a new instance of my SelectionSnapshot class, passing the
relevant Component to the constructor and then have a common set of
utility methods for expanding/clearing/querying/reapplying selections,
which work on both ListView and TableView. I took this approach
because it was quick to develop, easy to maintain outside of the Pivot
code base, and didn't require lots of discussion on the mailing lists.
The same sort of approach might be possible with the text based
methods you mention, but it does sound like it would be cleaner to
have the classes implement a common interface as proposed.
Is this something that you could quickly hack together a partial
prototype for and then post a patch for review? It needn't include
all methods, just enough to show the sort of changes to expect.
Chris
On 13 August 2011 04:48, Roger L. Whitcomb <[email protected]> wrote:
> I'm implementing Cut/Copy/Paste, etc. in our UI and it is fairly awkward
> to have to check for TextInput, TextArea and TextPane on every operation
> and do the casts three times for each. So, I was wondering what
> everyone thought of a "TextOperator" interface that these three classes
> implement that consists of the relevant methods:
>
>
>
> public void cut();
>
> public void copy();
>
> public void paste();
>
> public void undo();
>
> public void redo();
>
> public void clearSelection();
>
> public void selectAll();
>
> public int getSelectedLength(); // Necessary for
> determining enabling of cut/copy
>
> public String getSelectedText();
>
>
>
> This way I could cut my work to 1/3 because I simply have to cast to
> "TextOperator" and do the operations (checking for "instanceof" also).
>
>
>
> So, what does everyone think? It would be relatively trivial to
> implement the change (add the interface file and add "implements..."
> clause to the three classes (I think, not having prototyped it yet).
>
>
>
>
>
>
>
> Roger Whitcomb
>
> Architect, Engineering
>
> Ingres Corporation
>
> [email protected] <mailto:[email protected]>
>
>
>
> PHONE +1 650.587.5596
>
> FAX +1 650.587.5550
>
>
>
> www.ingres.com <http://www.ingres.com/>
>
>
>
> This transmission is confidential and intended solely for the use of the
> recipient named above. It may contain confidential, proprietary, or
> legally privileged information. If you are not the intended recipient,
> you are hereby notified that any unauthorized review, use, disclosure or
> distribution is strictly prohibited. If you have received this
> transmission in error, please contact the sender by reply e-mail and
> delete the original transmission and all copies from your system.
>
>
>
>