Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/paradoxpoint/libitina/option/OptionsContainer.java


1   /*
2    * Libitina - Funeral Monument Image Compositor
3    * Copyright (C) 2003,2004  Luke Imhoff
4    *
5    * Contact Info:
6    * luke@paradoxpoint.com
7    * Luke Imhoff
8    * 2514 Pied Piper Lane
9    * Wausau, WI 54403 
10   *
11   * This program is free software; you can redistribute it and/or
12   * modify it under the terms of the GNU General Public License
13   * as published by the Free Software Foundation; either version 2
14   * of the License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   *
21   * You should have received a copy of the GNU General Public License
22   * along with this program; if not, write to the Free Software
23   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24   *
25   * OptionsContainer.java
26   *
27   * Created on June 9, 2003, 8:23 AM
28   */
29  
30  package com.paradoxpoint.libitina.option;
31  
32  import java.awt.*;
33  import java.awt.event.*;
34  import java.util.*;
35  
36  import javax.swing.*;
37  import javax.swing.event.*;
38  
39  /** A Container that can manage multiple options and their associated
40   * <CODE>OptionListeners</CODE>
41   * @author Luke Imhoff
42   */
43  public class OptionsContainer implements ActionListener {
44      
45      /** list of OptionListeners */    
46      protected EventListenerList listenerList = new EventListenerList();
47      /** current event fired by invoking fireOptionStateChanged */    
48      protected OptionEvent optionEvent = null;
49      
50      
51      /** maps options to their editors */
52      protected HashMap optionEditors = new HashMap();
53      /** maps editors to their options */    
54      protected HashMap editorOptions = new HashMap();
55      
56      /** returns a list of <CODE>Option</CODE>s searchable by the <CODE>Option</CODE>'s
57       * name
58       */    
59      protected HashMap options = new HashMap();
60      
61      /** Creates a new instance of OptionsContainer */
62      public OptionsContainer() {
63      }
64      
65      /** Creates a new <CODE>Option</CODE> with the given name and adds it to the
66       * container
67       * @param name option name
68       * @see removeOption(String)
69       */    
70      public void addOption(String name) {
71          BasicOption temp = new BasicOption(name);
72          options.put(temp.getName(), temp);
73      }
74      
75      public void addOption(AbstractOption option) {
76          options.put(option.getName(), option);
77      }
78      
79      /** Return the <CODE>Option</CODE> with the given name
80       * @param name option name
81       * @return the option, or <CODE>null</CODE> if the option was not found
82       */    
83      public AbstractOption getOption(String name) {
84          return (AbstractOption) options.get(name);
85      }
86      
87      /** Removes the <CODE>Option</CODE> with the given name from the container.  NOTE: Does not
88       * remove any listeners associated with the <CODE>Option</CODE> from the list of
89       * <CODE>OptionListener</CODE>s maintained by the container
90       * @param name option name
91       * @see addOption(String)
92       */    
93      public void removeOption(String name) {
94          AbstractOption option = (AbstractOption) options.get(name);
95      }
96      
97      /** Sets the state of the option with the given name in this container to the given
98       * state
99       * @param name option name
100      * @param state new option state
101      * @see getOptionState(String)
102      */    
103     public void setOptionState(String name, Object state) {
104         AbstractOption option = (AbstractOption) options.get(name);
105         if (option != null) {
106             option.setState(state);
107             options.put(name, option);
108             fireOptionStateChanged(option);
109         }
110     }
111     
112     /** Returns the state of the option with the given name in this container
113      * @return the option state
114      * @param name the option name
115      * @see setOptionState(String, Object)
116      */    
117     public Object getOptionState(String name) {
118         AbstractOption option = (AbstractOption) options.get(name);
119         if (option != null)
120             return option.getState();
121         return null;
122     }
123     
124     /** Adds a listener to receive option events when the state of an option is changed. If l is <CODE>null</CODE>, no exception is thrown and no action is performed.
125      * @param l the listener to receive events
126      * @see removeOptionListener(OptionListener)
127      */    
128     public void addOptionListener(OptionListener l) {
129         if (l == null) return;
130         listenerList.add(l.getClass(), l);
131         if (l instanceof OptionButton) {
132             ((OptionButton) l).getButton().addActionListener(this);
133         }
134     }
135 
136     /** Removes an item listener. If l is null, no exception is thrown and no action is performed.
137      * @param l the listener being removed
138      * @see addOptionListener(OptionListener)
139      */    
140     public void removeOptionListener(OptionListener l) {
141         if (l != null)
142             listenerList.remove(OptionListener.class, l);
143     }
144 
145     // Notify all listeners that have registered interest for
146     // notification on this event type.  The event instance 
147     // is lazily created using the parameters passed into 
148     // the fire method.
149     /** Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the
150      * option parameter.
151      * @param option option that has changed state
152      */    
153     protected void fireOptionStateChanged(AbstractOption option) {
154         // reset option event
155         optionEvent = null;
156         // Guaranteed to return a non-null array
157         Object[] listeners = listenerList.getListenerList();
158         // Process the listeners last to first, notifying
159         // those that are interested in this event
160         for (int i = listeners.length-2; i>=0; i-=2) {
161             if (OptionListener.class.isAssignableFrom((Class) listeners[i]) && ((OptionListener) listeners[i+1]).getOption().equals(option)) {
162                 // Lazily create the event:
163                 if (optionEvent == null)
164                     optionEvent = new OptionEvent(this, option);
165             ((OptionListener)listeners[i+1]).optionStateChanged(optionEvent);
166             }
167         }   
168     }
169 
170     /** Invoked when an action occurs.
171      * @param e the event triggering this call
172      */
173     public void actionPerformed(ActionEvent e) {
174         AbstractButton editor = (AbstractButton) e.getSource();
175         EventListener[] listeners = listenerList.getListeners(OptionButton.class);
176         if (listeners == null) return;
177         for (int i = 0; i < listeners.length; i++) {
178             if (((OptionButton) listeners[i]).getButton().equals(editor)) {
179                 OptionButton ob = (OptionButton) listeners[i];
180                 OptionEditorStateMap oesm = ob.getStateMap();
181                 Object optionState = oesm.getOptionState(Boolean.valueOf(editor.isSelected()));
182                 ob.getOption().setState(optionState);
183                 fireOptionStateChanged(ob.getOption());
184             }
185         }
186     }
187     
188 }