package cz.muni.fi.bronchus.gui; /* * MyRespCellRenderer.java * * Created on November 18, 2004, 4:19 PM */ import javax.swing.event.*; import javax.swing.*; import javax.swing.table.*; import javax.swing.text.html.*; import java.awt.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.text.DefaultEditorKit; /** * * @author xudrzal */ public class MyTextPaneCellEditor extends JTextPane implements TableCellEditor { private static String emptyHTML="
"; // private HTMLDocument doc; private HTMLEditorKit htmlKit; private JTable table; protected EventListenerList listenerList = new EventListenerList(); transient protected ChangeEvent changeEvent = null; private void setHTMLKit(JTextPane textpane, String begin) { StyleSheet styleSheet = new StyleSheet(); styleSheet.addRule("body {font-family: Dialog; font-size: 11pt; font-style: normal; font-weight: normal;}"); // styleSheet.addRule(".nick {color: blue;}"); // styleSheet.addRule(".normal {color: black;}"); htmlKit = new HTMLEditorKit(); htmlKit.setDefaultCursor(new Cursor(Cursor.TEXT_CURSOR)); htmlKit.setStyleSheet(styleSheet); HTMLDocument doc = (HTMLDocument) htmlKit.createDefaultDocument(); doc.putProperty( DefaultEditorKit.EndOfLineStringProperty, "\n" ); textpane.setEditorKit(htmlKit); textpane.setDocument(doc); textpane.setMargin(new Insets(0, 0, 0, 0)); textpane.setContentType("text/html"); textpane.setText("
"+begin+"
"); textpane.setCaretPosition(doc.getLength()); } public CellEditorListener[] getCellEditorListeners() { return (CellEditorListener[])listenerList.getListeners( CellEditorListener.class); } public void addCellEditorListener(javax.swing.event.CellEditorListener l) { listenerList.add(CellEditorListener.class, l); } public void cancelCellEditing() { // int row=table.getEditingRow(); // int col=table.getEditingColumn(); fireEditingCanceled(); /* Object a=table.getValueAt(row, col); System.out.println("EDITOR cancelCellEditing"+a); //a=getDocument(); // a=getText();*/ } public Object getCellEditorValue() { // System.out.println("EDITOR getValue "+getText()); //return getDocument(); return getText(); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { //HTMLDocument d=(HTMLDocument)table.getValueAt(row,column); String d=(String)table.getValueAt(row,column); // System.out.println("EDITOR getComponent "+row+column+d); if (d!=null) { //setDocument(d); Pattern pat = Pattern.compile("(?i)(?s)(.*)"); Matcher matcher = pat.matcher(d); // System.out.println(matcher.find()+"#"+matcher.group(1).trim()+"$"); if (matcher.find() && matcher.groupCount()>0 && matcher.group(1).trim().equals("")) setText(emptyHTML); else setText(d); } else { // System.out.println("SETTING HTML KIT"); setHTMLKit(this, ""); //setText(emptyHTML); } // System.out.println(this.getText()); return this; } public boolean isCellEditable(java.util.EventObject anEvent) { return true; } public void removeCellEditorListener(javax.swing.event.CellEditorListener l) { listenerList.remove(CellEditorListener.class, l); } public boolean shouldSelectCell(java.util.EventObject anEvent) { return true; } public boolean stopCellEditing() { // int row=table.getEditingRow(); // int col=table.getEditingColumn(); fireEditingStopped(); /* Object a=table.getValueAt(row, col); System.out.println("EDITOR stopCellEditing "+row+col+a+getText()); //a=getDocument(); // a=getText();*/ return true; } public MyTextPaneCellEditor(JTable table) { setOpaque(true); //MUST do this for background to show up. setHTMLKit(this, ""); this.table=table; // System.out.println("EDITOR konstruktor"); } protected void fireEditingStopped() { // Guaranteed to return a non-null array Object[] listeners = listenerList.getListenerList(); // Process the listeners last to first, notifying // those that are interested in this event for (int i = listeners.length-2; i>=0; i-=2) { if (listeners[i]==CellEditorListener.class) { // Lazily create the event: if (changeEvent == null) changeEvent = new ChangeEvent(this); ((CellEditorListener)listeners[i+1]).editingStopped(changeEvent); } } } protected void fireEditingCanceled() { // Guaranteed to return a non-null array Object[] listeners = listenerList.getListenerList(); // Process the listeners last to first, notifying // those that are interested in this event for (int i = listeners.length-2; i>=0; i-=2) { if (listeners[i]==CellEditorListener.class) { // Lazily create the event: if (changeEvent == null) changeEvent = new ChangeEvent(this); ((CellEditorListener)listeners[i+1]).editingCanceled(changeEvent); } } } }