View Javadoc

1   package com.explosion.expfmodules.texteditor;
2   
3   import java.awt.Component;
4   import java.awt.event.ActionEvent;
5   import java.awt.event.FocusEvent;
6   import java.awt.event.FocusListener;
7   import java.util.HashMap;
8   import java.util.Map;
9   
10  import javax.swing.SwingUtilities;
11  
12  import com.explosion.datastream.exql.extensions.ExtensionsLoader;
13  import com.explosion.expf.Application;
14  import com.explosion.expf.ExpActionListener;
15  import com.explosion.expf.ExpConstants;
16  import com.explosion.expf.ExpFrame;
17  
18  
19  /* =============================================================================
20   *       
21   *     Copyright 2004 Stephen Cowx
22   *
23   *     Licensed under the Apache License, Version 2.0 (the "License");
24   *     you may not use this file except in compliance with the License.
25   *     You may obtain a copy of the License at
26   *
27   *     http://www.apache.org/licenses/LICENSE-2.0
28   *
29   *     Unless required by applicable law or agreed to in writing, software
30   *     distributed under the License is distributed on an "AS IS" BASIS,
31   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32   *     See the License for the specific language governing permissions and
33   *     limitations under the License.
34   * 
35   * =============================================================================
36   */
37  
38  /***
39   * @author Stephen
40   * Created on 28-Jun-2004
41   */
42  public class TextComponentLocalListener implements ExpActionListener, FocusListener
43  {
44    private static org.apache.log4j.Logger log = org.apache.log4j.LogManager.getLogger(TextComponentLocalListener.class);
45    private HashMap map;
46    private int newDocumentNumbers = 1;
47    private Editable component;
48    private Component highLevelParent;
49    
50    /***
51     * Constructor for TextEditorLocalListener.
52     */
53    public TextComponentLocalListener(Editable component, Component highLevelParent)
54    {
55      this.component = component;
56      this.highLevelParent = highLevelParent;
57      setCookie(1);
58      component.addFocusListener(this);
59      
60      map = new HashMap();
61      map.put(ExpConstants.MENU_CUT,ExpConstants.MENU_CUT);
62      map.put(ExpConstants.MENU_COPY,ExpConstants.MENU_COPY);
63      map.put(ExpConstants.MENU_PASTE,ExpConstants.MENU_PASTE);
64      map.put(ExpConstants.MENU_SELECTALL,ExpConstants.MENU_SELECTALL);
65      map.put(ExpConstants.MENU_CLEAR,ExpConstants.MENU_CLEAR);    
66      map.put(ExpConstants.MENU_UNDO,ExpConstants.MENU_UNDO); 
67      map.put(ExpConstants.MENU_REDO,ExpConstants.MENU_REDO); 
68      map.put(ExpConstants.MENU_PRINT,ExpConstants.MENU_PRINT);
69      map.put(ExpConstants.MENU_PRINTSETUP,ExpConstants.MENU_PRINTSETUP);
70  	map.put(TextEditorConstants.TO_UPPER,TextEditorConstants.TO_UPPER);
71  	map.put(TextEditorConstants.TO_LOWER,TextEditorConstants.TO_LOWER);
72      
73  	((ExpFrame) Application.getApplicationFrame()).getListener().addComponentActionListener(this,highLevelParent,component);
74  	log.debug("Component for TextComponentLocalListener is " + highLevelParent);
75    }
76  
77    /***
78     * @see package com.explosion.expf.Interfaces.ExpActionListener#getListensFor()
79     */
80    public Map getListensFor()
81    {
82      return map;
83    }
84  
85    /***
86     * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
87     */
88    public void actionPerformed(ActionEvent e)
89    {
90      try
91      {
92        if (e.getActionCommand().equals(ExpConstants.MENU_CUT))  { component.cut(); }
93          else if (e.getActionCommand().equals(ExpConstants.MENU_COPY))  { component.copy(); }
94          else if (e.getActionCommand().equals(ExpConstants.MENU_PASTE))  { component.paste(); }
95          else if (e.getActionCommand().equals(ExpConstants.MENU_SELECTALL))  { component.selectAll(); }
96          else if (e.getActionCommand().equals(ExpConstants.MENU_CLEAR))  { component.clear(); }
97          else if (e.getActionCommand().equals(ExpConstants.MENU_UNDO))  { component.undo() ;}
98          else if (e.getActionCommand().equals(ExpConstants.MENU_REDO))  { component.redo(); }
99          else if (e.getActionCommand().equals(TextEditorConstants.TO_UPPER))  
100 		{ 
101 		   String text = component.getTextComponent().getSelectedText();
102 		   component.getTextComponent().replaceSelection(text.toUpperCase());
103 		}
104 		else if (e.getActionCommand().equals(TextEditorConstants.TO_LOWER))  
105 		{ 
106 		   String text = component.getTextComponent().getSelectedText();
107 		   component.getTextComponent().replaceSelection(text.toLowerCase());
108 		}
109     }
110     catch (Exception ex)
111     {
112        com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, "Exception caught while responding to SimpleProcess Event." );
113     } 
114   }
115   
116   public void setCookie(int number)
117   {
118   	/* Initialise cookies */
119     Application.ensureComponentCookie(ExpConstants.MENU_EDIT,number,highLevelParent,component);
120 
121     Application.ensureComponentCookie(ExpConstants.MENU_SELECTALL,number,highLevelParent,component);
122     Application.ensureComponentCookie(ExpConstants.MENU_CLEAR,number,highLevelParent,component);
123 
124     Application.ensureComponentCookie(ExpConstants.MENU_PRINT,number,highLevelParent,component);
125     Application.ensureComponentCookie(ExpConstants.MENU_PRINTSETUP,number,highLevelParent,component);
126     
127     Application.ensureComponentCookie(ExpConstants.MENU_CUT,number,highLevelParent,component);	
128     Application.ensureComponentCookie(ExpConstants.MENU_COPY,number,highLevelParent,component);	
129     Application.ensureComponentCookie(ExpConstants.MENU_PASTE,number,highLevelParent,component);	
130     
131     Application.ensureComponentCookie(ExpConstants.MENU_PRINT,number,highLevelParent,component);
132   }
133   
134   public void focusGained(FocusEvent e)
135   {
136   	Application.setActiveComponent(highLevelParent,component);
137   }
138   
139   public void focusLost(FocusEvent e)
140   {
141   	Component opposite = e.getOppositeComponent();
142     if (opposite != null)
143   	{
144   	   if (SwingUtilities.isDescendingFrom(opposite,highLevelParent))
145   	      Application.unsetActiveComponent();
146   	}
147   }
148 }