PageRenderTime 144ms CodeModel.GetById 40ms app.highlight 21ms RepoModel.GetById 79ms app.codeStats 1ms

/src/mpv5/utils/renderer/TextAreaCellEditor.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 252 lines | 183 code | 42 blank | 27 comment | 36 complexity | ebead33ae1daadeb1d81704df954c5d9 MD5 | raw file
  1
  2/*
  3*  This file is part of YaBS.
  4*
  5*      YaBS is free software: you can redistribute it and/or modify
  6*      it under the terms of the GNU General Public License as published by
  7*      the Free Software Foundation, either version 3 of the License, or
  8*      (at your option) any later version.
  9*
 10*      YaBS is distributed in the hope that it will be useful,
 11*      but WITHOUT ANY WARRANTY; without even the implied warranty of
 12*      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13*      GNU General Public License for more details.
 14*
 15*      You should have received a copy of the GNU General Public License
 16*      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 17 */
 18package mpv5.utils.renderer;
 19
 20//~--- non-JDK imports --------------------------------------------------------
 21
 22import mpv5.logging.Log;
 23
 24
 25//~--- JDK imports ------------------------------------------------------------
 26
 27import java.applet.Applet;
 28
 29import java.awt.Component;
 30import java.awt.Container;
 31import java.awt.Dimension;
 32import java.awt.KeyboardFocusManager;
 33import java.awt.Point;
 34import java.awt.Rectangle;
 35import java.awt.Window;
 36import java.awt.event.ActionEvent;
 37import java.awt.event.ActionListener;
 38import java.awt.event.KeyEvent;
 39
 40import javax.swing.DefaultCellEditor;
 41import javax.swing.JDialog;
 42import javax.swing.JTable;
 43import javax.swing.JTextArea;
 44import javax.swing.JTextField;
 45import javax.swing.KeyStroke;
 46import javax.swing.SwingUtilities;
 47import javax.swing.table.TableColumn;
 48import javax.swing.text.JTextComponent;
 49import mpv5.db.objects.Product;
 50import mpv5.db.objects.SubItem;
 51import mpv5.ui.dialogs.subcomponents.ActivityTextAreaDialog;
 52import mpv5.ui.dialogs.subcomponents.ProductSelectDialog3;
 53import mpv5.utils.models.MPTableModel;
 54
 55public class TextAreaCellEditor extends DefaultCellEditor implements ActionListener {
 56    protected final static String CANCEL = "CANCEL";
 57    protected final static String OK     = "OK";
 58    private static final long serialVersionUID = 1L;
 59    protected JDialog             dialog;
 60    protected JTextComponent      dialogsTextComponent;
 61    private final JTable          table;
 62    protected JTextArea           textArea;
 63
 64    public TextAreaCellEditor(JTable table) {
 65        super(new JTextField());
 66        this.table = table;
 67        textArea   = new JTextArea() {
 68            private static final long serialVersionUID = 1L;
 69            @Override
 70            protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
 71                boolean retValue = super.processKeyBinding(ks, e, condition, pressed);
 72
 73                if (dialogsTextComponent != null) {
 74                    simulateKey(e, dialogsTextComponent);
 75                }
 76
 77                return retValue;
 78            }
 79        };
 80        textArea.setWrapStyleWord(true);
 81        textArea.setLineWrap(true);
 82        editorComponent = textArea;
 83        delegate        = new DefaultCellEditor.EditorDelegate() {
 84            private static final long serialVersionUID = 1L;
 85            @Override
 86            public void setValue(Object value) {
 87
 88                if (value != null && dialog instanceof ProductSelectDialog3 && value instanceof  Product){
 89                    value = ((Product)value).__getCnumber();
 90                }
 91                textArea.setText((value != null)
 92                                 ? value.toString()
 93                                 : "");
 94            }
 95            @Override
 96            public Object getCellEditorValue() {
 97                return textArea.getText();
 98            }           
 99        };
100    }
101
102    public void simulateKey(KeyEvent e, Component c) {
103        try {
104            KeyboardFocusManager.getCurrentKeyboardFocusManager().redispatchEvent(c, e);
105        } catch (Exception ex) {
106            Log.Debug(this, ex);
107        }
108    }
109
110    /**
111     * Set this renderer to the given column
112     * @param column
113     * @param editable
114     */
115    public void setEditorTo(int column) {
116        TableColumn col = table.getColumnModel().getColumn(column);
117
118        col.setCellEditor(this);
119    }
120
121    public void setDialog(JDialog dialog, JTextComponent dialogsTextComponent) {
122        this.dialog               = dialog;
123        this.dialogsTextComponent = dialogsTextComponent;
124    }
125
126    @Override
127    public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, final int row, final int column) {
128        if (value != null && dialog instanceof ProductSelectDialog3 && value instanceof  Product){
129            MPTableModel m = (MPTableModel) table.getModel();
130            SubItem it = m.getRowAt(row, SubItem.getDefaultItem());
131            ((ProductSelectDialog3) dialog).setTempSubItem(it);
132            value = ((Product)value).__getCnumber();
133        }
134        if (value == null) {
135            value = "";
136        }
137
138        Component component = super.getTableCellEditorComponent(table, value, isSelected, row, column);
139
140        dialogsTextComponent.setText(value.toString());    
141        SwingUtilities.invokeLater(new Runnable() {
142            public void run() {
143
144                // Point p =textArea.getLocationOnScreen();
145                // p.move((int)p.getX(),mpv5.YabsViewProxy.instance().identifierFrame.getY() + mpv5.YabsViewProxy.instance().identifierFrame.getHeight() - 310);
146                // dialog.setLocation(p);
147                setDialogLocation(textArea, dialog);
148                if (dialog instanceof ActivityTextAreaDialog && table.getValueAt(row, 9) != null && !table.getValueAt(row, 9).equals(0)) {
149                    Log.Debug(this, "ActivityDialog");
150                    ((ActivityTextAreaDialog) dialog).setSelectedProduct(
151                            Integer.valueOf(table.getValueAt(row, 9).toString()), table.getValueAt(row, column).toString());
152                }
153                dialog.setVisible(true);
154            }
155        });
156
157        return component;
158    }
159
160    @Override
161    public boolean stopCellEditing() {
162        dialog.setVisible(false);
163
164        return super.stopCellEditing();
165    }
166
167    @Override
168    public void cancelCellEditing() {
169        dialog.setVisible(false);
170        super.cancelCellEditing();
171    }
172
173    public void actionPerformed(ActionEvent e) {
174        if (OK.equals(e.getActionCommand())) {
175            dialog.setVisible(false);
176
177            if (dialogsTextComponent != null) {
178                textArea.setText(dialogsTextComponent.getText());
179            } else {
180                Log.Debug(this, "dialogsTextArea is null error");
181            }
182        } else if (CANCEL.equals(e.getActionCommand())) {
183            dialog.setVisible(false);
184        }
185
186        fireEditingStopped();    // Make the renderer reappear
187        SwingUtilities.invokeLater(new Runnable() {
188            public void run() {
189                table.requestFocusInWindow();
190            }
191        });
192    }
193
194    protected void setDialogLocation(Component c, JDialog dialog) {
195        Container root = null;
196
197        if ((c instanceof Window) || (c instanceof Applet)) {
198            root = (Container) c;
199        } else {
200            Container parent;
201
202            for (parent = c.getParent(); parent != null; parent = parent.getParent()) {
203                if ((parent instanceof Window) || (parent instanceof Applet)) {
204                    root = parent;
205
206                    break;
207                }
208            }
209        }
210
211        Dimension invokerSize           = c.getSize();
212        Point invokerScreenLocation = new Point(0, 0);
213        try {
214            invokerScreenLocation = c.getLocationOnScreen();
215        } catch (Exception e) {
216            Log.Debug(e);
217        }
218        Rectangle windowBounds          = dialog.getBounds();
219        int       dx                    = invokerScreenLocation.x;
220        int       dy                    = invokerScreenLocation.y;
221        Rectangle ss                    = root.getGraphicsConfiguration().getBounds();
222
223        ss.grow(-32, -32);
224
225        if (dy + windowBounds.height > ss.y + ss.height) {
226            dy = ss.y + ss.height - windowBounds.height;
227
228            if (invokerScreenLocation.x - ss.x + invokerSize.width / 2 < ss.width / 2) {
229                dx = invokerScreenLocation.x + invokerSize.width;
230            } else {
231                dx = invokerScreenLocation.x - windowBounds.width;
232            }
233        }
234
235        if (dx + windowBounds.width > ss.x + ss.width) {
236            dx = ss.x + ss.width - windowBounds.width;
237        }
238
239        if (dx < ss.x) {
240            dx = ss.x;
241        }
242
243        if (dy < ss.y) {
244            dy = ss.y;
245        }
246
247        dialog.setLocation(dx, dy);
248    }
249}
250
251
252//~ Formatted by Jindent --- http://www.jindent.com