sdeboy 2003/10/29 23:04:27 Modified: src/java/org/apache/log4j/chainsaw/filter EventTypeEntryContainer.java src/java/org/apache/log4j/chainsaw/rule RuleTest.java src/java/org/apache/log4j/chainsaw LoggingEventFieldResolver.java Log: updated context menu code - now support for context menu via right mouse button or ctrl-space mdc lookup now functions correctly Revision Changes Path 1.3 +5 -9 jakarta-log4j/src/java/org/apache/log4j/chainsaw/filter/EventTypeEntryContainer.java Index: EventTypeEntryContainer.java =================================================================== RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/filter/EventTypeEntryContainer.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- EventTypeEntryContainer.java 29 Oct 2003 08:50:36 -0000 1.2 +++ EventTypeEntryContainer.java 30 Oct 2003 07:04:27 -0000 1.3 @@ -93,7 +93,7 @@ private static final String FILE_FIELD = "FILE"; private static final String THREAD_FIELD = "THREAD"; private static final String METHOD_FIELD = "METHOD"; - private static final String MDC_FIELD = "MDC."; + private static final String MDC_FIELD = "MDC"; private static final String NDC_FIELD = "NDC"; public EventTypeEntryContainer() { @@ -104,23 +104,19 @@ modelMap.put(THREAD_FIELD, threadListModel); modelMap.put(METHOD_FIELD, methodListModel); modelMap.put(NDC_FIELD, ndcListModel); - //mdc supported, but not in map + modelMap.put(MDC_FIELD, mdcListModel); } public boolean modelExists(String fieldName) { if (fieldName != null) { - return ((fieldName.toUpperCase().startsWith(MDC_FIELD)) || (modelMap.keySet().contains(fieldName.toUpperCase()))); + return (modelMap.keySet().contains(fieldName.toUpperCase())); } return false; } public ListModel getModel(String fieldName) { - if (modelExists(fieldName)) { - if (fieldName.toUpperCase().startsWith(MDC_FIELD)) { - return mdcListModel; - } else { - return (ListModel)modelMap.get(fieldName.toUpperCase()); - } + if (fieldName != null) { + return (ListModel)modelMap.get(fieldName.toUpperCase()); } return null; } 1.12 +159 -93 jakarta-log4j/src/java/org/apache/log4j/chainsaw/rule/RuleTest.java Index: RuleTest.java =================================================================== RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/rule/RuleTest.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- RuleTest.java 29 Oct 2003 08:50:37 -0000 1.11 +++ RuleTest.java 30 Oct 2003 07:04:27 -0000 1.12 @@ -49,6 +49,14 @@ package org.apache.log4j.chainsaw.rule; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.MDC; +import org.apache.log4j.chainsaw.ChainsawConstants; +import org.apache.log4j.chainsaw.LoggingEventFieldResolver; +import org.apache.log4j.chainsaw.filter.FilterModel; +import org.apache.log4j.spi.LoggingEvent; + import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Point; @@ -57,6 +65,7 @@ import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; + import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -72,16 +81,6 @@ import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.WindowConstants; -import javax.swing.event.CaretEvent; -import javax.swing.event.CaretListener; - -import org.apache.log4j.Level; -import org.apache.log4j.Logger; -import org.apache.log4j.MDC; -import org.apache.log4j.chainsaw.ChainsawConstants; -import org.apache.log4j.chainsaw.LoggingEventFieldResolver; -import org.apache.log4j.chainsaw.filter.FilterModel; -import org.apache.log4j.spi.LoggingEvent; public class RuleTest extends JFrame { @@ -106,8 +105,9 @@ LoggingEventFieldResolver resolver = LoggingEventFieldResolver.getInstance(); - + filterModel = new FilterModel(); + final List eventList = new ArrayList(); MDC.put("entry1", "123"); eventList.add( @@ -133,10 +133,13 @@ "org.apache.log4j.chainsaw", Logger.getLogger("logger4"), System.currentTimeMillis(), Level.WARN, "message4", new Exception("test4"))); + Iterator iter = eventList.iterator(); + while (iter.hasNext()) { - LoggingEvent event = (LoggingEvent)iter.next(); - filterModel.processNewLoggingEvent(ChainsawConstants.LOG4J_EVENT_TYPE, event); + LoggingEvent event = (LoggingEvent) iter.next(); + filterModel.processNewLoggingEvent( + ChainsawConstants.LOG4J_EVENT_TYPE, event); } JPanel fieldPanel = new JPanel(new GridLayout(5, 1)); @@ -146,9 +149,9 @@ final JTextField inFixTextField = new JTextField(inFixText); fieldPanel.add(inFixTextField); + ContextListener listener = new ContextListener(inFixTextField); inFixTextField.addKeyListener(listener); - inFixTextField.addCaretListener(listener); JButton inFixButton = new JButton("Convert InFix to PostFix"); fieldPanel.add(inFixButton); @@ -217,96 +220,159 @@ public static void main(String[] args) { RuleTest test = - new RuleTest("( level ~= deb ) && ( logger like logger[1-2] || MDC.entry1 >= 234 )"); + new RuleTest( + "( level ~= deb ) && ( logger like logger[1-2] || MDC.entry1 >= 234 )"); test.pack(); test.setVisible(true); } - - class ContextListener extends KeyAdapter implements CaretListener { - LoggingEventFieldResolver resolver = LoggingEventFieldResolver.getInstance(); - String lastSymbol = null; - String lastField = null; - JPopupMenu contextMenu = new JPopupMenu(); - JList list = new JList(); - final JTextField textField; - + + class ContextListener extends KeyAdapter { + LoggingEventFieldResolver resolver = + LoggingEventFieldResolver.getInstance(); + String lastField = null; + JPopupMenu contextMenu = new JPopupMenu(); + JList list = new JList(); + final JTextField textField; + public ContextListener(final JTextField textField) { - this.textField = textField; - list.setVisibleRowCount(5); - list.addKeyListener(new KeyAdapter() { - public void keyPressed(KeyEvent e) { - System.out.println("key pressed"); - if (e.getKeyCode() == KeyEvent.VK_ENTER) { - System.out.println("enter pressed"); - updateField(list.getSelectedValue().toString()); - contextMenu.setVisible(false); - } - }}); - - list.addMouseListener(new MouseAdapter() { - public void mouseClicked(MouseEvent e) { - if (e.getClickCount() == 2) { - System.out.println("double clicked"); - updateField(list.getSelectedValue().toString()); - contextMenu.setVisible(false); - } - }}); + this.textField = textField; + list.setVisibleRowCount(5); + PopupListener popupListener = new PopupListener(); + textField.addMouseListener(popupListener); + + list.addKeyListener( + new KeyAdapter() { + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_ENTER) { + updateField(list.getSelectedValue().toString()); + contextMenu.setVisible(false); + } + } + }); + + list.addMouseListener( + new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + if (e.getClickCount() == 2) { + updateField(list.getSelectedValue().toString()); + contextMenu.setVisible(false); + } + } + }); - JScrollPane scrollPane = new JScrollPane(list); - contextMenu.insert(scrollPane, 0); + JScrollPane scrollPane = new JScrollPane(list); + contextMenu.insert(scrollPane, 0); } private void updateField(String value) { - String text = textField.getText(); - int position = textField.getCaretPosition(); - textField.setText(text.substring(0, position) + value + text.substring(position)); - textField.setCaretPosition(position + value.length()); + String text = textField.getText(); + int position = textField.getCaretPosition(); + textField.setText( + text.substring(0, position) + value + text.substring(position)); + textField.setCaretPosition(position + value.length()); } - - public void keyTyped(KeyEvent e) { - if ((e.getKeyCode() == KeyEvent.VK_SPACE) && (e.getModifiers() == KeyEvent.CTRL_MASK)); - System.out.println("PRESSED CTRL-SPACE"); - if (resolver.isField(lastField) && RuleFactory.isRule(lastSymbol)) { - System.out.println("showing popupmenu"); - if (filterModel.getContainer().modelExists(lastField)) { - list.setModel(filterModel.getContainer().getModel(lastField)); - list.setSelectedIndex(0); - Point p = ((JTextField)e.getComponent()).getCaret().getMagicCaretPosition(); - contextMenu.show(e.getComponent(), p.x, (p.y + (e.getComponent().getHeight() - 5))); - contextMenu.requestFocusInWindow(); - } - } + + public void keyPressed(KeyEvent e) { + if ( + (e.getKeyCode() == KeyEvent.VK_SPACE) + && (e.getModifiers() == KeyEvent.CTRL_MASK)) { + displayContext(); + } } - - public void caretUpdate(CaretEvent e) { - //( level ~= deb ) - String text = textField.getText(); - int endPosition = e.getDot() - 2; - if (endPosition > -1 && text.charAt(endPosition) == ' ') { - endPosition--; - } - System.out.println("position is " + endPosition); - int startPosition = text.lastIndexOf(" ", endPosition) + 1; - System.out.println("startposition is " + startPosition); - - if (startPosition > -1 && endPosition > -1) { - lastSymbol = text.substring(startPosition, endPosition + 1); - if (!RuleFactory.isRule(lastSymbol)) { - lastSymbol = null; - } - System.out.println("last SYMBOL IS " + lastSymbol); - - int fieldStartPosition = text.lastIndexOf(" ", startPosition - 2); - System.out.println("fieldstart is " + fieldStartPosition); - if (fieldStartPosition > -1 ) { - lastField = text.substring(fieldStartPosition + 1, startPosition - 1); - if (!resolver.isField(lastField)) { - lastField = null; - } - System.out.println("last field is " + lastField); - } + + public void displayContext() { + String lastField = getContextKey(); + + if (lastField != null) { + list.setModel(filterModel.getContainer().getModel(lastField)); + list.setSelectedIndex(0); + + Point p = textField.getCaret().getMagicCaretPosition(); + contextMenu.show(textField, p.x, (p.y + (textField.getHeight() - 6))); + list.requestFocus(); + } + } + + private String getContextKey() { + String field = getField(); + + if (field == null) { + field = getSubField(); + } + + return field; + } + + //returns the currently active field which can be used to display a context menu + //the field returned is the left hand portion of an expression (for example, logger == ) + //logger is the field that is returned + private String getField() { + String text = textField.getText(); + + int currentPosition = textField.getCaretPosition(); + + if ((currentPosition < 1) || (text.charAt(currentPosition - 1) != ' ')) { + return null; + } + + int symbolPosition = text.lastIndexOf(" ", currentPosition - 1); + + if (symbolPosition < 0) { + return null; + } + + int lastFieldPosition = text.lastIndexOf(" ", symbolPosition - 1); + + if (lastFieldPosition < 0) { + return null; + } + + int lastFieldStartPosition = + Math.max(0, text.lastIndexOf(" ", lastFieldPosition - 1)); + String lastSymbol = + text.substring(lastFieldPosition + 1, symbolPosition).trim(); + + String lastField = + text.substring(lastFieldStartPosition, lastFieldPosition).trim(); + + if (RuleFactory.isRule(lastSymbol) && resolver.isField(lastField)) { + return lastField; + } + + return null; + } + + //subfields allow the key portion of a field to provide context menu support + //and are available after the fieldname and a . (for example, MDC.) + private String getSubField() { + int currentPosition = textField.getCaretPosition(); + String text = textField.getText(); + + if (text.substring(0, currentPosition).endsWith("MDC.")) { + return "MDC"; + } + + return null; + } + + class PopupListener extends MouseAdapter { + PopupListener() { + } + + public void mousePressed(MouseEvent e) { + checkPopup(e); + } + + public void mouseReleased(MouseEvent e) { + checkPopup(e); + } + + private void checkPopup(MouseEvent e) { + if (e.isPopupTrigger()) { + displayContext(); } - } + } + } } } 1.10 +15 -21 jakarta-log4j/src/java/org/apache/log4j/chainsaw/LoggingEventFieldResolver.java Index: LoggingEventFieldResolver.java =================================================================== RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/LoggingEventFieldResolver.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- LoggingEventFieldResolver.java 29 Oct 2003 08:50:37 -0000 1.9 +++ LoggingEventFieldResolver.java 30 Oct 2003 07:04:27 -0000 1.10 @@ -68,21 +68,21 @@ * individual types returned per field are described here: * * Field Name Field value (String representation Return type - * LOGGER category name (logger) String - * LEVEL level Level - * CLASS locationInformation's class name String - * FILE locationInformation's file name String - * LINE locationInformation's line number String - * METHOD locationInformation's method name String - * MSG message Object - * NDC NDC String - * EXCEPTION throwable string representation ThrowableInformation - * TIMESTAMP timestamp Long - * THREAD thread String - * MDC.keyName entry in the MDC hashtable Object - * mapped to key 'keyName' - * PROP.keyName entry in the Property hashtable String - * mapped to the key 'keyName' + * LOGGER category name (logger) String + * LEVEL level Level + * CLASS locationInformation's class name String + * FILE locationInformation's file name String + * LINE locationInformation's line number String + * METHOD locationInformation's method name String + * MSG message Object + * NDC NDC String + * EXCEPTION throwable string representation ThrowableInformation + * TIMESTAMP timestamp Long + * THREAD thread String + * MDC.keyName entry in the MDC hashtable Object + * mapped to key [keyName] + * PROP.keyName entry in the Property hashtable String + * mapped to the key [keyName] * NOTE: the values for the 'keyName' portion of the MDC and PROP mappings must * be an exact match to the key in the hashTable (case sensitive). @@ -142,10 +142,6 @@ } public Object getValue(String fieldName, LoggingEvent event) { - if (fieldName == null) { - throw new RuntimeException("null field name"); - } - String upperField = fieldName.toUpperCase(); if (LOGGER_FIELD.equals(upperField)) { @@ -164,7 +160,6 @@ return event.getMessage(); } else if (NDC_FIELD.equals(upperField)) { String ndcValue = event.getNDC(); - return ((ndcValue == null) ? "" : ndcValue); } else if (EXCEPTION_FIELD.equals(upperField)) { return event.getThrowableInformation(); @@ -180,7 +175,6 @@ } else if (upperField.startsWith(PROP_FIELD)) { //note: need to use actual fieldname since case matters String propValue = event.getProperty(fieldName.substring(5)); - return ((propValue == null) ? EMPTY_STRING : propValue); }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]