0001: /*
0002: * Copyright (C) 2005 Jeff Tassin
0003: *
0004: * This library is free software; you can redistribute it and/or
0005: * modify it under the terms of the GNU Lesser General Public
0006: * License as published by the Free Software Foundation; either
0007: * version 2.1 of the License, or (at your option) any later version.
0008: *
0009: * This library is distributed in the hope that it will be useful,
0010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0012: * Lesser General Public License for more details.
0013: *
0014: * You should have received a copy of the GNU Lesser General Public
0015: * License along with this library; if not, write to the Free Software
0016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0017: */
0018:
0019: package com.jeta.swingbuilder.gui.main;
0020:
0021: import java.awt.Component;
0022: import java.awt.Dimension;
0023: import java.awt.Rectangle;
0024: import java.awt.event.ActionEvent;
0025: import java.awt.event.ActionListener;
0026: import java.awt.event.MouseAdapter;
0027: import java.awt.event.MouseEvent;
0028: import java.io.File;
0029: import java.io.FileInputStream;
0030: import java.io.FileOutputStream;
0031: import java.io.ObjectInputStream;
0032: import java.io.ObjectOutputStream;
0033: import java.util.Collection;
0034: import java.util.Iterator;
0035: import java.util.LinkedList;
0036:
0037: import javax.swing.JFrame;
0038: import javax.swing.JOptionPane;
0039:
0040: import com.jeta.forms.components.panel.FormPanel;
0041: import com.jeta.forms.gui.common.FormUtils;
0042: import com.jeta.forms.gui.form.FormComponent;
0043: import com.jeta.forms.gui.form.GridComponent;
0044: import com.jeta.forms.gui.formmgr.FormManager;
0045: import com.jeta.forms.logger.FormsLogger;
0046: import com.jeta.forms.project.ProjectManager;
0047: import com.jeta.forms.store.memento.ComponentMemento;
0048: import com.jeta.forms.store.memento.FormMemento;
0049: import com.jeta.forms.store.memento.FormPackage;
0050: import com.jeta.forms.store.memento.StateRequest;
0051: import com.jeta.open.gui.framework.JETADialog;
0052: import com.jeta.open.gui.utils.JETAToolbox;
0053: import com.jeta.open.i18n.I18N;
0054: import com.jeta.open.registry.JETARegistry;
0055: import com.jeta.swingbuilder.codegen.ForwardEngineer;
0056: import com.jeta.swingbuilder.gui.beanmgr.BeanManagerView;
0057: import com.jeta.swingbuilder.gui.beanmgr.DefaultBeanManager;
0058: import com.jeta.swingbuilder.gui.components.SystemPropertiesPanel;
0059: import com.jeta.swingbuilder.gui.components.TSErrorDialog2;
0060: import com.jeta.swingbuilder.gui.editor.FormEditor;
0061: import com.jeta.swingbuilder.gui.editor.RunFrame;
0062: import com.jeta.swingbuilder.gui.export.ComponentNamesExporter;
0063: import com.jeta.swingbuilder.gui.export.ExportNamesView;
0064: import com.jeta.swingbuilder.gui.filechooser.FileChooserConfig;
0065: import com.jeta.swingbuilder.gui.filechooser.TSFileChooserFactory;
0066: import com.jeta.swingbuilder.gui.filechooser.TSFileFilter;
0067: import com.jeta.swingbuilder.gui.formmgr.FormManagerDesignUtils;
0068: import com.jeta.swingbuilder.gui.lookandfeel.DefaultLookAndFeelManager;
0069: import com.jeta.swingbuilder.gui.lookandfeel.LookAndFeelInfo;
0070: import com.jeta.swingbuilder.gui.project.ProjectSettingsRule;
0071: import com.jeta.swingbuilder.gui.project.ProjectSettingsView;
0072: import com.jeta.swingbuilder.gui.project.UserPreferencesNames;
0073: import com.jeta.swingbuilder.gui.project.UserPreferencesView;
0074: import com.jeta.swingbuilder.gui.utils.FormDesignerUtils;
0075: import com.jeta.swingbuilder.interfaces.userprops.TSUserPropertiesUtils;
0076: import com.jeta.swingbuilder.project.DefaultProjectManager;
0077: import com.jeta.swingbuilder.store.ProjectLevelImportedBeansModel;
0078: import com.jeta.swingbuilder.store.ProjectModel;
0079:
0080: /**
0081: * Controller class for MainFrame
0082: *
0083: * @author Jeff Tassin
0084: */
0085: public class MainFrameController extends FormEditorController {
0086: private MainFrame m_frame;
0087:
0088: /**
0089: * The last preview frame that was active
0090: */
0091: private RunFrame m_runframe;
0092:
0093: /**
0094: * Special action for handling look and feel changes.
0095: */
0096: LookAndFeelAction m_lfaction = new LookAndFeelAction();
0097:
0098: static final int CLOSE_OK = 0;
0099:
0100: static final int CLOSE_CANCELED = 1;
0101:
0102: /**
0103: * The controller for the MainFrame window. Handles all user events.
0104: *
0105: * @param frame
0106: */
0107: MainFrameController(MainFrame frame) {
0108: super (frame, frame.m_form_popup);
0109: m_frame = frame;
0110: assignAction(MainFrameNames.ID_CREATE_FORM, new NewFormAction());
0111: assignAction(MainFrameNames.ID_CREATE_PROJECT,
0112: new NewProjectAction());
0113: assignAction(MainFrameNames.ID_CLOSE_PROJECT,
0114: new CloseProjectAction());
0115: assignAction(MainFrameNames.ID_OPEN_FORM, new OpenFormAction());
0116: assignAction(MainFrameNames.ID_OPEN_PROJECT,
0117: new OpenProjectAction());
0118: assignAction(MainFrameNames.ID_SHOW_FORM, new ShowFormAction());
0119: assignAction(MainFrameNames.ID_SAVE_FORM, new SaveFormAction());
0120: assignAction(MainFrameNames.ID_SAVE_FORM_AS, new SaveAsAction());
0121: assignAction(MainFrameNames.ID_CLOSE_FORM,
0122: new CloseFormAction());
0123:
0124: assignAction(FormEditorNames.ID_EXPORT_COMPONENT_NAMES,
0125: new ExportNamesAction());
0126: assignAction(MainFrameNames.ID_FORWARD_ENGINEER,
0127: new ForwardEngineerAction());
0128: assignAction(MainFrameNames.ID_PROJECT_SETTINGS,
0129: new ProjectSettingsAction());
0130: assignAction(MainFrameNames.ID_BEAN_MANAGER,
0131: new BeanManagerAction());
0132: assignAction(MainFrameNames.ID_SYSTEM_PROPERTIES,
0133: new SystemPropertiesAction());
0134: assignAction(MainFrameNames.ID_ENV_SETTINGS,
0135: new UserPreferencesAction());
0136:
0137: assignAction(MainFrameNames.ID_ABOUT, new AboutAction());
0138: assignAction(MainFrameNames.ID_EXIT, new ExitAction());
0139:
0140: assignAction(MainFrameNames.ID_FORM_PROPERTIES,
0141: new TogglePropertiesFrame());
0142: m_frame.getComponentByName(FormPropertiesNames.ID_CLOSE_FRAME)
0143: .addMouseListener(new ClosePropertiesFrame());
0144: m_frame.getComponentByName(FormPropertiesNames.ID_DOCK_FRAME)
0145: .addMouseListener(new DockPropertiesFrame());
0146:
0147: if (FormDesignerUtils.isDebug()) {
0148: assignAction(MainFrameNames.ID_SHOW_FORM_MANAGER,
0149: new ShowFormManagerAction());
0150: assignAction(MainFrameNames.ID_SHOW_UNDO_MANAGER,
0151: new ShowUndoManagerAction());
0152: }
0153:
0154: /**
0155: * clear the clipboard of any previous form components. This causes
0156: * problems with falsely enabling the paste command
0157: */
0158: try {
0159:
0160: java.awt.Toolkit kit = java.awt.Toolkit.getDefaultToolkit();
0161: java.awt.datatransfer.Clipboard clipboard = kit
0162: .getSystemClipboard();
0163: java.awt.datatransfer.Transferable transferable = clipboard
0164: .getContents(null);
0165: if (com.jeta.swingbuilder.gui.dnd.FormObjectFlavor
0166: .isDesignerFlavorSupported(transferable)) {
0167: clipboard.setContents(
0168: new java.awt.datatransfer.StringSelection(""),
0169: null);
0170: }
0171: } catch (Exception e) {
0172: e.printStackTrace();
0173: }
0174:
0175: }
0176:
0177: /**
0178: * Attempts to copy the given collection of form files (String objects) to
0179: * the classpath. This will only happen if the classpath is valid in the
0180: * project settings.
0181: */
0182: private void copyToClasspath(Collection files) {
0183: try {
0184: ProjectManager pmgr = (ProjectManager) JETARegistry
0185: .lookup(ProjectManager.COMPONENT_ID);
0186: ProjectModel pmodel = m_frame.getProject();
0187: if (pmodel != null) {
0188: String classpath = FormDesignerUtils.fastTrim(pmodel
0189: .getClassPath());
0190:
0191: if ((classpath != null) && (!"".equals(classpath))) {
0192: File rootDir = pmodel.getProjectRootDir();
0193: File f = new File(rootDir, classpath);
0194: if (f.isDirectory())
0195: classpath = f.getPath();
0196: else
0197: f = new File(classpath);
0198:
0199: if (f.isDirectory()) {
0200: char lastchar = classpath.charAt(classpath
0201: .length() - 1);
0202: if (lastchar != '\\' && lastchar != '/') {
0203: classpath = classpath + File.separatorChar;
0204: }
0205: Iterator iter = files.iterator();
0206: while (iter.hasNext()) {
0207: String abspath = (String) iter.next();
0208: String relativepath = pmgr
0209: .getRelativePath(abspath);
0210: if (relativepath != null) {
0211: File outputfile = new File(classpath
0212: + relativepath);
0213: File outputdir = new File(outputfile
0214: .getParent());
0215: if (!outputdir.exists()) {
0216: outputdir.mkdirs();
0217: }
0218:
0219: FormDesignerUtils.copyFile(outputfile
0220: .getPath(), abspath);
0221: }
0222: }
0223: } else {
0224: System.err
0225: .println("Error. Unable to determine classes directory: "
0226: + pmodel.getClassPath());
0227: }
0228: }
0229: }
0230: } catch (Exception e) {
0231: e.printStackTrace();
0232: }
0233: }
0234:
0235: private void editProject(ProjectModel model) {
0236: JETADialog dlg = (JETADialog) JETAToolbox.createDialog(
0237: JETADialog.class, m_frame, true);
0238: ProjectSettingsView view = new ProjectSettingsView(model);
0239: dlg.addValidator(view, new ProjectSettingsRule());
0240: dlg.setPrimaryPanel(view);
0241: if (model == null) {
0242: dlg.setTitle(I18N.getLocalizedMessage("New Project"));
0243: } else {
0244: dlg.setTitle(I18N.getLocalizedMessage("Edit Project"));
0245: }
0246: dlg.setSize(dlg.getPreferredSize());
0247: dlg.showCenter();
0248: if (dlg.isOk()) {
0249: ProjectModel pmodel = view.getModel();
0250: try {
0251: String path = pmodel.getProjectPath();
0252: int pos = path.lastIndexOf(".jfpr");
0253: if (pos != path.length() - 5) {
0254: path = path + ".jfpr";
0255: pmodel.setProjectPath(path);
0256: }
0257:
0258: DefaultBeanManager bmgr = (DefaultBeanManager) JETARegistry
0259: .lookup(DefaultBeanManager.COMPONENT_ID);
0260: bmgr.setModel(pmodel
0261: .getProjectLevelImportedBeansModel());
0262: m_frame.reloadComponentsToolbar();
0263:
0264: if (!pmodel.equals(model)) {
0265: FileOutputStream fos = new FileOutputStream(pmodel
0266: .getProjectPath());
0267: ObjectOutputStream oos = new ObjectOutputStream(fos);
0268: oos.writeObject(pmodel);
0269: oos.close();
0270: }
0271: m_frame.setProject(pmodel);
0272: } catch (Exception e) {
0273: FormsLogger.severe(e);
0274: }
0275: }
0276: }
0277:
0278: /**
0279: * @return the current form editor
0280: */
0281: public FormEditor getCurrentEditor() {
0282: return m_frame.getCurrentEditor();
0283: }
0284:
0285: /**
0286: * Closes the current form.
0287: *
0288: * @return CLOSE_OK(0) if the form was successfully closed CLOSE_CANCELED
0289: * (1) if the operation was canceled by the user
0290: */
0291: private int closeEditor(FormEditor editor) {
0292: if (editor != null) {
0293: if (editor.isModified() && editor.isLinked()) {
0294: String filename = editor.getForm().getFileName();
0295: if (filename == null)
0296: filename = I18N.getLocalizedMessage("New Form");
0297: String msg = I18N.format("Form_is_modified_save_1",
0298: filename);
0299: String title = I18N.getLocalizedMessage("Confirm");
0300: int result = JOptionPane.showConfirmDialog(m_frame,
0301: msg, title, JOptionPane.YES_NO_CANCEL_OPTION);
0302: if (result == JOptionPane.YES_OPTION) {
0303: if (saveForm(false) == null)
0304: return CLOSE_CANCELED;
0305: } else if (result == JOptionPane.CANCEL_OPTION) {
0306: return CLOSE_CANCELED;
0307: }
0308: }
0309: FormManager fmgr = (FormManager) JETARegistry
0310: .lookup(FormManager.COMPONENT_ID);
0311: fmgr.closeForm(editor.getForm().getId());
0312: FormManagerDesignUtils.clearUnreferencedForms();
0313: }
0314: return CLOSE_OK;
0315: }
0316:
0317: /**
0318: * Closes the current project
0319: *
0320: * @return CLOSE_OK(0) if the project was successfully closed CLOSE_CANCELED
0321: * (1) if the operation was canceled by the user
0322: */
0323: int closeProject() {
0324: ProjectModel pmodel = m_frame.getProject();
0325: if (pmodel != null) {
0326: TSUserPropertiesUtils.setString(
0327: UserPreferencesNames.ID_LAST_PROJECT, pmodel
0328: .getProjectPath());
0329: }
0330: Collection editors = m_frame.getEditors();
0331: Iterator iter = editors.iterator();
0332: while (iter.hasNext()) {
0333: FormEditor editor = (FormEditor) iter.next();
0334: if (editor.isModified()) {
0335: if (closeEditor(editor) == CLOSE_CANCELED)
0336: return CLOSE_CANCELED;
0337: }
0338: }
0339:
0340: /**
0341: * now close all unmodified forms.
0342: */
0343: editors = m_frame.getEditors();
0344: iter = editors.iterator();
0345: while (iter.hasNext()) {
0346: FormEditor editor = (FormEditor) iter.next();
0347: closeEditor(editor);
0348: }
0349:
0350: m_frame.setProject(null);
0351:
0352: DefaultBeanManager bmgr = (DefaultBeanManager) JETARegistry
0353: .lookup(DefaultBeanManager.COMPONENT_ID);
0354: DefaultProjectManager projectMgr = (DefaultProjectManager) JETARegistry
0355: .lookup(DefaultProjectManager.COMPONENT_ID);
0356: bmgr.setModel((projectMgr.getProject() != null ? projectMgr
0357: .getProject().getProjectLevelImportedBeansModel()
0358: : (ProjectLevelImportedBeansModel) null));
0359: m_frame.reloadComponentsToolbar();
0360:
0361: return CLOSE_OK;
0362: }
0363:
0364: /**
0365: * Opens the last project if the OPEN_LAST_PROJECT flag is true.
0366: */
0367: void openLastProject() {
0368: if (m_frame.getProject() != null)
0369: return;
0370:
0371: try {
0372: String projpath = (String) JETARegistry
0373: .lookup(UserPreferencesNames.ID_LAST_PROJECT);
0374:
0375: if (projpath != null
0376: || TSUserPropertiesUtils.getBoolean(
0377: UserPreferencesNames.ID_OPEN_LAST_PROJECT,
0378: true)) {
0379: if (projpath == null)
0380: projpath = TSUserPropertiesUtils.getString(
0381: UserPreferencesNames.ID_LAST_PROJECT, null);
0382:
0383: if (projpath != null) {
0384: File f = new File(projpath);
0385: if (f.exists()) {
0386: FileInputStream fis = new FileInputStream(f);
0387: ObjectInputStream ois = new ObjectInputStream(
0388: fis);
0389: ProjectModel pmodel = (ProjectModel) ois
0390: .readObject();
0391: pmodel.setProjectPath(f.getPath());
0392: m_frame.setProject(pmodel);
0393: validateProject(pmodel);
0394:
0395: DefaultBeanManager bm = (DefaultBeanManager) JETARegistry
0396: .lookup(DefaultBeanManager.COMPONENT_ID);
0397: bm.setModel(pmodel
0398: .getProjectLevelImportedBeansModel());
0399: m_frame.reloadComponentsToolbar();
0400: }
0401: }
0402: }
0403: } catch (Exception e) {
0404: FormsLogger.severe(e);
0405: }
0406: }
0407:
0408: private void postSaveForm(boolean saveAs) {
0409: final boolean sa = saveAs;
0410: javax.swing.SwingUtilities.invokeLater(new Runnable() {
0411: public void run() {
0412: saveForm(sa);
0413: }
0414: });
0415: }
0416:
0417: /**
0418: * Saves the form to a file.
0419: *
0420: * @param saveAs
0421: * if true, then prompts the user for a new file name. If false,
0422: * uses the current filename for the selected form.
0423: */
0424: private File saveForm(boolean saveAs) {
0425: m_frame.unitTest();
0426: m_frame.getPropertyContainer().stopEditing();
0427:
0428: FormEditor editor = m_frame.getCurrentEditor();
0429: FormComponent fc = null;
0430: if (editor != null) {
0431: editor.saveFocusPolicy();
0432: fc = editor.getFormComponent();
0433: }
0434:
0435: /**
0436: * the list of form files that we are saving. This is the current form
0437: * and any linked forms
0438: */
0439: LinkedList files = new LinkedList();
0440: File file = null;
0441: try {
0442: if (fc != null) {
0443: FormManager fmgr = (FormManager) JETARegistry
0444: .lookup(FormManager.COMPONENT_ID);
0445:
0446: String path = fc.getAbsolutePath();
0447: if (saveAs)
0448: path = null;
0449:
0450: if (path == null) {
0451: FileChooserConfig fcc = new FileChooserConfig(
0452: ".form", new TSFileFilter("jfrm,xml",
0453: "Form Files(*.jfrm,*.xml)"));
0454: fcc.setParentComponent(m_frame);
0455: file = TSFileChooserFactory.showSaveDialog(fcc);
0456: if (file == null)
0457: return null;
0458:
0459: path = file.getPath();
0460: int pos = path.lastIndexOf(".jfrm");
0461: if (pos != path.length() - 5) {
0462: pos = path.lastIndexOf(".xml");
0463: if (pos != path.length() - 4) {
0464: String ext = ".jfrm";
0465: if (TSUserPropertiesUtils
0466: .getBoolean(
0467: UserPreferencesNames.ID_STORE_AS_XML,
0468: false))
0469: ext = ".xml";
0470:
0471: path = path + ext;
0472: file = new File(path);
0473: }
0474: }
0475:
0476: /** see if form is already opened */
0477: if (fmgr.isOpened(file.getPath())) {
0478: String msg = I18N.format(
0479: "Form_is_already_opened_in_editor_1",
0480: file.getName());
0481: String title = I18N
0482: .getLocalizedMessage("Error");
0483: JOptionPane.showMessageDialog(m_frame, msg,
0484: title, JOptionPane.ERROR_MESSAGE);
0485: return null;
0486: }
0487: } else {
0488: file = new File(path);
0489: }
0490:
0491: String oldid = fc.getId();
0492: path = file.getPath();
0493:
0494: FormPackage fpackage = new FormPackage(fc
0495: .getExternalState(StateRequest.SHALLOW_COPY));
0496: FormDesignerUtils.saveForm(fpackage, file);
0497:
0498: files.add(path);
0499: if (!path.equals(oldid)) {
0500: // the clone operation will update the forms ids with the
0501: // FormManager
0502: fc = FormManagerDesignUtils.clone(fmgr, editor,
0503: path);
0504: FormManagerDesignUtils.registerForms(fmgr, fc);
0505: editor.setFormComponent(fc);
0506: fmgr.installHandlers(m_frame, fc);
0507: installHandlers(editor);
0508: fmgr.activateForm(editor.getTopParent().getId());
0509: editor.activate();
0510:
0511: /** unit test after the clone */
0512: m_frame.unitTest();
0513: }
0514: m_frame.formNameChanged(fc);
0515:
0516: /**
0517: * now, save linked forms that are located in the form we are
0518: * saving
0519: */
0520: Collection links = FormManagerDesignUtils
0521: .getNestedForms(fmgr, fc);
0522: Iterator iter = links.iterator();
0523: while (iter.hasNext()) {
0524: String fid = (String) iter.next();
0525: FormComponent nest = fmgr.getForm(fid);
0526: if (nest != null) {
0527: if (nest != fc && nest.isLinked()) {
0528: fpackage = new FormPackage(
0529: nest
0530: .getExternalState(StateRequest.SHALLOW_COPY));
0531: FormDesignerUtils.saveForm(fpackage,
0532: new File(nest.getAbsolutePath()));
0533: files.add(nest.getAbsolutePath());
0534: }
0535: FormEditor parent_editor = m_frame
0536: .getEditor(nest);
0537: if (parent_editor != null)
0538: parent_editor.clearUndoableEdits();
0539: }
0540: }
0541:
0542: editor.clearUndoableEdits();
0543: FormManagerDesignUtils.clearUnreferencedForms();
0544: copyToClasspath(files);
0545: m_frame.unitTest();
0546:
0547: return file;
0548:
0549: }
0550: } catch (Exception e) {
0551: TSErrorDialog2 dlg = null;
0552:
0553: String caption = I18N
0554: .getLocalizedMessage("Error. Unable to save file");
0555: if (file == null) {
0556: dlg = TSErrorDialog2.createDialog(m_frame, caption,
0557: null, e);
0558: } else {
0559: String msg = I18N.format("Unable_to_save_file_1", file
0560: .getName());
0561: dlg = TSErrorDialog2.createDialog(m_frame, caption,
0562: msg, e);
0563: }
0564:
0565: dlg.showCenter();
0566: }
0567: m_frame.updateModifiedStatus();
0568: return null;
0569: }
0570:
0571: /** Sets the look and feel for the application based on user settings */
0572: public static void setDefaultLookAndFeel() {
0573: DefaultLookAndFeelManager dfm = (DefaultLookAndFeelManager) JETARegistry
0574: .lookup(DefaultLookAndFeelManager.COMPONENT_ID);
0575: if (dfm != null)
0576: setLookAndFeel(null, null, dfm.getDefaultLookAndFeel());
0577: }
0578:
0579: /** sets the look and feel for the given frame and look an feel class name */
0580: public static void setLookAndFeel(MainFrame mainframe,
0581: JFrame runframe, LookAndFeelInfo lookandfeel) {
0582: try {
0583: if (lookandfeel == null)
0584: return;
0585:
0586: DefaultLookAndFeelManager dfm = (DefaultLookAndFeelManager) JETARegistry
0587: .lookup(DefaultLookAndFeelManager.COMPONENT_ID);
0588: dfm.setLookAndFeel(lookandfeel);
0589:
0590: if (mainframe != null) {
0591: FormUtils.updateLookAndFeel(mainframe);
0592: mainframe.updateUI();
0593: }
0594:
0595: if (runframe != null && runframe.isVisible()) {
0596: FormUtils.updateLookAndFeel(runframe);
0597: }
0598: } catch (Exception e) {
0599: FormsLogger.debug(e);
0600: }
0601: }
0602:
0603: private void validateProject(ProjectModel pmodel) {
0604: try {
0605: if (pmodel == null)
0606: return;
0607:
0608: String emsg = ProjectSettingsRule.validateProject(pmodel);
0609: if (emsg != null) {
0610: TSErrorDialog2 dlg = TSErrorDialog2.createDialog(
0611: m_frame, "Error", emsg, null);
0612: dlg
0613: .showErrorIcon(I18N
0614: .getLocalizedMessage("Project has invalid paths"));
0615: dlg.showCenter();
0616: }
0617: } catch (Exception e) {
0618: FormsLogger.severe(e);
0619: }
0620: }
0621:
0622: /**
0623: * Show About dialog
0624: */
0625: public class AboutAction implements ActionListener {
0626: public void actionPerformed(ActionEvent evt) {
0627: JETADialog dlg = (JETADialog) JETAToolbox.createDialog(
0628: JETADialog.class, m_frame, true);
0629: AboutView view = new AboutView();
0630: dlg.setPrimaryPanel(view);
0631: dlg.setTitle(I18N.getLocalizedMessage("About"));
0632: dlg.pack();
0633: dlg.showOkButton(false);
0634: dlg.setCloseText(I18N.getLocalizedMessage("Close"));
0635: Dimension d = dlg.getSize();
0636: if (JETAToolbox.isWindows() || JETAToolbox.isOSX()) {
0637: d.width -= 2;
0638: dlg.setSize(d);
0639: }
0640: dlg.showCenter();
0641: }
0642: }
0643:
0644: /**
0645: * Invokes the Bean Manager dialog
0646: */
0647: public class BeanManagerAction implements ActionListener {
0648: public void actionPerformed(ActionEvent evt) {
0649: JETADialog dlg = (JETADialog) JETAToolbox.createDialog(
0650: JETADialog.class, m_frame, true);
0651: DefaultBeanManager bmgr = (DefaultBeanManager) JETARegistry
0652: .lookup(DefaultBeanManager.COMPONENT_ID);
0653: BeanManagerView view = new BeanManagerView(bmgr.getModel());
0654: dlg.setPrimaryPanel(view);
0655: dlg.setTitle(I18N.getLocalizedMessage("Java Bean Manager"));
0656: dlg.setSize(dlg.getPreferredSize());
0657: dlg.showCenter();
0658: if (dlg.isOk()) {
0659: bmgr.setModel(view.getImportedBeansModel());
0660: m_frame.reloadComponentsToolbar();
0661: }
0662: }
0663: }
0664:
0665: /**
0666: * Closes the form
0667: */
0668: public class CloseFormAction implements ActionListener {
0669: public void actionPerformed(ActionEvent evt) {
0670: closeEditor(getCurrentEditor());
0671: }
0672: }
0673:
0674: /**
0675: * Closes the current project.
0676: */
0677: public class CloseProjectAction implements ActionListener {
0678: public void actionPerformed(ActionEvent evt) {
0679: closeProject();
0680: }
0681: }
0682:
0683: public class TogglePropertiesFrame implements ActionListener {
0684: public void actionPerformed(ActionEvent evt) {
0685: m_frame.getDocker().togglePropertiesFrame();
0686: }
0687: }
0688:
0689: public class ClosePropertiesFrame extends MouseAdapter {
0690: public void mouseClicked(MouseEvent evt) {
0691: m_frame.getDocker().togglePropertiesFrame();
0692: }
0693: }
0694:
0695: public class DockPropertiesFrame extends MouseAdapter {
0696: public void mouseClicked(MouseEvent evt) {
0697: m_frame.getDocker().dockPropertiesFrame(null);
0698: }
0699: }
0700:
0701: /**
0702: * Exit action handler
0703: */
0704: public class ExitAction implements ActionListener {
0705: public void actionPerformed(ActionEvent evt) {
0706: m_frame.shutDown();
0707: }
0708: }
0709:
0710: /**
0711: * Exports the component names on a given form.
0712: */
0713: public class ExportNamesAction implements ActionListener {
0714: public void actionPerformed(ActionEvent evt) {
0715: FormEditor editor = m_frame.getCurrentEditor();
0716: if (editor != null) {
0717: GridComponent gc = editor.getSelectedComponent();
0718: if (gc instanceof FormComponent) {
0719: JETADialog dlg = (JETADialog) JETAToolbox
0720: .createDialog(JETADialog.class, m_frame,
0721: true);
0722: ExportNamesView view = new ExportNamesView();
0723: dlg
0724: .setTitle(I18N
0725: .getLocalizedMessage("Export Component Names"));
0726: dlg.setPrimaryPanel(view);
0727: dlg.setSize(dlg.getPreferredSize());
0728: dlg.showCenter();
0729: if (dlg.isOk()) {
0730: view.saveToModel();
0731: ComponentNamesExporter exporter = new ComponentNamesExporter(
0732: view);
0733: exporter.exportToClipboard((FormComponent) gc);
0734: }
0735: }
0736: }
0737: }
0738: }
0739:
0740: /**
0741: * Exports a Form to Java Source
0742: */
0743: public class ForwardEngineerAction implements ActionListener {
0744: public void actionPerformed(ActionEvent evt) {
0745: FormEditor editor = m_frame.getCurrentEditor();
0746: if (editor != null) {
0747: try {
0748: FormComponent fc = editor.getFormComponent();
0749: FormMemento fm = (FormMemento) fc
0750: .getExternalState(StateRequest.DEEP_COPY);
0751: ForwardEngineer fe = new ForwardEngineer();
0752: fe.generate(m_frame, fm);
0753: } catch (Exception e) {
0754: e.printStackTrace();
0755: }
0756: }
0757: }
0758: }
0759:
0760: /**
0761: * Focus Management
0762: */
0763: public class FocusManagerAction implements ActionListener {
0764: public void actionPerformed(ActionEvent evt) {
0765: FormEditor editor = m_frame.getCurrentEditor();
0766: if (editor != null) {
0767: boolean show_focus = false;
0768: Collection comps = m_frame
0769: .getComponentsByName(MainFrameNames.ID_FOCUS_MANAGER);
0770: Iterator iter = comps.iterator();
0771: while (iter.hasNext()) {
0772: Component comp = (Component) iter.next();
0773: if (comp instanceof javax.swing.AbstractButton) {
0774: show_focus = ((javax.swing.AbstractButton) comp)
0775: .isSelected();
0776: break;
0777: }
0778: }
0779: editor.setFocusViewVisible(show_focus);
0780: editor.revalidate();
0781: editor.repaint();
0782: }
0783: }
0784: }
0785:
0786: /**
0787: * Sets the look and feel for the application
0788: */
0789: public class LookAndFeelAction implements ActionListener {
0790: public void actionPerformed(ActionEvent evt) {
0791: try {
0792: DefaultLookAndFeelManager lfm = (DefaultLookAndFeelManager) JETARegistry
0793: .lookup(DefaultLookAndFeelManager.COMPONENT_ID);
0794: if (lfm != null) {
0795: javax.swing.AbstractButton btn = (javax.swing.AbstractButton) evt
0796: .getSource();
0797: // the menu's action command will be the LookAndFeelInfo Id.
0798: setLookAndFeel(m_frame, m_runframe, lfm
0799: .findById(btn.getActionCommand()));
0800: }
0801: } catch (Exception e) {
0802: e.printStackTrace();
0803: }
0804: }
0805: }
0806:
0807: /**
0808: * ActionListener for creating a new form.
0809: */
0810: public class NewFormAction implements ActionListener {
0811: /**
0812: * @param evt
0813: */
0814: public void actionPerformed(ActionEvent evt) {
0815: FormEditor editor = new FormEditor(m_frame, 20, 20);
0816: FormManager fmgr = (FormManager) JETARegistry
0817: .lookup(FormManager.COMPONENT_ID);
0818: fmgr.registerForm(editor.getForm());
0819: m_frame.addForm(editor);
0820: editor.getForm().setControlButtonsVisible(false);
0821: }
0822: }
0823:
0824: /**
0825: * Create a new project
0826: */
0827: public class NewProjectAction implements ActionListener {
0828: public void actionPerformed(ActionEvent evt) {
0829: editProject(null);
0830: }
0831: }
0832:
0833: /**
0834: * Opens a form from a previously saved file
0835: */
0836: public class OpenFormAction implements ActionListener {
0837: public void actionPerformed(ActionEvent evt) {
0838: try {
0839: FileChooserConfig fcc = new FileChooserConfig(".form",
0840: new TSFileFilter("jfrm,xml",
0841: "Form Files(*.jfrm,*.xml)"));
0842: fcc.setParentComponent(m_frame);
0843: File f = TSFileChooserFactory.showOpenDialog(fcc);
0844: if (f != null) {
0845: FormManager fmgr = (FormManager) JETARegistry
0846: .lookup(FormManager.COMPONENT_ID);
0847: fmgr.deactivateForms(m_frame.getCurrentEditor());
0848: FormComponent fc = fmgr.openLinkedForm(f);
0849: fmgr.activateForm(fc.getId());
0850: fmgr.showForm(fc.getId());
0851: }
0852: } catch (Exception e) {
0853: e.printStackTrace();
0854: }
0855: }
0856: }
0857:
0858: /**
0859: * Opens a project from a previously saved file
0860: */
0861: public class OpenProjectAction implements ActionListener {
0862: public void actionPerformed(ActionEvent evt) {
0863: try {
0864: FileChooserConfig fcc = new FileChooserConfig(
0865: "project", new TSFileFilter("jfpr",
0866: "Project Files (*.jfpr)"));
0867: fcc.setParentComponent(m_frame);
0868: File f = TSFileChooserFactory.showOpenDialog(fcc);
0869: if (f != null) {
0870: FileInputStream fis = new FileInputStream(f);
0871: ObjectInputStream ois = new ObjectInputStream(fis);
0872: ProjectModel pmodel = (ProjectModel) ois
0873: .readObject();
0874: pmodel.setProjectPath(f.getPath());
0875: m_frame.setProject(pmodel);
0876: validateProject(pmodel);
0877:
0878: DefaultBeanManager bmgr = (DefaultBeanManager) JETARegistry
0879: .lookup(DefaultBeanManager.COMPONENT_ID);
0880: bmgr.setModel(pmodel
0881: .getProjectLevelImportedBeansModel());
0882: m_frame.reloadComponentsToolbar();
0883: }
0884: } catch (Exception e) {
0885: e.printStackTrace();
0886: }
0887: }
0888: }
0889:
0890: /**
0891: * Invokes the project settings dialog
0892: */
0893: public class ProjectSettingsAction implements ActionListener {
0894: public void actionPerformed(ActionEvent evt) {
0895: editProject(m_frame.getProject());
0896: }
0897: }
0898:
0899: /**
0900: * Saves the form to a file
0901: */
0902: public class SaveFormAction implements ActionListener {
0903: public void actionPerformed(ActionEvent evt) {
0904: saveForm(false);
0905: }
0906: }
0907:
0908: /**
0909: * Saves the form to a new file
0910: */
0911: public class SaveAsAction implements ActionListener {
0912: public void actionPerformed(ActionEvent evt) {
0913: saveForm(true);
0914: }
0915: }
0916:
0917: /**
0918: * Shows the form as it would appear when running
0919: */
0920: public class ShowFormAction implements ActionListener {
0921:
0922: private Rectangle getFrameBounds() {
0923: Rectangle result = null;
0924: try {
0925: if (m_runframe != null) {
0926: Dimension screensz = java.awt.Toolkit
0927: .getDefaultToolkit().getScreenSize();
0928:
0929: Rectangle frect = m_runframe.getBounds();
0930: if (frect.width > 100 && frect.height > 100
0931: && frect.x > 0 && frect.y > 0) {
0932: if ((frect.x + frect.width) < screensz.width
0933: && (frect.y + frect.height) < screensz.height) {
0934: result = frect;
0935: }
0936: }
0937: }
0938:
0939: if (result == null || result.width < 100
0940: || result.height < 100)
0941: result = new Rectangle(100, 100, 600, 400);
0942: } catch (Exception e) {
0943: // ignore
0944: result = new Rectangle(100, 100, 600, 400);
0945: }
0946: return result;
0947: }
0948:
0949: public void actionPerformed(ActionEvent evt) {
0950: try {
0951: FormEditor editor = m_frame.getCurrentEditor();
0952: if (editor != null) {
0953: editor.unitTest();
0954: FormComponent fc = editor.getFormComponent();
0955: ComponentMemento cm = fc
0956: .getExternalState(StateRequest.DEEP_COPY);
0957:
0958: FormUtils.setDesignMode(false);
0959:
0960: fc = FormComponent.create();
0961: fc.setState(cm);
0962:
0963: FormPanel jetapanel = new FormPanel(fc);
0964: fc.postInitialize(jetapanel);
0965:
0966: if (FormDesignerUtils.isTest()) {
0967: // com.jeta.swingbuilder.test.JETATestFactory.runTest(
0968: // "test.jeta.forms.gui.form.FormValidator", fc );
0969: }
0970:
0971: Rectangle rect = getFrameBounds();
0972:
0973: if (m_runframe == null) {
0974: m_runframe = new RunFrame(jetapanel);
0975: } else {
0976: m_runframe.setForm(jetapanel);
0977: }
0978: m_runframe.setSize(rect.width, rect.height);
0979: m_runframe.setLocation(rect.x, rect.y);
0980: m_runframe.setVisible(true);
0981: jetapanel.transferFocusDownCycle();
0982: }
0983: } catch (Exception e) {
0984: e.printStackTrace();
0985: } finally {
0986: FormUtils.setDesignMode(true);
0987: }
0988: }
0989: }
0990:
0991: public class ShowFormManagerAction implements ActionListener {
0992: private com.jeta.swingbuilder.gui.formmgr.FormManagerFrame m_frame;
0993:
0994: public void actionPerformed(ActionEvent evt) {
0995: if (m_frame == null) {
0996: m_frame = new com.jeta.swingbuilder.gui.formmgr.FormManagerFrame();
0997: m_frame.setSize(600, 400);
0998: m_frame.setLocation(150, 150);
0999: }
1000: m_frame.reload();
1001: m_frame.setVisible(true);
1002: }
1003: }
1004:
1005: public class ShowUndoManagerAction implements ActionListener {
1006: private com.jeta.swingbuilder.gui.undo.UndoManagerFrame m_frame;
1007:
1008: public void actionPerformed(ActionEvent evt) {
1009: if (m_frame == null) {
1010: m_frame = new com.jeta.swingbuilder.gui.undo.UndoManagerFrame(
1011: MainFrameController.this .m_frame);
1012: m_frame.setSize(600, 400);
1013: m_frame.setLocation(150, 150);
1014: }
1015: m_frame.reload();
1016: m_frame.setVisible(true);
1017: }
1018: }
1019:
1020: public class SystemPropertiesAction implements ActionListener {
1021: public void actionPerformed(ActionEvent evt) {
1022: JETADialog dlg = (JETADialog) JETAToolbox.createDialog(
1023: JETADialog.class, m_frame, true);
1024: dlg.setTitle(I18N.getLocalizedMessage("System Properties"));
1025: SystemPropertiesPanel view = new SystemPropertiesPanel();
1026: dlg.setPrimaryPanel(view);
1027: dlg.setSize(dlg.getPreferredSize());
1028: dlg.showCenter();
1029: }
1030: }
1031:
1032: public class UserPreferencesAction implements ActionListener {
1033: public void actionPerformed(ActionEvent evt) {
1034: JETADialog dlg = (JETADialog) JETAToolbox.createDialog(
1035: JETADialog.class, m_frame, true);
1036: UserPreferencesView view = new UserPreferencesView();
1037: dlg.setPrimaryPanel(view);
1038: dlg.setTitle(I18N.getLocalizedMessage("Preferences"));
1039: dlg.setSize(dlg.getPreferredSize());
1040: dlg.showCenter();
1041: if (dlg.isOk()) {
1042: view.saveToModel();
1043: Collection editors = m_frame.getEditors();
1044: Iterator iter = editors.iterator();
1045: while (iter.hasNext()) {
1046: FormEditor editor = (FormEditor) iter.next();
1047: editor.updatePreferences();
1048: }
1049: }
1050: }
1051: }
1052:
1053: }
|