All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codename1.designer.AddUIResource Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores
 * CA 94065 USA or visit www.oracle.com if you need additional information or
 * have any questions.
 */


package com.codename1.designer;

import com.codename1.ui.util.EditableResources;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

/**
 * Allows adding a GUI builder entry to the resource editor and basing it on a template
 *
 * @author Shai Almog
 */
public class AddUIResource extends javax.swing.JDialog {
    private boolean okPressed;


    /** Creates new form AddThemeResource */
    public AddUIResource(java.awt.Component c, EditableResources res) {
        super((java.awt.Frame)SwingUtilities.windowForComponent(c), true);
        initComponents();
        ModifiableJOptionPane.reverseOKCancel(ok, cancel);
        name.setText(AddResourceDialog.nextAvailableName(res, "GUI"));
        try {
            templateDetails.setPage(getClass().getResource("/templates/BlankForm.html"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        pack();
        setLocationRelativeTo(c);
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        name = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        template = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        templateDetails = new javax.swing.JTextPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        ok = new javax.swing.JButton();
        cancel = new javax.swing.JButton();

        FormListener formListener = new FormListener();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add UI");

        jLabel1.setText("Name");
        jLabel1.setName("jLabel1"); // NOI18N

        name.setText("GUI 1");
        name.setName("name"); // NOI18N

        jLabel2.setText("Template");
        jLabel2.setName("jLabel2"); // NOI18N

        template.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "BlankForm", "BlankDialog", "BlankContainer", "Tabs", "Settings", "ListOfItems", "Splash", "LoginDialog", "UserDetailsForm", "NavigationForm", "ExitDialog", "AreYouSureDialog" }));
        template.setName("template"); // NOI18N
        template.addActionListener(formListener);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        templateDetails.setContentType("text/html");
        templateDetails.setEditable(false);
        templateDetails.setName("templateDetails"); // NOI18N
        jScrollPane1.setViewportView(templateDetails);

        jPanel1.setName("jPanel1"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new java.awt.GridLayout(1, 0, 10, 0));

        ok.setText("OK");
        ok.setName("ok"); // NOI18N
        ok.addActionListener(formListener);
        jPanel2.add(ok);

        cancel.setText("Cancel");
        cancel.setName("cancel"); // NOI18N
        cancel.addActionListener(formListener);
        jPanel2.add(cancel);

        jPanel1.add(jPanel2);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jLabel2))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(template, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 332, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(name, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 332, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE))
                .addContainerGap())
        );

        layout.linkSize(new java.awt.Component[] {name, template}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(name, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(template, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == template) {
                AddUIResource.this.templateActionPerformed(evt);
            }
            else if (evt.getSource() == cancel) {
                AddUIResource.this.cancelActionPerformed(evt);
            }
            else if (evt.getSource() == ok) {
                AddUIResource.this.okActionPerformed(evt);
            }
        }
    }// //GEN-END:initComponents

    private void templateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_templateActionPerformed
        try {
            templateDetails.setPage(getClass().getResource("/templates/" + template.getSelectedItem().toString() + ".html"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_templateActionPerformed

    public String addResource(EditableResources res, ResourceEditorView view) {
        String newName = name.getText();
        for(String r : res.getResourceNames()) {
            if(r.equalsIgnoreCase(newName)) {
                JOptionPane.showMessageDialog(this, "A resource called: " + newName + " already exists\nYou must delete the resource first.", "Add Resource", JOptionPane.ERROR_MESSAGE);
                return null;
            }
        }
        Object ui = null;
        InputStream is = getClass().getResourceAsStream("/templates/" + template.getSelectedItem().toString() + ".res");
        if(is != null) {
            try {
                EditableResources r = new EditableResources();
                r.openFile(is);
                is.close();
                ui = r.getResourceObject("Main");
                r.remove("Main");
                view.checkDuplicateResourcesLoop(r, res.getImageResourceNames(),
                        r.getImageResourceNames(), "Rename Image", "Image ", true);
                view.checkDuplicateResourcesLoop(r, res.getL10NResourceNames(),
                        r.getL10NResourceNames(), "Rename Localization", "Localization ", true);
                view.checkDuplicateResourcesLoop(r, res.getDataResourceNames(),
                        r.getDataResourceNames(), "Rename Data", "Data ", true);
                view.checkDuplicateResourcesLoop(r, res.getUIResourceNames(),
                        r.getUIResourceNames(), "Rename GUI", "GUI ", true);
                view.checkDuplicateResourcesLoop(r, res.getFontResourceNames(),
                        r.getFontResourceNames(), "Rename Font", "Font ", true);

                for (String s : r.getImageResourceNames()) {
                    res.setImage(s, r.getImage(s));
                }
                for (String s : r.getL10NResourceNames()) {
                    res.setL10N(s, (Hashtable)r.getResourceObject(s));
                }
                for (String s : r.getDataResourceNames()) {
                    res.setData(s, (byte[])r.getResourceObject(s));
                }
                for (String s : r.getUIResourceNames()) {
                    res.setUi(s, (byte[])r.getResourceObject(s));
                }
                for (String s : r.getFontResourceNames()) {
                    res.setFont(s, r.getFont(s));
                }
            } catch(IOException err) {
                err.printStackTrace();
            }
        }
        res.setUi(name.getText(), (byte[])ui);
        view.setSelectedResource(name.getText());
        return name.getText();
    }

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        okPressed = true;
        dispose();
    }//GEN-LAST:event_okActionPerformed

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        dispose();
    }//GEN-LAST:event_cancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField name;
    private javax.swing.JButton ok;
    private javax.swing.JComboBox template;
    private javax.swing.JTextPane templateDetails;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the okPressed
     */
    public boolean isOkPressed() {
        return okPressed;
    }

    /**
     * @param okPressed the okPressed to set
     */
    public void setOkPressed(boolean okPressed) {
        this.okPressed = okPressed;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy