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

Quick Search    Search Deep

Source code: javax/ide/command/Context.java


1   package javax.ide.command;
2   
3   import java.util.HashMap;
4   import javax.ide.model.Document;
5   import javax.ide.model.Element;
6   import javax.ide.model.Project;
7   
8   /**
9    * The Context class provides information about the state of the IDE when
10   * a command is in the process of being executed. Such information 
11   * includes:<p>
12   * 
13   * <ul>
14   *    <li>the {@link Document} the user is working on,</li><p>
15   *    <li>the {@link Project} the user is working on, and</li><p>
16   *    <li>the currently selected {@link Element}s in the active 
17   *    {@link javax.ide.view.View}.</li><p>
18   * </ul>
19   *
20   * In addition, to this information, the context allows clients to store
21   * arbitrary data to pass on to a command. Such data is stored in the 
22   * context under a client specified string key. The data can be retrieved 
23   * using that same key.
24   */
25  public final class Context 
26  {
27    private HashMap _properties = new HashMap();
28    private Document _document;
29    private Project _project;
30    private Element[] _selection = new Element[ 0 ];
31  
32    /**
33     * Constructor.
34     * @param document The document the user is working on when a command is 
35     * executed.
36     * @param project The project the document is in.
37     * @param selection The selected objects.
38     */
39    public Context( Document document, 
40                    Project project,
41                    Element[] selection )
42    {
43      _document = document;
44      _project = project;
45      _selection = selection;
46    }
47  
48    /**
49     * Constructor.
50     * @param document The document the user is working on when a command is 
51     * executed.
52     * @param project The project the document is in.
53     */
54    public Context( Document document, 
55                    Project project)
56    {
57      this( document, project, null );
58    }
59  
60    /**
61     * The current {@link Project} the user is working on.
62     */
63    public Project getProject()
64    {
65      return _project;
66    }
67  
68    /**
69     * The current {@link Document} the user is working on.
70     */
71    public Document getDocument()
72    {
73      return _document;
74    }
75  
76    /**
77     * Get the currently selected objects. These are {@link Element}s
78     * that users can select inside IDE views.
79     *
80     * @return the currently selected objects. If nothing selected this method 
81     * returns an empty array.
82     */
83    public Element[] getSelection()
84    {
85      return _selection;
86    }
87    
88    /**
89     * Set the currently selected objects. These are {@link Element}s 
90     * that users can select inside IDE views.
91     *
92     * @param selection An array of {@link Element}s.
93     */
94    public void setSelection( Element[] selection )
95    {
96      _selection = selection != null ? selection : new Element[ 0 ];
97    }
98  
99    /**
100    * Retrieves the value associated with a property.  If no value
101    * exists for the requested property, the specified default value
102    * is returned. Use context properties to pass along extra data 
103    * meaningful to an extension.
104    *
105    * @param name the property key for which a value is desired.
106    * @param defaultValue the value to return if no value currently
107    * exists.
108    * 
109    * @return the value of the requested property, or the default value
110    * if the property does not exist.
111    */
112   public Object getProperty( String name, Object defaultValue )
113   {
114     final Object  property = _properties.get( name );
115 
116     return ( property == null ? defaultValue : property );
117   }
118 
119   /**
120    * Sets the value for a property. Setting a value to <CODE>null</CODE>
121    * removes that property.  Use context properties to pass along extra data 
122    * meaningful to an extension.
123    * 
124    * @param name the property key to set
125    * @param value the value to set
126    */
127   public void setProperty( String name, Object value )
128   {
129     if ( name != null )
130     {
131       if ( value == null )
132       {
133         _properties.remove( name );
134       }
135       else
136       {
137         _properties.put( name, value );
138       }
139     }
140   }
141 }