View Javadoc

1   package com.explosion.datastream.exql.gui;
2   
3   /* =============================================================================
4    *       
5    *     Copyright 2004 Stephen Cowx
6    *
7    *     Licensed under the Apache License, Version 2.0 (the "License");
8    *     you may not use this file except in compliance with the License.
9    *     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, software
14   *     distributed under the License is distributed on an "AS IS" BASIS,
15   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *     See the License for the specific language governing permissions and
17   *     limitations under the License.
18   * 
19   * =============================================================================
20   */
21  
22  import java.awt.Component;
23  import java.awt.event.ActionEvent;
24  import java.awt.event.FocusEvent;
25  import java.awt.event.FocusListener;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import javax.swing.SwingUtilities;
30  
31  import com.explosion.datastream.exql.EXQLConstants;
32  import com.explosion.expf.Application;
33  import com.explosion.expf.ExpActionListener;
34  import com.explosion.expf.ExpConstants;
35  import com.explosion.expf.ExpFrame;
36  import com.explosion.expfmodules.rdbmsconn.dbom.utils.SQLFormatter;
37  import com.explosion.expfmodules.texteditor.ExpfTextArea;
38  
39  /***
40   * @author Steve.Cowx
41   * Created on Jan 1, 2004
42   */
43  public class SQLTextActionsListener implements ExpActionListener, FocusListener
44  {
45  
46    private HashMap map;
47    private ExpfTextArea component;
48    private Component highLevelParent;
49    
50    /***
51     * Constructor for TextEditorLocalListener.
52     */
53    public SQLTextActionsListener(ExpfTextArea 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(EXQLConstants.MENU_FORMAT_SQL,EXQLConstants.MENU_FORMAT_SQL);
62      
63  	((ExpFrame) Application.getApplicationFrame()).getListener().addComponentActionListener(this,highLevelParent,component);
64    }
65  
66    /***
67     * @see package com.explosion.expf.Interfaces.ExpActionListener#getListensFor()
68     */
69    public Map getListensFor()
70    {
71  	return map;
72    }
73  
74    /***
75     * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
76     */
77    public void actionPerformed(ActionEvent e)
78    {
79  	try
80  	{
81  	  if (e.getActionCommand().equals(EXQLConstants.MENU_FORMAT_SQL))  
82  	  { 
83  		int startSelection = component.getSelectionStart();
84  		int endSelection = component.getSelectionEnd();
85  		boolean selectionMade = startSelection < endSelection;
86  	  	
87  		if (selectionMade)
88  		{
89  		   StringBuffer allText = new StringBuffer(component.getText());
90  		   allText.replace(startSelection,endSelection,SQLFormatter.format(component.getSelectedText()));
91  		   component.setText(allText.toString());
92  		}
93  		else
94  		{
95  			component.setText(SQLFormatter.format(component.getText()));
96  		}   
97  	   }
98  	}
99  	catch (Exception ex)
100 	{
101 	   com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, "Exception caught while responding to Event." );
102 	} 
103   }
104   
105   public void setCookie(int number)
106   {
107 	/* Initialise cookies */
108 	Application.ensureComponentCookie(EXQLConstants.MENU_FORMAT_SQL,number,highLevelParent,component);
109 	Application.ensureComponentCookie(ExpConstants.MENU_PRINT,number,highLevelParent,component);
110   }
111   
112   public void focusGained(FocusEvent e)
113   {
114 	Application.setActiveComponent(highLevelParent,component);
115   }
116   
117   public void focusLost(FocusEvent e)
118   {
119 	Component opposite = e.getOppositeComponent();
120 	if (opposite != null)
121 	{
122 	   if (SwingUtilities.isDescendingFrom(opposite,highLevelParent))
123 		  Application.unsetActiveComponent();
124 	}
125   }
126   
127  
128 }