KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > views > modificationlogs > ModificationLogsViewUniversalListener


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.ui.views.modificationlogs;
22
23
24 import java.io.File JavaDoc;
25 import java.io.FileReader JavaDoc;
26
27 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
28 import org.apache.directory.ldapstudio.browser.common.actions.SelectionUtils;
29 import org.apache.directory.ldapstudio.browser.core.events.AttributesInitializedEvent;
30 import org.apache.directory.ldapstudio.browser.core.events.ChildrenInitializedEvent;
31 import org.apache.directory.ldapstudio.browser.core.events.EntryModificationEvent;
32 import org.apache.directory.ldapstudio.browser.core.events.EntryUpdateListener;
33 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
34 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
35 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer;
36 import org.apache.directory.ldapstudio.browser.ui.views.connection.ConnectionView;
37 import org.eclipse.jface.viewers.ISelection;
38 import org.eclipse.ui.INullSelectionListener;
39 import org.eclipse.ui.IWorkbenchPart;
40
41
42 /**
43  * The ModificationLogsViewUniversalListener manages all events for the modification logs view.
44  *
45  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
46  * @version $Rev$, $Date$
47  */

48 public class ModificationLogsViewUniversalListener implements EntryUpdateListener
49 {
50
51     /** The modification log view. */
52     private ModificationLogsView view;
53
54     /** The current input */
55     private ModificationLogsViewInput input;
56
57     /** Listener that listens for selections of connections */
58     private INullSelectionListener connectionSelectionListener = new INullSelectionListener()
59     {
60         /**
61          * {@inheritDoc}
62          *
63          * This implementation sets the input when another connection was selected.
64          */

65         public void selectionChanged( IWorkbenchPart part, ISelection selection )
66         {
67             if ( view != null && part != null )
68             {
69                 if ( view.getSite().getWorkbenchWindow() == part.getSite().getWorkbenchWindow() )
70                 {
71                     IConnection[] connections = SelectionUtils.getConnections( selection );
72                     if ( connections.length == 1 )
73                     {
74                         ModificationLogsViewInput input = new ModificationLogsViewInput( connections[0], 0 );
75                         setInput( input );
76                         scrollToNewest();
77                     }
78                 }
79             }
80         }
81     };
82
83
84     /**
85      * Creates a new instance of ModificationLogsViewUniversalListener.
86      *
87      * @param view the modification logs view
88      */

89     public ModificationLogsViewUniversalListener( ModificationLogsView view )
90     {
91         this.view = view;
92         this.input = null;
93
94         EventRegistry.addEntryUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
95         view.getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener( ConnectionView.getId(),
96             connectionSelectionListener );
97     }
98
99
100     /**
101      * Disposed this listener
102      */

103     public void dispose()
104     {
105         if ( view != null )
106         {
107             view.getSite().getWorkbenchWindow().getSelectionService().removePostSelectionListener(
108                 ConnectionView.getId(), connectionSelectionListener );
109
110             EventRegistry.removeEntryUpdateListener( this );
111             view = null;
112         }
113     }
114
115
116     /**
117      * Refreshes the input.
118      */

119     void refreshInput()
120     {
121         ModificationLogsViewInput newInput = input;
122         input = null;
123         setInput( newInput );
124     }
125
126
127     /**
128      * Sets the input.
129      *
130      * @param input the input
131      */

132     void setInput( ModificationLogsViewInput input )
133     {
134         // only if another connection is selected
135
if ( this.input != input )
136         {
137             this.input = input;
138
139             // load file %u %g
140
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
141             File JavaDoc[] files = input.getConnection().getModificationLogger().getFiles();
142             int i = input.getIndex();
143             if ( 0 <= i && i < files.length && files[i] != null && files[i].exists() && files[i].canRead() )
144             {
145                 try
146                 {
147                     FileReader JavaDoc fr = new FileReader JavaDoc( files[i] );
148                     char[] cbuf = new char[4096];
149                     for ( int length = fr.read( cbuf ); length > 0; length = fr.read( cbuf ) )
150                     {
151                         sb.append( cbuf, 0, length );
152                     }
153                 }
154                 catch ( Exception JavaDoc e )
155                 {
156                     sb.append( e.getMessage() );
157                 }
158             }
159
160             // change input
161
view.getMainWidget().getSourceViewer().getDocument().set( sb.toString() );
162             view.getActionGroup().setInput( input );
163         }
164     }
165
166
167     /**
168      * {@inheritDoc}
169      *
170      * This implementation refreshes the input.
171      */

172     public void entryUpdated( EntryModificationEvent event )
173     {
174         if ( !( event instanceof AttributesInitializedEvent ) && !( event instanceof ChildrenInitializedEvent ) )
175         {
176             refreshInput();
177             scrollToNewest();
178         }
179     }
180
181
182     /**
183      * Scroll to oldest log entry.
184      */

185     public void scrollToOldest()
186     {
187         view.getMainWidget().getSourceViewer().setTopIndex( 0 );
188     }
189
190
191     /**
192      * Scroll to newest log entry.
193      */

194     public void scrollToNewest()
195     {
196         try
197         {
198             LdifContainer record = view.getMainWidget().getLdifModel().getLastContainer();
199             int offset = record.getOffset();
200             int line = view.getMainWidget().getSourceViewer().getDocument().getLineOfOffset( offset );
201             if ( line > 3 )
202                 line -= 3;
203             view.getMainWidget().getSourceViewer().setTopIndex( line );
204         }
205         catch ( Exception JavaDoc e )
206         {
207         }
208
209     }
210
211 }
212
Popular Tags