1 20 21 package org.apache.directory.ldapstudio.browser.ui.views.modificationlogs; 22 23 24 import java.io.File ; 25 import java.io.FileReader ; 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 48 public class ModificationLogsViewUniversalListener implements EntryUpdateListener 49 { 50 51 52 private ModificationLogsView view; 53 54 55 private ModificationLogsViewInput input; 56 57 58 private INullSelectionListener connectionSelectionListener = new INullSelectionListener() 59 { 60 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 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 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 119 void refreshInput() 120 { 121 ModificationLogsViewInput newInput = input; 122 input = null; 123 setInput( newInput ); 124 } 125 126 127 132 void setInput( ModificationLogsViewInput input ) 133 { 134 if ( this.input != input ) 136 { 137 this.input = input; 138 139 StringBuffer sb = new StringBuffer (); 141 File [] 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 fr = new FileReader ( 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 e ) 155 { 156 sb.append( e.getMessage() ); 157 } 158 } 159 160 view.getMainWidget().getSourceViewer().getDocument().set( sb.toString() ); 162 view.getActionGroup().setInput( input ); 163 } 164 } 165 166 167 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 185 public void scrollToOldest() 186 { 187 view.getMainWidget().getSourceViewer().setTopIndex( 0 ); 188 } 189 190 191 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 e ) 206 { 207 } 208 209 } 210 211 } 212 | Popular Tags |