1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 package org.apache.openoffice.comp.sdbc.dbtools.comphelper; 23 24 import com.sun.star.container.NoSuchElementException; 25 import com.sun.star.container.XEnumeration; 26 import com.sun.star.container.XNameAccess; 27 import com.sun.star.lang.EventObject; 28 import com.sun.star.lang.WrappedTargetException; 29 import com.sun.star.lang.XComponent; 30 import com.sun.star.lang.XEventListener; 31 import com.sun.star.lib.uno.helper.WeakBase; 32 import com.sun.star.uno.UnoRuntime; 33 34 public class OEnumerationByName extends WeakBase implements XEnumeration, XEventListener { 35 private String[] names; 36 private int position; 37 private XNameAccess collection; 38 private boolean isListening; 39 OEnumerationByName(XNameAccess collection)40 public OEnumerationByName(XNameAccess collection) { 41 this.collection = collection; 42 names = collection.getElementNames(); 43 startDisposeListening(); 44 } 45 46 @Override disposing(EventObject event)47 public void disposing(EventObject event) { 48 synchronized (this) { 49 if (event.Source == collection) { 50 collection = null; 51 } 52 } 53 } 54 55 @Override hasMoreElements()56 public boolean hasMoreElements() { 57 synchronized (this) { 58 if (collection != null) { 59 if (position < names.length) { 60 return true; 61 } else { 62 stopDisposeListening(); 63 collection = null; 64 } 65 } 66 return false; 67 } 68 } 69 70 @Override nextElement()71 public Object nextElement() 72 throws NoSuchElementException, WrappedTargetException { 73 Object value = null; 74 synchronized (this) { 75 if (collection != null) { 76 if (position < names.length) { 77 value = collection.getByName(names[position++]); 78 } 79 if (position >= names.length) { 80 stopDisposeListening(); 81 collection = null; 82 } 83 } 84 } 85 if (value == null) { 86 throw new NoSuchElementException(); 87 } 88 return value; 89 } 90 startDisposeListening()91 private void startDisposeListening() { 92 synchronized (this) { 93 if (isListening) { 94 return; 95 } 96 XComponent component = UnoRuntime.queryInterface(XComponent.class, collection); 97 if (component != null) { 98 component.addEventListener(this); 99 isListening = true; 100 } 101 } 102 } 103 stopDisposeListening()104 private void stopDisposeListening() { 105 synchronized (this) { 106 if (!isListening) { 107 return; 108 } 109 XComponent component = UnoRuntime.queryInterface(XComponent.class, collection); 110 if (component != null) { 111 component.removeEventListener(this); 112 isListening = false; 113 } 114 } 115 } 116 } 117