"Fossies" - the Fresh Open Source Software Archive

Member "groovy-3.0.9/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ButtonOrTextEditor.groovy" (3 Sep 2021, 2342 Bytes) of package /linux/misc/apache-groovy-src-3.0.9.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "ButtonOrTextEditor.groovy": 3.0.8_vs_3.0.9.

    1 /*
    2  *  Licensed to the Apache Software Foundation (ASF) under one
    3  *  or more contributor license agreements.  See the NOTICE file
    4  *  distributed with this work for additional information
    5  *  regarding copyright ownership.  The ASF licenses this file
    6  *  to you under the Apache License, Version 2.0 (the
    7  *  "License"); you may not use this file except in compliance
    8  *  with the License.  You may obtain a copy of the License at
    9  *
   10  *    http://www.apache.org/licenses/LICENSE-2.0
   11  *
   12  *  Unless required by applicable law or agreed to in writing,
   13  *  software distributed under the License is distributed on an
   14  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15  *  KIND, either express or implied.  See the License for the
   16  *  specific language governing permissions and limitations
   17  *  under the License.
   18  */
   19 package groovy.console.ui
   20 
   21 import groovy.transform.CompileStatic
   22 
   23 import javax.swing.AbstractCellEditor
   24 import javax.swing.JButton
   25 import javax.swing.JComponent
   26 import javax.swing.JTable
   27 import javax.swing.JTextArea
   28 import javax.swing.table.TableCellEditor
   29 import java.awt.Component
   30 import java.awt.event.ActionListener
   31 import java.awt.event.FocusListener
   32 
   33 /**
   34  * A table cell editor that will return a button automatically if it is the cell value,
   35  * a text field if the value exists, or null otherwise (non editable cell).
   36  * This hack allows to interact with buttons in a cell.
   37  */
   38 @CompileStatic
   39 class ButtonOrTextEditor extends AbstractCellEditor implements TableCellEditor {
   40     /** The Swing component being edited. */
   41     protected JComponent editorComponent
   42 
   43     @Override
   44     Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
   45         if (value instanceof JButton) {
   46             editorComponent = value
   47             ((JButton) editorComponent).addActionListener({ e -> fireEditingStopped() } as ActionListener)
   48         } else if (value instanceof JTextArea) {
   49             editorComponent = value
   50         } else if (value) {
   51             editorComponent = new JTextArea(value.toString())
   52             editorComponent.addFocusListener({ e -> fireEditingCanceled() } as FocusListener)
   53         } else {
   54             editorComponent = null
   55         }
   56         editorComponent
   57     }
   58 
   59     @Override
   60     Object getCellEditorValue() {
   61         editorComponent
   62     }
   63 }