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.beanmgr;
020:
021: import java.awt.Component;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.io.File;
025:
026: import javax.swing.JOptionPane;
027:
028: import com.jeta.forms.gui.common.FormException;
029: import com.jeta.forms.store.memento.IconMemento;
030: import com.jeta.open.gui.framework.JETAController;
031: import com.jeta.open.gui.framework.JETADialog;
032: import com.jeta.open.gui.utils.JETAToolbox;
033: import com.jeta.open.i18n.I18N;
034: import com.jeta.swingbuilder.gui.filechooser.FileChooserConfig;
035: import com.jeta.swingbuilder.gui.filechooser.TSFileChooserFactory;
036: import com.jeta.swingbuilder.gui.filechooser.TSFileFilter;
037: import com.jeta.swingbuilder.store.ImportedBeanInfo;
038:
039: /**
040: * The controller for the BeanManagerView
041: *
042: * @author Jeff Tassin
043: */
044: public class BeanManagerController extends JETAController {
045: /**
046: * The view
047: */
048: private BeanManagerView m_view;
049:
050: /**
051: * The beans table model
052: */
053: private BeansModel m_beansmodel;
054:
055: /**
056: * ctor
057: */
058: public BeanManagerController(BeanManagerView view) {
059: super (view);
060: m_view = view;
061: m_beansmodel = view.getBeansModel();
062: assignAction(BeanManagerNames.ID_ADD_BEAN, new AddBeanAction());
063: assignAction(BeanManagerNames.ID_DELETE_BEAN,
064: new DeleteBeanAction());
065: assignAction(BeanManagerNames.ID_SET_BEAN_ICON,
066: new SetBeanIconAction());
067: assignAction(BeanManagerNames.ID_ADD_JAR, new AddPathAction(
068: false));
069: assignAction(BeanManagerNames.ID_ADD_PATH, new AddPathAction(
070: true));
071: assignAction(BeanManagerNames.ID_DELETE_JAR,
072: new DeletePathAction());
073: }
074:
075: /**
076: * Adds a bean to the view
077: */
078: public class AddBeanAction implements ActionListener {
079: public void actionPerformed(ActionEvent evt) {
080: BeanDefinitionView view = new BeanDefinitionView();
081: JETADialog dlg = (JETADialog) JETAToolbox.createDialog(
082: JETADialog.class, m_view, true);
083: dlg.setPrimaryPanel(view);
084: dlg.setTitle(I18N.getLocalizedMessage("Bean Definition"));
085: dlg.setSize(dlg.getPreferredSize());
086: dlg.addValidator(new Object[] { view,
087: m_view.getBeanLoader() },
088: new BeanDefinitionValidator());
089: dlg.showCenter();
090: if (dlg.isOk()) {
091: BeanLoader loader = m_view.getBeanLoader();
092: try {
093: Component comp = loader.createBean(view
094: .getBeanName());
095: ImportedBeanInfo info = new ImportedBeanInfo(view
096: .getBeanName(), view.isScrollable());
097: m_beansmodel.addRow(info);
098:
099: if (!(comp.isLightweight() || comp instanceof javax.swing.JComponent)) {
100: String msg = I18N
101: .getLocalizedMessage("Warning. Only lightweight Java beans are supported.");
102: String title = I18N
103: .getLocalizedMessage("Error");
104: JOptionPane.showMessageDialog(m_view, msg,
105: title, JOptionPane.ERROR_MESSAGE);
106: }
107: } catch (FormException e) {
108: e.printStackTrace();
109: }
110: }
111: }
112: }
113:
114: /**
115: * Adds a path to the project
116: */
117: public class AddPathAction implements ActionListener {
118: private boolean m_dir_only = false;
119:
120: public AddPathAction(boolean dir_only) {
121: m_dir_only = dir_only;
122: }
123:
124: public void actionPerformed(ActionEvent evt) {
125: int mode = (m_dir_only ? javax.swing.JFileChooser.DIRECTORIES_ONLY
126: : javax.swing.JFileChooser.FILES_ONLY);
127:
128: TSFileFilter filter = null;
129: if (!m_dir_only)
130: filter = new TSFileFilter("jar,zip",
131: "JAR Files(*.jar,*.zip)");
132:
133: FileChooserConfig fcc = new FileChooserConfig(null, mode,
134: filter);
135: fcc.setParentComponent(m_view);
136: File f = TSFileChooserFactory.showOpenDialog(fcc);
137: if (f != null && f.exists()) {
138: try {
139: m_view.addUrl(f.toURL());
140: } catch (Exception e) {
141: e.printStackTrace();
142: }
143: }
144: }
145: }
146:
147: /**
148: * Deletes a bean from the view
149: */
150: public class DeleteBeanAction implements ActionListener {
151: public void actionPerformed(ActionEvent evt) {
152: m_view.deleteSelectedBean();
153: }
154: }
155:
156: /**
157: * Deletes a path from the project
158: */
159: public class DeletePathAction implements ActionListener {
160: public void actionPerformed(ActionEvent evt) {
161: m_view.deleteSelectedUrl();
162: }
163: }
164:
165: /**
166: * Sets an icon for the selected bean
167: */
168: public class SetBeanIconAction implements ActionListener {
169: public void actionPerformed(ActionEvent evt) {
170: ImportedBeanInfo bi = (ImportedBeanInfo) m_view
171: .getSelectedBean();
172: if (bi != null) {
173: File f = TSFileChooserFactory.showOpenDialog(".img",
174: new TSFileFilter("gif,png,jpg,jpeg",
175: "Image Files(*.gif,*.png,*.jpg)"));
176: if (f != null) {
177: bi.setIconMemento(new IconMemento(f));
178: m_beansmodel.fireTableDataChanged();
179: }
180: }
181: }
182: }
183:
184: }
|