-
Notifications
You must be signed in to change notification settings - Fork 47
javax.swing.TextComponent getDocument to scala.swing.document. #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /* | ||
| * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions | ||
| * are met: | ||
| * | ||
| * - Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * | ||
| * - Redistributions in binary form must reproduce the above copyright | ||
| * notice, this list of conditions and the following disclaimer in the | ||
| * documentation and/or other materials provided with the distribution. | ||
| * | ||
| * - Neither the name of Oracle or the names of its | ||
| * contributors may be used to endorse or promote products derived | ||
| * from this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
| * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package scala.swing.examples.tutorials.events | ||
|
|
||
| import scala.swing._ | ||
| import scala.swing.event.{ButtonClicked, SelectionChanged} | ||
| import java.awt.Dimension | ||
| import javax.swing.text.Document | ||
|
|
||
| /** | ||
| * Tutorial: How to Write a List Selection Listener | ||
| * [[http://docs.oracle.com/javase/tutorial/uiswing/events/listselectionlistener.html]] | ||
| * | ||
| * Source code reference: | ||
| * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/events/ListSelectionDemoProject/src/events/ListSelectionDemo.java]] | ||
| * | ||
| * ListSelectionDemo.scala requires no other files. | ||
| */ | ||
| class ListSelectionDemo extends BorderPanel { | ||
| val listData = Array( "one", "two", "three", "four", | ||
| "five", "six", "seven" ) | ||
| val columnNames = Array( "French", "Spanish", "Italian" ) | ||
| val list = new ListView(listData) | ||
| val listPane = new ScrollPane(list) | ||
|
|
||
| val modes = Array( "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", | ||
| "MULTIPLE_INTERVAL_SELECTION") | ||
| val comboBox = new ComboBox(modes) { | ||
| selection.index = 2 | ||
| } | ||
| val controlPane = new FlowPanel() { | ||
| contents += new Label("Selection mode:") | ||
| contents += comboBox | ||
| } | ||
|
|
||
| //Build output area. | ||
| val output = new TextArea(1, 10) { | ||
| editable = false | ||
| } | ||
| val outputPane = new ScrollPane(output) { | ||
| verticalScrollBarPolicy = ScrollPane.BarPolicy.Always | ||
| horizontalScrollBarPolicy = ScrollPane.BarPolicy.AsNeeded | ||
| } | ||
| val listContainer = new GridPanel(1, 1) { | ||
| contents += listPane | ||
| border = Swing.TitledBorder(Swing.EmptyBorder, "List") | ||
| } | ||
| val topHalf = new BoxPanel(Orientation.Horizontal) { | ||
| contents += listContainer | ||
| border = Swing.EmptyBorder(5, 5, 0, 5) | ||
| minimumSize = new Dimension(100, 50) | ||
| preferredSize = new Dimension(100, 110) | ||
| } | ||
|
|
||
| val bottomHalf = new BorderPanel() { | ||
| layout(controlPane) = BorderPanel.Position.North | ||
| layout(outputPane) = BorderPanel.Position.Center | ||
| preferredSize = new Dimension(450, 135) | ||
| } | ||
|
|
||
| //Do the layout. | ||
| val splitPane = new SplitPane(Orientation.Horizontal, topHalf, bottomHalf) | ||
|
|
||
| layout(splitPane) = BorderPanel.Position.Center | ||
|
|
||
| listenTo(comboBox.selection) | ||
| listenTo(list.selection) | ||
|
|
||
| reactions += { | ||
| case SelectionChanged(`comboBox`) => | ||
| val newMode = comboBox.selection.item | ||
| newMode match { | ||
| case "SINGLE_SELECTION" => | ||
| list.selection.intervalMode = ListView.IntervalMode.Single | ||
| case "SINGLE_INTERVAL_SELECTION" => | ||
| list.selection.intervalMode = ListView.IntervalMode.SingleInterval | ||
| case "MULTIPLE_INTERVAL_SELECTION" => | ||
| list.selection.intervalMode = ListView.IntervalMode.MultiInterval | ||
| } | ||
| output.append("----------" + "Mode: " + newMode + "----------" + "\n") | ||
| case SelectionChanged(`list`) => | ||
| val firstIndex = list.selection.leadIndex | ||
| val lastIndex = list.selection.anchorIndex // anchor = last? | ||
| val isAdjusting = list.selection.adjusting | ||
| output.append("Event for indexes " + firstIndex + " - " + lastIndex + | ||
| "; isAdjusting is " + isAdjusting + "; selected indexes:") | ||
| if (list.selection.indices.isEmpty) { | ||
| output.append(" <none>") | ||
| } | ||
| else { | ||
| // Find out which indexes are selected. | ||
| for (i <- list.selection.indices) { | ||
| output.append(" " + i) | ||
| } | ||
| } | ||
| output.append("\n") | ||
| output.caret.position = output.document.getLength() | ||
| } | ||
| } | ||
|
|
||
| object ListSelectionDemo extends SimpleSwingApplication { | ||
| //Create and set up the window. | ||
| lazy val top = new MainFrame { | ||
| title = "ListSelectionDemo" | ||
| contents = new ListSelectionDemo() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions | ||
| * are met: | ||
| * | ||
| * - Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * | ||
| * - Redistributions in binary form must reproduce the above copyright | ||
| * notice, this list of conditions and the following disclaimer in the | ||
| * documentation and/or other materials provided with the distribution. | ||
| * | ||
| * - Neither the name of Oracle or the names of its | ||
| * contributors may be used to endorse or promote products derived | ||
| * from this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | ||
| * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
| * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package scala.swing.examples.tutorials.components | ||
|
|
||
| import scala.swing._ | ||
| import scala.swing.event.EditDone | ||
|
|
||
| /** | ||
| * Tutorials: How to Use Text Fields | ||
| * [[http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html]] | ||
| * How to Use Text Areas | ||
| * [[http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html]] | ||
| * | ||
| * Source code references: | ||
| * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TextDemoProject/src/components/TextDemo.java]] | ||
| * | ||
| * TextDemo.scala requires no other files. | ||
| */ | ||
| class TextDemo extends GridBagPanel { | ||
| private val newline = "\n" | ||
| val textField = new TextField(20) | ||
| val textArea = new TextArea(5, 20) { | ||
| editable = false | ||
| } | ||
| val scrollPane = new ScrollPane(textArea) | ||
|
|
||
| //Add Components to this panel. | ||
| val c: Constraints = new Constraints() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this look better There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not obvious which form is simpler. Typical uses of grid The original source code changes from c.gridwidth = java.awt.GridBagConstraints.REMAINDER to val c1: Constraints = new Constraints() { val c2: Constraints = new Constraints() { What do you think?
|
||
|
|
||
| c.gridwidth = java.awt.GridBagConstraints.REMAINDER | ||
| c.fill = GridBagPanel.Fill.Horizontal | ||
| layout(textField) = c | ||
| c.fill = GridBagPanel.Fill.Both | ||
| c.weightx = 1.0 | ||
| c.weighty = 1.0 | ||
| layout(scrollPane) = c | ||
|
|
||
| listenTo(textField) | ||
| reactions += { | ||
| case EditDone(`textField`) => | ||
| textArea.text += textField.text + newline | ||
| textField.selectAll() | ||
| //Make sure the new text is visible, even if there | ||
| //was a selection in the text area. | ||
| textArea.caret.position = textArea.document.getLength() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document should be wrapped so that this reads |
||
| } | ||
| } | ||
|
|
||
| object TextDemo extends SimpleSwingApplication { | ||
| lazy val top = new MainFrame() { | ||
| title = "TextDemo" | ||
| contents = new TextDemo() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ class TextComponent extends Component with Publisher { | |
| override lazy val peer: JTextComponent = new JTextComponent with SuperMixin {} | ||
| def text: String = peer.getText | ||
| def text_=(t: String) = peer.setText(t) | ||
| def document: Document = peer.getDocument() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
|
|
||
| class Caret extends Publisher { | ||
| def dot: Int = peer.getCaret.getDot | ||
|
|
@@ -48,7 +49,7 @@ class TextComponent extends Component with Publisher { | |
| def color_=(c: Color) = peer.setCaretColor(c) | ||
| def position: Int = peer.getCaretPosition | ||
| def position_=(p: Int) = peer.setCaretPosition(p) | ||
|
|
||
| peer.addCaretListener { | ||
| new CaretListener { | ||
| def caretUpdate(e: CaretEvent) { publish(CaretUpdate(TextComponent.this)) } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document should be wrapped so that this reads
output.document.lengthThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a lot of wrapper work to be done. Table has at least one model
that needs this treatment and probably more. How does the work get
allocated among the contributors?
On 10/20/2014 06:16 AM, Andy Hicks wrote:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, its a lots of work, I guess thats why nobody has done it. :-), and a lot of other wrappers.
As for allocation, its whats you're interested in or probably more traditionally somebody has found has a scala-swing project and found something missing. (so far we haven't had 2 different people working on the same part, but an email to the google group could be a good idea).
It may be that this gets put on hold and 'attached' to an issue saying text.Document needs implementing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started on Document and the classes it uses.
The Document has a forest (multiple trees) of Elements attached (the
tree is for HTML). The peer relationships among Element nodes will need
to be kept up to date with events and listeners. I have not started
that part yet.
A Bugzilla issue sounds like a good idea.
Allan
On 10/23/2014 06:38 AM, Andy Hicks wrote:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to raise it in a bigger issue tracking system then there you can add it to issues.scala-lang.org and assign it to me. However given the code is in github I have a preference to trying to link up the issues and the code.
If you do have a go at this then you should remeber that you are trying to make this easy for a scala dev to use it, so there may not be a 1-to-1 mapping between java functions and scala-swing.
As well as all the issues you mentioned we could add 'should addDocumentListener()' be more a stream/reactive system and not just a renaming wrapper to the existing code. Probably a lot of experimentation with different styles may be needed to come up with a good one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are on the same page. I have a preliminary version up and running
with enough mistakes to make it a throw-away system. A design based on
the experience will go into github bug report shortly.
Allan
On 10/23/2014 01:55 PM, Andy Hicks wrote: