View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.gui.swing;
18  
19  import javax.swing.JComponent;
20  import javax.swing.JPanel;
21  
22  import org.noos.xing.mydoggy.Content;
23  import org.noos.xing.mydoggy.ContentManager;
24  import org.noos.xing.mydoggy.DockedTypeDescriptor;
25  import org.noos.xing.mydoggy.ToolWindow;
26  import org.noos.xing.mydoggy.ToolWindowActionHandler;
27  import org.noos.xing.mydoggy.ToolWindowAnchor;
28  import org.noos.xing.mydoggy.ToolWindowManager;
29  import org.noos.xing.mydoggy.ToolWindowTab;
30  import org.noos.xing.mydoggy.ToolWindowType;
31  import org.noos.xing.mydoggy.plaf.MyDoggyToolWindowManager;
32  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
33  
34  import ch.elca.el4j.services.gui.swing.frames.ApplicationFrame;
35  import ch.elca.el4j.services.gui.swing.frames.ContentApplicationFrame;
36  import ch.elca.el4j.services.gui.swing.frames.ContentConfiguration;
37  import ch.elca.el4j.services.gui.swing.frames.ToolWindowTabApplicationFrame;
38  import ch.elca.el4j.services.gui.swing.frames.ToolWindowTabConfiguration;
39  import ch.elca.el4j.services.gui.swing.wrapper.ContentWrapperFactory;
40  import ch.elca.el4j.services.gui.swing.wrapper.ToolWindowWrapperFactory;
41  
42  /**
43   * Parent class for Docking applications.
44   *
45   * @svnLink $Revision: 3956 $;$Date: 2009-10-21 11:14:09 +0200 (Mi, 21. Okt 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/swing/src/main/java/ch/elca/el4j/services/gui/swing/DockingApplication.java $
46   *
47   * @author Stefan Wismer (SWI)
48   */
49  public abstract class DockingApplication extends GUIApplication {
50  	/**
51  	 * The tool window manager.
52  	 */
53  	protected ToolWindowManager toolWindowManager = null;
54  	
55  	/**
56  	 * @return    the current {@link ToolWindowManager}.
57  	 */
58  	public ToolWindowManager getToolWindowManager() {
59  		if (toolWindowManager == null) {
60  			// Create a new instance of MyDoggyToolWindowManager
61  			toolWindowManager = new MyDoggyToolWindowManager();
62  		}
63  		return toolWindowManager;
64  	}
65  	
66  	/** {@inheritDoc} */
67  	@Override
68  	public void show(JComponent component) {
69  		show(ContentWrapperFactory.wrap(component));
70  	}
71  	
72  	/** {@inheritDoc} */
73  	@Override
74  	public void show(ApplicationFrame frame) {
75  		if (frame instanceof ContentApplicationFrame) {
76  			showContent((ContentApplicationFrame) frame);
77  		}
78  	}
79  	
80  	/**
81  	 * @param beanName        the Spring bean name
82  	 * @param toolWindowId    the id of the toolWindow to add a new tab
83  	 * @param anchor          the anchor of the docked window
84  	 */
85  	public void show(String beanName, String toolWindowId, ToolWindowAnchor anchor)
86  		throws NoSuchBeanDefinitionException {
87  		
88  		if (!springContext.containsBean(beanName)) {
89  			throw new NoSuchBeanDefinitionException(beanName);
90  		}
91  		show((JPanel) springContext.getBean(beanName), toolWindowId, anchor);
92  	}
93  	
94  	/**
95  	 * @param component       the component to show
96  	 * @param toolWindowId    the id of the toolWindow to add a new tab
97  	 * @param anchor          the anchor of the docked window
98  	 */
99  	public void show(JComponent component, String toolWindowId, ToolWindowAnchor anchor) {
100 		ToolWindowTabApplicationFrame frame = ToolWindowWrapperFactory.wrap(component);
101 		frame.getConfiguration().setId(toolWindowId);
102 		frame.getConfiguration().setAnchor(anchor);
103 		showToolWindow(frame);
104 	}
105 	
106 	/**
107 	 * Show a frame as content.
108 	 * @param frame    the frame to show as content
109 	 */
110 	public void showContent(ContentApplicationFrame frame) {
111 		ContentManager contentManager = toolWindowManager.getContentManager();
112 		Content content = (Content) frame.getFrame();
113 		if (content == null) {
114 			ContentConfiguration config = frame.getConfiguration();
115 			content = contentManager.addContent(config.getId(), config.getTitle(),
116 				config.getIcon(), config.getComponent(), config.getToolTip(), config.getConstraints());
117 			// default settings
118 			content.getContentUI().setMinimizable(false);
119 			content.getContentUI().setDetachable(false);
120 			
121 			frame.setFrame(content);
122 			frame.setContent(config.getComponent());
123 		}
124 		
125 		super.show(frame);
126 		
127 		frame.setSelected(true);
128 	}
129 	
130 	/**
131 	 * Show a frame as toolbox.
132 	 * @param frame           the frame to show as toolbox
133 	 */
134 	public void showToolWindow(final ToolWindowTabApplicationFrame frame) {
135 		if (frame.getFrame() == null) {
136 			ToolWindowTab toolWindowTab = null;
137 			ToolWindowTabConfiguration config = frame.getConfiguration();
138 			ToolWindow toolWindow = toolWindowManager.getToolWindow(config.getId());
139 			if (toolWindow == null) {
140 				toolWindow = toolWindowManager.registerToolWindow(config.getId(), config.getTitle(),
141 					config.getIcon(), config.getComponent(), config.getAnchor());
142 				
143 				DockedTypeDescriptor typeDescriptor = (DockedTypeDescriptor)
144 					toolWindow.getTypeDescriptor(ToolWindowType.DOCKED);
145 				
146 				// default behavior: close tool window when clicking on X
147 				typeDescriptor.setToolWindowActionHandler(new ToolWindowActionHandler() {
148 					public void onHideButtonClick(ToolWindow toolWindow) {
149 						frame.close();
150 						toolWindowManager.unregisterToolWindow(toolWindow.getId());
151 					}
152 				});
153 				toolWindowTab = toolWindow.getToolWindowTabs()[0];
154 			} else {
155 				toolWindowTab = toolWindow.addToolWindowTab(
156 					((ToolWindowTab) ToolWindowWrapperFactory.wrap(frame.getContent()).getFrame()).getOwner());
157 			}
158 			toolWindow.setAvailable(true);
159 			
160 			frame.setFrame(toolWindowTab);
161 		}
162 	}
163 }