GUIApplication.java :  » Code-Analyzer » beautyJ » de » gulden » framework » amoda » environment » gui » Java Open Source

Java Open Source » Code Analyzer » beautyJ 
beautyJ » de » gulden » framework » amoda » environment » gui » GUIApplication.java
/*
 * Project: AMODA - Abstract Modeled Application
 * Class:   de.gulden.framework.amoda.environment.gui.GUIApplication
 * Version: snapshot-beautyj-1.1
 *
 * Date:    2004-09-29
 *
 * This is a snapshot version of the AMODA 0.2 development branch,
 * it is not released as a seperate version.
 * For AMODA, see http://amoda.berlios.de/.
 *  
 * This is licensed under the GNU Lesser General Public License (LGPL)
 * and comes with NO WARRANTY.
 *
 * Author:  Jens Gulden
 * Email:   amoda@jensgulden.de
 */

package de.gulden.framework.amoda.environment.gui;

import de.gulden.framework.amoda.environment.commandline.*;
import de.gulden.framework.amoda.generic.core.*;
import de.gulden.framework.amoda.model.core.*;
import de.gulden.framework.amoda.model.core.WorkspaceProvider;
import de.gulden.framework.amoda.model.document.*;
import de.gulden.framework.amoda.model.document.ClipboardHandler;
import de.gulden.framework.amoda.model.document.Document;
import de.gulden.framework.amoda.model.document.DocumentHandler;
import de.gulden.util.Toolbox;
import de.gulden.util.xml.XMLToolbox;
import java.awt.*;
import java.io.*;
import java.lang.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import org.w3c.dom.*;

/**
 * Class GUIApplication.
 *  
 * @author  Jens Gulden
 * @version  snapshot-beautyj-1.1
 */
public abstract class GUIApplication extends CommandLineApplication implements WorkspaceProvider, ClipboardHandler, DocumentHandler {

    // ------------------------------------------------------------------------
    // --- fields                                                           ---
    // ------------------------------------------------------------------------

    protected boolean modeMultiDocuments;

    protected boolean modeMultiViews;

    protected JDialog aboutDialog;

    protected JWindow aboutSplashWindow;

    protected JDialog helpDialog;


    // ------------------------------------------------------------------------
    // --- methods                                                          ---
    // ------------------------------------------------------------------------

    public Workspace getWorkspace() {
        return ((GUIApplicationEnvironment)getEnvironment()).getWorkspace();
    }

    public JComponent createGUIComponent(DocumentView view) {
        // to be overwritten by subclass if used
        throw new AbstractMethodError("createDefaultView is not implemented by subclass");
    }

    public void cutSelection(DocumentSelection sel) {
        // your code here
    }

    public void copySelection(DocumentSelection sel) {
        // your code here
    }

    public void pasteSelection(DocumentSelection sel) {
        // your code here
    }

    public DocumentHandler getDocumentHandler() {
        return this; // default is this, may be overwritten
    }

    public ClipboardHandler getClipboardHandler() {
        return this; // default is this, may be overwritten
    }

    public void destroy() {
        ((GUIApplicationEnvironment)getGenericApplicationEnvironment()).getFrame().dispose();
        System.exit(0);
    }

    public void start() {
        // init recent files list
        getRecentFilesList().init(this);
        // open window
        ((GUIApplicationEnvironment)getEnvironment()).getFrame().setVisible(true);
        // open initial documents or whatever else on the workspace
        startWorkspace();
        if (aboutSplashWindow!=null) {
            aboutSplashWindow.dispose();
        }
        super.start();
    }

    public void about() {
        if (((GUIApplicationEnvironment)getEnvironment()).getFrame().isVisible()) { // normal mode
            if (aboutDialog==null) {
                aboutDialog=createAboutDialog();
            }
            de.gulden.util.Toolbox.centerOnScreen(aboutDialog);
            aboutDialog.setVisible(true);
        } else {
            aboutSplash(); // special mode during init
        }
    }

    public void aboutSplash() {
        JWindow window=new JWindow(((GUIApplicationEnvironment)getEnvironment()).getFrame());
        JComponent aboutComponent=createAboutComponent();
        window.getContentPane().add(aboutComponent);
        window.pack();
        Dimension size=window.getSize();
        size.width=500;
        window.setSize(size);
        de.gulden.util.Toolbox.centerOnScreen(window);
        this.aboutSplashWindow=window;
        window.setVisible(true);
    }

    public Document newDocument() {
        // your code here
        return null;
    }

    public Document openDocument(File file) {
        // your code here
        return null;
    }

    public void saveDocument(Document doc) {
        // your code here
    }

    public Workspace createWorkspace() {
        // may be overwritten by sublcasses
        GUIWorkspace workspace=new GUIWorkspace();
        workspace.setEnvironment((de.gulden.framework.amoda.generic.core.GenericApplicationEnvironment)getEnvironment());
        return workspace;
    }

    public Document openDocument(URL url) {
        String protocol=url.getProtocol();
        if (protocol.equals("file")) {
            return openDocument(new java.io.File(url.getPath()));
        } else {
            error("URLs other than file:// are not supported.");
            return null;
        }
    }

    public void saveDocumentAs(Document doc, File file) {
        // your code here
    }

    public void closeDocument(Document doc) {
        if (confirm("Really close this document and all its views?")) {
            Collection views=getWorkspace().getViews(doc);
            views=(Collection)views; // avoid concurrent modification exception
            for (Iterator it=views.iterator();it.hasNext();) {
                DocumentView view=(DocumentView)it.next();
                getWorkspace().removeView(view);
            }
        }
    }

    public void loadDocument(File file) {
        openDocumentOnWorkspace(openDocument(file));
    }

    public void loadDocument(URL url) {
        openDocumentOnWorkspace(openDocument(url));
    }

    public void storeDocument(Document document, File file) {
        // your code here
    }

    public void storeDocument(Document document, URL url) {
        // your code here
    }

    public void help() {
        if (helpDialog==null) {
            helpDialog=createHelpDialog();
        }
        de.gulden.util.Toolbox.centerOnScreen(helpDialog);
        helpDialog.setVisible(true);
    }

    protected JDialog createOptionsDialog() {
        // your code here
        return null;
    }

    protected JDialog createAboutDialog() {
        de.gulden.framework.amoda.environment.gui.component.JDialogCloseable dialog=new de.gulden.framework.amoda.environment.gui.component.JDialogCloseable(((GUIApplicationEnvironment)getEnvironment()).getFrame(),"About",false);
        JTabbedPane tabbedPane=new JTabbedPane();
        tabbedPane.setFont(((GUIApplicationEnvironment)getEnvironment()).getFont(GUIApplicationEnvironment.FONT_MENU));

        JComponent comp=createAboutComponent();
        tabbedPane.add("About",comp);

        // set size now (after filling text will be too late)
        dialog.getContentPane().add(tabbedPane);

        String licenseText=createLicenseText();
        if (licenseText!=null) {
            JTextArea textArea=new JTextArea(licenseText);
            JScrollPane scrollPane=new JScrollPane(textArea);
            comp=new de.gulden.framework.amoda.environment.gui.component.PresentationPanel(getImage("SecurityModern13.png"),scrollPane);
            tabbedPane.add("License",comp);
        }

        de.gulden.util.swing.MapTableModel tableModel=new de.gulden.util.swing.MapTableModel(System.getProperties());
        JTable table=new JTable(tableModel);
        JScrollPane scrollPane=new JScrollPane(table);
        comp=new de.gulden.framework.amoda.environment.gui.component.PresentationPanel(getImage("ManagementModern02.png"),scrollPane);
        tabbedPane.add("System",comp);

        dialog.pack(); // will lead to extremely over-wide dialog
        Dimension size=dialog.getSize();
        //size.height-=10; // ******************************************
        size.height = 400;
        dialog.setSize(size);
        return dialog;
    }

    protected JComponent createAboutComponent() {
        String aboutHTML=createAboutHTML();
        return new de.gulden.framework.amoda.environment.gui.component.PresentationPanel(getImage("SetupModern24.png"), "<html>"+aboutHTML+"</html>");
    }

    protected ApplicationEnvironment createApplicationEnvironment() {
        // overwrites CommandlineApplication.createApplicationEnvironment
        de.gulden.framework.amoda.model.core.ApplicationEnvironmentFactory factory=new GUIApplicationEnvironmentFactory(getArgs());//,getConfigurationResourceURL());
        return factory.createApplicationEnvironment();
    }

    protected String createAboutHTML() {
        StringBuffer html=new StringBuffer();
        html.append("<h2>"+de.gulden.framework.amoda.generic.metadata.GenericMetadata.findTitle(this)+"</h2>");
        String subtitle=getMetadata().get("subtitle");
        if (!Toolbox.empty(subtitle)) {
            html.append("<h3>"+subtitle+"</h3>");
        }
        String description=getMetadata().get("description");
        if (!Toolbox.empty(description)) {
            html.append("<h4>"+description+"</h4>");
        }
        String licenseMessage=getMetadata().get("license-message");
        if (!Toolbox.empty(subtitle)) {
            html.append("<h4>"+licenseMessage+"</h4>");
        }
        html.append("<table>");
        for (Iterator it=getMetadata().getEntries().iterator();it.hasNext();) {
            de.gulden.framework.amoda.model.metadata.MetadataEntry entry=(de.gulden.framework.amoda.model.metadata.MetadataEntry)it.next();
            String name=entry.getName();
            String value=entry.getString();
            if (!(name.equals("license-message")
                ||name.equals("license-text")
                ||name.equals("description")
                ||name.equals("title"))) {
                html.append("<tr><td><i>"+Toolbox.capitalize(entry.getName())+"</i></td><td><b>"+entry.getString()+"</b></td></tr>");
            }
        }
        html.append("</table>");
        return html.toString();
    }

    protected String createLicenseText() {
        String license=getMetadata().get("license-text");
        if (!Toolbox.empty(license)) {
            return license;
        } else {
            return null;
        }
    }

    protected void startWorkspace() {
        // default implementation, opens 1 new blank document
        de.gulden.framework.amoda.model.document.Document doc=getInitialDocument();
        if (doc!=null) {
            openDocumentOnWorkspace(doc);
        }
    }

    protected Document getInitialDocument() {
        if (getOptions().getBoolean("open-most-recent-file-on-startup")) {
            Document doc=null;
            de.gulden.framework.amoda.generic.core.GenericRecentFilesList rfl=(de.gulden.framework.amoda.generic.core.GenericRecentFilesList)getRecentFilesList();
            if (rfl!=null) {
                if (rfl.size()>0) {
                    Object o=rfl.get(0);
                    if (o instanceof java.io.File) {
                        doc=openDocument((java.io.File)o);
                    } else if (o instanceof java.net.URL) {
                        doc=openDocument((java.net.URL)o);
                    }
                }
            }
            if (doc!=null) {
                return doc;
            }
        }
        //FALLSTHROUGH on all else-branches
        return getDocumentHandler().newDocument();
    }

    protected RecentFilesList createRecentFilesList() {
        return new GUIRecentFilesList();
    }

    protected void openDocumentOnWorkspace(Document document) {
        if (document!=null) {
            de.gulden.framework.amoda.model.document.DocumentView view=document.createDefaultView();
            getWorkspace().addView(view);
        }
    }

    protected JDialog createHelpDialog() {
        de.gulden.framework.amoda.environment.gui.component.JDialogCloseable dialog=new de.gulden.framework.amoda.environment.gui.component.JDialogCloseable(((GUIApplicationEnvironment)getEnvironment()).getFrame(),"Help",false);
        JPanel panel = new de.gulden.framework.amoda.environment.gui.component.PresentationPanel(getImage("SecurityModern13.png"), "<html><body>"+createHelpHTML()+"</body></html>");
        dialog.getContentPane().add(panel);
        /*dialog.pack(); // will lead to extremely over-wide dialog
        Dimension size=dialog.getSize();
        size.height = 800;
        size.height = 600;
        dialog.setSize(size);*/
        dialog.setSize(800,800);
        return dialog;
    }

    protected String createUsageHTML() {
        StringBuffer sb=new StringBuffer();
        /*String description = getMetadata().get("description").trim();
        if (description.length()!=0) {
            sb.append("<p>"+description+"</p>");
        }*/
        String usageLine = getUsageLine();
        if (usageLine!=null) {
            sb.append("<h4>Usage:</h4><code>"+de.gulden.util.xml.XMLToolbox.xmlEscape(usageLine)+"</code></p>");
        }
        return sb.toString();
    }

    protected String createHelpHTML() {
        StringBuffer sb=new StringBuffer();
        sb.append(createAboutHTML());
        sb.append(createUsageHTML());
        Collection allOptions = getOptions().getAll(de.gulden.framework.amoda.model.option.OptionEntry.class, true).values();
        if (!allOptions.isEmpty()) {
            sb.append("<h4>options are:</h4><table>");
            for (Iterator it=allOptions.iterator();it.hasNext();) {
                de.gulden.framework.amoda.generic.option.GenericOptionEntry o=(de.gulden.framework.amoda.generic.option.GenericOptionEntry)it.next();
                if (!(o.isSystem())) {
                    String name = "-" + o.getId();
                    String shortcut = o.getShortcut();
                    if (shortcut != null) {
                        name = "-" + shortcut + " or " + name;
                    }
                    String description=de.gulden.util.Toolbox.noNull(o.getMetadata().get("description"));
                    Class typeClass = o.getType();
                    if (typeClass == null) {
                        typeClass = String.class;
                    }
                    String type="<i>"+de.gulden.util.Toolbox.unqualify(typeClass.getName()).toLowerCase()+"</i>";
                    String defaultValue=o.getValue(o.STATE_DEFAULT).getString();
                    if (defaultValue!=null) {
                        description+=" (default: "+defaultValue+")";
                    }
                    String row="<tr><td valign='top' nowrap>"+ name +"</td><td valign='top'>"+type+"</td><td>"+description+"</td></tr>";
                    sb.append(row);
                }
            }
            sb.append("</table>");
        }
        return sb.toString();
    }

    void internalSetGenericApplicationEnvironment(GenericApplicationEnvironment env) {
        this.genericApplicationEnvironment = env; // set without backward-reference (for dummy-instances of GUIApplication)
    }

} // end GUIApplication
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.