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 /* 23 * To change this template, choose Tools | Templates 24 * and open the template in the editor. 25 */ 26 27 package com.sun.star.wizards.common; 28 29 import com.sun.star.beans.PropertyAttribute; 30 import com.sun.star.beans.PropertyState; 31 import com.sun.star.beans.PropertyValue; 32 import com.sun.star.uno.UnoRuntime; 33 import com.sun.star.uno.XInterface; 34 import java.util.HashMap; 35 import java.util.Iterator; 36 import java.util.Map.Entry; 37 38 /** 39 * 40 * @author frank.schoenheit@sun.com 41 */ 42 public class NamedValueCollection 43 { 44 final private HashMap< String, Object > m_values = new HashMap< String, Object >(); 45 NamedValueCollection()46 public NamedValueCollection() 47 { 48 } 49 NamedValueCollection( final PropertyValue[] i_values )50 public NamedValueCollection( final PropertyValue[] i_values ) 51 { 52 for ( int i = 0; i < i_values.length; ++i ) 53 m_values.put( i_values[i].Name, i_values[i].Value ); 54 } 55 put( final String i_name, final Object i_value )56 public final void put( final String i_name, final Object i_value ) 57 { 58 m_values.put( i_name, i_value ); 59 } 60 61 @SuppressWarnings("unchecked") getOrDefault( final String i_key, final T i_default )62 public final < T > T getOrDefault( final String i_key, final T i_default ) 63 { 64 if ( m_values.containsKey( i_key ) ) 65 { 66 final Object value = m_values.get( i_key ); 67 try 68 { 69 return (T)value; 70 } 71 catch ( ClassCastException e ) { } 72 } 73 return i_default; 74 } 75 76 @SuppressWarnings("unchecked") queryOrDefault( final String i_key, final T i_default, Class i_interfaceClass )77 public final < T extends XInterface > T queryOrDefault( final String i_key, final T i_default, Class i_interfaceClass ) 78 { 79 if ( m_values.containsKey( i_key ) ) 80 { 81 final Object value = m_values.get( i_key ); 82 return (T)UnoRuntime.queryInterface( i_interfaceClass, value ); 83 } 84 return i_default; 85 } 86 has( final String i_key )87 public final boolean has( final String i_key ) 88 { 89 return m_values.containsKey( i_key ); 90 } 91 getPropertyValues()92 public final PropertyValue[] getPropertyValues() 93 { 94 PropertyValue[] values = new PropertyValue[ m_values.size() ]; 95 96 Iterator< Entry< String, Object > > iter = m_values.entrySet().iterator(); 97 int i = 0; 98 while ( iter.hasNext() ) 99 { 100 Entry< String, Object > entry = iter.next(); 101 values[i++] = new PropertyValue( 102 entry.getKey(), 103 0, 104 entry.getValue(), 105 PropertyState.DIRECT_VALUE 106 ); 107 } 108 109 return values; 110 } 111 } 112