001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.main;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Component;
023: import java.awt.Container;
024: import java.awt.Dimension;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import java.util.Collection;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.LinkedList;
031:
032: import javax.swing.AbstractButton;
033: import javax.swing.BorderFactory;
034: import javax.swing.ButtonGroup;
035: import javax.swing.Icon;
036: import javax.swing.JPanel;
037: import javax.swing.event.ChangeEvent;
038: import javax.swing.event.ChangeListener;
039:
040: import com.jeta.forms.beanmgr.BeanManager;
041: import com.jeta.forms.components.line.HorizontalLineComponent;
042: import com.jeta.forms.gui.components.ComponentFactory;
043: import com.jeta.forms.gui.components.ComponentSource;
044: import com.jeta.forms.logger.FormsLogger;
045: import com.jeta.open.gui.framework.JETAController;
046: import com.jeta.open.gui.framework.JETAPanel;
047: import com.jeta.open.i18n.I18N;
048: import com.jeta.open.registry.JETARegistry;
049: import com.jeta.open.support.EmptyCollection;
050: import com.jeta.swingbuilder.gui.beanmgr.DefaultBeanManager;
051: import com.jeta.swingbuilder.gui.components.EmbeddedFormComponentFactory;
052: import com.jeta.swingbuilder.gui.components.GenericComponentFactory;
053: import com.jeta.swingbuilder.gui.components.LinkedFormComponentFactory;
054: import com.jeta.swingbuilder.gui.components.SwingComponentFactory;
055: import com.jeta.swingbuilder.gui.utils.FormDesignerUtils;
056: import com.jeta.swingbuilder.resources.Icons;
057: import com.jeta.swingbuilder.store.RegisteredBean;
058: import com.jgoodies.forms.layout.CellConstraints;
059: import com.jgoodies.forms.layout.FormLayout;
060: import com.jgoodies.forms.layout.RowSpec;
061:
062: /**
063: * The frame used to contain the toolbar for editing forms.
064: *
065: * @author Jeff Tassin
066: */
067: public class ComponentsToolBar extends JETAPanel implements
068: ComponentSource {
069: public static final int MAX_TOOLBAR_ROWS = 20;
070:
071: public static final String ID_SELECTION_TOOL = "selection.tool";
072: public static final String ID_EMBEDDED_FORM_COMPONENT = "embedded.form.tool";
073: public static final String ID_LINKED_FORM_COMPONENT = "linked.form.tool";
074: public static final String ID_GENERIC_COMPONENT = "generic.component";
075: public static final String ID_TEST_COMPONENT = "com.jeta.swingbuilder.gui.components.TestBeanFactory";
076:
077: /** the selected tool */
078: private String m_current_tool = ID_SELECTION_TOOL;
079:
080: /**
081: * Factories for creating components
082: */
083: private HashMap m_factories = new HashMap();
084:
085: /** @param model */
086: public ComponentsToolBar() {
087: try {
088: FormLayout layout = new FormLayout("pref", "fill:pref:grow");
089: setLayout(layout);
090: CellConstraints cc = new CellConstraints();
091: add(createToolbar(), cc.xy(1, 1));
092: } catch (Exception e) {
093: FormsLogger.debug(e);
094: }
095: }
096:
097: /** Creates a button for our toolbar */
098: private Component createPaletteButton(Icon icon, String cmd,
099: String tooltip) {
100: AbstractButton btn = new javax.swing.JToggleButton();
101: Dimension d = new Dimension(20, 20);
102: btn.addChangeListener(new ChangeListener() {
103: public void stateChanged(ChangeEvent e) {
104: AbstractButton src = (AbstractButton) e.getSource();
105: JPanel btn_panel = (JPanel) src.getParent();
106: if (src.isSelected()) {
107: btn_panel.setBackground(java.awt.Color.lightGray);
108: btn_panel.setBorder(BorderFactory
109: .createLineBorder(java.awt.Color.gray));
110: } else {
111: btn_panel.setBackground(javax.swing.UIManager
112: .getColor("control"));
113: btn_panel.setBorder(BorderFactory
114: .createEmptyBorder(1, 1, 1, 1));
115: }
116: }
117: });
118:
119: btn.setOpaque(false);
120: btn.setContentAreaFilled(false);
121: btn.setFocusPainted(false);
122: btn.setPreferredSize(d);
123: btn.setMinimumSize(d);
124: btn.setSize(d);
125: btn.setBorderPainted(false);
126: btn.setIcon(icon);
127: btn.setActionCommand(cmd);
128: btn.setName(cmd);
129: if (tooltip != null)
130: btn.setToolTipText(tooltip);
131: return btn;
132: }
133:
134: /**
135: * Creates the java beans palette toolbar.
136: *
137: */
138: private Container createToolbar() {
139:
140: LinkedList button_list = new LinkedList();
141: button_list.addAll(registerDefaultBeans());
142: button_list.addAll(registerImportedBeans());
143:
144: StringBuffer colspec = new StringBuffer();
145: StringBuffer rowspec = new StringBuffer();
146:
147: int cols = button_list.size() / MAX_TOOLBAR_ROWS
148: + (button_list.size() % MAX_TOOLBAR_ROWS == 0 ? 0 : 1);
149: int rows = Math.min(button_list.size(), MAX_TOOLBAR_ROWS);
150: for (int col = 1; col <= cols; col++) {
151: if (col > 1)
152: colspec.append(",");
153:
154: colspec.append("pref");
155: }
156:
157: for (int row = 1; row <= rows; row++) {
158: if (row > 1)
159: rowspec.append(",");
160:
161: rowspec.append("pref");
162: }
163:
164: ButtonGroup bgroup = new ButtonGroup();
165:
166: JETAPanel toolbar = new JETAPanel();
167: toolbar.setBorder(javax.swing.BorderFactory.createEmptyBorder(
168: 0, 4, 0, 4));
169: FormLayout layout = new FormLayout(colspec.toString(), rowspec
170: .toString());
171: CellConstraints cc = new CellConstraints();
172: toolbar.setLayout(layout);
173:
174: int row_count = button_list.size() / cols
175: + (button_list.size() % cols == 0 ? 0 : 1);
176: Iterator iter = button_list.iterator();
177: for (int col = 1; col <= cols; col++) {
178: for (int row = 1; row <= row_count; row++) {
179: if (iter.hasNext()) {
180: Component btn = (Component) iter.next();
181: bgroup.add((AbstractButton) btn);
182: JPanel btn_panel = new JPanel(new BorderLayout());
183: btn_panel.setBorder(BorderFactory
184: .createEmptyBorder(1, 1, 1, 1));
185: btn_panel.add(btn, BorderLayout.CENTER);
186: toolbar.add(btn_panel, cc.xy(col, row));
187: } else {
188: break;
189: }
190: }
191: }
192: layout.insertRow(1, new RowSpec("5px"));
193: toolbar.setController(new ToolbarController(toolbar,
194: button_list));
195:
196: assert (!iter.hasNext());
197: return toolbar;
198: }
199:
200: /** ComponentSource imlementation */
201: public ComponentFactory getComponentFactory() {
202: if (!isSelectionTool()) {
203: String toolname = getCurrentTool();
204: return (ComponentFactory) m_factories.get(toolname);
205: }
206: return null;
207: }
208:
209: /** @return the classname of the currently selected tool in the tool palette. */
210: public String getCurrentTool() {
211: return m_current_tool;
212: }
213:
214: /** ComponentSource imlementation */
215: public boolean isSelectionTool() {
216: return (m_current_tool == ID_SELECTION_TOOL);
217: }
218:
219: /**
220: * Registers all default bean factories
221: */
222: private Collection registerDefaultBeans() {
223: try {
224: LinkedList btns = new LinkedList();
225:
226: btns.add(registerBean(ID_SELECTION_TOOL, I18N
227: .getLocalizedMessage("Selection Tool"), null,
228: Icons.MOUSE_16));
229:
230: DefaultBeanManager bm = (DefaultBeanManager) JETARegistry
231: .lookup(DefaultBeanManager.COMPONENT_ID);
232: if (bm == null)
233: return btns;
234:
235: boolean add_forms = true;
236: Collection default_beans = bm.getDefaultBeans();
237: Iterator iter = default_beans.iterator();
238: while (iter.hasNext()) {
239: RegisteredBean rbean = (RegisteredBean) iter.next();
240: Icon icon = rbean.getIcon();
241: if (icon == null) {
242: icon = FormDesignerUtils.loadImage(Icons.BEAN_16);
243: }
244:
245: String bname = rbean.getClassName();
246: try {
247:
248: /**
249: * insert the form beans just before the horizontal line
250: * component - legacy reasons
251: */
252: if (HorizontalLineComponent.class.getName().equals(
253: bname)
254: && add_forms) {
255: btns
256: .add(registerBean(
257: ID_EMBEDDED_FORM_COMPONENT,
258: I18N
259: .getLocalizedMessage("Embedded Form"),
260: new EmbeddedFormComponentFactory(
261: this ),
262: Icons.EMBEDDED_FORM_16));
263:
264: btns.add(registerBean(ID_LINKED_FORM_COMPONENT,
265: I18N.getLocalizedMessage("LinkedForm"),
266: new LinkedFormComponentFactory(this ),
267: Icons.LINKED_FORM_16));
268:
269: btns
270: .add(registerBean(
271: ID_GENERIC_COMPONENT,
272: I18N
273: .getLocalizedMessage("Generic Component"),
274: new GenericComponentFactory(
275: this ),
276: Icons.GENERIC_COMPONENT_16));
277:
278: add_forms = false;
279: }
280:
281: Class bean_class = bm.getBeanClass(bname);
282: btns.add(registerBean(bname,
283: rbean.getDescription(),
284: new SwingComponentFactory(this , bname),
285: icon));
286: } catch (Exception e) {
287: FormsLogger.debug(e);
288: }
289: }
290: return btns;
291: } catch (Exception e) {
292: FormsLogger.debug(e);
293: }
294: return EmptyCollection.getInstance();
295:
296: }
297:
298: /**
299: * Registers all imported java beans
300: */
301: private Collection registerImportedBeans() {
302: try {
303: LinkedList btns = new LinkedList();
304: BeanManager bm = (BeanManager) JETARegistry
305: .lookup(DefaultBeanManager.COMPONENT_ID);
306: if (bm == null)
307: return btns;
308:
309: Collection imported_beans = bm.getImportedBeans();
310: Iterator iter = imported_beans.iterator();
311: while (iter.hasNext()) {
312: RegisteredBean rbean = (RegisteredBean) iter.next();
313: Icon icon = rbean.getIcon();
314: if (icon == null) {
315: icon = FormDesignerUtils.loadImage(Icons.BEAN_16);
316: }
317:
318: String bname = rbean.getClassName();
319: try {
320: Class bean_class = bm.getBeanClass(bname);
321: btns.add(registerBean(bname,
322: rbean.getDescription(),
323: new SwingComponentFactory(this , bname),
324: icon));
325: } catch (Exception e) {
326: FormsLogger.debug(e);
327: }
328: }
329: return btns;
330: } catch (Exception e) {
331: FormsLogger.debug(e);
332: }
333: return EmptyCollection.getInstance();
334: }
335:
336: /**
337: * Registers a Java bean for this toolbar
338: */
339: public Component registerBean(String toolName, String tooltip,
340: ComponentFactory factory, String imageName) {
341: return registerBean(toolName, tooltip, factory,
342: FormDesignerUtils.loadImage(imageName));
343: }
344:
345: /**
346: * Registers a Java bean for this toolbar
347: */
348: public Component registerBean(String toolName, String tooltip,
349: ComponentFactory factory, Icon icon) {
350: if (factory != null) {
351: if (m_factories.get(toolName) != null) {
352: System.out
353: .println("ComponentsToolbar.registerBean: factory registered twice: "
354: + toolName);
355: }
356: m_factories.put(toolName, factory);
357: }
358: return createPaletteButton(icon, toolName, tooltip);
359: }
360:
361: /**
362: * Reloads the toolbar
363: */
364: public void reload() {
365: m_factories.clear();
366: removeAll();
367: CellConstraints cc = new CellConstraints();
368: add(createToolbar(), cc.xy(1, 1));
369: revalidate();
370: }
371:
372: /**
373: * ComponentSource implementation Sets the active component factory to the
374: * selection tool
375: */
376: public void setSelectionTool() {
377: AbstractButton btn = (AbstractButton) getComponentByName(ID_SELECTION_TOOL);
378: btn.setSelected(true);
379: m_current_tool = ID_SELECTION_TOOL;
380: }
381:
382: /** The controller for this frame */
383: public class ToolbarController extends JETAController {
384: public ToolbarController(JETAPanel view, Collection btns) {
385: super (view);
386:
387: Iterator iter = btns.iterator();
388: while (iter.hasNext()) {
389: Component comp = (Component) iter.next();
390: if (comp instanceof AbstractButton) {
391: ((AbstractButton) comp)
392: .addActionListener(new StandardComponentAction(
393: comp.getName()));
394: }
395: }
396: }
397: }
398:
399: /** ActionHandler for standard Swing components */
400: public class StandardComponentAction implements ActionListener {
401: private String m_compname;
402:
403: public StandardComponentAction(String compName) {
404: m_compname = compName;
405: }
406:
407: public void actionPerformed(ActionEvent evt) {
408: m_current_tool = m_compname;
409: }
410: }
411:
412: }
|