1 20 21 package org.apache.directory.ldapstudio.browser.ui.actions; 22 23 24 import java.util.ArrayList ; 25 import java.util.List ; 26 27 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants; 28 import org.apache.directory.ldapstudio.browser.common.actions.BrowserAction; 29 import org.apache.directory.ldapstudio.browser.common.jobs.RunnableContextJobAdapter; 30 import org.apache.directory.ldapstudio.browser.core.jobs.InitializeAttributesJob; 31 import org.apache.directory.ldapstudio.browser.core.jobs.ReadEntryJob; 32 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 33 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult; 34 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin; 35 import org.eclipse.swt.dnd.Clipboard; 36 import org.eclipse.swt.dnd.TextTransfer; 37 import org.eclipse.swt.dnd.Transfer; 38 import org.eclipse.swt.widgets.Display; 39 40 41 47 public abstract class CopyEntryAsAction extends BrowserAction 48 { 49 52 public static final int MODE_DN_ONLY = 1; 53 54 57 public static final int MODE_RETURNING_ATTRIBUTES_ONLY = 2; 58 59 62 public static final int MODE_NORMAL = 3; 63 64 67 public static final int MODE_INCLUDE_OPERATIONAL_ATTRIBUTES = 4; 68 69 protected int mode; 70 71 protected String type; 72 73 protected String appendix; 74 75 76 84 public CopyEntryAsAction( String type, int mode ) 85 { 86 super(); 87 this.type = type; 88 this.mode = mode; 89 if ( this.mode == MODE_DN_ONLY ) 90 { 91 this.appendix = " (DN only)"; 92 } 93 else if ( this.mode == MODE_RETURNING_ATTRIBUTES_ONLY ) 94 { 95 this.appendix = " (returning attributes only)"; 96 } 97 else if ( this.mode == MODE_INCLUDE_OPERATIONAL_ATTRIBUTES ) 98 { 99 this.appendix = " (include operational attributes)"; 100 } 101 else if ( this.mode == MODE_NORMAL ) 102 { 103 this.appendix = " (all user attributes)"; 104 } 105 else 106 { 107 appendix = ""; 108 } 109 } 110 111 112 115 public String getText() 116 { 117 if ( getSelectedEntries().length + getSelectedSearchResults().length + getSelectedBookmarks().length > 0 118 && getSelectedSearches().length == 0 ) 119 { 120 String text = ( getSelectedEntries().length + getSelectedSearchResults().length 121 + getSelectedBookmarks().length > 1 ? "Copy Entries as " + type : "Copy Entry as " + type ) 122 + appendix; 123 return text; 124 } 125 else if ( getSelectedEntries().length + getSelectedSearchResults().length + getSelectedBookmarks().length == 0 126 && getSelectedSearches().length == 1 && getSelectedSearches()[0].getSearchResults() != null 127 && getSelectedSearches()[0].getSearchResults().length > 0 ) 128 { 129 String text = ( getSelectedSearches()[0].getSearchResults().length > 1 ? "Copy Search Results as " + type 130 : "Copy Search Result as " + type ) 131 + appendix; 132 return text; 133 } 134 135 return "Copy Entry as " + type + appendix; 136 } 137 138 139 142 public String getCommandId() 143 { 144 return null; 145 } 146 147 148 151 public void run() 152 { 153 List <IEntry> entryList = new ArrayList <IEntry>(); 155 for ( int i = 0; i < getSelectedEntries().length; i++ ) 156 { 157 entryList.add( getSelectedEntries()[i] ); 158 } 159 for ( int i = 0; i < getSelectedSearchResults().length; i++ ) 160 { 161 entryList.add( getSelectedSearchResults()[i].getEntry() ); 162 } 163 for ( int i = 0; i < getSelectedBookmarks().length; i++ ) 164 { 165 IEntry entry = getSelectedBookmarks()[0].getConnection().getEntryFromCache( 166 getSelectedBookmarks()[0].getDn() ); 167 if ( entry == null ) 168 { 169 ReadEntryJob job = new ReadEntryJob( getSelectedBookmarks()[0].getConnection(), 170 getSelectedBookmarks()[0].getDn() ); 171 RunnableContextJobAdapter.execute( job ); 172 entry = job.getReadEntry(); 173 } 174 entryList.add( entry ); 175 } 176 if ( getSelectedSearches().length == 1 ) 177 { 178 ISearchResult[] results = getSelectedSearches()[0].getSearchResults(); 179 for ( int k = 0; k < results.length; k++ ) 180 { 181 entryList.add( results[k].getEntry() ); 182 } 183 } 184 IEntry[] entries = ( IEntry[] ) entryList.toArray( new IEntry[entryList.size()] ); 185 186 List <IEntry> uninitializedEntryList = new ArrayList <IEntry>(); 188 for ( int i = 0; entries != null && i < entries.length; i++ ) 189 { 190 if ( !entries[i].isAttributesInitialized() ) 191 { 192 uninitializedEntryList.add( entries[i] ); 193 } 194 } 195 if ( uninitializedEntryList.size() > 0 196 && ( this.mode == MODE_NORMAL || this.mode == MODE_INCLUDE_OPERATIONAL_ATTRIBUTES ) ) 197 { 198 IEntry[] uninitializedEntries = ( IEntry[] ) uninitializedEntryList 199 .toArray( new IEntry[uninitializedEntryList.size()] ); 200 201 InitializeAttributesJob job = new InitializeAttributesJob( uninitializedEntries, false ); 202 RunnableContextJobAdapter.execute( job ); 203 204 } 209 210 StringBuffer text = new StringBuffer (); 212 serialializeEntries( entries, text ); 213 copyToClipboard( text.toString() ); 214 } 215 216 217 225 protected abstract void serialializeEntries( IEntry[] entries, StringBuffer text ); 226 227 228 231 public boolean isEnabled() 232 { 233 boolean showOperational = BrowserUIPlugin.getDefault().getPreferenceStore().getBoolean( 234 BrowserCommonConstants.PREFERENCE_ENTRYEDITOR_SHOW_OPERATIONAL_ATTRIBUTES ); 235 236 if ( getSelectedSearchResults().length > 0 237 && getSelectedEntries().length + getSelectedBookmarks().length + getSelectedSearches().length == 0 ) 238 { 239 return ( this.mode == MODE_RETURNING_ATTRIBUTES_ONLY || this.mode == MODE_NORMAL 240 || this.mode == MODE_DN_ONLY || ( this.mode == MODE_INCLUDE_OPERATIONAL_ATTRIBUTES && showOperational ) ); 241 } 242 if ( getSelectedEntries().length + getSelectedSearchResults().length + getSelectedBookmarks().length > 0 243 && getSelectedSearches().length == 0 ) 244 { 245 return ( this.mode == MODE_NORMAL || this.mode == MODE_DN_ONLY || ( this.mode == MODE_INCLUDE_OPERATIONAL_ATTRIBUTES && showOperational ) ); 246 } 247 if ( getSelectedEntries().length + getSelectedSearchResults().length + getSelectedBookmarks().length == 0 248 && getSelectedSearches().length == 1 && getSelectedSearches()[0].getSearchResults() != null 249 && getSelectedSearches()[0].getSearchResults().length > 0 ) 250 { 251 return ( this.mode != MODE_INCLUDE_OPERATIONAL_ATTRIBUTES || ( this.mode == MODE_INCLUDE_OPERATIONAL_ATTRIBUTES && showOperational ) ); 252 } 253 return false; 254 } 255 256 257 263 protected void copyToClipboard( String text ) 264 { 265 Clipboard clipboard = null; 266 try 267 { 268 clipboard = new Clipboard( Display.getCurrent() ); 269 clipboard.setContents( new Object [] 270 { text }, new Transfer[] 271 { TextTransfer.getInstance() } ); 272 } 273 finally 274 { 275 if ( clipboard != null ) 276 clipboard.dispose(); 277 } 278 } 279 } 280 | Popular Tags |