You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by se...@apache.org on 2008/05/18 13:12:01 UTC

svn commit: r657537 [2/2] - in /directory/studio/trunk: connection-core/ connection-core/src/main/java/org/apache/directory/studio/connection/core/ connection-core/src/main/java/org/apache/directory/studio/connection/core/io/jndi/ ldapbrowser-core/src/...

Added: directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewActionGroup.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewActionGroup.java?rev=657537&view=auto
==============================================================================
--- directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewActionGroup.java (added)
+++ directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewActionGroup.java Sun May 18 04:12:00 2008
@@ -0,0 +1,194 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+
+package org.apache.directory.studio.ldapbrowser.ui.views.searchlogs;
+
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.directory.studio.connection.core.ConnectionCoreConstants;
+import org.apache.directory.studio.connection.core.ConnectionCorePlugin;
+import org.apache.directory.studio.ldapbrowser.common.actions.proxy.ActionHandlerManager;
+import org.apache.directory.studio.ldapbrowser.ui.actions.proxy.SearchLogsViewActionProxy;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.action.IMenuListener;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.Separator;
+import org.eclipse.jface.text.source.SourceViewer;
+import org.eclipse.ui.IActionBars;
+
+
+/**
+ * The SearchLogsViewActionGroup manages all the actions of the search logs view.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class SearchLogsViewActionGroup implements ActionHandlerManager, IMenuListener
+{
+
+    /** The view. */
+    private SearchLogsView view;
+
+    /** The Constant olderAction. */
+    private static final String olderAction = "olderAction";
+
+    /** The Constant newerAction. */
+    private static final String newerAction = "newerAction";
+
+    /** The Constant refreshAction. */
+    private static final String refreshAction = "refreshAction";
+
+    /** The Constant refreshAction. */
+    private static final String clearAction = "clearAction";
+
+    /** The enable search request logs action. */
+    private EnableSearchRequestLogsAction enableSearchRequestLogsAction;
+
+    /** The enable search result entry logs action. */
+    private EnableSearchResultEntryLogsAction enableSearchResultEntryLogsAction;
+
+    /** The open search logs preference page action. */
+    private OpenSearchLogsPreferencePageAction openSearchLogsPreferencePageAction;
+
+    /** The search logs view action map. */
+    private Map<String, SearchLogsViewActionProxy> searchLogsViewActionMap;
+
+
+    /**
+     * Creates a new instance of ModificationLogsViewActionGroup.
+     *
+     * @param view the modification logs view
+     */
+    public SearchLogsViewActionGroup( SearchLogsView view )
+    {
+        this.view = view;
+        SourceViewer viewer = this.view.getMainWidget().getSourceViewer();
+
+        searchLogsViewActionMap = new HashMap<String, SearchLogsViewActionProxy>();
+        searchLogsViewActionMap.put( olderAction, new SearchLogsViewActionProxy( viewer, new OlderAction(
+            view ) ) );
+        searchLogsViewActionMap.put( newerAction, new SearchLogsViewActionProxy( viewer, new NewerAction(
+            view ) ) );
+        searchLogsViewActionMap.put( refreshAction, new SearchLogsViewActionProxy( viewer,
+            new RefreshAction( view ) ) );
+        searchLogsViewActionMap.put( clearAction, new SearchLogsViewActionProxy( viewer, new ClearAction(
+            view ) ) );
+        enableSearchRequestLogsAction = new EnableSearchRequestLogsAction();
+        enableSearchResultEntryLogsAction = new EnableSearchResultEntryLogsAction();
+        openSearchLogsPreferencePageAction = new OpenSearchLogsPreferencePageAction();
+    }
+
+
+    /**
+     * Disposes this action group.
+     */
+    public void dispose()
+    {
+        if ( view != null )
+        {
+            for ( SearchLogsViewActionProxy action : searchLogsViewActionMap.values() )
+            {
+                action.dispose();
+                action = null;
+            }
+            searchLogsViewActionMap.clear();
+            searchLogsViewActionMap = null;
+
+            enableSearchRequestLogsAction = null;
+            enableSearchResultEntryLogsAction = null;
+            openSearchLogsPreferencePageAction = null;
+
+            view = null;
+        }
+    }
+
+
+    /**
+     * Fill the action bars.
+     * 
+     * @param actionBars the action bars
+     */
+    public void fillActionBars( IActionBars actionBars )
+    {
+        // Tool Bar
+        actionBars.getToolBarManager().add( ( IAction ) searchLogsViewActionMap.get( clearAction ) );
+        actionBars.getToolBarManager().add( ( IAction ) searchLogsViewActionMap.get( refreshAction ) );
+        actionBars.getToolBarManager().add( new Separator() );
+        actionBars.getToolBarManager().add( ( IAction ) searchLogsViewActionMap.get( olderAction ) );
+        actionBars.getToolBarManager().add( ( IAction ) searchLogsViewActionMap.get( newerAction ) );
+
+        // Menu Bar
+        actionBars.getMenuManager().add( enableSearchRequestLogsAction );
+        actionBars.getMenuManager().add( enableSearchResultEntryLogsAction );
+        actionBars.getMenuManager().add( new Separator() );
+        actionBars.getMenuManager().add( openSearchLogsPreferencePageAction );
+        actionBars.getMenuManager().addMenuListener( new IMenuListener()
+        {
+            public void menuAboutToShow( IMenuManager manager )
+            {
+                enableSearchRequestLogsAction.setChecked( ConnectionCorePlugin.getDefault().getPluginPreferences()
+                    .getBoolean( ConnectionCoreConstants.PREFERENCE_SEARCHREQUESTLOGS_ENABLE ) );
+                enableSearchResultEntryLogsAction.setChecked( ConnectionCorePlugin.getDefault().getPluginPreferences()
+                    .getBoolean( ConnectionCoreConstants.PREFERENCE_SEARCHRESULTENTRYLOGS_ENABLE ) );
+            }
+        } );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void menuAboutToShow( IMenuManager menuManager )
+    {
+    }
+
+
+    /**
+     * Propagates the input to all actions.
+     * 
+     * @param input the input
+     */
+    public void setInput( SearchLogsViewInput input )
+    {
+        for ( SearchLogsViewActionProxy action : searchLogsViewActionMap.values() )
+        {
+            action.inputChanged( input );
+        }
+    }
+
+
+    /**
+     * @see org.apache.directory.studio.ldapbrowser.common.actions.proxy.ActionHandlerManager#activateGlobalActionHandlers()
+     */
+    public void activateGlobalActionHandlers()
+    {
+    }
+
+
+    /**
+     * @see org.apache.directory.studio.ldapbrowser.common.actions.proxy.ActionHandlerManager#deactivateGlobalActionHandlers()
+     */
+    public void deactivateGlobalActionHandlers()
+    {
+    }
+
+}

Added: directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewInput.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewInput.java?rev=657537&view=auto
==============================================================================
--- directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewInput.java (added)
+++ directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewInput.java Sun May 18 04:12:00 2008
@@ -0,0 +1,78 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+
+package org.apache.directory.studio.ldapbrowser.ui.views.searchlogs;
+
+
+import org.apache.directory.studio.ldapbrowser.core.model.IBrowserConnection;
+
+
+/**
+ * A SearchLogsViewInput represents the input of the search logs view.
+ * It consists of a connection and the index of the displayed log file.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class SearchLogsViewInput
+{
+
+    /** The connection. */
+    private IBrowserConnection connection;
+
+    /** The index of the displayed log file */
+    private int index;
+
+
+    /**
+     * Creates a new instance of ModificationLogsViewInput.
+     * 
+     * @param connection the connection
+     * @param index the index of the displayed log file
+     */
+    public SearchLogsViewInput( IBrowserConnection connection, int index )
+    {
+        this.connection = connection;
+        this.index = index;
+    }
+
+
+    /**
+     * Gets the connection.
+     * 
+     * @return the connection
+     */
+    public IBrowserConnection getConnection()
+    {
+        return connection;
+    }
+
+
+    /**
+     * Gets the index of the displayed log file.
+     * 
+     * @return the index
+     */
+    public int getIndex()
+    {
+        return index;
+    }
+
+}

Added: directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewUniversalListener.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewUniversalListener.java?rev=657537&view=auto
==============================================================================
--- directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewUniversalListener.java (added)
+++ directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/views/searchlogs/SearchLogsViewUniversalListener.java Sun May 18 04:12:00 2008
@@ -0,0 +1,303 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+
+package org.apache.directory.studio.ldapbrowser.ui.views.searchlogs;
+
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+
+import org.apache.directory.studio.connection.core.Connection;
+import org.apache.directory.studio.connection.core.ConnectionCorePlugin;
+import org.apache.directory.studio.connection.core.io.jndi.LdifSearchLogger;
+import org.apache.directory.studio.ldapbrowser.common.BrowserCommonActivator;
+import org.apache.directory.studio.ldapbrowser.common.actions.BrowserSelectionUtils;
+import org.apache.directory.studio.ldapbrowser.core.BrowserCorePlugin;
+import org.apache.directory.studio.ldapbrowser.core.events.AttributesInitializedEvent;
+import org.apache.directory.studio.ldapbrowser.core.events.BrowserConnectionUpdateEvent;
+import org.apache.directory.studio.ldapbrowser.core.events.BrowserConnectionUpdateListener;
+import org.apache.directory.studio.ldapbrowser.core.events.ChildrenInitializedEvent;
+import org.apache.directory.studio.ldapbrowser.core.events.EntryModificationEvent;
+import org.apache.directory.studio.ldapbrowser.core.events.EntryUpdateListener;
+import org.apache.directory.studio.ldapbrowser.core.events.EventRegistry;
+import org.apache.directory.studio.ldapbrowser.core.events.SearchUpdateEvent;
+import org.apache.directory.studio.ldapbrowser.core.events.SearchUpdateListener;
+import org.apache.directory.studio.ldapbrowser.core.model.IBrowserConnection;
+import org.apache.directory.studio.ldapbrowser.ui.views.connection.ConnectionView;
+import org.apache.directory.studio.ldifparser.model.container.LdifContainer;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.ui.INullSelectionListener;
+import org.eclipse.ui.IWorkbenchPart;
+
+
+/**
+ * The SearchLogsViewUniversalListener manages all events for the search logs view.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class SearchLogsViewUniversalListener implements BrowserConnectionUpdateListener, SearchUpdateListener,
+    EntryUpdateListener
+{
+
+    /** The search log view. */
+    private SearchLogsView view;
+
+    /** The current input */
+    private SearchLogsViewInput input;
+
+    /** The last refresh timestamp. */
+    private long lastRefreshTimestamp;
+
+    /** Listener that listens for selections of connections */
+    private INullSelectionListener connectionSelectionListener = new INullSelectionListener()
+    {
+        /**
+         * {@inheritDoc}
+         *
+         * This implementation sets the input when another connection was selected.
+         */
+        public void selectionChanged( IWorkbenchPart part, ISelection selection )
+        {
+            if ( view != null && part != null )
+            {
+                if ( view.getSite().getWorkbenchWindow() == part.getSite().getWorkbenchWindow() )
+                {
+                    Connection[] connections = BrowserSelectionUtils.getConnections( selection );
+                    if ( connections.length == 1 )
+                    {
+                        IBrowserConnection connection = BrowserCorePlugin.getDefault().getConnectionManager()
+                            .getBrowserConnectionById( connections[0].getId() );
+                        SearchLogsViewInput input = new SearchLogsViewInput( connection, 0 );
+                        setInput( input );
+                        scrollToNewest();
+                    }
+                }
+            }
+        }
+    };
+
+
+    /**
+     * Creates a new instance of SearchLogsViewUniversalListener.
+     *
+     * @param view the search logs view
+     */
+    public SearchLogsViewUniversalListener( SearchLogsView view )
+    {
+        this.view = view;
+        this.input = null;
+
+        EventRegistry.addEntryUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
+        EventRegistry.addSearchUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
+        EventRegistry.addBrowserConnectionUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
+        view.getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener( ConnectionView.getId(),
+            connectionSelectionListener );
+    }
+
+
+    /**
+     * Disposed this listener
+     */
+    public void dispose()
+    {
+        if ( view != null )
+        {
+            view.getSite().getWorkbenchWindow().getSelectionService().removePostSelectionListener(
+                ConnectionView.getId(), connectionSelectionListener );
+
+            EventRegistry.removeEntryUpdateListener( this );
+            EventRegistry.removeSearchUpdateListener( this );
+            EventRegistry.removeBrowserConnectionpdateListener( this );
+            view = null;
+        }
+    }
+
+
+    /**
+     * Refreshes the input.
+     */
+    void refreshInput()
+    {
+        SearchLogsViewInput newInput = input;
+        input = null;
+        setInput( newInput );
+    }
+
+
+    /**
+     * Sets the input.
+     *
+     * @param input the input
+     */
+    void setInput( SearchLogsViewInput input )
+    {
+        // only if another connection is selected
+        if ( this.input != input )
+        {
+            this.input = input;
+
+            // load file %u %g
+            StringBuffer sb = new StringBuffer();
+            LdifSearchLogger searchLogger = ConnectionCorePlugin.getDefault().getLdifSearchLogger();
+            File[] files = searchLogger.getFiles( input.getConnection().getConnection() );
+            int i = input.getIndex();
+            if ( 0 <= i && i < files.length && files[i] != null && files[i].exists() && files[i].canRead() )
+            {
+                try
+                {
+                    FileReader fr = new FileReader( files[i] );
+                    char[] cbuf = new char[4096];
+                    for ( int length = fr.read( cbuf ); length > 0; length = fr.read( cbuf ) )
+                    {
+                        sb.append( cbuf, 0, length );
+                    }
+                }
+                catch ( Exception e )
+                {
+                    sb.append( e.getMessage() );
+                }
+            }
+
+            // change input
+            view.getMainWidget().getSourceViewer().getDocument().set( sb.toString() );
+            view.getActionGroup().setInput( input );
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     *
+     * This implementation refreshes the input.
+     */
+    public void entryUpdated( EntryModificationEvent event )
+    {
+        if ( event instanceof AttributesInitializedEvent || event instanceof ChildrenInitializedEvent )
+        {
+            updateInput();
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     *
+     * This implementation refreshes the input.
+     */
+    public void searchUpdated( SearchUpdateEvent searchUpdateEvent )
+    {
+        if ( searchUpdateEvent.getDetail() == SearchUpdateEvent.EventDetail.SEARCH_PERFORMED )
+        {
+            updateInput();
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     *
+     * This implementation refreshes the input.
+     */
+    public void browserConnectionUpdated( BrowserConnectionUpdateEvent browserConnectionUpdateEvent )
+    {
+        if ( browserConnectionUpdateEvent.getDetail() == BrowserConnectionUpdateEvent.Detail.BROWSER_CONNECTION_OPENED
+            || browserConnectionUpdateEvent.getDetail() == BrowserConnectionUpdateEvent.Detail.SCHEMA_UPDATED )
+        {
+            updateInput();
+        }
+    }
+
+
+    private void updateInput()
+    {
+        // performance optimization: refresh only once per second
+        long now = System.currentTimeMillis();
+        if ( lastRefreshTimestamp + 1000 < now )
+        {
+            refreshInput();
+            scrollToNewest();
+            lastRefreshTimestamp = now;
+        }
+    }
+
+
+    /**
+     * Scroll to oldest log entry.
+     */
+    public void scrollToOldest()
+    {
+        view.getMainWidget().getSourceViewer().setTopIndex( 0 );
+    }
+
+
+    /**
+     * Scroll to newest log entry.
+     */
+    public void scrollToNewest()
+    {
+        try
+        {
+            LdifContainer record = view.getMainWidget().getLdifModel().getLastContainer();
+            int offset = record.getOffset();
+            int line = view.getMainWidget().getSourceViewer().getDocument().getLineOfOffset( offset );
+            if ( line > 3 )
+                line -= 3;
+            view.getMainWidget().getSourceViewer().setTopIndex( line );
+        }
+        catch ( Exception e )
+        {
+        }
+    }
+
+
+    /**
+     * Clears the input and deletes the logfiles for it
+     * 
+     */
+    public void clearInput()
+    {
+        StringBuffer sb = new StringBuffer( "" );
+        FileWriter fw = null;
+        LdifSearchLogger searchLogger = ConnectionCorePlugin.getDefault().getLdifSearchLogger();
+        File[] files = searchLogger.getFiles( input.getConnection().getConnection() );
+        searchLogger.dispose( input.getConnection().getConnection() );
+        for ( int i = 0; i < files.length; i++ )
+        {
+            try
+            {
+                if ( files[i] != null && files[i].exists() && !files[i].delete() )
+                {
+                    fw = new FileWriter( files[i] );
+                    fw.write( "" );
+                }
+
+            }
+            catch ( Exception e )
+            {
+                sb.append( e.getMessage() );
+            }
+        }
+        view.getMainWidget().getSourceViewer().setTopIndex( 0 );
+        view.getMainWidget().getSourceViewer().getDocument().set( sb.toString() );
+    }
+
+}