PageRenderTime 48ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

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