nbandroid_patches / logcat_persistent_tabs.patch

# HG changeset patch
# Parent d3a5c26113ae4977ccc1ba465fa467049e31b446
LogCat: tabs persistent over sessions

diff -r d3a5c26113ae -r 2b3153e0e67d logcat/src/org/nyerel/nbandroid/logcat/LogTopComponent.java
--- a/logcat/src/org/nyerel/nbandroid/logcat/LogTopComponent.java	Tue Jun 14 03:01:45 2011 +0200
+++ b/logcat/src/org/nyerel/nbandroid/logcat/LogTopComponent.java	Thu Jun 23 01:50:38 2011 +0200
@@ -64,6 +64,13 @@
   private static final String ICON_PATH = "org/nyerel/nbandroid/logcat/androidIcon.png";
 
   private static final String PREFERRED_ID = "LogTopComponent";
+  
+  private static final String SERIALIZE_VERSION             = "version";
+  private static final String SERIALIZE_VERSION_CURRENT     = "1.0";
+  private static final String SERIALIZE_FILTER_STRING       = "filter.string";
+  private static final String SERIALIZE_FILTER_LEVEL        = "filter.level";
+  private static final String SERIALIZE_TAB_COUNT           = "tab.count";
+  private static final String SERIALIZE_TAB                 = "tab";
 
   private static LogTopComponent instance;
 
@@ -150,19 +157,100 @@
     void writeProperties(java.util.Properties p) {
         // better to version settings since initial version as advocated at
         // http://wiki.apidesign.org/wiki/PropertyFiles
-        p.setProperty("version", "1.0");
-        // TODO store your settings
+        p.setProperty(SERIALIZE_VERSION, SERIALIZE_VERSION_CURRENT);
+
+        try{
+            // write current filter settings
+            p.setProperty(SERIALIZE_FILTER_STRING, txtFilterText.getText());
+            p.setProperty(SERIALIZE_FILTER_LEVEL,  rowFilter.getLogLevel().toString());
+
+            // export tab filter count
+            p.setProperty(SERIALIZE_TAB_COUNT,     Integer.toString(tabManagers.size()));
+
+            // export all filters
+            for(int i=0; i<tabManagers.size(); i++) {
+                String prefix = SERIALIZE_TAB + "." + i;
+                LogFilter filter = tabManagers.get(i).getModel().getFilter();
+
+                if (filter != null) {
+                    filter.serialize(p, prefix);
+                }
+            }
+        }
+        catch(Exception e) {
+            LOG.log(Level.INFO, "error on saving LogCat TopComponent", e);
+        }
+        
+        return;
     }
 
     Object readProperties(java.util.Properties p) {
         LogTopComponent singleton = LogTopComponent.getDefault();
-        singleton.readPropertiesImpl(p);
+        
+        try {
+            singleton.readPropertiesImpl(p);
+        }
+        catch(Exception e) {
+            LOG.log(Level.INFO, "error on restore LogCat TopComponent", e);
+        }
+        
         return singleton;
     }
+    
+    private void readPropertiesImpl(java.util.Properties p) {
+        String version = p.getProperty(SERIALIZE_VERSION);
+        
+        if (SERIALIZE_VERSION_CURRENT.equals(version)) {
+            String filterString = p.getProperty(SERIALIZE_FILTER_STRING, "");
+            String filterLevel  = p.getProperty(SERIALIZE_FILTER_LEVEL,  LogLevel.VERBOSE.toString());
+            
+            // apply filter string
+            if (filterString != null) {
+                txtFilterText.setText(filterString);
+                rowFilter.setFilterString(filterString);
+            }
+            
+            // apply filter level
+            if (filterLevel != null) {
+                // find matching button to select
+                for(Enumeration<AbstractButton> buttons = btGrpLogLevel.getElements(); buttons.hasMoreElements();) {
+                    AbstractButton bt = buttons.nextElement();
+                    
+                    if (filterLevel.equals(bt.getActionCommand())) {
+                        bt.setSelected(true);
+                        break;
+                    }
+                }
+                
+                // convert LogLevel name into enum and configure filter
+                try {
+                    rowFilter.setLogLevel(LogLevel.valueOf(filterLevel));
+                }
+                catch(IllegalArgumentException e) {
+                }
+            }
+        }
+        
+        // try to restore filter tabs
+        try {
+            int count = Integer.parseInt(p.getProperty(SERIALIZE_TAB_COUNT, "0"));
 
-    private void readPropertiesImpl(java.util.Properties p) {
-        String version = p.getProperty("version");
-        // TODO read your settings according to their version
+            for(int i=1; i<count; i++) {
+                String prefix = SERIALIZE_TAB + "." + i;
+                LogFilter filter = LogFilter.deserialize(p, prefix);
+                
+                if (filter != null) {
+                    addTable(filter);
+                }
+            }
+        }
+        catch(IllegalArgumentException e) {
+        }
+        
+        // re-apply filter on current tab
+        if (selectedTable != null) {
+            ((TableRowSorter)selectedTable.getRowSorter()).sort();
+        }
     }
 
     @Override
@@ -439,17 +527,7 @@
 
         LogFilter filter = LogFilterDialog.showDialog();
         if(filter != null) {
-            
-            LogTableModel model = new LogTableModel(filter);
-            JTable table = new JTable(model);
-            
-           // Prepare table for listening
-            LogTableManager manager = prepareTable(table);
-            reader.addLogListener(manager);
-            
-            JScrollPane scroll = new JScrollPane(table);
-            
-            tabPane.addTab(filter.getName(), scroll);
+            addTable(filter);
         }
     }//GEN-LAST:event_addFilterButtonActionPerformed
 
@@ -578,6 +656,23 @@
     }
     
     
+    private void addTable(LogFilter filter) {
+        LogTableModel model = new LogTableModel(filter);
+        JTable table = new JTable(model);
+
+        // Prepare table for listening
+        LogTableManager manager = prepareTable(table);
+        
+        if (reader != null) {
+            reader.addLogListener(manager);
+        }
+
+        JScrollPane scroll = new JScrollPane(table);
+
+        tabPane.addTab(filter.getName(), scroll);
+    }
+    
+    
     private LogTableManager prepareTable(final JTable table) {
         table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
         table.setShowHorizontalLines(false);
diff -r d3a5c26113ae -r 2b3153e0e67d logcat/src/org/nyerel/nbandroid/logcat/logtable/LogFilter.java
--- a/logcat/src/org/nyerel/nbandroid/logcat/logtable/LogFilter.java	Tue Jun 14 03:01:45 2011 +0200
+++ b/logcat/src/org/nyerel/nbandroid/logcat/logtable/LogFilter.java	Thu Jun 23 01:50:38 2011 +0200
@@ -14,6 +14,7 @@
 package org.nyerel.nbandroid.logcat.logtable;
 
 import com.android.ddmlib.Log.LogLevel;
+import java.util.Properties;
 import org.nyerel.nbandroid.logcat.LogEvent;
 /**
  *
@@ -21,6 +22,11 @@
  */
 public class LogFilter {
     
+    private final static String SERIALIZE_NAME      = "name";
+    private final static String SERIALIZE_TAG       = "tag";
+    private final static String SERIALIZE_PROCESS   = "process";
+    private final static String SERIALIZE_LEVEL     = "level";
+
     private final String name;
     private final String filterTag;
     private final String filterProcess;
@@ -50,16 +56,64 @@
     }
     
     
+    /**
+     * Store all attrobutes of this filter into the given properties-list.
+     */
+    public void serialize(Properties p, String prefix) {
+        p.put(prefix + "." + SERIALIZE_NAME, name);
+        
+        if (filterTag != null) {
+            p.put(prefix + "." + SERIALIZE_TAG, filterTag);
+        }
+        
+        if (filterProcess != null) {
+            p.put(prefix + "." + SERIALIZE_PROCESS, filterProcess);
+        }
+        
+        if (filterLevel != null) {
+            p.put(prefix + "." + SERIALIZE_LEVEL, filterLevel.toString());
+        }
+        
+        return;
+    }
+    
+    
+    /**
+     * Create a new filter, which was previously stored in the given properties list.
+     */
+    public static LogFilter deserialize(Properties p, String prefix) {
+        String name     = p.getProperty(prefix + "." + SERIALIZE_NAME);
+        String process  = p.getProperty(prefix + "." + SERIALIZE_PROCESS);
+        String tag      = p.getProperty(prefix + "." + SERIALIZE_TAG);
+        String lvlname  = p.getProperty(prefix + "." + SERIALIZE_LEVEL);
+        LogLevel level  = null;
+        
+        if (lvlname != null) {
+            try {
+                level = LogLevel.valueOf(lvlname);
+            }
+            catch(IllegalArgumentException e) {
+            }
+        }
+        
+        if (name != null) {
+            return new LogFilter(name, tag, process, level);
+        }
+        
+        return null;
+    }
+    
+    
     public boolean satisfy(LogEvent event) {
         
         boolean ok = true;
         
         if(filterTag != null) {
-            ok &= filterTag.equals(event.getTag());
+            ok &= filterTag.equalsIgnoreCase(event.getTag());
         }
         
         if (filterProcess != null) {
-            ok &= filterProcess.equals(event.getProcessName());
+            ok &= filterProcess.equalsIgnoreCase(event.getProcessName());
         }
         
         if(filterPid != -1) {
@@ -67,7 +121,7 @@
         }
         
         if(filterLevel != null) {
-            ok &= (filterLevel == event.getLevel());
+            ok &= (event.getLevel().getPriority() >= filterLevel.getPriority());
         }
         
         return ok;
diff -r d3a5c26113ae -r 2b3153e0e67d logcat/src/org/nyerel/nbandroid/logcat/logtable/LogTableManager.java
--- a/logcat/src/org/nyerel/nbandroid/logcat/logtable/LogTableManager.java	Tue Jun 14 03:01:45 2011 +0200
+++ b/logcat/src/org/nyerel/nbandroid/logcat/logtable/LogTableManager.java	Thu Jun 23 01:50:38 2011 +0200
@@ -69,6 +69,10 @@
         levelColumn.setMaxWidth(80);
 
     }
+    
+    public LogTableModel getModel() {
+        return model;
+    }
 
     public void scrollToBottom() {
         SwingUtilities.invokeLater(new Runnable() {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.